|  |  |  | 
|---|
|  |  |  | package com.moral.api.service.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSON; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.moral.api.service.UnitConversionService; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
|---|
|  |  |  | import com.moral.constant.Constants; | 
|---|
|  |  |  | import com.moral.constant.RedisConstants; | 
|---|
|  |  |  | import com.moral.constant.ResponseCodeEnum; | 
|---|
|  |  |  | import com.moral.util.ConvertUtils; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.data.redis.core.RedisTemplate; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.transaction.annotation.Transactional; | 
|---|
|  |  |  | import org.springframework.util.ObjectUtils; | 
|---|
|  |  |  | 
|---|
|  |  |  | UnitConversionMapper unitConversionMapper; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | SysDictDataService sysDictDataService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | RedisTemplate redisTemplate; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public UnitConversionDTO insertUnitConversion(UnitConversionInsertForm form) { | 
|---|
|  |  |  | //创建返回对象 | 
|---|
|  |  |  | UnitConversionDTO dto  = new UnitConversionDTO(); | 
|---|
|  |  |  | UnitConversionDTO dto = new UnitConversionDTO(); | 
|---|
|  |  |  | //取参 | 
|---|
|  |  |  | UnitConversion unitConversion = form.formConvertEntity(); | 
|---|
|  |  |  | //判断原单位和目标单位计算公式是否已经存在于数据库 | 
|---|
|  |  |  | QueryWrapper existWrapper = new QueryWrapper(); | 
|---|
|  |  |  | existWrapper.eq("original_unit_key",unitConversion.getOriginalUnitKey()); | 
|---|
|  |  |  | existWrapper.eq("target_unit_key",unitConversion.getTargetUnitKey()); | 
|---|
|  |  |  | existWrapper.eq("is_delete",Constants.NOT_DELETE); | 
|---|
|  |  |  | UnitConversion existConversion = unitConversionMapper.selectOne(existWrapper); | 
|---|
|  |  |  | if(!ObjectUtils.isEmpty(existConversion)){ | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.UNIT_CONVERSION_EXIST.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.UNIT_CONVERSION_EXIST.getMsg()); | 
|---|
|  |  |  | return dto; | 
|---|
|  |  |  | QueryWrapper<UnitConversion> existWrapper = new QueryWrapper(); | 
|---|
|  |  |  | existWrapper.eq("original_unit_key", unitConversion.getOriginalUnitKey()); | 
|---|
|  |  |  | existWrapper.eq("target_unit_key", unitConversion.getTargetUnitKey()); | 
|---|
|  |  |  | existWrapper.eq("is_delete", Constants.NOT_DELETE); | 
|---|
|  |  |  | List<UnitConversion> unitConversions = unitConversionMapper.selectList(existWrapper); | 
|---|
|  |  |  | if (ObjectUtils.isEmpty(unitConversion.getSensorCode())) { | 
|---|
|  |  |  | if (!ObjectUtils.isEmpty(unitConversions)) { | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.UNIT_CONVERSION_EXIST.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.UNIT_CONVERSION_EXIST.getMsg()); | 
|---|
|  |  |  | return dto; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!ObjectUtils.isEmpty(unitConversion.getSensorCode())) { | 
|---|
|  |  |  | for (UnitConversion conversion : unitConversions) { | 
|---|
|  |  |  | if (ObjectUtils.isEmpty(conversion.getSensorCode())||conversion.getSensorCode().equals(unitConversion.getSensorCode())) { | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.UNIT_CONVERSION_EXIST.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.UNIT_CONVERSION_EXIST.getMsg()); | 
|---|
|  |  |  | return dto; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //插入数据 | 
|---|
|  |  |  | unitConversionMapper.insert(unitConversion); | 
|---|
|  |  |  | //刷新缓存 | 
|---|
|  |  |  | refreshCache(); | 
|---|
|  |  |  | //封装返回数据 | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
|---|
|  |  |  | 
|---|
|  |  |  | //组装查询条件 | 
|---|
|  |  |  | Page<UnitConversion> queryPage = new Page<>(page, size); | 
|---|
|  |  |  | NullFilterWrapper<UnitConversion> wrapper = new NullFilterWrapper<>(); | 
|---|
|  |  |  | wrapper.eq("original_unit_key",originalUnitKey); | 
|---|
|  |  |  | wrapper.eq("target_unit_key",targetUnitKey); | 
|---|
|  |  |  | wrapper.eq("original_unit_key", originalUnitKey); | 
|---|
|  |  |  | wrapper.eq("target_unit_key", targetUnitKey); | 
|---|
|  |  |  | //排序顺序条件构造 | 
|---|
|  |  |  | if (!ObjectUtils.isEmpty(order) && !ObjectUtils.isEmpty(orderType)) { | 
|---|
|  |  |  | if (orderType.equals(Constants.ORDER_ASC)) | 
|---|
|  |  |  | 
|---|
|  |  |  | oldUnitConversion = unitConversionMapper.selectOne(oldWrapper); | 
|---|
|  |  |  | //更新 | 
|---|
|  |  |  | unitConversionMapper.updateById(unitConversion); | 
|---|
|  |  |  | //刷新缓存 | 
|---|
|  |  |  | refreshCache(); | 
|---|
|  |  |  | //封装返回结果 | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
|---|
|  |  |  | 
|---|
|  |  |  | oldUnitConversion = unitConversionMapper.selectOne(oldWrapper); | 
|---|
|  |  |  | //执行逻辑删除 | 
|---|
|  |  |  | UpdateWrapper deleteWrapper = new UpdateWrapper(); | 
|---|
|  |  |  | deleteWrapper.set("is_delete",Constants.DELETE); | 
|---|
|  |  |  | deleteWrapper.eq("id",id); | 
|---|
|  |  |  | unitConversionMapper.update(null,deleteWrapper); | 
|---|
|  |  |  | deleteWrapper.set("is_delete", Constants.DELETE); | 
|---|
|  |  |  | deleteWrapper.eq("id", id); | 
|---|
|  |  |  | unitConversionMapper.update(null, deleteWrapper); | 
|---|
|  |  |  | //刷新缓存 | 
|---|
|  |  |  | refreshCache(); | 
|---|
|  |  |  | //封装返回对象 | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
|---|
|  |  |  | return dto; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /*刷新缓存*/ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void refreshCache(){ | 
|---|
|  |  |  | redisTemplate.delete((RedisConstants.UNIT_CONVERSION)); | 
|---|
|  |  |  | QueryWrapper<UnitConversion> queryWrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | queryWrapper.eq("is_delete",Constants.NOT_DELETE); | 
|---|
|  |  |  | List<UnitConversion> unitConversions = unitConversionMapper.selectList(queryWrapper); | 
|---|
|  |  |  | redisTemplate.opsForList().leftPushAll(RedisConstants.UNIT_CONVERSION,unitConversions); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|