|
@@ -1,13 +1,31 @@
|
|
|
package com.qmth.sop.business.service.impl;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.qmth.sop.business.bean.params.ServiceUnitParam;
|
|
|
+import com.qmth.sop.business.bean.result.ReductionResult;
|
|
|
+import com.qmth.sop.business.bean.result.ServiceUnitResult;
|
|
|
+import com.qmth.sop.business.entity.SysRole;
|
|
|
+import com.qmth.sop.business.entity.SysUser;
|
|
|
import com.qmth.sop.business.entity.TBService;
|
|
|
import com.qmth.sop.business.mapper.TBServiceMapper;
|
|
|
+import com.qmth.sop.business.service.SysUserRoleService;
|
|
|
import com.qmth.sop.business.service.TBServiceService;
|
|
|
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.qmth.sop.common.contant.SystemConstant;
|
|
|
+import com.qmth.sop.common.enums.ExceptionResultEnum;
|
|
|
+import com.qmth.sop.common.enums.ProductTypeEnum;
|
|
|
+import com.qmth.sop.common.enums.RoleTypeEnum;
|
|
|
+import com.qmth.sop.common.enums.ServiceStatusEnum;
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* <p>
|
|
@@ -19,9 +37,161 @@ import java.util.List;
|
|
|
*/
|
|
|
@Service
|
|
|
public class TBServiceServiceImpl extends ServiceImpl<TBServiceMapper, TBService> implements TBServiceService {
|
|
|
+ @Resource
|
|
|
+ private SysUserRoleService sysUserRoleService;
|
|
|
|
|
|
@Override
|
|
|
public List<TBService> listEnable(Long id) {
|
|
|
- return this.list(new QueryWrapper<TBService>().lambda().eq(TBService::getEnable,true).eq(TBService::getId,id));
|
|
|
+ return this.list(new QueryWrapper<TBService>().lambda().eq(TBService::getEnable, true).eq(TBService::getId, id));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ @Override
|
|
|
+ public Long edit(ServiceUnitParam serviceUnitParam, SysUser requestUser) {
|
|
|
+ Long id = serviceUnitParam.getId();
|
|
|
+ Long leaderId = serviceUnitParam.getServiceLeadId();
|
|
|
+ Long startTime = serviceUnitParam.getStartTime();
|
|
|
+ Long endTime = serviceUnitParam.getEndTime();
|
|
|
+
|
|
|
+ Integer regionPeopleCount = serviceUnitParam.getRegionPeopleCount();
|
|
|
+ Integer regionProjectCount = serviceUnitParam.getRegionProjectCount();
|
|
|
+ ReductionResult reduction = this.reduction(regionPeopleCount, regionProjectCount);
|
|
|
+
|
|
|
+
|
|
|
+ if (endTime < startTime) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("服务截止时间不能小于服务开始时间");
|
|
|
+ }
|
|
|
+ List<SysRole> sysRoleList = sysUserRoleService.listRoleByUserId(leaderId);
|
|
|
+ if (CollectionUtils.isNotEmpty(sysRoleList)) {
|
|
|
+ if (!sysRoleList.stream().map(SysRole::getType).collect(Collectors.toList()).contains(RoleTypeEnum.BUSSINESS)) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("不包含'业务线负责人'角色的用户不能充当服务单元的负责人");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("不包含'业务线负责人'角色的用户不能充当服务单元负责人");
|
|
|
+ }
|
|
|
+
|
|
|
+ TBService tbService = new TBService();
|
|
|
+ tbService.setName(serviceUnitParam.getName());
|
|
|
+ tbService.setType(serviceUnitParam.getType());
|
|
|
+ tbService.setStartTime(serviceUnitParam.getStartTime());
|
|
|
+ tbService.setEndTime(serviceUnitParam.getEndTime());
|
|
|
+ tbService.setRegionPeopleCount(reduction.getMolecular());
|
|
|
+ tbService.setRegionProjectCount(reduction.getDenominator());
|
|
|
+ tbService.setServiceLeadId(serviceUnitParam.getServiceLeadId());
|
|
|
+ if (Objects.isNull(id)) {
|
|
|
+ // id不存在新增
|
|
|
+ tbService.setEnable(true);
|
|
|
+ tbService.setStatus(ServiceStatusEnum.NEW);
|
|
|
+ this.save(tbService);
|
|
|
+ id = tbService.getId();
|
|
|
+ } else {
|
|
|
+ // id存在更新
|
|
|
+ tbService.setId(id);
|
|
|
+ this.updateById(tbService);
|
|
|
+ }
|
|
|
+ return id;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public IPage<ServiceUnitResult> page(ProductTypeEnum type, Long serviceLeadId, ServiceStatusEnum status,
|
|
|
+ Long createStartTime, Long createEndTime, String serviceUnitName, Integer pageNumber, Integer pageSize) {
|
|
|
+ if (serviceUnitName != null && serviceUnitName.length() > 0) {
|
|
|
+ serviceUnitName = SystemConstant.translateSpecificSign(serviceUnitName);
|
|
|
+ }
|
|
|
+ return this.baseMapper.findServiceUnitPage(new Page<>(pageNumber, pageSize), type, serviceLeadId, status, createStartTime, createEndTime, serviceUnitName);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ @Override
|
|
|
+ public void cancelServiceUnit(Long serviceUnitId) {
|
|
|
+ TBService tbService = this.getById(serviceUnitId);
|
|
|
+ if (Objects.isNull(tbService)) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("未找到服务单元");
|
|
|
+ }
|
|
|
+ ServiceStatusEnum status = tbService.getStatus();
|
|
|
+ if (!ServiceStatusEnum.NEW.equals(status)) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception(String.format("只有[%s]状态的服务单元可以被删除,当前状态[%s]", ServiceStatusEnum.NEW, status));
|
|
|
+ }
|
|
|
+ tbService.setStatus(ServiceStatusEnum.CANCEL);
|
|
|
+ tbService.setEnable(false);
|
|
|
+ this.updateById(tbService);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ @Override
|
|
|
+ public void publishServiceUnit(Long serviceUnitId) {
|
|
|
+ TBService tbService = this.getById(serviceUnitId);
|
|
|
+ if (Objects.isNull(tbService)) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("未找到服务单元");
|
|
|
+ }
|
|
|
+ ServiceStatusEnum status = tbService.getStatus();
|
|
|
+ if (!ServiceStatusEnum.NEW.equals(status)) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception(String.format("只有[%s]状态的服务单元可以被发布,当前状态[%s]", ServiceStatusEnum.NEW, status));
|
|
|
+ }
|
|
|
+ // TODO: 2023/8/8 业务校验 服务单元完成了相关准备工作:服务范围划定、供应商划定
|
|
|
+ tbService.setStatus(ServiceStatusEnum.PUBLISH);
|
|
|
+ this.updateById(tbService);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ @Override
|
|
|
+ public void clozeServiceUnit(Long serviceUnitId) {
|
|
|
+ TBService tbService = this.getById(serviceUnitId);
|
|
|
+ if (Objects.isNull(tbService)) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("未找到服务单元");
|
|
|
+ }
|
|
|
+ ServiceStatusEnum status = tbService.getStatus();
|
|
|
+ if (!ServiceStatusEnum.PUBLISH.equals(status)) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception(String.format("只有[%s]状态的服务单元可以被关闭,当前状态[%s]", ServiceStatusEnum.PUBLISH, status));
|
|
|
+ }
|
|
|
+ tbService.setStatus(ServiceStatusEnum.FINISH);
|
|
|
+ this.updateById(tbService);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ @Override
|
|
|
+ public void restartServiceUnit(Long serviceUnitId) {
|
|
|
+ TBService tbService = this.getById(serviceUnitId);
|
|
|
+ if (Objects.isNull(tbService)) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("未找到服务单元");
|
|
|
+ }
|
|
|
+ ServiceStatusEnum status = tbService.getStatus();
|
|
|
+ if (!ServiceStatusEnum.FINISH.equals(status)) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception(String.format("只有[%s]状态的服务单元可以被重启,当前状态[%s]", ServiceStatusEnum.FINISH, status));
|
|
|
+ }
|
|
|
+ tbService.setStatus(ServiceStatusEnum.PUBLISH);
|
|
|
+ this.updateById(tbService);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 约分
|
|
|
+ *
|
|
|
+ * @param molecular 分子 eg.10
|
|
|
+ * @param denominator 分母 eg.8
|
|
|
+ * @return 约分后的结果 map -> k:分子 v:分母 eg. map->(5,4)
|
|
|
+ */
|
|
|
+ private ReductionResult reduction(Integer molecular, Integer denominator) {
|
|
|
+ if (SystemConstant.isOneNull(molecular, denominator)) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("约分失败,分子或分母不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ int min = (molecular < denominator) ? molecular : denominator;
|
|
|
+ int max = (molecular > denominator) ? molecular : denominator;
|
|
|
+
|
|
|
+ if (max % min != 0) {
|
|
|
+ min = min / 2 + 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ int maxMultiple = 1;
|
|
|
+ for (int i = min; i > 1; i--) {
|
|
|
+ if (molecular % i == 0 && denominator % i == 0) {
|
|
|
+ maxMultiple = i;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ReductionResult result = new ReductionResult();
|
|
|
+ result.setMolecular(molecular / maxMultiple);
|
|
|
+ result.setDenominator(denominator / maxMultiple);
|
|
|
+ return result;
|
|
|
}
|
|
|
-}
|
|
|
+}
|