lishuai
2023-12-26 70577a6d6cf2bded29e71dcad11a0c2605193d20
server/dmvisit_service/src/main/java/com/doumee/service/system/impl/SystemDictDataServiceImpl.java
@@ -1,12 +1,13 @@
package com.doumee.service.system.impl;
import com.alibaba.fastjson.JSON;
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.model.LoginUserInfo;
import com.doumee.core.utils.Constants;
import com.doumee.dao.admin.request.LaborConfigDTO;
import com.doumee.dao.admin.request.VisitConfigDTO;
import com.doumee.dao.admin.request.*;
import com.doumee.dao.system.SystemDictMapper;
import com.doumee.dao.system.model.SystemDict;
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -21,14 +22,19 @@
import com.doumee.service.system.SystemDictDataService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.SecurityUtils;
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.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import static net.sf.jsqlparser.parser.feature.Feature.set;
import static net.sf.jsqlparser.parser.feature.Feature.update;
/**
 * 字典数据Service实现
@@ -126,10 +132,6 @@
    public VisitConfigDTO getVisitConfigDTO() {
        try {
            String jasonStr = Constants.toUnderlineJSONString(new VisitConfigDTO());
            JSONObject parse = (JSONObject) JSONObject.parse(jasonStr);
            List<String> collect = parse.entrySet().stream().map(s -> s.getKey().toUpperCase()).collect(Collectors.toList());
            QueryWrapper<SystemDict> systemDictQuery = new QueryWrapper<>();
            systemDictQuery.lambda()
                            .eq(SystemDict::getDeleted,Boolean.FALSE)
@@ -141,18 +143,19 @@
            }
            QueryWrapper<SystemDictData> wrapper = new QueryWrapper<>();
            wrapper.lambda()
                    .eq(SystemDictData::getDictId,systemDict.getId())
                    .in(SystemDictData::getLabel,collect);
            List<SystemDictData> systemDictData = systemDictDataMapper.selectList(wrapper);
            if (CollectionUtils.isEmpty(systemDictData)){
                    .eq(SystemDictData::getDictId,systemDict.getId());
            List<SystemDictData> systemDictDatas = systemDictDataMapper.selectList(wrapper);
            if (CollectionUtils.isEmpty(systemDictDatas)){
                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;
            VisitConfigDTO visitConfigDTO = null;
            for (SystemDictData obj:systemDictDatas) {
                visitConfigDTO = new VisitConfigDTO();
                visitConfigDTO.setVisitConfigParam(JSONObject.parseObject(obj.getCode(),VisitConfigParam.class));
                visitConfigDTO.setDescription(obj.getRemark());
                visitConfigDTO.setTheme(obj.getLabel());
            }
            return visitConfigDTO;
        } catch (Exception e) {
            throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"字典值解析有误");
        }
@@ -160,7 +163,6 @@
    @Override
    public void updateVisitConfig(VisitConfigDTO miniProgrammeDTO) {
        try {
            QueryWrapper<SystemDict> systemDictQuery = new QueryWrapper<>();
            systemDictQuery.lambda()
@@ -171,19 +173,33 @@
            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<SystemDictData> 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(),"字典值解析有误");
        }
            QuerySystemDictDataDTO dictData = new QuerySystemDictDataDTO();
            dictData.setDictId((systemDict.getId()));
            List<SystemDictDataListVO> systemDictDataListVOS = systemDictDataMapper.selectManageList(dictData);
            SystemDictData systemDictData = getSystemDictDataListVO(systemDict);
            systemDictData.setCode(JSON.toJSONString(miniProgrammeDTO.getVisitConfigParam()));
            systemDictData.setLabel(miniProgrammeDTO.getTheme());
            systemDictData.setRemark(miniProgrammeDTO.getDescription());
            if(null != systemDictDataListVOS && systemDictDataListVOS.size() > 0) {
                systemDictData.setId(systemDictDataListVOS.get(0).getId());
                systemDictDataMapper.updateById(systemDictData);
            }else {
                systemDictDataMapper.insert(systemDictData);
            }
    }
    private SystemDictData getSystemDictDataListVO(SystemDict systemDict) {
        LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        SystemDictData systemDictData = new SystemDictData();
        systemDictData.setDictId(systemDict.getId());
        systemDictData.setDisabled(true);
        systemDictData.setSort(Constants.ONE);
        systemDictData.setCreateTime(new Date());
        systemDictData.setUpdateTime(new Date());
        systemDictData.setCreateUser(loginUserInfo.getId());
        systemDictData.setUpdateUser(loginUserInfo.getId());
        return systemDictData;
    }
    @Override
@@ -205,18 +221,16 @@
            }
            QueryWrapper<SystemDictData> wrapper = new QueryWrapper<>();
            wrapper.lambda()
                    .eq(SystemDictData::getDictId,systemDict.getId())
                    .in(SystemDictData::getLabel,collect);
            List<SystemDictData> systemDictData = systemDictDataMapper.selectList(wrapper);
            if (CollectionUtils.isEmpty(systemDictData)){
                throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(),"字典值不存在");
                    .eq(SystemDictData::getDictId,systemDict.getId());
            List<SystemDictData> systemDictDatas = systemDictDataMapper.selectList(wrapper);
            LaborConfigDTO laborConfigDTO = null;
            for (SystemDictData obj:systemDictDatas) {
                laborConfigDTO = new LaborConfigDTO();
                laborConfigDTO.setLaborConfigParam(JSONObject.parseObject(obj.getCode(), LaborConfigParam.class));
                laborConfigDTO.setDescription(obj.getRemark());
                laborConfigDTO.setTheme(obj.getLabel());
            }
            systemDictData.forEach(s->{
                parse.put(s.getLabel().toLowerCase(),s.getCode());
            });
            String s = parse.toJSONString();
            LaborConfigDTO miniProgrammeDTO = Constants.toSnakeObject(s, LaborConfigDTO.class);
            return miniProgrammeDTO;
            return laborConfigDTO;
        } catch (Exception e) {
            throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"字典值解析有误");
        }
@@ -224,29 +238,27 @@
    @Override
    public void updateLaborConfigDTO(LaborConfigDTO miniProgrammeDTO) {
        try {
            QueryWrapper<SystemDict> systemDictQuery = new QueryWrapper<>();
            systemDictQuery.lambda()
                    .eq(SystemDict::getDeleted,Boolean.FALSE)
                    .eq(SystemDict::getCode,Constants.VISIT_CONFIG);
                    .eq(SystemDict::getCode,Constants.LABOR_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<SystemDictData> 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(),"字典值解析有误");
        }
            QuerySystemDictDataDTO dictData = new QuerySystemDictDataDTO();
            dictData.setDictId((systemDict.getId()));
            List<SystemDictDataListVO> systemDictDataListVOS = systemDictDataMapper.selectManageList(dictData);
            SystemDictData systemDictData = getSystemDictDataListVO(systemDict);
            systemDictData.setCode(JSON.toJSONString(miniProgrammeDTO.getLaborConfigParam()));
            systemDictData.setLabel(miniProgrammeDTO.getTheme());
            systemDictData.setRemark(miniProgrammeDTO.getDescription());
            if(null != systemDictDataListVOS && systemDictDataListVOS.size() > 0) {
                systemDictData.setId(systemDictDataListVOS.get(0).getId());
                systemDictDataMapper.updateById(systemDictData);
            }else {
                systemDictDataMapper.insert(systemDictData);
            }
    }
}