package com.doumee.service.business.impl; import cn.emay.sdk.util.StringUtil; import com.doumee.core.constants.ResponseStatus; import com.doumee.core.exception.BusinessException; import com.doumee.core.model.LoginUserInfo; import com.doumee.core.model.PageData; import com.doumee.core.model.PageWrap; import com.doumee.core.utils.Constants; import com.doumee.core.utils.Utils; import com.doumee.dao.business.CustomerCompanyMapper; import com.doumee.dao.business.CustomerServiceMapper; import com.doumee.dao.business.join.CustomerCompanyJoinMapper; import com.doumee.dao.business.join.CustomerServiceJoinMapper; import com.doumee.dao.business.model.ApplyChagneDetail; import com.doumee.dao.business.model.Company; import com.doumee.dao.business.model.CustomerCompany; import com.doumee.dao.business.model.CustomerService; import com.doumee.dao.system.model.SystemUser; import com.doumee.service.business.CompanyService; import com.doumee.service.business.CustomerCompanyService; import com.doumee.service.business.CustomerServiceService; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.github.yulichang.base.mapper.MPJJoinMapper; import com.github.yulichang.wrapper.MPJLambdaWrapper; import io.swagger.models.auth.In; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import java.util.Date; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; /** * 客服信息表Service实现 * @author 江蹄蹄 * @date 2024/11/05 18:41 */ @Service public class CustomerServiceServiceImpl implements CustomerServiceService { @Autowired private CustomerServiceMapper customerServiceMapper; @Autowired private CustomerCompanyMapper customerCompanyMapper; @Autowired private CustomerServiceJoinMapper customerServiceJoinMapper; @Autowired private CustomerCompanyJoinMapper customerCompanyJoinMapper; @Override @Transactional(rollbackFor = {Exception.class,BusinessException.class}) public Integer create(CustomerService customerService) { LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); if(Objects.isNull(customerService) || StringUtils.isBlank(customerService.getMobile()) || StringUtils.isBlank(customerService.getName()) ){ throw new BusinessException(ResponseStatus.BAD_REQUEST); } if(customerServiceMapper.selectCount(new QueryWrapper().lambda() .eq(CustomerService::getMobile,customerService.getMobile()))>Constants.ZERO){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"手机号["+customerService.getMobile()+"]已存在客服数据"); } customerService.setCreator(loginUserInfo.getId()); customerService.setCreateDate(new Date()); customerService.setIsdeleted(Constants.ZERO); customerServiceMapper.insert(customerService); return customerService.getId(); } @Override @Transactional(rollbackFor = {Exception.class,BusinessException.class}) public void authCompany(CustomerService customerService){ LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); if(Objects.isNull(customerService) || Objects.isNull(customerService.getId()) ){ throw new BusinessException(ResponseStatus.BAD_REQUEST); } customerCompanyMapper.delete(new QueryWrapper().lambda().eq(CustomerCompany::getCustomerServiceId,customerService.getId())); if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(customerService.getCustomerCompanyList())){ List customerCompanyList = customerService.getCustomerCompanyList(); //判断数据是否存在异常 for (CustomerCompany customerCompany:customerCompanyList) { if(Objects.isNull(customerCompany.getCompanyId())){ throw new BusinessException(ResponseStatus.BAD_REQUEST); } } List companyIdList = customerCompanyList.stream().map(m->m.getCompanyId()).collect(Collectors.toList()); //查询当前选中的组织是否已被添加存在 if(customerCompanyMapper.selectCount(new QueryWrapper().lambda().in(CustomerCompany::getCompanyId,companyIdList))>0){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"存在企业数据已被添加,请刷新重试"); }; for (CustomerCompany customerCompany:customerCompanyList) { customerCompany.setCustomerServiceId(customerService.getId()); customerCompany.setCreator(loginUserInfo.getId()); customerCompany.setCreateDate(new Date()); customerCompany.setCustomerServiceId(customerService.getId()); customerCompany.setIsdeleted(Constants.ZERO); customerCompanyMapper.insert(customerCompany); } } } @Override public void deleteById(Integer id) { customerServiceMapper.deleteById(id); customerCompanyMapper.delete(new QueryWrapper().lambda().eq(CustomerCompany::getCustomerServiceId,id)); } @Override public void delete(CustomerService customerService) { UpdateWrapper deleteWrapper = new UpdateWrapper<>(customerService); customerServiceMapper.delete(deleteWrapper); } @Override public void deleteByIdInBatch(List ids) { if (CollectionUtils.isEmpty(ids)) { return; } customerServiceMapper.deleteBatchIds(ids); } @Override @Transactional(rollbackFor = {Exception.class,BusinessException.class}) public void updateById(CustomerService customerService) { LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); if(Objects.isNull(customerService) || Objects.isNull(customerService.getId()) || StringUtils.isBlank(customerService.getMobile()) || StringUtils.isBlank(customerService.getName())){ throw new BusinessException(ResponseStatus.BAD_REQUEST); } CustomerService model = customerServiceMapper.selectById(customerService.getId()); if(Objects.isNull(model)){ throw new BusinessException(ResponseStatus.DATA_EMPTY); } if(customerServiceMapper.selectCount(new QueryWrapper().lambda() .ne(CustomerService::getId,customerService.getId()) .eq(CustomerService::getMobile,customerService.getMobile()))>Constants.ZERO){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"手机号["+customerService.getMobile()+"]已存在客服数据"); } customerService.setEditor(loginUserInfo.getId()); customerService.setEditDate(new Date()); customerServiceMapper.updateById(customerService); } @Override public void updateByIdInBatch(List customerServices) { if (CollectionUtils.isEmpty(customerServices)) { return; } for (CustomerService customerService: customerServices) { this.updateById(customerService); } } @Override public CustomerService findById(Integer id) { CustomerService customerService = customerServiceMapper.selectById(id); List customerCompanyList = customerCompanyJoinMapper. selectJoinList(CustomerCompany.class, new MPJLambdaWrapper() .selectAll(CustomerCompany.class) .selectAs(Company::getName,CustomerCompany::getCompanyName) .leftJoin(Company.class,Company::getId,CustomerCompany::getCompanyId) .eq(CustomerCompany::getCustomerServiceId,id)); customerService.setCustomerCompanyList(customerCompanyList); return customerService; } @Override public CustomerService findOne(CustomerService customerService) { QueryWrapper wrapper = new QueryWrapper<>(customerService); return customerServiceMapper.selectOne(wrapper); } @Override public List findList(CustomerService customerService) { QueryWrapper wrapper = new QueryWrapper<>(customerService); return customerServiceMapper.selectList(wrapper); } @Override public PageData findPage(PageWrap pageWrap) { IPage page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity()); MPJLambdaWrapper queryWrapper = new MPJLambdaWrapper(); Utils.MP.blankToNull(pageWrap.getModel()); queryWrapper.selectAll(CustomerService.class); queryWrapper.selectAs(SystemUser::getRealname,CustomerService::getCreateUserName); queryWrapper.select(" ( select count(1) from customer_company cc where cc.CUSTOMER_SERVICE_ID = t.id ) as companyNum "); queryWrapper.leftJoin(SystemUser.class,SystemUser::getId,CustomerService::getCreator); queryWrapper.eq(CustomerService::getIsdeleted,Constants.ZERO); if (pageWrap.getModel().getStatus() != null) { queryWrapper.eq(CustomerService::getStatus, pageWrap.getModel().getStatus()); } if (pageWrap.getModel().getSortnum() != null) { queryWrapper.eq(CustomerService::getSortnum, pageWrap.getModel().getSortnum()); } if (pageWrap.getModel().getName() != null) { queryWrapper.like(CustomerService::getName, pageWrap.getModel().getName()); } if (pageWrap.getModel().getMobile() != null) { queryWrapper.like(CustomerService::getMobile, pageWrap.getModel().getMobile()); } if (pageWrap.getModel().getCompanyName() != null) { queryWrapper.apply(" t.id in ( select cc.COMPANY_ID from customer_company cc inner join company c on cc.COMPANY_ID = c.id where c.name like '%"+pageWrap.getModel().getCompanyName()+"%' ) "); } for(PageWrap.SortData sortData: pageWrap.getSorts()) { if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) { queryWrapper.orderByDesc(sortData.getProperty()); } else { queryWrapper.orderByAsc(sortData.getProperty()); } } PageData pageData = PageData.from(customerServiceJoinMapper.selectJoinPage(page,CustomerService.class, queryWrapper)); return pageData; } @Override public long count(CustomerService customerService) { QueryWrapper wrapper = new QueryWrapper<>(customerService); return customerServiceMapper.selectCount(wrapper); } /** * 根据企业查询客服人员 * @param companyId * @return */ @Override public CustomerService findByCompanyId(Integer companyId) { CustomerCompany customerCompany = customerCompanyMapper.selectOne(new QueryWrapper() .lambda().eq(CustomerCompany::getCompanyId,companyId) .last(" limit 1 ") ); if(Objects.nonNull(customerCompany)){ CustomerService customerService = customerServiceMapper.selectById(customerCompany.getCustomerServiceId()); if(Objects.isNull(customerService)){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"未配置客服人员,请联系管理员"); } return customerService; }else{ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"未配置客服人员,请联系管理员"); } } }