123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- 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<CustomOptionMapper, CustomOption> {
- @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<MultipartFile> files = new ArrayList<>();
- Integer id = (Integer) treeNodeService.addMessage(treeNodeDTO, files);
- return id;
- } else {
- //检查名称是否规范
- customOptionDTO.setName(name.trim());
- //同一个自定义栏位下值选项不能重复
- LambdaQueryWrapper<CustomOption> queryWrapper = new LambdaQueryWrapper<>();
- queryWrapper.eq(CustomOption::getName, name);
- queryWrapper.eq(CustomOption::getCustomFieldId, customOptionDTO.getCustomFieldId());
- List<CustomOption> 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<MultipartFile> files = new ArrayList<>();
- id = (Integer) treeNodeService.updateMessage(updateTreeNodeDTO, files);
- } else {
- CustomOption customOption = this.getById(updateCustomOptionDTO.getId());
- //检查名称是否规范
- updateCustomOptionDTO.setName(name.trim());
- //同一个自定义栏位下值选项不能重复
- LambdaQueryWrapper<CustomOption> queryWrapper = new LambdaQueryWrapper<>();
- queryWrapper.eq(CustomOption::getName, name);
- queryWrapper.eq(CustomOption::getCustomFieldId, updateCustomOptionDTO.getCustomFieldId());
- List<CustomOption> 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<CustomOptionVO> customOptionVOS = new ArrayList<>();
- if (!customFieldId.equals(null) && customFieldId != 0) {
- List<String> createIds = new ArrayList<>();
- LambdaQueryWrapper<CustomOption> queryWrapper = new LambdaQueryWrapper<>();
- queryWrapper.eq(CustomOption::getCustomFieldId, customFieldId);
- List<CustomOption> customOptions = this.list(queryWrapper);
- customOptions.forEach(
- item -> {
- if (item.getCreateId() != null) {
- createIds.add(item.getCreateId());
- }
- }
- );
- List<Personnel> 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<CustomOptionDeleteDTO> 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<Integer> deleteIds = new ArrayList<>();
- deleteIds.add(customOptionDeleteDTO.getId());
- if (customFieldType.equals(6)) {
- treeNodeService.deleteMessage(deleteIds);
- } else {
- this.removeBatchByIds(deleteIds);
- }
- }
- } else {
- throw new XiaoShiException("参数错误");
- }
- }
- public List<String> getIdsByNames(List<String> names,Integer fieldId){
- List<String> ids =new ArrayList<>();
- names.forEach(item->{
- //根据名称和id查询
- LambdaQueryWrapper<CustomOption> 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;
- }
- }
|