|  |  |  | 
|---|
|  |  |  | package com.doumee.service.business.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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.InsuranceMapper; | 
|---|
|  |  |  | import com.doumee.dao.business.SolutionWorktypeMapper; | 
|---|
|  |  |  | import com.doumee.dao.business.SolutionsMapper; | 
|---|
|  |  |  | import com.doumee.dao.business.model.Solutions; | 
|---|
|  |  |  | import com.doumee.dao.business.WorktypeMapper; | 
|---|
|  |  |  | import com.doumee.dao.business.model.*; | 
|---|
|  |  |  | import com.doumee.service.business.SolutionsService; | 
|---|
|  |  |  | 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 org.apache.commons.lang3.StringUtils; | 
|---|
|  |  |  | import org.apache.shiro.SecurityUtils; | 
|---|
|  |  |  | import org.springframework.beans.BeanUtils; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.util.CollectionUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.math.BigDecimal; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.UUID; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 保险方案信息表Service实现 | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SolutionsMapper solutionsMapper; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private WorktypeMapper worktypeMapper; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SolutionWorktypeMapper solutionWorktypeMapper; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private InsuranceMapper insuranceMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Integer create(Solutions solutions) { | 
|---|
|  |  |  | solutionsMapper.insert(solutions); | 
|---|
|  |  |  | LoginUserInfo user= (LoginUserInfo)SecurityUtils.getSubject().getPrincipal(); | 
|---|
|  |  |  | initCreateParam(solutions);//工种数据有效性检验,去除空白行数据 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | solutions.setIsdeleted(Constants.ZERO); | 
|---|
|  |  |  | solutions.setCreator(user.getId()); | 
|---|
|  |  |  | solutions.setCreateDate(new Date()); | 
|---|
|  |  |  | solutions.setVersion(UUID.randomUUID().toString());//版本号 | 
|---|
|  |  |  | solutions.setDataType(Constants.ZERO); | 
|---|
|  |  |  | solutions.setStatus(Constants.ZERO); | 
|---|
|  |  |  | solutionsMapper.insert(solutions);//基础版本 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //如果有工种,则产生一个新的有效历史版本 ~ | 
|---|
|  |  |  | Solutions newModel = new Solutions(); | 
|---|
|  |  |  | BeanUtils.copyProperties(solutions,newModel); | 
|---|
|  |  |  | newModel.setId(null); | 
|---|
|  |  |  | newModel.setBaseId(solutions.getId()); | 
|---|
|  |  |  | newModel.setDataType(Constants.TWO); | 
|---|
|  |  |  | solutionsMapper.insert(newModel); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | dealWorkType(solutions,newModel,solutions.getWorktypeIdList(),true); | 
|---|
|  |  |  | return solutions.getId(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void dealWorkType(Solutions solutions, Solutions newModel, List<Integer> worktypeIdList, boolean b) { | 
|---|
|  |  |  | Insurance  model = insuranceMapper.selectById(solutions.getInsuranceId()); | 
|---|
|  |  |  | if(model == null  || !Constants.equalsInteger(model.getIsdeleted(),Constants.ZERO) | 
|---|
|  |  |  | || !Constants.equalsInteger(model.getDataType(),Constants.TWO)){ | 
|---|
|  |  |  | throw  new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"对不起,该保险公司信息不存在,请尝试刷新页面重试~" ); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if( !Constants.equalsInteger(model.getStatus(),Constants.ZERO)){ | 
|---|
|  |  |  | throw  new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"对不起,该保险公司已被,请尝试刷新页面重试~" ); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<Worktype> worktypeList = worktypeMapper.selectList(new QueryWrapper<Worktype>().lambda() | 
|---|
|  |  |  | .eq(Worktype::getInsuranceId,solutions.getInsuranceId()) | 
|---|
|  |  |  | .eq(Worktype::getIsdeleted,Constants.ZERO) | 
|---|
|  |  |  | .in(Worktype::getId,worktypeIdList)); | 
|---|
|  |  |  | if(worktypeList==null ||worktypeIdList.size()==0){ | 
|---|
|  |  |  | throw  new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"对不起,工种信息选择有误,请尝试刷新页面重试~" ); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | for(Worktype type : worktypeList) { | 
|---|
|  |  |  | //基础版本 | 
|---|
|  |  |  | SolutionWorktype w = new SolutionWorktype(); | 
|---|
|  |  |  | w.setSolutionId(solutions.getId()); | 
|---|
|  |  |  | w.setIsdeleted(Constants.ZERO); | 
|---|
|  |  |  | w.setCreator(newModel.getCreator()); | 
|---|
|  |  |  | w.setCreateDate(solutions.getCreateDate()); | 
|---|
|  |  |  | w.setWorktypeId(type.getId()); | 
|---|
|  |  |  | w.setStatus(Constants.ZERO); | 
|---|
|  |  |  | solutionWorktypeMapper.insert(w); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //历史版本的工种信息 | 
|---|
|  |  |  | SolutionWorktype newType = new SolutionWorktype(); | 
|---|
|  |  |  | BeanUtils.copyProperties(w, newType); | 
|---|
|  |  |  | newType.setSolutionId(newModel.getId()); | 
|---|
|  |  |  | solutionWorktypeMapper.insert(newType); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void initCreateParam(Solutions solutions) { | 
|---|
|  |  |  | if(StringUtils.isBlank(solutions.getName()) | 
|---|
|  |  |  | ||solutions.getInsuranceId()==null | 
|---|
|  |  |  | ||solutions.getWorktypeIdList()==null | 
|---|
|  |  |  | ||solutions.getWorktypeIdList().size()==0 | 
|---|
|  |  |  | ||Constants.formatIntegerNum(solutions.getMaxAge()) < 0 | 
|---|
|  |  |  | ||Constants.formatIntegerNum(solutions.getMinAge()) < 0 | 
|---|
|  |  |  | ||Constants.formatIntegerNum(solutions.getPriceCycleUnit()) < 0 | 
|---|
|  |  |  | ||Constants.formatIntegerNum(solutions.getInsureCycle()) < 0 | 
|---|
|  |  |  | ||Constants.formatIntegerNum(solutions.getInsureCycleUnit()) < 0 | 
|---|
|  |  |  | || solutions.getMinAge() >solutions.getMaxAge() | 
|---|
|  |  |  | || Constants.formatBigdecimal(solutions.getPrice()).compareTo(new BigDecimal(0)) !=1 | 
|---|
|  |  |  | ){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.BAD_REQUEST); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void updateById(Solutions solutions) { | 
|---|
|  |  |  | LoginUserInfo user= (LoginUserInfo)SecurityUtils.getSubject().getPrincipal(); | 
|---|
|  |  |  | Solutions model = findById(solutions.getId()); | 
|---|
|  |  |  | if(model == null  || !Constants.equalsInteger(model.getIsdeleted(),Constants.ZERO) | 
|---|
|  |  |  | || !Constants.equalsInteger(model.getDataType(),Constants.ZERO)){ | 
|---|
|  |  |  | throw  new BusinessException(ResponseStatus.DATA_EMPTY ); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //数据有效性校验 | 
|---|
|  |  |  | initCreateParam(solutions); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | solutions.setEditor(user.getId()); | 
|---|
|  |  |  | solutions.setVersion(UUID.randomUUID().toString()); | 
|---|
|  |  |  | solutions.setEditDate(new Date()); | 
|---|
|  |  |  | solutionsMapper.updateById(solutions); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //如果修改,则产生一个新的历史版本 ~ | 
|---|
|  |  |  | Solutions newModel = new Solutions(); | 
|---|
|  |  |  | BeanUtils.copyProperties(model,newModel); | 
|---|
|  |  |  | newModel.setId(null); | 
|---|
|  |  |  | newModel.setVersion(solutions.getVersion()); | 
|---|
|  |  |  | newModel.setCreateDate(new Date()); | 
|---|
|  |  |  | newModel.setBaseId(solutions.getId()); | 
|---|
|  |  |  | newModel.setDataType(Constants.TWO); | 
|---|
|  |  |  | solutionsMapper.insert(newModel); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //删除所有工种数据 | 
|---|
|  |  |  | solutionWorktypeMapper.delete(new UpdateWrapper<SolutionWorktype>() | 
|---|
|  |  |  | .lambda() | 
|---|
|  |  |  | .eq(SolutionWorktype::getSolutionId,solutions.getId()) | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | solutionsMapper.update(null,new UpdateWrapper<Solutions>() | 
|---|
|  |  |  | .lambda() | 
|---|
|  |  |  | .eq(Solutions::getBaseId,solutions.getId()) | 
|---|
|  |  |  | .eq(Solutions::getDataType,Constants.TWO) | 
|---|
|  |  |  | .set(Solutions::getDataType,Constants.ONE) | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | //处理工作信息,新增最新的,同时产生历史版本 | 
|---|
|  |  |  | dealWorkType(solutions,newModel,solutions.getWorktypeIdList(),false); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | 
|---|
|  |  |  | this.updateById(solutions); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void updateStatus(Solutions solutions){ | 
|---|
|  |  |  | if(solutions.getId() == null || solutions.getStatus()==null || solutions.getStatus()<0||solutions.getStatus()>1){ | 
|---|
|  |  |  | throw  new BusinessException(ResponseStatus.BAD_REQUEST ); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Solutions model = findById(solutions.getId()); | 
|---|
|  |  |  | if(model == null || !Constants.equalsInteger(model.getIsdeleted(),Constants.ZERO) | 
|---|
|  |  |  | || !Constants.equalsInteger(model.getDataType(),Constants.ZERO)){ | 
|---|
|  |  |  | throw  new BusinessException(ResponseStatus.DATA_EMPTY ); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(Constants.equalsInteger(model.getStatus(),solutions.getStatus())){ | 
|---|
|  |  |  | //如果状态不发生改变,直接返回 | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | LoginUserInfo user= (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); | 
|---|
|  |  |  | //同时更新基表和历史版本所有数据状态 | 
|---|
|  |  |  | solutionsMapper.update(null,new UpdateWrapper<Solutions>() | 
|---|
|  |  |  | .lambda() | 
|---|
|  |  |  | .and(m -> m.eq(Solutions::getId,model.getId()).or().eq(Solutions::getBaseId,model.getId())) | 
|---|
|  |  |  | .eq(Solutions::getIsdeleted,Constants.ZERO) | 
|---|
|  |  |  | .set(Solutions::getEditDate,new Date()) | 
|---|
|  |  |  | .set(Solutions::getEditor,user.getId()) | 
|---|
|  |  |  | .set(Solutions::getStatus,solutions.getStatus()) | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Solutions findById(Integer id) { | 
|---|
|  |  |  | return solutionsMapper.selectById(id); | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public List<Solutions> findList(Solutions solutions) { | 
|---|
|  |  |  | solutions.setIsdeleted(Constants.ZERO); | 
|---|
|  |  |  | QueryWrapper<Solutions> wrapper = new QueryWrapper<>(solutions); | 
|---|
|  |  |  | return solutionsMapper.selectList(wrapper); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | IPage<Solutions> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity()); | 
|---|
|  |  |  | QueryWrapper<Solutions> queryWrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | Utils.MP.blankToNull(pageWrap.getModel()); | 
|---|
|  |  |  | pageWrap.getModel().setDataType(Constants.ZERO);//只选择基表数据 | 
|---|
|  |  |  | pageWrap.getModel().setIsdeleted(Constants.ZERO); | 
|---|
|  |  |  | if (pageWrap.getModel().getId() != null) { | 
|---|
|  |  |  | queryWrapper.lambda().eq(Solutions::getId, pageWrap.getModel().getId()); | 
|---|
|  |  |  | } | 
|---|