package com.mzl.flower.service.supplier; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.mzl.flower.config.exception.ValidationException; import com.mzl.flower.config.security.SecurityUtils; import com.mzl.flower.constant.Constants; import com.mzl.flower.dto.request.supplier.*; import com.mzl.flower.dto.response.supplier.SupplierDTO; import com.mzl.flower.entity.supplier.Supplier; import com.mzl.flower.mapper.supplier.SupplierMapper; import com.mzl.flower.service.BaseService; import com.mzl.flower.utils.DateUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.oauth2.common.OAuth2AccessToken; import org.springframework.security.oauth2.common.OAuth2RefreshToken; import org.springframework.security.oauth2.provider.token.TokenStore; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.List; @Service @Transactional public class SupplierService { private final static String SUPPLIER_STATUS_AUDIT = "U"; // 待审核 private final static String SUPPLIER_STATUS_PASS = "P"; // 审核通过 private final static String SUPPLIER_STATUS_REJECT = "R"; // 审核拒绝 private final SupplierMapper supplierMapper; @Resource private BaseService baseService; public SupplierService(SupplierMapper supplierMapper) { this.supplierMapper = supplierMapper; } public void addOrUpdateSupplier(UpdateSupplierDTO dto) { Supplier supplier; if(dto.getId()==null){ //注册 if(checkExist(dto.getUserId())){ throw new ValidationException("供应商信息已登记"); } if(checkExistName(dto.getName(),null)){ throw new ValidationException("店铺名称已存在"); } supplier = new Supplier(); BeanUtils.copyProperties(dto, supplier,"id"); supplier.setIdCards(JSON.toJSONString(dto.getIdCards())); supplier.setPictures(JSON.toJSONString(dto.getPictures())); supplier.create(SecurityUtils.getUserId()); supplier.setShowed(true); supplier.setStatus(SUPPLIER_STATUS_AUDIT); supplier.setIsEnabled(true); supplierMapper.insert(supplier); }else{//重新修改 supplier = supplierMapper.selectById(dto.getId()); if(supplier==null){ throw new ValidationException("供应商信息未登记"); } if(checkExistName(dto.getName(),supplier.getId())){ throw new ValidationException("店铺名称已存在"); } BeanUtils.copyProperties(dto, supplier,"id"); supplier.setIdCards(JSON.toJSONString(dto.getIdCards())); supplier.setPictures(JSON.toJSONString(dto.getPictures())); supplier.setStatus(SUPPLIER_STATUS_AUDIT); supplier.update(SecurityUtils.getUserId()); supplierMapper.updateById(supplier); } } public void operationUpdate(UpdateSupplierOpDTO dto) { if(dto.getId()==null || dto.getId()==0){ throw new ValidationException("id不能为空"); } Supplier supplier = supplierMapper.selectById(dto.getId()); if(supplier==null){ throw new ValidationException("供应商信息未登记"); } if(checkExistName(dto.getName(),supplier.getId())){ throw new ValidationException("店铺名称已存在"); } BeanUtils.copyProperties(dto, supplier,"id"); supplier.update(SecurityUtils.getUserId()); supplierMapper.updateById(supplier); } private boolean checkExistName(String name, Long id) { if(StringUtils.isNotBlank(name)){ LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper(); lambdaQueryWrapper.eq(Supplier::getName,name); if(id != null){ lambdaQueryWrapper.ne(Supplier::getId,id); } return supplierMapper.selectCount(lambdaQueryWrapper)>0; } return false; } private boolean checkExist(String userId) { return supplierMapper.selectCount(new LambdaQueryWrapper().eq(Supplier::getUserId,userId))>0; } public Page querySupplier(QuerySupplierDTO dto, Page page) { if(StringUtils.isNotBlank(dto.getCreateDateBeginStr())){ dto.setCreateDateBegin(DateUtils.dateToLocalDateTime(dto.getCreateDateBeginStr(),true)); } if(StringUtils.isNotBlank(dto.getCreateDateEndStr())){ dto.setCreateDateEnd(DateUtils.dateToLocalDateTime(dto.getCreateDateEndStr(),false)); } List list = supplierMapper.querySupplier(dto,page); page.setRecords(list); return page; } public SupplierDTO findSupplierDetail(Long id) { SupplierDTO dto = supplierMapper.findSupplierDetail(id); return dto; } public SupplierDTO findSupplierByPhone(String phone) { SupplierDTO dto = supplierMapper.findSupplierByPhone(phone); dto.setOverTime(LocalDate.from(dto.getPassTime())); return dto; } public void auditSupplier(AuditSupplierDTO dto) { Supplier supplier = supplierMapper.selectById(dto.getId()); if(supplier==null){ throw new ValidationException("供应商信息不存在"); } if(Constants.AUDIT_STATUS.Y.name().equals(dto.getResult())){ supplier.setStatus(SUPPLIER_STATUS_PASS); supplier.setPassTime(LocalDateTime.now()); }else if(Constants.AUDIT_STATUS.N.name().equals(dto.getResult())){ supplier.setStatus(SUPPLIER_STATUS_REJECT); supplier.setRejectReason(dto.getRejectReason()); }else{ throw new ValidationException("审核结果不正确"); } supplier.update(SecurityUtils.getUserId()); supplierMapper.updateById(supplier); } public void changeStation(ChangeStationDTO dto) { Supplier supplier = supplierMapper.selectById(dto.getId()); if(supplier==null){ throw new ValidationException("供应商信息不存在"); } supplier.setStationId(dto.getStationId()); supplier.update(SecurityUtils.getUserId()); supplierMapper.updateById(supplier); } public SupplierDTO getCurrentSupplier() { if(!Constants.USER_TYPE.supplier.name().equals(SecurityUtils.getUserType())){ throw new ValidationException("当前登录用户不是供应商账号"); } SupplierDTO dto = supplierMapper.getCurrentSupplier(SecurityUtils.getUserId()); return dto; } public void updateSupplier(UpdateSupplierDTO dto) { if(dto.getId() == null || dto.getId() == 0){ throw new ValidationException("id不能为空"); } Supplier supplier = supplierMapper.selectById(dto.getId()); if(supplier ==null) { throw new ValidationException("供应商信息不存在"); } if(checkExistName(dto.getName(),supplier.getId())){ throw new ValidationException("店铺名称已存在"); } supplier.setName(dto.getName()); supplier.setCover(dto.getCover()); supplier.setDescription(dto.getDescription()); supplier.update(SecurityUtils.getUserId()); supplierMapper.updateById(supplier); } public void configShow(Long id) { Supplier supplier = supplierMapper.selectById(id); if(supplier ==null) { throw new ValidationException("供应商信息不存在"); } if(supplier.getShowed()){ supplier.setShowed(false); }else { supplier.setShowed(true); } supplier.update(SecurityUtils.getUserId()); supplierMapper.updateById(supplier); } public void isEnable(Long id) { Supplier supplier = supplierMapper.selectById(id); if (supplier == null) { throw new ValidationException("供应商信息不存在"); } if (supplier.getIsEnabled()) { supplier.setIsEnabled(false); //强制下线 baseService.removeToken(supplier.getUserId()); } else { supplier.setIsEnabled(true); } supplier.update(SecurityUtils.getUserId()); supplierMapper.updateById(supplier); } public Supplier getSupplierById(Long supplierId) { return supplierMapper.selectById(supplierId); } }