package com.doumee.service.system.impl; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.doumee.core.constants.ResponseStatus; import com.doumee.core.exception.BusinessException; import com.doumee.core.utils.Constants; import com.doumee.dao.admin.request.LaborConfigDTO; import com.doumee.dao.admin.request.VisitConfigDTO; import com.doumee.dao.system.SystemDictMapper; import com.doumee.dao.system.model.SystemDict; import com.fasterxml.jackson.core.JsonProcessingException; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.doumee.core.model.PageData; import com.doumee.core.model.PageWrap; import com.doumee.dao.system.SystemDictDataMapper; import com.doumee.dao.system.dto.QuerySystemDictDataDTO; import com.doumee.dao.system.model.SystemDictData; import com.doumee.dao.system.vo.SystemDictDataListVO; import com.doumee.service.system.SystemDictDataService; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; /** * 字典数据Service实现 * @author Eva.Caesar Liu * @date 2023/03/21 14:49 */ @Service public class SystemDictDataServiceImpl implements SystemDictDataService { @Autowired private SystemDictDataMapper systemDictDataMapper; @Autowired private SystemDictMapper systemDictMapper; @Override public Integer create(SystemDictData systemDictData) { systemDictDataMapper.insert(systemDictData); return systemDictData.getId(); } @Override public void deleteById(Integer id) { SystemDictData systemDictData = new SystemDictData(); systemDictData.setId(id); systemDictData.setDeleted(Boolean.TRUE); this.updateById(systemDictData); } @Override @Transactional public void deleteByIdInBatch(List ids) { if (CollectionUtils.isEmpty(ids)) { return; } for (Integer id : ids) { this.deleteById(id); } } @Override public void updateById(SystemDictData systemDictData) { systemDictDataMapper.updateById(systemDictData); } @Override @Transactional public void updateByIdInBatch(List systemDictDatas) { if (CollectionUtils.isEmpty(systemDictDatas)) { return; } for (SystemDictData systemDictData: systemDictDatas) { this.updateById(systemDictData); } } @Override public SystemDictData findById(Integer id) { return systemDictDataMapper.selectById(id); } @Override public SystemDictData findOne(SystemDictData systemDictData) { Wrapper wrapper = new QueryWrapper<>(systemDictData); return systemDictDataMapper.selectOne(wrapper); } @Override public List findList(SystemDictData systemDictData) { Wrapper wrapper = new QueryWrapper<>(systemDictData); return systemDictDataMapper.selectList(wrapper); } @Override public PageData findPage(PageWrap pageWrap) { PageHelper.startPage(pageWrap.getPage(), pageWrap.getCapacity()); return PageData.from(new PageInfo<>(systemDictDataMapper.selectManageList(pageWrap.getModel()))); } @Override public long count(SystemDictData systemDictData) { Wrapper wrapper = new QueryWrapper<>(systemDictData); return systemDictDataMapper.selectCount(wrapper); } @Override public List findList(Integer dicId,List codes) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.lambda() .eq(Objects.nonNull(dicId),SystemDictData::getDictId,dicId) .in(SystemDictData::getLabel,codes); return systemDictDataMapper.selectList(wrapper); } @Override public VisitConfigDTO getVisitConfigDTO() { try { String jasonStr = Constants.toUnderlineJSONString(new VisitConfigDTO()); JSONObject parse = (JSONObject) JSONObject.parse(jasonStr); List collect = parse.entrySet().stream().map(s -> s.getKey().toUpperCase()).collect(Collectors.toList()); QueryWrapper systemDictQuery = new QueryWrapper<>(); systemDictQuery.lambda() .eq(SystemDict::getDeleted,Boolean.FALSE) .eq(SystemDict::getCode,Constants.VISIT_CONFIG); SystemDict systemDict = systemDictMapper.selectOne(systemDictQuery); if (Objects.isNull(systemDict)){ throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(),"字典不存在"); } QueryWrapper wrapper = new QueryWrapper<>(); wrapper.lambda() .eq(SystemDictData::getDictId,systemDict.getId()) .in(SystemDictData::getLabel,collect); List systemDictData = systemDictDataMapper.selectList(wrapper); if (CollectionUtils.isEmpty(systemDictData)){ throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(),"字典值不存在"); } systemDictData.forEach(s->{ parse.put(s.getLabel().toLowerCase(),s.getCode()); }); String s = parse.toJSONString(); VisitConfigDTO miniProgrammeDTO = Constants.toSnakeObject(s, VisitConfigDTO.class); return miniProgrammeDTO; } catch (Exception e) { throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"字典值解析有误"); } } @Override public void updateVisitConfig(VisitConfigDTO miniProgrammeDTO) { try { QueryWrapper systemDictQuery = new QueryWrapper<>(); systemDictQuery.lambda() .eq(SystemDict::getDeleted,Boolean.FALSE) .eq(SystemDict::getCode,Constants.VISIT_CONFIG); SystemDict systemDict = systemDictMapper.selectOne(systemDictQuery); if (Objects.isNull(systemDict)){ throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(),"字典不存在"); } String jasonStr = Constants.toUnderlineJSONString(miniProgrammeDTO); JSONObject parse = (JSONObject) JSONObject.parse(jasonStr); parse.entrySet().forEach(s->{ UpdateWrapper wrapper = new UpdateWrapper<>(); wrapper.lambda() .eq(SystemDictData::getDictId,systemDict.getId()) .eq(SystemDictData::getLabel,s.getKey().toUpperCase()) .set(SystemDictData::getCode,s.getValue()); systemDictDataMapper.update(null,wrapper); }); } catch (JsonProcessingException e) { throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"字典值解析有误"); } } @Override public LaborConfigDTO getLaborConfigDTO() { try { String jasonStr = Constants.toUnderlineJSONString(new LaborConfigDTO()); JSONObject parse = (JSONObject) JSONObject.parse(jasonStr); List collect = parse.entrySet().stream().map(s -> s.getKey().toUpperCase()).collect(Collectors.toList()); QueryWrapper systemDictQuery = new QueryWrapper<>(); systemDictQuery.lambda() .eq(SystemDict::getDeleted,Boolean.FALSE) .eq(SystemDict::getCode,Constants.LABOR_CONFIG); SystemDict systemDict = systemDictMapper.selectOne(systemDictQuery); if (Objects.isNull(systemDict)){ throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(),"字典不存在"); } QueryWrapper wrapper = new QueryWrapper<>(); wrapper.lambda() .eq(SystemDictData::getDictId,systemDict.getId()) .in(SystemDictData::getLabel,collect); List systemDictData = systemDictDataMapper.selectList(wrapper); if (CollectionUtils.isEmpty(systemDictData)){ throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(),"字典值不存在"); } systemDictData.forEach(s->{ parse.put(s.getLabel().toLowerCase(),s.getCode()); }); String s = parse.toJSONString(); LaborConfigDTO miniProgrammeDTO = Constants.toSnakeObject(s, LaborConfigDTO.class); return miniProgrammeDTO; } catch (Exception e) { throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"字典值解析有误"); } } @Override public void updateLaborConfigDTO(LaborConfigDTO miniProgrammeDTO) { try { QueryWrapper systemDictQuery = new QueryWrapper<>(); systemDictQuery.lambda() .eq(SystemDict::getDeleted,Boolean.FALSE) .eq(SystemDict::getCode,Constants.VISIT_CONFIG); SystemDict systemDict = systemDictMapper.selectOne(systemDictQuery); if (Objects.isNull(systemDict)){ throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(),"字典不存在"); } String jasonStr = Constants.toUnderlineJSONString(miniProgrammeDTO); JSONObject parse = (JSONObject) JSONObject.parse(jasonStr); parse.entrySet().forEach(s->{ UpdateWrapper wrapper = new UpdateWrapper<>(); wrapper.lambda() .eq(SystemDictData::getDictId,systemDict.getId()) .eq(SystemDictData::getLabel,s.getKey().toUpperCase()) .set(SystemDictData::getCode,s.getValue()); systemDictDataMapper.update(null,wrapper); }); } catch (JsonProcessingException e) { throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"字典值解析有误"); } } }