package cn.cslg.pas.service.business; import cn.cslg.pas.common.dto.business.*; import cn.cslg.pas.common.model.cronModel.Personnel; import cn.cslg.pas.common.model.cronModel.PersonnelVO; import cn.cslg.pas.common.model.cronModel.Records; import cn.cslg.pas.common.utils.CacheUtils; import cn.cslg.pas.common.utils.LoginUtils; import cn.cslg.pas.common.vo.business.CustomOptionVO; import cn.cslg.pas.domain.business.CustomField; import cn.cslg.pas.domain.business.CustomOption; import cn.cslg.pas.exception.UnLoginException; import cn.cslg.pas.exception.XiaoShiException; import cn.cslg.pas.mapper.CustomOptionMapper; import cn.cslg.pas.service.permissions.PermissionService; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * 自定义选项的Service层 * @Author xiexiang * @Date 2023/11/8 */ @Service public class CustomOptionService extends ServiceImpl { @Autowired private CacheUtils cacheUtils; @Autowired private LoginUtils loginUtils; @Autowired private PermissionService permissionService; @Autowired private CustomFieldService customFieldService; @Autowired private TreeNodeService treeNodeService; /** * 新增自定义选项 * @param customOptionDTO * @return */ public Integer addCustomOption(CustomOptionDTO customOptionDTO) { if (customOptionDTO.getName().equals(null)) { throw new XiaoShiException("选项值不能为空!"); } if (customOptionDTO.getCustomFieldId().equals(null)) { throw new XiaoShiException("所属自定义栏位不能为空!"); } String name = customOptionDTO.getName(); //所属自定义栏位的id Integer customFieldId = customOptionDTO.getCustomFieldId(); //查询自定义栏位的类型,如果为树,则调用架构部分 CustomField customField = customFieldService.getById(customFieldId); Integer customFieldType = customField.getType(); if (customFieldType.equals(6)) { TreeNodeDTO treeNodeDTO = new TreeNodeDTO(); treeNodeDTO.setName(name); //自定义树 设置为4 treeNodeDTO.setType(4); treeNodeDTO.setParentId(customOptionDTO.getParentId()); treeNodeDTO.setTypeId(customOptionDTO.getCustomFieldId()); List files = new ArrayList<>(); Integer id = (Integer) treeNodeService.addMessage(treeNodeDTO, files); return id; } else { //检查名称是否规范 customOptionDTO.setName(name.trim()); //同一个自定义栏位下值选项不能重复 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(CustomOption::getName, name); queryWrapper.eq(CustomOption::getCustomFieldId, customOptionDTO.getCustomFieldId()); List customOptions = this.list(queryWrapper); if (customOptions != null && customOptions.size() != 0) { throw new XiaoShiException("名称不能重复"); } //获取登录人信息 PersonnelVO personnelVO = new PersonnelVO(); try { personnelVO = cacheUtils.getLoginUser(loginUtils.getId()); } catch (Exception e) { throw new UnLoginException("未登录"); } CustomOption customOption = new CustomOption(); BeanUtils.copyProperties(customOptionDTO, customOption); customOption.setCreateId(personnelVO.getId()); customOption.insert(); return customOption.getId(); } } /** * 更新自定义选项 * @param updateCustomOptionDTO * @return */ public Integer updateCustomOption(UpdateCustomOptionDTO updateCustomOptionDTO) { if (updateCustomOptionDTO.getName().equals(null)) { throw new XiaoShiException("选项值不能为空!"); } if (updateCustomOptionDTO.getCustomFieldId().equals(null)) { throw new XiaoShiException("所属自定义栏位不能为空!"); } Integer id = null; String name = updateCustomOptionDTO.getName(); //所属自定义栏位的id Integer customFieldId = updateCustomOptionDTO.getCustomFieldId(); //查询自定义栏位的类型,如果为树,则调用架构部分 CustomField customField = customFieldService.getById(customFieldId); Integer customFieldType = customField.getType(); if (customFieldType.equals(6)) { UpdateTreeNodeDTO updateTreeNodeDTO = new UpdateTreeNodeDTO(); BeanUtils.copyProperties(updateCustomOptionDTO, updateTreeNodeDTO); List files = new ArrayList<>(); id = (Integer) treeNodeService.updateMessage(updateTreeNodeDTO, files); } else { CustomOption customOption = this.getById(updateCustomOptionDTO.getId()); //检查名称是否规范 updateCustomOptionDTO.setName(name.trim()); //同一个自定义栏位下值选项不能重复 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(CustomOption::getName, name); queryWrapper.eq(CustomOption::getCustomFieldId, updateCustomOptionDTO.getCustomFieldId()); List customOptions = this.list(queryWrapper); if (!name.equals(customOption.getName()) && customOptions.size() != 0) { throw new XiaoShiException("名称重复"); } BeanUtils.copyProperties(updateCustomOptionDTO, customOption); customOption.updateById(); id = customOption.getId(); } return id; } /** * 查询自定义选项 * @param getCustomOptionDTO * @return * @throws Exception */ public Records getCustomOption(GetCustomOptionDTO getCustomOptionDTO) throws Exception { Integer customFieldId = getCustomOptionDTO.getCustomFieldId(); Records records = new Records(); CustomField customField = customFieldService.getById(customFieldId); Integer customFieldType = customField.getType(); if (customFieldType.equals(6)) { QueryTreeNodeDTO queryTreeNodeDTO = new QueryTreeNodeDTO(); queryTreeNodeDTO.setName(getCustomOptionDTO.getName()); queryTreeNodeDTO.setType(4); queryTreeNodeDTO.setTypeId(customFieldId); records = treeNodeService.queryMessage(queryTreeNodeDTO); } else { List customOptionVOS = new ArrayList<>(); if (!customFieldId.equals(null) && customFieldId != 0) { List createIds = new ArrayList<>(); LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(CustomOption::getCustomFieldId, customFieldId); List customOptions = this.list(queryWrapper); customOptions.forEach( item -> { if (item.getCreateId() != null) { createIds.add(item.getCreateId()); } } ); List personnels = new ArrayList<>(); //查询创建人名称 if (createIds.size() != 0) { String res = permissionService.getPersonnelByIdsFromPCS(createIds); JSONObject jsonObject = JSONObject.parseObject(res); personnels = JSONObject.parseArray(jsonObject.getString("data"), Personnel.class); } if (customOptions != null && customOptions.size() != 0) { for (CustomOption customOption : customOptions) { CustomOptionVO customOptionVO = new CustomOptionVO(); BeanUtils.copyProperties(customOption, customOptionVO); //装载人员信息 Personnel personnel = personnels.stream().filter(item -> item.getId().equals(customOptionVO.getCreateId())).findFirst().orElse(null); if (personnel != null) { customOptionVO.setCreateName(personnel.getPersonnelName()); } customOptionVOS.add(customOptionVO); } } } else { throw new XiaoShiException("错误参数"); } records.setData(customOptionVOS); } return records; } /** * 删除自定义选项 * @param customOptionDeleteDTOS * @throws IOException */ public void deleteCustomOption(List customOptionDeleteDTOS) throws IOException { if (customOptionDeleteDTOS != null && customOptionDeleteDTOS.size() > 0) { for (CustomOptionDeleteDTO customOptionDeleteDTO : customOptionDeleteDTOS) { Integer customFieldId = customOptionDeleteDTO.getCustomFieldId(); CustomField customField = customFieldService.getById(customFieldId); Integer customFieldType = customField.getType(); List deleteIds = new ArrayList<>(); deleteIds.add(customOptionDeleteDTO.getId()); if (customFieldType.equals(6)) { treeNodeService.deleteMessage(deleteIds); } else { this.removeBatchByIds(deleteIds); } } } else { throw new XiaoShiException("参数错误"); } } public List getIdsByNames(List names,Integer fieldId){ List ids =new ArrayList<>(); names.forEach(item->{ //根据名称和id查询 LambdaQueryWrapper queryWrapper =new LambdaQueryWrapper<>(); queryWrapper.eq(CustomOption::getName,item) .eq(CustomOption::getCustomFieldId,fieldId); CustomOption customOption =this.getOne(queryWrapper); if (customOption==null){ customOption =new CustomOption(); customOption.setCustomFieldId(fieldId); customOption.setName(item); customOption.insert(); } ids.add(customOption.getId().toString()); }); return ids; } }