CustomOptionService.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package cn.cslg.pas.service.business;
  2. import cn.cslg.pas.common.dto.business.*;
  3. import cn.cslg.pas.common.model.cronModel.Personnel;
  4. import cn.cslg.pas.common.model.cronModel.PersonnelVO;
  5. import cn.cslg.pas.common.model.cronModel.Records;
  6. import cn.cslg.pas.common.utils.CacheUtils;
  7. import cn.cslg.pas.common.utils.LoginUtils;
  8. import cn.cslg.pas.common.vo.business.CustomOptionVO;
  9. import cn.cslg.pas.domain.business.CustomField;
  10. import cn.cslg.pas.domain.business.CustomOption;
  11. import cn.cslg.pas.exception.UnLoginException;
  12. import cn.cslg.pas.exception.XiaoShiException;
  13. import cn.cslg.pas.mapper.CustomOptionMapper;
  14. import cn.cslg.pas.service.permissions.PermissionService;
  15. import com.alibaba.fastjson.JSONObject;
  16. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  17. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  18. import org.springframework.beans.BeanUtils;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Service;
  21. import org.springframework.web.multipart.MultipartFile;
  22. import java.io.IOException;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. /**
  26. * 自定义选项的Service层
  27. * @Author xiexiang
  28. * @Date 2023/11/8
  29. */
  30. @Service
  31. public class CustomOptionService extends ServiceImpl<CustomOptionMapper, CustomOption> {
  32. @Autowired
  33. private CacheUtils cacheUtils;
  34. @Autowired
  35. private LoginUtils loginUtils;
  36. @Autowired
  37. private PermissionService permissionService;
  38. @Autowired
  39. private CustomFieldService customFieldService;
  40. @Autowired
  41. private TreeNodeService treeNodeService;
  42. /**
  43. * 新增自定义选项
  44. * @param customOptionDTO
  45. * @return
  46. */
  47. public Integer addCustomOption(CustomOptionDTO customOptionDTO) {
  48. if (customOptionDTO.getName().equals(null)) {
  49. throw new XiaoShiException("选项值不能为空!");
  50. }
  51. if (customOptionDTO.getCustomFieldId().equals(null)) {
  52. throw new XiaoShiException("所属自定义栏位不能为空!");
  53. }
  54. String name = customOptionDTO.getName();
  55. //所属自定义栏位的id
  56. Integer customFieldId = customOptionDTO.getCustomFieldId();
  57. //查询自定义栏位的类型,如果为树,则调用架构部分
  58. CustomField customField = customFieldService.getById(customFieldId);
  59. Integer customFieldType = customField.getType();
  60. if (customFieldType.equals(6)) {
  61. TreeNodeDTO treeNodeDTO = new TreeNodeDTO();
  62. treeNodeDTO.setName(name);
  63. //自定义树 设置为4
  64. treeNodeDTO.setType(4);
  65. treeNodeDTO.setParentId(customOptionDTO.getParentId());
  66. treeNodeDTO.setTypeId(customOptionDTO.getCustomFieldId());
  67. List<MultipartFile> files = new ArrayList<>();
  68. Integer id = (Integer) treeNodeService.addMessage(treeNodeDTO, files);
  69. return id;
  70. } else {
  71. //检查名称是否规范
  72. customOptionDTO.setName(name.trim());
  73. //同一个自定义栏位下值选项不能重复
  74. LambdaQueryWrapper<CustomOption> queryWrapper = new LambdaQueryWrapper<>();
  75. queryWrapper.eq(CustomOption::getName, name);
  76. queryWrapper.eq(CustomOption::getCustomFieldId, customOptionDTO.getCustomFieldId());
  77. List<CustomOption> customOptions = this.list(queryWrapper);
  78. if (customOptions != null && customOptions.size() != 0) {
  79. throw new XiaoShiException("名称不能重复");
  80. }
  81. //获取登录人信息
  82. PersonnelVO personnelVO = new PersonnelVO();
  83. try {
  84. personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
  85. } catch (Exception e) {
  86. throw new UnLoginException("未登录");
  87. }
  88. CustomOption customOption = new CustomOption();
  89. BeanUtils.copyProperties(customOptionDTO, customOption);
  90. customOption.setCreateId(personnelVO.getId());
  91. customOption.insert();
  92. return customOption.getId();
  93. }
  94. }
  95. /**
  96. * 更新自定义选项
  97. * @param updateCustomOptionDTO
  98. * @return
  99. */
  100. public Integer updateCustomOption(UpdateCustomOptionDTO updateCustomOptionDTO) {
  101. if (updateCustomOptionDTO.getName().equals(null)) {
  102. throw new XiaoShiException("选项值不能为空!");
  103. }
  104. if (updateCustomOptionDTO.getCustomFieldId().equals(null)) {
  105. throw new XiaoShiException("所属自定义栏位不能为空!");
  106. }
  107. Integer id = null;
  108. String name = updateCustomOptionDTO.getName();
  109. //所属自定义栏位的id
  110. Integer customFieldId = updateCustomOptionDTO.getCustomFieldId();
  111. //查询自定义栏位的类型,如果为树,则调用架构部分
  112. CustomField customField = customFieldService.getById(customFieldId);
  113. Integer customFieldType = customField.getType();
  114. if (customFieldType.equals(6)) {
  115. UpdateTreeNodeDTO updateTreeNodeDTO = new UpdateTreeNodeDTO();
  116. BeanUtils.copyProperties(updateCustomOptionDTO, updateTreeNodeDTO);
  117. List<MultipartFile> files = new ArrayList<>();
  118. id = (Integer) treeNodeService.updateMessage(updateTreeNodeDTO, files);
  119. } else {
  120. CustomOption customOption = this.getById(updateCustomOptionDTO.getId());
  121. //检查名称是否规范
  122. updateCustomOptionDTO.setName(name.trim());
  123. //同一个自定义栏位下值选项不能重复
  124. LambdaQueryWrapper<CustomOption> queryWrapper = new LambdaQueryWrapper<>();
  125. queryWrapper.eq(CustomOption::getName, name);
  126. queryWrapper.eq(CustomOption::getCustomFieldId, updateCustomOptionDTO.getCustomFieldId());
  127. List<CustomOption> customOptions = this.list(queryWrapper);
  128. if (!name.equals(customOption.getName()) && customOptions.size() != 0) {
  129. throw new XiaoShiException("名称重复");
  130. }
  131. BeanUtils.copyProperties(updateCustomOptionDTO, customOption);
  132. customOption.updateById();
  133. id = customOption.getId();
  134. }
  135. return id;
  136. }
  137. /**
  138. * 查询自定义选项
  139. * @param getCustomOptionDTO
  140. * @return
  141. * @throws Exception
  142. */
  143. public Records getCustomOption(GetCustomOptionDTO getCustomOptionDTO) throws Exception {
  144. Integer customFieldId = getCustomOptionDTO.getCustomFieldId();
  145. Records records = new Records();
  146. CustomField customField = customFieldService.getById(customFieldId);
  147. Integer customFieldType = customField.getType();
  148. if (customFieldType.equals(6)) {
  149. QueryTreeNodeDTO queryTreeNodeDTO = new QueryTreeNodeDTO();
  150. queryTreeNodeDTO.setName(getCustomOptionDTO.getName());
  151. queryTreeNodeDTO.setType(4);
  152. queryTreeNodeDTO.setTypeId(customFieldId);
  153. records = treeNodeService.queryMessage(queryTreeNodeDTO);
  154. } else {
  155. List<CustomOptionVO> customOptionVOS = new ArrayList<>();
  156. if (!customFieldId.equals(null) && customFieldId != 0) {
  157. List<String> createIds = new ArrayList<>();
  158. LambdaQueryWrapper<CustomOption> queryWrapper = new LambdaQueryWrapper<>();
  159. queryWrapper.eq(CustomOption::getCustomFieldId, customFieldId);
  160. List<CustomOption> customOptions = this.list(queryWrapper);
  161. customOptions.forEach(
  162. item -> {
  163. if (item.getCreateId() != null) {
  164. createIds.add(item.getCreateId());
  165. }
  166. }
  167. );
  168. List<Personnel> personnels = new ArrayList<>();
  169. //查询创建人名称
  170. if (createIds.size() != 0) {
  171. String res = permissionService.getPersonnelByIdsFromPCS(createIds);
  172. JSONObject jsonObject = JSONObject.parseObject(res);
  173. personnels = JSONObject.parseArray(jsonObject.getString("data"), Personnel.class);
  174. }
  175. if (customOptions != null && customOptions.size() != 0) {
  176. for (CustomOption customOption : customOptions) {
  177. CustomOptionVO customOptionVO = new CustomOptionVO();
  178. BeanUtils.copyProperties(customOption, customOptionVO);
  179. //装载人员信息
  180. Personnel personnel = personnels.stream().filter(item -> item.getId().equals(customOptionVO.getCreateId())).findFirst().orElse(null);
  181. if (personnel != null) {
  182. customOptionVO.setCreateName(personnel.getPersonnelName());
  183. }
  184. customOptionVOS.add(customOptionVO);
  185. }
  186. }
  187. } else {
  188. throw new XiaoShiException("错误参数");
  189. }
  190. records.setData(customOptionVOS);
  191. }
  192. return records;
  193. }
  194. /**
  195. * 删除自定义选项
  196. * @param customOptionDeleteDTOS
  197. * @throws IOException
  198. */
  199. public void deleteCustomOption(List<CustomOptionDeleteDTO> customOptionDeleteDTOS) throws IOException {
  200. if (customOptionDeleteDTOS != null && customOptionDeleteDTOS.size() > 0) {
  201. for (CustomOptionDeleteDTO customOptionDeleteDTO : customOptionDeleteDTOS) {
  202. Integer customFieldId = customOptionDeleteDTO.getCustomFieldId();
  203. CustomField customField = customFieldService.getById(customFieldId);
  204. Integer customFieldType = customField.getType();
  205. List<Integer> deleteIds = new ArrayList<>();
  206. deleteIds.add(customOptionDeleteDTO.getId());
  207. if (customFieldType.equals(6)) {
  208. treeNodeService.deleteMessage(deleteIds);
  209. } else {
  210. this.removeBatchByIds(deleteIds);
  211. }
  212. }
  213. } else {
  214. throw new XiaoShiException("参数错误");
  215. }
  216. }
  217. public List<String> getIdsByNames(List<String> names,Integer fieldId){
  218. List<String> ids =new ArrayList<>();
  219. names.forEach(item->{
  220. //根据名称和id查询
  221. LambdaQueryWrapper<CustomOption> queryWrapper =new LambdaQueryWrapper<>();
  222. queryWrapper.eq(CustomOption::getName,item)
  223. .eq(CustomOption::getCustomFieldId,fieldId);
  224. CustomOption customOption =this.getOne(queryWrapper);
  225. if (customOption==null){
  226. customOption =new CustomOption();
  227. customOption.setCustomFieldId(fieldId);
  228. customOption.setName(item);
  229. customOption.insert();
  230. }
  231. ids.add(customOption.getId().toString());
  232. });
  233. return ids;
  234. }
  235. }