package com.doumee.service.business.impl; import com.doumee.biz.system.SystemDictDataBiz; 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.*; import com.doumee.dao.business.join.CompanyUserApplyJoinMapper; import com.doumee.dao.business.model.*; import com.doumee.dao.system.SystemUserMapper; import com.doumee.dao.system.model.SystemDictData; import com.doumee.dao.system.model.SystemUser; import com.doumee.service.business.CompanyUserApplyService; 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.MPJBaseMapper; import com.github.yulichang.query.MPJQueryWrapper; import com.github.yulichang.wrapper.MPJLambdaWrapper; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; import org.checkerframework.checker.units.qual.A; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import java.util.*; /** * 集团申请记录表Service实现 * @author 江蹄蹄 * @date 2024/10/28 19:16 */ @Service public class CompanyUserApplyServiceImpl implements CompanyUserApplyService { @Autowired private CompanyUserApplyMapper companyUserApplyMapper; @Autowired private SystemUserMapper systemUserMapper; @Autowired private CompanyMapper companyMapper; @Autowired private MultifileMapper multifileMapper; @Autowired private CompanyUserApplyJoinMapper companyUserApplyJoinMapper; @Autowired private SystemDictDataBiz systemDictDataBiz; @Autowired private CompanyPermissionMapper companyPermissionMapper; @Autowired private NoticesMapper noticesMapper; @Override public Integer create(CompanyUserApply companyUserApply) { LoginUserInfo loginUserInfo =(LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); if(Objects.isNull(companyUserApply) || Objects.isNull(companyUserApply.getUserId()) || CollectionUtils.isEmpty(companyUserApply.getMultifileList()) || StringUtils.isBlank(companyUserApply.getCompanyIds()) || StringUtils.isBlank(companyUserApply.getCompanyNames())){ throw new BusinessException(ResponseStatus.BAD_REQUEST); } if(!Constants.equalsInteger(loginUserInfo.getType(),Constants.ONE)){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"登录用户不允许当前操作"); } //查询当前用户是否存在申请中 if(companyUserApplyMapper.selectCount(new QueryWrapper().lambda() .eq(CompanyUserApply::getUserId,companyUserApply.getUserId()) .eq(CompanyUserApply::getIsdeleted, Constants.ZERO) .eq(CompanyUserApply::getStatus,Constants.ZERO))>Constants.ZERO){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"当前账户存在待审批数据,无法进行申请"); }; SystemUser applyUser = systemUserMapper.selectById(companyUserApply.getUserId()); if(Objects.isNull(applyUser)){ throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到申请用户信息"); } Company applyCompany = companyMapper.selectById(applyUser.getCompanyId()); if(Objects.isNull(applyCompany)){ throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到申请用户企业信息"); } List companyIds = Arrays.asList(companyUserApply.getCompanyIds().split(",")); for (String companyId:companyIds) { if(Constants.equalsInteger(loginUserInfo.getCompanyId(),Integer.valueOf(companyId))){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"无法申请归属公司权限"); } if(companyPermissionMapper.selectCount(new QueryWrapper().lambda() .eq(CompanyPermission::getCompanyId,companyId) .eq(CompanyPermission::getIsdeleted,Constants.ZERO) .eq(CompanyPermission::getUserId,companyUserApply.getUserId()))>Constants.ZERO ){ Company company = companyMapper.selectById(companyId); throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"企业【"+company.getName()+"】已申请,请勿重复申请!"); }; } List companyList = companyMapper.selectList(new QueryWrapper().lambda() .eq(Company::getIsdeleted, Constants.ZERO) .eq(Company::getType,Constants.ZERO) .in(Company::getId, companyIds) ); if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isEmpty(companyList) || !Constants.equalsInteger(companyList.size(),companyIds.size())){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"企业信息异常,请检查选择企业信息"); } companyUserApply.setStatus(Constants.ZERO); companyUserApply.setIsdeleted(Constants.ZERO); companyUserApply.setCreateDate(new Date()); companyUserApply.setCreator(loginUserInfo.getId()); companyUserApplyMapper.insert(companyUserApply); List multifileList = companyUserApply.getMultifileList(); for (Multifile multifile:multifileList) { multifile.setIsdeleted(Constants.ZERO); multifile.setCreator(loginUserInfo.getId()); multifile.setCreateDate(new Date()); multifile.setObjId(companyUserApply.getId()); multifile.setObjType(Constants.MultiFile.COMPANY_USER_APPLY.getKey()); multifileMapper.insert(multifile); } //存储待办信息 Constants.NoticeObjectType noticeObjectType = Constants.NoticeObjectType.COMPANY_USER_APPLY; //平台待办 Notices notices = new Notices(noticeObjectType, Constants.ZERO ,companyUserApply.getId(), applyCompany.getName() + " - " + applyUser.getRealname() +"提交集团账号申请" , null ,Constants.NoticeType.ZERO); noticesMapper.insert(notices); return companyUserApply.getId(); } @Override public void deleteById(Integer id) { LoginUserInfo loginUserInfo =(LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); CompanyUserApply companyUserApply = companyUserApplyMapper.selectById(id); if(Objects.isNull(companyUserApply)){ throw new BusinessException(ResponseStatus.DATA_EMPTY); } if(!Constants.equalsInteger(companyUserApply.getStatus(),Constants.ZERO)){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"当前业务状态已流转,不允许该操作"); } companyUserApplyMapper.update(null,new UpdateWrapper().lambda() .set(CompanyUserApply::getIsdeleted,Constants.ONE) .set(CompanyUserApply::getEditDate,new Date()) .set(CompanyUserApply::getEditor,loginUserInfo.getId()) .eq(CompanyUserApply::getId,id) ); } @Override public void delete(CompanyUserApply companyUserApply) { UpdateWrapper deleteWrapper = new UpdateWrapper<>(companyUserApply); companyUserApplyMapper.delete(deleteWrapper); } @Override public void deleteByIdInBatch(List ids) { if (CollectionUtils.isEmpty(ids)) { return; } companyUserApplyMapper.deleteBatchIds(ids); } @Override public void updateById(CompanyUserApply companyUserApply) { companyUserApplyMapper.updateById(companyUserApply); } @Override public void updateByIdInBatch(List companyUserApplys) { if (CollectionUtils.isEmpty(companyUserApplys)) { return; } for (CompanyUserApply companyUserApply: companyUserApplys) { this.updateById(companyUserApply); } } @Override public CompanyUserApply findById(Integer id) { CompanyUserApply companyUserApply = companyUserApplyJoinMapper.selectJoinOne(CompanyUserApply.class,new MPJLambdaWrapper() .selectAll(CompanyUserApply.class) .select(" t3.name as companyName ") .select(" t1.REALNAME as realName ") .select(" t2.USERNAME as userName ") .leftJoin(" system_user t1 on t1.id = t.creator ") .leftJoin(" system_user t2 on t2.id = t.user_id ") .leftJoin(" company t3 on t3.id = t2.company_id ") .eq(CompanyUserApply::getId,id)); if(Objects.isNull(companyUserApply)){ throw new BusinessException(ResponseStatus.DATA_EMPTY); } List multifileList = multifileMapper.selectList(new QueryWrapper().lambda() .eq(Multifile::getObjId,id) .eq(Multifile::getObjType,Constants.MultiFile.COMPANY_USER_APPLY.getKey()) .eq(Multifile::getIsdeleted,Constants.ZERO) .orderByDesc(Multifile::getId) ); if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(multifileList)){ String url = systemDictDataBiz.queryByCode(Constants.OSS,Constants.RESOURCE_PATH).getCode()+ systemDictDataBiz.queryByCode(Constants.OSS,Constants.COMPANY_USER_APPLY).getCode(); for (Multifile multifile:multifileList) { if(StringUtils.isNotBlank(multifile.getFileurl())){ multifile.setFileurlFull(url + multifile.getFileurl()); } } companyUserApply.setMultifileList(multifileList); } return companyUserApply; } @Override public CompanyUserApply findOne(CompanyUserApply companyUserApply) { QueryWrapper wrapper = new QueryWrapper<>(companyUserApply); return companyUserApplyMapper.selectOne(wrapper); } @Override public List findList(CompanyUserApply companyUserApply) { QueryWrapper wrapper = new QueryWrapper<>(companyUserApply); return companyUserApplyMapper.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(CompanyUserApply.class); queryWrapper.select(" t3.name as companyName "); queryWrapper.select(" t1.REALNAME as realName "); queryWrapper.select(" t2.USERNAME as userName "); queryWrapper.leftJoin(" system_user t1 on t1.id = t.creator "); queryWrapper.leftJoin(" system_user t2 on t2.id = t.user_id "); queryWrapper.leftJoin(" company t3 on t3.id = t2.company_id "); queryWrapper.eq(CompanyUserApply::getIsdeleted,Constants.ZERO); if (pageWrap.getModel().getCompanyId() != null) { queryWrapper.apply(" t3.id = "+pageWrap.getModel().getCompanyId()+" "); } if (pageWrap.getModel().getStatus() != null) { queryWrapper.eq(CompanyUserApply::getStatus, pageWrap.getModel().getStatus()); } if (pageWrap.getModel().getUserId() != null) { queryWrapper.eq(CompanyUserApply::getUserId, pageWrap.getModel().getUserId()); } queryWrapper.orderByDesc(CompanyUserApply::getCreateDate); IPage iPage = companyUserApplyJoinMapper.selectJoinPage(page,CompanyUserApply.class,queryWrapper); return PageData.from(iPage); } @Override public long count(CompanyUserApply companyUserApply) { QueryWrapper wrapper = new QueryWrapper<>(companyUserApply); return companyUserApplyMapper.selectCount(wrapper); } @Override public void audit(CompanyUserApply companyUserApply){ LoginUserInfo loginUserInfo =(LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); if(Objects.isNull(companyUserApply) ||Objects.isNull(companyUserApply.getId()) || Objects.isNull(companyUserApply.getStatus()) || !(Constants.equalsInteger(companyUserApply.getStatus(),Constants.ONE) || Constants.equalsInteger(companyUserApply.getStatus(),Constants.TWO)) || (StringUtils.isBlank(companyUserApply.getCheckInfo()) && Constants.equalsInteger(companyUserApply.getStatus(),Constants.TWO)) ){ throw new BusinessException(ResponseStatus.BAD_REQUEST); } CompanyUserApply userApply = companyUserApplyJoinMapper.selectById(companyUserApply.getId()); if(Objects.isNull(userApply)){ throw new BusinessException(ResponseStatus.DATA_EMPTY); } if(!Constants.equalsInteger(userApply.getStatus(),Constants.ZERO)){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"业务状态已流转"); } CompanyUserApply update = new CompanyUserApply(); update.setId(companyUserApply.getId()); update.setEditDate(new Date()); update.setEditor(loginUserInfo.getId()); update.setStatus(companyUserApply.getStatus()); update.setCheckInfo(companyUserApply.getCheckInfo()); update.setCheckDate(new Date()); update.setCheckorId(loginUserInfo.getId()); companyUserApplyJoinMapper.updateById(update); if(Constants.equalsInteger(companyUserApply.getStatus(),Constants.ONE) && StringUtils.isNotBlank(userApply.getCompanyIds())){ List ids = Arrays.asList(userApply.getCompanyIds().split(",")); List companyPermissionList = new ArrayList<>(); for (String id:ids) { CompanyPermission companyPermission = new CompanyPermission(); companyPermission.setCreator(loginUserInfo.getId()); companyPermission.setCreateDate(new Date()); companyPermission.setIsdeleted(Constants.ZERO); companyPermission.setUserId(userApply.getUserId()); companyPermission.setCompanyId(Integer.valueOf(id)); companyPermission.setType(Constants.ONE); companyPermissionList.add(companyPermission); } companyPermissionMapper.insert(companyPermissionList); } //存储待办信息 Constants.NoticeObjectType noticeObjectType = Constants.NoticeObjectType.COMPANY_USER_APPLY; //删除其他待办 noticesMapper.delete(new QueryWrapper().lambda().eq(Notices::getObjType,noticeObjectType.getKey()) .ne(Notices::getType,Constants.NoticeType.SIX.getStatus()) .eq(Notices::getObjId,update.getId())); } }