| package com.mzl.flower.service.partner; | 
|   | 
| import cn.binarywang.wx.miniapp.api.WxMaService; | 
| 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.partner.*; | 
| import com.mzl.flower.dto.response.partner.PartnerDTO; | 
| import com.mzl.flower.entity.partner.Partner; | 
| import com.mzl.flower.entity.system.User; | 
| import com.mzl.flower.mapper.partner.PartnerMapper; | 
| import com.mzl.flower.mapper.system.UserMapper; | 
| import com.mzl.flower.service.BaseService; | 
| import com.mzl.flower.service.UploadService; | 
| import com.mzl.flower.service.system.UserService; | 
| import com.mzl.flower.utils.DateUtils; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.apache.commons.lang3.StringUtils; | 
| import org.springframework.beans.BeanUtils; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.stereotype.Service; | 
| import org.springframework.transaction.annotation.Transactional; | 
|   | 
| import javax.annotation.Resource; | 
| import java.io.File; | 
| import java.io.FileInputStream; | 
| import java.io.InputStream; | 
| import java.time.LocalDateTime; | 
| import java.util.List; | 
|   | 
| @Service | 
| @Transactional | 
| @Slf4j | 
| public class PartnerService { | 
|   | 
|     private final static String PARTNER_STATUS_AUDIT = "U"; // 待审核 | 
|     private final static String PARTNER_STATUS_PASS = "P"; // 审核通过 | 
|     private final static String PARTNER_STATUS_REJECT = "R"; // 审核拒绝 | 
|   | 
|     private final PartnerMapper partnerMapper; | 
|   | 
|     private final UserMapper userMapper; | 
|   | 
|   | 
|     private final WxMaService maService; | 
|   | 
|   | 
|     private final UploadService uploadService; | 
|   | 
|     @Resource | 
|     private BaseService baseService; | 
|   | 
|     @Autowired | 
|     private UserService userService; | 
|   | 
|   | 
|     public PartnerService(PartnerMapper partnerMapper, UserMapper userMapper, WxMaService maService, UploadService uploadService) { | 
|         this.partnerMapper = partnerMapper; | 
|         this.userMapper = userMapper; | 
|         this.maService = maService; | 
|         this.uploadService = uploadService; | 
|     } | 
|   | 
|     public void addOrUpdatePartner(UpdatePartnerDTO dto) { | 
|         Partner partner; | 
|         if(dto.getId()==null){ //注册 | 
|             if(checkExist(dto.getUserId())){ | 
|                 throw new ValidationException("合伙人信息已登记"); | 
|             } | 
|             if(StringUtils.isNotBlank(dto.getContactTel()) && StringUtils.isNotBlank(dto.getUserId())){ | 
|                 //验证手机号 | 
|                 User user = userMapper.selectById(dto.getUserId()); | 
|                 if(user==null){ | 
|                     throw new ValidationException("合伙人账号信息不存在"); | 
|                 } | 
|                 if(userMapper.selectCount(new LambdaQueryWrapper<User>().eq(User::getTel, dto.getContactTel()) | 
|                         .eq(User::getType, Constants.USER_TYPE.partner.name()) | 
|                         .ne(User::getId, dto.getUserId()) | 
|                         .eq(User::getDeleted, 0) | 
|                 )>0){ | 
|                     throw new ValidationException("手机号已被注册使用"); | 
|                 } | 
|                 user.setTel(dto.getContactTel()); | 
|                 userMapper.updateById(user); | 
|             } | 
|             partner = new Partner(); | 
|             BeanUtils.copyProperties(dto, partner,"id"); | 
|             partner.setIdCards(JSON.toJSONString(dto.getIdCards())); | 
|             partner.create(SecurityUtils.getUserId()); | 
|             partner.setStatus(PARTNER_STATUS_AUDIT); | 
|             partner.setIsEnabled(true); | 
|             partnerMapper.insert(partner); | 
|         }else{//重新修改 | 
|             partner = partnerMapper.selectById(dto.getId()); | 
|             if(partner==null){ | 
|                 throw new ValidationException("合伙人信息未登记"); | 
|             } | 
|             if(StringUtils.isNotBlank(dto.getContactTel()) && !partner.getContactTel().equals(dto.getContactTel())){ | 
|                 //验证手机号 | 
|                 User user = userMapper.selectById(partner.getUserId()); | 
|                 if(user==null){ | 
|                     throw new ValidationException("合伙人账号信息不存在"); | 
|                 } | 
|                 if(userMapper.selectCount(new LambdaQueryWrapper<User>().eq(User::getTel, dto.getContactTel()) | 
|                         .eq(User::getType, Constants.USER_TYPE.partner.name()) | 
|                         .ne(User::getId, partner.getUserId()) | 
|                         .eq(User::getDeleted, 0) | 
|                 )>0){ | 
|                     throw new ValidationException("手机号已被注册使用"); | 
|                 } | 
|                 user.setTel(dto.getContactTel()); | 
|                 partner.setContactTel(dto.getContactTel()); | 
|                 userMapper.updateById(user); | 
|             } | 
|             BeanUtils.copyProperties(dto, partner,"id"); | 
|             partner.setIdCards(JSON.toJSONString(dto.getIdCards())); | 
|             partner.setStatus(PARTNER_STATUS_AUDIT); | 
|             partner.update(SecurityUtils.getUserId()); | 
|             partnerMapper.updateById(partner); | 
|         } | 
|     } | 
|   | 
|     private boolean checkExist(String userId) { | 
|         return partnerMapper.selectCount(new LambdaQueryWrapper<Partner>().eq(Partner::getUserId,userId))>0; | 
|     } | 
|   | 
|     public Page<PartnerDTO> queryPartner(QueryPartnerDTO 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<PartnerDTO> list = partnerMapper.queryPartner(dto,page); | 
|         page.setRecords(list); | 
|         return page; | 
|     } | 
|   | 
|     public PartnerDTO findPartnerDetail(Long id) { | 
|         Partner partner = partnerMapper.selectById(id); | 
|         PartnerDTO dto = new PartnerDTO(); | 
|         if(dto!=null){ | 
|             BeanUtils.copyProperties(partner,dto); | 
|         } | 
|         return dto; | 
|     } | 
|   | 
|     public void auditPartner(AuditPartnerDTO dto) { | 
|         Partner partner = partnerMapper.selectById(dto.getId()); | 
|         if(partner==null){ | 
|             throw new ValidationException("合伙人信息不存在"); | 
|         } | 
|         if(Constants.AUDIT_STATUS.Y.name().equals(dto.getResult())){ | 
|             partner.setStatus(PARTNER_STATUS_PASS); | 
|             partner.setPassTime(LocalDateTime.now()); | 
|         }else if(Constants.AUDIT_STATUS.N.name().equals(dto.getResult())){ | 
|             partner.setStatus(PARTNER_STATUS_REJECT); | 
|             partner.setRejectReason(dto.getRejectReason()); | 
|         }else{ | 
|             throw new ValidationException("审核结果不正确"); | 
|         } | 
|         partner.update(SecurityUtils.getUserId()); | 
|         partnerMapper.updateById(partner); | 
|     } | 
|   | 
|     public PartnerDTO getCurrentPartner() { | 
|         return partnerMapper.getCurrentPartner(SecurityUtils.getUserId()); | 
|     } | 
|   | 
|     public void changePartnerArea(ChangePartnerAreaDTO dto) { | 
|         Partner partner = partnerMapper.selectById(dto.getId()); | 
|         if(partner==null){ | 
|             throw new ValidationException("合伙人信息不存在"); | 
|         } | 
|         partner.setProvince(dto.getProvince()); | 
|         partner.setCity(dto.getCity()); | 
|         partner.setRegion(dto.getRegion()); | 
|         if(StringUtils.isNotBlank(dto.getName())){ | 
|             partner.setName(dto.getName()); | 
|         } | 
|         if(StringUtils.isNotBlank(dto.getContactTel()) && !partner.getContactTel().equals(dto.getContactTel())){ | 
|             //验证手机号 | 
|             User user = userMapper.selectById(partner.getUserId()); | 
|             if(user==null){ | 
|                 throw new ValidationException("合伙人账号信息不存在"); | 
|             } | 
|             if(userMapper.selectCount(new LambdaQueryWrapper<User>().eq(User::getTel, dto.getContactTel()) | 
|                     .eq(User::getType, Constants.USER_TYPE.partner.name()) | 
|                     .ne(User::getId, partner.getUserId()) | 
|                     .eq(User::getDeleted, 0) | 
|             )>0){ | 
|                 throw new ValidationException("手机号已被注册使用"); | 
|             } | 
|             user.setTel(dto.getContactTel()); | 
|             partner.setContactTel(dto.getContactTel()); | 
|             userMapper.updateById(user); | 
|         } | 
|         partner.update(SecurityUtils.getUserId()); | 
|         partnerMapper.updateById(partner); | 
|   | 
|   | 
|   | 
|   | 
|     } | 
|     public String generateCodeDTO(GenerateCodeDTO dto) { | 
|         try { | 
|             Partner partner = partnerMapper.selectById(dto.getId()); | 
|             if(partner==null){ | 
|                 throw new ValidationException("合伙人信息不存在"); | 
|             } | 
|             File file = maService.getQrcodeService() | 
|                     .createWxaCodeUnlimit("partnerUserId="+partner.getId(),"pages/login/supplier-login","/opt/wx-code"); | 
|             InputStream in = new FileInputStream(file); | 
|             String url = uploadService.upload(in, file.getName()); | 
|   | 
|             partner.setWechatUrl("https://" + url); | 
|             partner.update(SecurityUtils.getUserId()); | 
|             partnerMapper.updateById(partner); | 
|             return partner.getWechatUrl(); | 
|         } catch (Exception e) { | 
|             log.error("生成二维码失败",e.getMessage()); | 
|             throw new RuntimeException("生成二维码失败"); | 
|         } | 
|     } | 
|   | 
|     public void uploadCodeDTO(UploadCodeDTO dto) { | 
|         Partner partner = partnerMapper.selectById(dto.getId()); | 
|         if(partner==null){ | 
|             throw new ValidationException("合伙人信息不存在"); | 
|         } | 
|         partner.setCodeUrl(dto.getCodeUrl()); | 
|         partner.update(SecurityUtils.getUserId()); | 
|         partnerMapper.updateById(partner); | 
|     } | 
|   | 
|     public void isEnable(Long id) { | 
|         Partner partner = partnerMapper.selectById(id); | 
|         if (partner == null) { | 
|             throw new ValidationException("合伙人信息不存在"); | 
|         } | 
|         if (partner.getIsEnabled()) { | 
|             partner.setIsEnabled(false); | 
|             //强制下线 | 
|             baseService.removeToken(partner.getUserId()); | 
|         } else { | 
|             partner.setIsEnabled(true); | 
|         } | 
|         partner.update(SecurityUtils.getUserId()); | 
|         partnerMapper.updateById(partner); | 
|     } | 
|   | 
|     public void baseUpdate(UpdatePartnerBaseDTO dto) { | 
|         Partner partner = partnerMapper.selectById(dto.getId()); | 
|         if(partner==null){ | 
|             throw new ValidationException("合伙人信息未登记"); | 
|         } | 
|         BeanUtils.copyProperties(dto, partner,"id"); | 
|         partner.update(SecurityUtils.getUserId()); | 
|         partnerMapper.updateById(partner); | 
|     } | 
|   | 
|     @Transactional | 
|     public void phoneUpdate(UpdatePhoneDTO dto) { | 
|         Partner partner = partnerMapper.selectById(dto.getId()); | 
|         if(partner==null){ | 
|             throw new ValidationException("合伙人信息未登记"); | 
|         } | 
|         User user = userService.getUserById(partner.getUserId()); | 
|         if(user == null){ | 
|             throw new ValidationException("用户不存在"); | 
|         } | 
|         partner.setContactTel(dto.getUsername()); | 
|         partner.update(SecurityUtils.getUserId()); | 
|         partnerMapper.updateById(partner); | 
|   | 
|         user.setTel(dto.getUsername()); | 
|         user.setLoginName(dto.getUsername()); | 
|         user.update(SecurityUtils.getUserId()); | 
|         userMapper.updateById(user); | 
|   | 
|     } | 
| } |