| package com.doumee.biz.system.impl; | 
|   | 
| import com.alibaba.fastjson.JSONArray; | 
| import com.alibaba.fastjson.JSONObject; | 
| import com.doumee.biz.system.SystemDictDataBiz; | 
| 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.core.utils.HttpsUtil; | 
| import com.doumee.dao.business.vo.UnitCodeVo; | 
| import com.doumee.dao.system.model.SystemDict; | 
| import com.doumee.dao.system.model.SystemDictData; | 
| import com.doumee.service.system.SystemDictDataService; | 
| import com.doumee.service.system.SystemDictService; | 
| import org.apache.commons.lang3.StringUtils; | 
| import org.apache.shiro.SecurityUtils; | 
| import org.apache.shiro.mgt.DefaultSecurityManager; | 
| import org.springframework.beans.BeanUtils; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.stereotype.Service; | 
|   | 
| import java.util.*; | 
|   | 
| @Service | 
| public class SystemDictDataBizImpl implements SystemDictDataBiz { | 
|   | 
|   | 
|     @Autowired | 
|     private SystemDictService systemDictService; | 
|     @Autowired | 
|     private SystemDictDataService systemDictDataService; | 
|   | 
|     @Override | 
|     public Integer create(SystemDictData systemDictData) { | 
|         SystemDictData queryDto = new SystemDictData(); | 
|         queryDto.setDictId(systemDictData.getDictId()); | 
|         queryDto.setLabel(systemDictData.getLabel()); | 
|         queryDto.setDeleted(Boolean.FALSE); | 
|         SystemDictData dictData = systemDictDataService.findOne(queryDto); | 
|         if (dictData != null) { | 
|             throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "数据标签已存在"); | 
|         } | 
|         return systemDictDataService.create(systemDictData); | 
|     } | 
|   | 
|     @Override | 
|     public void updateById(SystemDictData systemDictData) { | 
|         SystemDictData queryDto = new SystemDictData(); | 
|         queryDto.setDictId(systemDictData.getDictId()); | 
|         queryDto.setLabel(systemDictData.getLabel()); | 
|         queryDto.setDeleted(Boolean.FALSE); | 
|         SystemDictData dictData = systemDictDataService.findOne(queryDto); | 
|         if (dictData != null && !dictData.getId().equals(systemDictData.getId())) { | 
|             throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "数据标签已存在"); | 
|         } | 
|         systemDictDataService.updateById(systemDictData); | 
|     } | 
|     @Override | 
|     public  String getUnitName(String unitCode, List<UnitCodeVo> unitCodeVoList) { | 
|         if(unitCodeVoList!=null){ | 
|             for(UnitCodeVo  c :unitCodeVoList){ | 
|                 if(StringUtils.equals(c.getCode(),unitCode)){ | 
|                     return  c.getName(); | 
|                 } | 
|             } | 
|         } | 
|         return null; | 
|     } | 
|     @Override | 
|     public void updateByIdNew(SystemDictData systemDictData) { | 
|         systemDictDataService.updateById(systemDictData); | 
|     } | 
|     @Override | 
|     public   List<UnitCodeVo> getUnitList(){ | 
|         List<UnitCodeVo> list = new ArrayList<>(); | 
|         try { | 
|             String  temp =  queryByCode(Constants.ZBOM,Constants.UNIT_CODE).getCode(); | 
|             JSONArray array  = JSONObject.parseArray(temp); | 
|             if(array!=null ){ | 
|                 for (int i = 0; i < array.size(); i++) { | 
|                     list.add(JSONObject.toJavaObject(array.getJSONObject(i),UnitCodeVo.class)); | 
|                 } | 
|             } | 
|         }catch (Exception e){ | 
|             e.printStackTrace(); | 
|         } | 
|         return list; | 
|     } | 
|   | 
|     @Override | 
|     public SystemDictData queryByCode(String dicCode, String label){ | 
|         SystemDict queryDto = new SystemDict(); | 
|         queryDto.setCode(dicCode); | 
|         queryDto.setDeleted(Boolean.FALSE); | 
|         SystemDict dictData = systemDictService.findOne(queryDto); | 
|         if (dictData == null) { | 
| //            return new SystemDictData(); | 
|             throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "数据值不存在"); | 
|         } | 
|         SystemDictData data = new SystemDictData(); | 
|         data.setDictId(dictData.getId()); | 
|         data.setLabel(label); | 
|         data.setDeleted(Boolean.FALSE); | 
|         data = systemDictDataService.findOne(data); | 
|         if(data == null){ | 
|             data = new SystemDictData(); | 
|         } | 
|         return data; | 
|     } | 
|     @Override | 
|     public  String  queryCodeById(Integer id){ | 
|         SystemDictData data =  systemDictDataService.findById(id); | 
|         if(data != null){ | 
|             return  data.getCode(); | 
|         } | 
|         return null; | 
|     } | 
|   | 
|   | 
|     @Override | 
|     public List<SystemDictData> queryListByCode(String dicCode, String label){ | 
|         SystemDict queryDto = new SystemDict(); | 
|         queryDto.setCode(dicCode); | 
|         queryDto.setDeleted(Boolean.FALSE); | 
|         SystemDict dictData = systemDictService.findOne(queryDto); | 
|         if (dictData == null) { | 
|             throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "数据值不存在"); | 
|         } | 
|         SystemDictData data = new SystemDictData(); | 
|         data.setDictId(dictData.getId()); | 
|         data.setLabel(label); | 
|         data.setDeleted(Boolean.FALSE); | 
|         List<SystemDictData> list= systemDictDataService.findList(data); | 
|   | 
|         return list; | 
|     } | 
|   | 
|   | 
|     @Override | 
|     public List<SystemDictData> queryDataByCode(String dicCode, String code){ | 
|         SystemDict queryDto = new SystemDict(); | 
|         queryDto.setCode(dicCode); | 
|         queryDto.setDeleted(Boolean.FALSE); | 
|         SystemDict dictData = systemDictService.findOne(queryDto); | 
|         if (dictData == null) { | 
|             throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "数据值不存在"); | 
|         } | 
|         SystemDictData data = new SystemDictData(); | 
|         data.setDictId(dictData.getId()); | 
|         data.setCode(code); | 
|         data.setDeleted(Boolean.FALSE); | 
|         List<SystemDictData> list= systemDictDataService.findList(data); | 
|   | 
|         return list; | 
|     } | 
|   | 
|     @Override | 
|     public void updateByDicId(List<SystemDictData> list) { | 
|         LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); | 
|         list.stream().forEach(s->{ | 
|             s.setUpdateTime(new Date()); | 
|             s.setUpdateUser(user.getId()); | 
|             systemDictDataService.updateById(s); | 
|         }); | 
|   | 
|     } | 
|   | 
|     @Override | 
|     public String getPreFixPath(String resourceCode, String targetCode) { | 
|   | 
|         if (StringUtils.isBlank(resourceCode) || StringUtils.isBlank(targetCode)){ | 
|             throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"路径参数不完整"); | 
|         } | 
|         SystemDict queryDto = new SystemDict(); | 
|         queryDto.setCode(Constants.OBJCET_STORAGE); | 
|         queryDto.setDeleted(Boolean.FALSE); | 
|         SystemDict dictData = systemDictService.findOne(queryDto); | 
|         if (dictData == null) { | 
|             throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "数据值不存在"); | 
|         } | 
|         List<SystemDictData> list = systemDictDataService.findList(dictData.getId(),Arrays.asList(resourceCode, targetCode)); | 
|         String resourcePath = list.stream().filter(s->s.getLabel().equals(resourceCode)).findFirst().map(s->s.getCode()).orElse(""); | 
|         String targetPath = list.stream().filter(s -> s.getLabel().equals(targetCode)).findFirst().map(s -> s.getCode()).orElse(""); | 
|         return resourcePath+targetPath; | 
|     } | 
|     @Override | 
|     public  void updWxMiniToken() { | 
|         updWxMiniTokenDo(Constants.WX_APPID_CUSTOMER,Constants.WX_SECRET_CUSTOMER,Constants.WX_TOKEN_CUSTOMER); | 
|         updWxMiniTokenDo(Constants.WX_APPID_PERSONNEL,Constants.WX_SECRET_PERSONNEL,Constants.WX_TOKEN_PERSONNEL); | 
|   | 
|     } | 
|     public  void updWxMiniTokenDo(String  appIdKey,String secretKey,String tokenKey) { | 
|         DefaultSecurityManager securityManager = new DefaultSecurityManager(); | 
|         SecurityUtils.setSecurityManager(securityManager); | 
|   | 
|         String appId = queryByCode(Constants.WX_MINI_CONFIG,appIdKey).getCode(); | 
|         String appSecret = queryByCode(Constants.WX_MINI_CONFIG,secretKey).getCode(); | 
|         SystemDictData systemDictData = this.queryByCode(Constants.WX_MINI_CONFIG,tokenKey); | 
|         //生成微信token | 
|         String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+appSecret; | 
|         String response = HttpsUtil.sendGet(url); | 
|         JSONObject json = JSONObject.parseObject(response); | 
|         systemDictData.setCode(json.getString("access_token")); | 
|         systemDictData.setUpdateTime(new Date()); | 
|         systemDictDataService.updateById(systemDictData); | 
|     } | 
|   | 
|   | 
| } |