DeviceChargingConfigServiceImpl.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package com.xy.service;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.collection.CollUtil;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  7. import com.github.yitter.idgen.YitIdHelper;
  8. import com.xy.device.EnumDeviceCharging;
  9. import com.xy.device.EnumDeviceChargingConfigType;
  10. import com.xy.dto.DeviceChargingConfigDto;
  11. import com.xy.dto.be.MercDto;
  12. import com.xy.entity.DeviceChargingConfig;
  13. import com.xy.error.CommRuntimeException;
  14. import com.xy.mapper.DeviceChargingConfigMapper;
  15. import com.xy.service.be.MercService;
  16. import com.xy.utils.Beans;
  17. import com.xy.utils.MybatisPlusQuery;
  18. import com.xy.utils.R;
  19. import com.xy.utils.SysDictUtils;
  20. import io.swagger.annotations.Api;
  21. import io.swagger.annotations.ApiOperation;
  22. import lombok.AllArgsConstructor;
  23. import org.springframework.stereotype.Service;
  24. import org.springframework.transaction.annotation.Transactional;
  25. import org.springframework.validation.annotation.Validated;
  26. import org.springframework.web.bind.annotation.PostMapping;
  27. import org.springframework.web.bind.annotation.RequestBody;
  28. import java.time.LocalDateTime;
  29. import java.util.ArrayList;
  30. import java.util.List;
  31. import java.util.Optional;
  32. import static com.xy.utils.Beans.copy;
  33. /**
  34. * <p>
  35. * 设备计费配置表 服务实现类
  36. * </p>
  37. *
  38. * @author lijin
  39. * @since 2023-07-28
  40. */
  41. @Service
  42. @AllArgsConstructor
  43. @Api(tags = "设备计费配置表")
  44. public class DeviceChargingConfigServiceImpl extends ServiceImpl<DeviceChargingConfigMapper, DeviceChargingConfig> implements DeviceChargingConfigService {
  45. private MercService mercService;
  46. private DeviceAlgorithmChargingServiceImpl deviceAlgorithmChargingService;
  47. @PostMapping("list")
  48. @ApiOperation("集合查询")
  49. public R<List<DeviceChargingConfigDto.Vo>> list(@RequestBody @Validated DeviceChargingConfigDto.SelectList selectList) {
  50. MercDto.Vo merc = mercService.obj(new MercDto.Vo().setId(selectList.getMercId())).getData();
  51. LambdaQueryWrapper<DeviceChargingConfig> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(selectList, DeviceChargingConfig.class)
  52. .in(DeviceChargingConfig::getDeviceType, selectList.getDeviceTypes())
  53. .eq(DeviceChargingConfig::getMercCode, merc.getMercCode())
  54. .build();
  55. List<DeviceChargingConfig> list = list(lambdaQueryWrapper);
  56. return R.ok(Beans.copy(DeviceChargingConfigDto.Vo.class, list));
  57. }
  58. @PostMapping("listByFeeConfig")
  59. @ApiOperation("设备管理费用配置查询")
  60. public R<DeviceChargingConfigDto.DeviceManagerFeeConfigVO> listByFeeConfig(@RequestBody @Validated DeviceChargingConfigDto.ListByFeeConfigDTO dto) {
  61. DeviceChargingConfigDto.DeviceManagerFeeConfigVO data = new DeviceChargingConfigDto.DeviceManagerFeeConfigVO();
  62. List<Integer> deviceTypes = dto.getDeviceTypes();
  63. // 设备管理费-平台默认配置
  64. List<DeviceChargingConfigDto.DeviceManagerFeePlatformVO> dmFeePlatforms = new ArrayList<>();
  65. for (Integer type : deviceTypes) {// 默认年费,赠送天数
  66. DeviceChargingConfigDto.DeviceManagerFeePlatformVO vo = new DeviceChargingConfigDto.DeviceManagerFeePlatformVO();
  67. int defaultMoney = SysDictUtils.getValue(EnumDeviceCharging.Code.CODE.getCode(), String.valueOf(type), Integer.class);
  68. // 默认赠送天数
  69. int giveDays = SysDictUtils.getValue(EnumDeviceCharging.Code.CODE.getCode(), EnumDeviceCharging.N_X.getCode(), Integer.class);
  70. vo.setDefFee(defaultMoney);
  71. vo.setDefGiveDay(giveDays);
  72. vo.setDeviceType(type);
  73. dmFeePlatforms.add(vo);
  74. }
  75. MercDto.Vo merc = mercService.obj(new MercDto.Vo().setId(dto.getMercId())).getData();
  76. // 设备管理费-商家自定义费用
  77. List<DeviceChargingConfigDto.DeviceManagerFeeMercVO> dmFeeMercs = new ArrayList<>();
  78. LambdaQueryWrapper<DeviceChargingConfig> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(dto, DeviceChargingConfig.class)
  79. .in(DeviceChargingConfig::getDeviceType, deviceTypes)
  80. .eq(DeviceChargingConfig::getMercCode, merc.getMercCode())
  81. .build();
  82. // 默认配置 type 2预充 1赠送天数
  83. List<DeviceChargingConfig> list = list(lambdaQueryWrapper);
  84. if (CollUtil.isNotEmpty(list)) {
  85. for (DeviceChargingConfig dcc : list) {
  86. Integer type = dcc.getType();
  87. Integer deviceType = dcc.getDeviceType();
  88. // 预充
  89. if (String.valueOf(type).equals(EnumDeviceChargingConfigType.N_2.getCode())) {
  90. int defaultMoney = SysDictUtils.getValue(EnumDeviceCharging.Code.CODE.getCode(), String.valueOf(deviceType), Integer.class);
  91. // 默认赠送天数
  92. int giveDays = SysDictUtils.getValue(EnumDeviceCharging.Code.CODE.getCode(), EnumDeviceCharging.N_X.getCode(), Integer.class);
  93. DeviceChargingConfigDto.DeviceManagerFeeMercVO vo = new DeviceChargingConfigDto.DeviceManagerFeeMercVO();
  94. BeanUtil.copyProperties(dcc, vo);
  95. vo.setDefFee(defaultMoney);
  96. vo.setDefGiveDay(giveDays);
  97. vo.setFee(dcc.getConfigValue());
  98. Optional<DeviceChargingConfig> dcObj = list.stream()
  99. .filter(dc -> String.valueOf(dc.getType()).equals(EnumDeviceChargingConfigType.N_1.getCode()))
  100. .filter(dc -> dc.getDeviceType().intValue() == deviceType.intValue())
  101. .findFirst();
  102. vo.setGiveDay(dcObj.get().getConfigValue());
  103. dmFeeMercs.add(vo);
  104. }
  105. }
  106. }
  107. data.setDmFeeMercs(dmFeeMercs);
  108. data.setDmFeePlatforms(dmFeePlatforms);
  109. return R.ok(data);
  110. }
  111. @PostMapping("save")
  112. @ApiOperation("添加")
  113. public R save(@RequestBody @Validated DeviceChargingConfigDto.Save save) {
  114. LocalDateTime now = LocalDateTime.now();
  115. Integer deviceType = save.getDeviceType();
  116. Long mercId = save.getMercId();
  117. MercDto.Vo merc = R.feignCheckData(mercService.obj(new MercDto.Vo().setId(mercId)));
  118. String mercCode = merc.getMercCode();
  119. DeviceChargingConfig dc = this.getOne(Wrappers.<DeviceChargingConfig>lambdaQuery()
  120. .eq(DeviceChargingConfig::getMercCode, mercCode)
  121. .eq(DeviceChargingConfig::getDeviceType, deviceType)
  122. .eq(DeviceChargingConfig::getType, EnumDeviceChargingConfigType.N_2.getCode())
  123. );
  124. if (dc != null) {
  125. throw new CommRuntimeException("该类型配置已存在,请勿重复添加!");
  126. }
  127. // 默认赠送天数
  128. int giveDays = SysDictUtils.getValue(EnumDeviceCharging.Code.CODE.getCode(), EnumDeviceCharging.N_X.getCode(), Integer.class);
  129. // 新增赠送天数
  130. DeviceChargingConfig newDc = new DeviceChargingConfig().setDeviceType(deviceType).setConfigValue(giveDays).setMercCode(mercCode)
  131. .setType(Integer.valueOf(EnumDeviceChargingConfigType.N_1.getCode())).setCreateTime(now)
  132. .setUpdateTime(now).setMercCode(mercCode);
  133. save(newDc);
  134. DeviceChargingConfig saveInfo = copy(DeviceChargingConfig.class, save)
  135. .setCreateTime(now)
  136. .setUpdateTime(now).setMercCode(mercCode);
  137. save(saveInfo);
  138. return R.ok();
  139. }
  140. @PostMapping("update")
  141. @ApiOperation("修改")
  142. public R update(@RequestBody @Validated DeviceChargingConfigDto.Update update) {
  143. // DeviceChargingConfig dcc = getById(update.getId());
  144. DeviceChargingConfig updateInfo = copy(DeviceChargingConfig.class, update)
  145. .setUpdateTime(LocalDateTime.now());
  146. updateById(updateInfo);
  147. return R.ok();
  148. }
  149. @PostMapping("delete")
  150. @ApiOperation("删除")
  151. @Transactional(rollbackFor = Exception.class)
  152. public R delete(@RequestBody @Validated DeviceChargingConfigDto.Delete delete) {
  153. Long id = delete.getId();
  154. DeviceChargingConfig dcc = getById(delete.getId());
  155. DeviceChargingConfig one = getOne(Wrappers.<DeviceChargingConfig>lambdaQuery()
  156. .eq(DeviceChargingConfig::getMercCode, dcc.getMercCode())
  157. .eq(DeviceChargingConfig::getDeviceType, dcc.getDeviceType())
  158. .eq(DeviceChargingConfig::getType, Integer.valueOf(EnumDeviceChargingConfigType.N_1.getCode()))
  159. );
  160. if (one != null) {
  161. removeById(one.getId());
  162. }
  163. removeById(delete.getId());
  164. return R.ok();
  165. }
  166. @PostMapping("saveOrUpdate")
  167. @ApiOperation("添加或修改")
  168. public R saveOrUpdate(@RequestBody @Validated DeviceChargingConfigDto.SaveOrUpdate saveOrUpdate) {
  169. LocalDateTime now = LocalDateTime.now();
  170. MercDto.Vo merc = mercService.obj(new MercDto.Vo().setId(saveOrUpdate.getMercId())).getData();
  171. List<DeviceChargingConfigDto.SaveOrUpdate.ConfigValue> values = saveOrUpdate.getConfigValues();
  172. for (DeviceChargingConfigDto.SaveOrUpdate.ConfigValue value : values) {
  173. LambdaQueryWrapper<DeviceChargingConfig> lambdaQueryWrapper = new LambdaQueryWrapper<DeviceChargingConfig>()
  174. .eq(DeviceChargingConfig::getMercCode, merc.getMercCode())
  175. .eq(DeviceChargingConfig::getDeviceType, saveOrUpdate.getDeviceType())
  176. .eq(DeviceChargingConfig::getType, value.getType());
  177. DeviceChargingConfig deviceChargingConfig = getOne(lambdaQueryWrapper);
  178. if (deviceChargingConfig == null) {
  179. if (value.getConfigValue() == -1) {
  180. continue;
  181. }
  182. deviceChargingConfig = new DeviceChargingConfig()
  183. .setId(YitIdHelper.nextId())
  184. .setMercCode(merc.getMercCode())
  185. .setDeviceType(saveOrUpdate.getDeviceType())
  186. .setConfigValue(value.getConfigValue())
  187. .setNote(saveOrUpdate.getNote())
  188. .setType(value.getType())
  189. .setCreateTime(now)
  190. .setUpdateTime(now);
  191. save(deviceChargingConfig);
  192. } else {
  193. if (value.getConfigValue() == -1) {
  194. removeById(deviceChargingConfig.getId());
  195. continue;
  196. }
  197. deviceChargingConfig.setConfigValue(value.getConfigValue())
  198. .setNote(saveOrUpdate.getNote())
  199. .setUpdateTime(now);
  200. updateById(deviceChargingConfig);
  201. }
  202. }
  203. return R.ok();
  204. }
  205. @Override
  206. @ApiOperation("集合查询2")
  207. public R<List<DeviceChargingConfigDto.Vo>> list2(DeviceChargingConfigDto.SelectList selectList) {
  208. MercDto.Vo merc = mercService.obj(new MercDto.Vo().setId(selectList.getMercId())).getData();
  209. List<String> mercCodes = new ArrayList<>();
  210. deviceAlgorithmChargingService.getMercCodes(merc.getMercCode(), mercCodes);
  211. LambdaQueryWrapper<DeviceChargingConfig> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(selectList, DeviceChargingConfig.class)
  212. .in(DeviceChargingConfig::getMercCode, mercCodes)
  213. .build()
  214. .orderByDesc(DeviceChargingConfig::getMercCode);
  215. List<DeviceChargingConfig> list = list(lambdaQueryWrapper);
  216. return R.ok(Beans.copy(DeviceChargingConfigDto.Vo.class, list));
  217. }
  218. }