| package com.mzl.flower.service.customer; | 
|   | 
| import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
| 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.customer.BindPartnerDTO; | 
| import com.mzl.flower.dto.request.customer.ChangePartnerDTO; | 
| import com.mzl.flower.dto.request.customer.QueryCustomerDTO; | 
| import com.mzl.flower.dto.request.customer.UpdateCustomerDTO; | 
| import com.mzl.flower.dto.request.menber.UserGrowthRecordDTO; | 
| import com.mzl.flower.dto.response.customer.CustomerDTO; | 
| import com.mzl.flower.dto.response.partner.PartnerDTO; | 
| import com.mzl.flower.entity.customer.Customer; | 
| import com.mzl.flower.entity.partner.Partner; | 
| import com.mzl.flower.enums.TrueOrFalseEnum; | 
| import com.mzl.flower.mapper.customer.CustomerMapper; | 
| import com.mzl.flower.mapper.partner.PartnerMapper; | 
| import com.mzl.flower.service.menber.MemberGrowthRecordService; | 
| import lombok.extern.slf4j.Slf4j; | 
| import com.mzl.flower.service.BaseService; | 
| import org.apache.commons.lang3.StringUtils; | 
| import org.springframework.beans.BeanUtils; | 
| import org.springframework.stereotype.Service; | 
| import org.springframework.transaction.annotation.Transactional; | 
| import org.springframework.util.ObjectUtils; | 
|   | 
| import javax.annotation.Resource; | 
| import java.util.List; | 
|   | 
| @Service | 
| @Transactional | 
| @Slf4j | 
| public class CustomerService { | 
|   | 
|   | 
|     private final CustomerMapper customerMapper; | 
|   | 
|     private final PartnerMapper partnerMapper; | 
|     @Resource | 
|     private MemberGrowthRecordService memberGrowthRecordService; | 
|   | 
|     @Resource | 
|     private BaseService baseService; | 
|   | 
|     public CustomerService(CustomerMapper customerMapper, PartnerMapper partnerMapper) { | 
|         this.customerMapper = customerMapper; | 
|         this.partnerMapper = partnerMapper; | 
|     } | 
|   | 
|     public CustomerDTO getCurrentCustomer() { | 
|         CustomerDTO currentCustomer = customerMapper.getCurrentCustomer(SecurityUtils.getUserId()); | 
|         UserGrowthRecordDTO userGrowthRecordDTO = memberGrowthRecordService.getInfoByUserId(SecurityUtils.getUserId()); | 
|         if(!ObjectUtils.isEmpty(userGrowthRecordDTO)){ | 
|             currentCustomer.setUserGrowthRecord(userGrowthRecordDTO); | 
|         } | 
|         return currentCustomer; | 
|     } | 
|   | 
|     public PartnerDTO getCurrentBindPartner() { | 
|         CustomerDTO cus = customerMapper.getCurrentCustomer(SecurityUtils.getUserId()); | 
|         if (cus != null && StringUtils.isNotBlank(cus.getPartnerId())) { | 
|             return partnerMapper.getCurrentPartner(cus.getPartnerUserId()); | 
|         } | 
|         return null; | 
|     } | 
|   | 
|     public void addOrUpdateCustomer(UpdateCustomerDTO dto) { | 
|   | 
|         Customer customer; | 
|         if (dto.getId() == null) { //注册 | 
|             if (checkExist(dto.getUserId())) { | 
|                 throw new ValidationException("商户信息已注册"); | 
|             } | 
|             customer = new Customer(); | 
|             BeanUtils.copyProperties(dto, customer, "id"); | 
|             Partner byPartnerId = getByPartnerId(dto.getPartnerUserId()); | 
|             if (byPartnerId != null) { | 
|                 customer.setPartnerUserId(byPartnerId.getUserId()); | 
|                 customer.setPartnerId(byPartnerId.getId()); | 
|             } | 
|   | 
|             customer.create(SecurityUtils.getUserId()); | 
|             customer.setIsEnabled(true); | 
|             customer.setLevelId(Long.valueOf(Constants.DEFAULT_MEMBER_ID)); | 
|             customerMapper.insert(customer); | 
|         } else {//重新修改 | 
|             customer = customerMapper.selectById(dto.getId()); | 
|             if (customer == null) { | 
|                 throw new ValidationException("商户信息不存在"); | 
|             } | 
|             BeanUtils.copyProperties(dto, customer, "id"); | 
| //            customer.setPartnerUserId(getByPartnerId(dto.getPartnerUserId())); | 
|             Partner byPartnerId = getByPartnerId(dto.getPartnerUserId()); | 
|             if (byPartnerId != null) { | 
|                 customer.setPartnerUserId(byPartnerId.getUserId()); | 
|                 customer.setPartnerId(byPartnerId.getId()); | 
|             } | 
|             customer.update(SecurityUtils.getUserId()); | 
|             customerMapper.updateById(customer); | 
|         } | 
|   | 
|   | 
|     } | 
|   | 
|     private Partner getByPartnerId(String partnerId) { | 
|         if (StringUtils.isNotBlank(partnerId)) { | 
|             Partner partnerByUserId = partnerMapper.selectOne(new QueryWrapper<Partner>().eq("user_id", partnerId)); | 
|             if (partnerByUserId != null) { | 
|                 return partnerByUserId; | 
|             } | 
|             Partner partner = partnerMapper.selectById(partnerId); | 
|             if (partner == null) { | 
|                 throw new ValidationException("合伙人不存在"); | 
|             } else { | 
|                 //判断id和userid是否相同,避免string被转为int | 
|                 if (!partner.getUserId().equals(partnerId) && !partner.getId().toString().equals(partnerId)) { | 
|                     throw new ValidationException("合伙人不存在"); | 
|                 } | 
|             } | 
| //            return partner.getUserId(); | 
|             return partner; | 
|         } | 
|         return null; | 
|   | 
|     } | 
|   | 
|     private boolean checkExist(String userId) { | 
|         return customerMapper.selectCount(new LambdaQueryWrapper<Customer>().eq(Customer::getUserId, userId)) > 0; | 
|     } | 
|   | 
|     public Page<CustomerDTO> queryCustomer(QueryCustomerDTO dto, Page page) { | 
|         List<CustomerDTO> list = customerMapper.queryCustomer(dto, page); | 
|         page.setRecords(list); | 
|         return page; | 
|     } | 
|   | 
|     public CustomerDTO findDetail(Long id) { | 
|         Customer customer = customerMapper.selectById(id); | 
|         if (customer == null) { | 
|             return null; | 
|         } | 
|         if (Constants.USER_TYPE.admin.name().equals(SecurityUtils.getUserType())) { | 
|             CustomerDTO customerDTO = new CustomerDTO(); | 
|             BeanUtils.copyProperties(customer, customerDTO); | 
|             return customerDTO; | 
|         } else if (Constants.USER_TYPE.partner.name().equals(SecurityUtils.getUserType())) { | 
|             if (customer.getPartnerUserId().equals(SecurityUtils.getUserId())) { | 
|                 CustomerDTO customerDTO = new CustomerDTO(); | 
|                 BeanUtils.copyProperties(customer, customerDTO); | 
|                 return customerDTO; | 
|             } | 
|         } | 
|         return null; | 
|     } | 
|   | 
|     public void changeCustomerPartner(ChangePartnerDTO dto) { | 
|         Customer customer = customerMapper.selectById(dto.getId()); | 
|         if (customer == null) { | 
|             throw new ValidationException("商户信息不存在"); | 
|         } | 
|         if (dto.getPartnerId() == null || dto.getPartnerId() <= 0) { | 
|             customer.setPartnerUserId(null); | 
|             customer.setPartnerId(null); | 
|         } else { | 
|             Partner partner = partnerMapper.selectById(dto.getPartnerId()); | 
|             if (partner == null) { | 
|                 throw new ValidationException("合伙人不存在"); | 
|             } | 
|             if (!"P".equals(partner.getStatus())) { | 
|                 throw new ValidationException("合伙人信息未审核通过"); | 
|             } | 
|             customer.setPartnerUserId(partner.getUserId()); | 
|             customer.setPartnerId(partner.getId()); | 
|         } | 
|         customerMapper.updateById(customer); | 
|     } | 
|   | 
|     public void bindPartner(BindPartnerDTO dto) { | 
|         CustomerDTO c = getCurrentCustomer(); | 
|         if (c == null) { | 
|             throw new ValidationException("商户信息不存在"); | 
|         } | 
|   | 
|         if (StringUtils.isNotBlank(c.getPartnerId()) || StringUtils.isNotBlank(c.getPartnerUserId())) { | 
|             throw new ValidationException("商户已绑定合伙人,请联系客服人员进行解绑后再进行绑定"); | 
|         } | 
|         Partner partner; | 
|         try { | 
|             long id = Long.parseLong(dto.getPartnerUserId()); | 
|             partner = partnerMapper.selectById(id); | 
|         } catch (Exception e) { | 
|             partner =partnerMapper.selectOne(new QueryWrapper<Partner>().eq("user_id", dto.getPartnerUserId())); | 
|         } | 
|         if (partner == null) { | 
|             throw new ValidationException("合伙人不存在"); | 
|         } | 
|         if (!"P".equals(partner.getStatus())) { | 
|             throw new ValidationException("合伙人信息未审核通过,请联系客服人员"); | 
|         } | 
|   | 
|         customerMapper.bindPartner(c.getId(), partner.getId(), partner.getUserId()); | 
|     } | 
|   | 
|     public String getPartnerName(String partnerUserId) { | 
|         if (StringUtils.isNotBlank(partnerUserId)) { | 
|             Partner partner; | 
|             try { | 
|                 long id = Long.parseLong(partnerUserId); | 
|                 partner = partnerMapper.selectById(id); | 
|             } catch (Exception e) { | 
|                 partner =partnerMapper.selectOne(new QueryWrapper<Partner>().eq("user_id", partnerUserId)); | 
|             } | 
|             if (partner == null) { | 
|                 throw new ValidationException("合伙人不存在"); | 
|             } | 
|             return partner.getName(); | 
|         } | 
|         return null; | 
|     } | 
|     public void isEnable(Long id) { | 
|         Customer customer = customerMapper.selectById(id); | 
|         if (customer == null) { | 
|             throw new ValidationException("商户信息不存在"); | 
|         } | 
|         if (customer.getIsEnabled()) { | 
|             customer.setIsEnabled(false); | 
|             //强制下线 | 
|             baseService.removeToken(customer.getUserId()); | 
|         } else { | 
|             customer.setIsEnabled(true); | 
|         } | 
|         customer.update(SecurityUtils.getUserId()); | 
|         customerMapper.updateById(customer); | 
|     } | 
|   | 
|     /** | 
|      * 根据会员等级获取等级下的customer信息 | 
|      * @param levelId | 
|      * @return | 
|      */ | 
|     public List<Customer> getCustomerListByLevelId(Integer levelId){ | 
|         if(null != levelId){ | 
|             QueryWrapper<Customer> customerQueryWrapper=new QueryWrapper<>(); | 
|             customerQueryWrapper.lambda() | 
|                     .eq(Customer::getDeleted, TrueOrFalseEnum.FALSE.isFlag()) | 
|                     .eq(Customer::getLevelId,levelId); | 
|             return customerMapper.selectList(customerQueryWrapper); | 
|         } | 
|         return null; | 
|     } | 
|   | 
| } |