TbItemService.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package com.pj.project.tb_item;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.util.NumberUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import cn.hutool.json.JSONUtil;
  6. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  7. import com.baomidou.mybatisplus.extension.service.IService;
  8. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  9. import com.pj.constants.business.CarEnum;
  10. import com.pj.constants.business.SyncTypeEnum;
  11. import com.pj.project.relation_type_item.RelationTypeItem;
  12. import com.pj.project.relation_type_item.RelationTypeItemService;
  13. import com.pj.project.sync.HelpService;
  14. import com.pj.project.sync.bo.ItemTypeBO;
  15. import com.pj.project.sync.response.item.IExpenseItem;
  16. import com.pj.project.sync.response.item.IrulesItem;
  17. import com.pj.project.tb_costomer.TbCostomer;
  18. import com.pj.project.tb_fee_item.TbFeeItem;
  19. import com.pj.project.tb_fee_item.TbFeeItemService;
  20. import com.pj.project.tb_item_fac.TbItemFac;
  21. import com.pj.project.tb_item_fac.TbItemFacService;
  22. import com.pj.project.tb_item_type.TbItemType;
  23. import com.pj.project.tb_item_type.TbItemTypeService;
  24. import com.pj.utils.so.SoMap;
  25. import lombok.extern.slf4j.Slf4j;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.scheduling.annotation.Async;
  28. import org.springframework.stereotype.Service;
  29. import org.springframework.transaction.annotation.Transactional;
  30. import javax.annotation.Resource;
  31. import java.util.Date;
  32. import java.util.List;
  33. /**
  34. * Service: tb_item -- 作业配置项
  35. *
  36. * @author qzy
  37. */
  38. @Service
  39. @Slf4j
  40. @Transactional
  41. public class TbItemService extends ServiceImpl<TbItemMapper, TbItem> implements IService<TbItem> {
  42. /**
  43. * 底层 Mapper 对象
  44. */
  45. @Autowired
  46. TbItemMapper tbItemMapper;
  47. @Resource
  48. private HelpService helpService;
  49. @Resource
  50. RelationTypeItemService relationTypeItemService;
  51. @Resource
  52. private TbItemTypeService tbItemTypeService;
  53. @Resource
  54. private TbItemFacService tbItemFacService;
  55. @Resource
  56. private TbFeeItemService tbFeeItemService;
  57. /**
  58. * 删
  59. */
  60. void delete(Long id) {
  61. this.removeById(id);
  62. }
  63. /**
  64. * 查集合 - 根据条件(参数为空时代表忽略指定条件)
  65. */
  66. List<TbItem> getList(SoMap so) {
  67. return tbItemMapper.getList(so);
  68. }
  69. public List<TbItem> getTypeItemList(SoMap soMap) {
  70. return tbItemMapper.getTypeItemList(soMap);
  71. }
  72. public List<TbItem> getItemTypeById(String typeId) {
  73. return tbItemMapper.getItemTypeById(typeId);
  74. }
  75. public TbItem findByCode(String code) {
  76. QueryWrapper<TbItem> ew = new QueryWrapper<>();
  77. ew.eq("item_code", code);
  78. List<TbItem> list = list(ew);
  79. return list.isEmpty() ? null : list.get(0);
  80. }
  81. /**
  82. * 费项详情
  83. *
  84. * @param request
  85. */
  86. public void receive(String request) {
  87. IExpenseItem iExpenseItem = helpService.deCrypte(request, IExpenseItem.class);
  88. TbItemType tbItemType = tbItemTypeService.findByCode(iExpenseItem.getBizTypeNo());
  89. log.info("detail:{}", JSONUtil.toJsonStr(iExpenseItem));
  90. if (tbItemType == null) {
  91. log.error("类型不存在");
  92. return;
  93. }
  94. String uniqueExpenseId = iExpenseItem.getUniqueExpenseId();
  95. String syncType = iExpenseItem.getSyncType();
  96. if (StrUtil.equals(syncType, SyncTypeEnum.DELETE.getCode())) {
  97. TbItem db = findByCode(uniqueExpenseId);
  98. if (db != null) {
  99. this.removeById(db.getId());
  100. relationTypeItemService.removeByItemId(db.getId());
  101. tbItemFacService.removeByItemId(db.getId());
  102. }
  103. } else {
  104. TbItem tbItem = handlerItem(tbItemType, iExpenseItem, new Date());
  105. //多规格费项因子
  106. if (StrUtil.equals(iExpenseItem.getPattern(), "RULES")) {
  107. handlerFac(iExpenseItem, tbItem, iExpenseItem.getUniqueExpenseId());
  108. }
  109. }
  110. }
  111. /**
  112. * 处理费项详情
  113. *
  114. * @param tbItemType
  115. * @param item
  116. * @param now
  117. * @return
  118. */
  119. private TbItem handlerItem(TbItemType tbItemType, IExpenseItem item, Date now) {
  120. String name = item.getExpenseName();
  121. String code = item.getUniqueExpenseId();
  122. TbItem tbItem = this.findByCode(code.trim());
  123. if (tbItem == null) {
  124. tbItem = new TbItem();
  125. }
  126. String payType = tbItemType.getPayType();
  127. tbItem.setPayType(Integer.parseInt(payType))
  128. .setPayTypeName(tbItemType.getPayTypeName());
  129. String typeId = tbItemType.getId();
  130. tbItem.setItemCode(code).setItemName(name).setUnit(item.getExpenseUnit()).setStatus(1)
  131. .setInc(1).setNeedRemark(0).setPrice(item.getFixedPrice()).setMinLength(0).setCarLength(25)
  132. .setMinWeight(0).setMaxWeight(999999).setTaxRate(item.getTaxRate().doubleValue())
  133. .setTypeId(typeId)
  134. .setTypeName(tbItemType.getName())
  135. .setItemType(CarEnum.CarTypeEnum.EMPTY_TYPE.getDesc() + "," + CarEnum.CarTypeEnum.WEIGHT_TYPE.getDesc());
  136. String businessType = tbItemType.getBusinessType();
  137. if (StrUtil.isEmpty(businessType) || StrUtil.equals("0", businessType)) {
  138. tbItem.setBusinessType(TbCostomer.CustomerEnum.BUSINESS_TYPE.getType())
  139. .setBusinessTypeName("默认");
  140. } else {
  141. tbItem.setBusinessType(businessType).setBusinessTypeName(tbItemType.getPayTypeName());
  142. }
  143. this.saveOrUpdate(tbItem);
  144. RelationTypeItem relationTypeItem = relationTypeItemService.findByTypeIdAndItemId(typeId, tbItem.getId());
  145. if (relationTypeItem == null) {
  146. relationTypeItem = new RelationTypeItem();
  147. relationTypeItem.setInc(1).setNeedRemark(0).setMustRemark(0);
  148. }
  149. relationTypeItem.setTypeId(typeId).setItemId(tbItem.getId())
  150. .setTypeName(tbItemType.getName());
  151. relationTypeItemService.saveOrUpdate(relationTypeItem);
  152. return tbItem;
  153. }
  154. /**
  155. * 费项因子
  156. *
  157. * @param item
  158. * @param tbItem
  159. */
  160. private void handlerFac(IExpenseItem item, TbItem tbItem, String uniqueExpenseId) {
  161. List<IrulesItem> rules = item.getRules();
  162. doHanlder(rules, tbItem, uniqueExpenseId);
  163. }
  164. private void doHanlder(List<IrulesItem> rules, TbItem tbItem, String uniqueExpenseId) {
  165. rules.forEach(irulesItem -> {
  166. Long facId = irulesItem.getFacId();
  167. TbItemFac tbItemFac = tbItemFacService.getByFaceId(facId);
  168. if (tbItemFac == null) {
  169. tbItemFac = new TbItemFac();
  170. }
  171. String rangeVal = irulesItem.getPriceFactor().getRangeVal();
  172. List<String> rangeVals = StrUtil.splitTrim(rangeVal, "$$");
  173. String minVal = rangeVals.get(0);
  174. if (NumberUtil.isNumber(minVal)) {
  175. tbItemFac.setMinRange(Double.valueOf(minVal));
  176. }
  177. String maxVal = rangeVals.get(1);
  178. if (NumberUtil.isNumber(maxVal)) {
  179. tbItemFac.setMaxRange(Double.valueOf(maxVal));
  180. }
  181. tbItemFac.setName(irulesItem.getPriceFactor().getFacName()).setOperator(irulesItem.getPriceFactor().getAliasName())
  182. .setTextType(irulesItem.getPriceFactor().getTextType())
  183. .setFacId(facId).setRangeVal(rangeVal).setUniqueExpenseId(uniqueExpenseId)
  184. .setUnit(irulesItem.getPriceFactor().getUnit());
  185. tbItemFac.setItemId(tbItem.getId());
  186. tbItemFacService.saveOrUpdate(tbItemFac);
  187. List<IrulesItem> children = irulesItem.getChildren();
  188. if (children != null) {
  189. doHanlder(children, tbItem, uniqueExpenseId);
  190. }
  191. });
  192. }
  193. /**
  194. * 停车费规则
  195. *
  196. * @return
  197. */
  198. public TbItem getPartItem() {
  199. QueryWrapper<TbItem> ew = new QueryWrapper<>();
  200. ew.lambda().like(TbItem::getItemName, "停车费");
  201. List<TbItem> list = this.list(ew);
  202. return list.isEmpty() ? null : list.get(0);
  203. }
  204. /**
  205. * 根据code删除
  206. *
  207. * @param uniqueExpenseId
  208. */
  209. private void removeByCode(String uniqueExpenseId) {
  210. QueryWrapper<TbItem> ew = new QueryWrapper<>();
  211. ew.lambda().eq(TbItem::getItemCode, uniqueExpenseId);
  212. this.remove(ew);
  213. }
  214. /**
  215. * 查找停车费项
  216. *
  217. * @param itemTypeNameKeyword
  218. * @param itemNameKeyword
  219. * @return
  220. */
  221. public TbItem findByTypeNameAndItemName(String itemTypeNameKeyword, String itemNameKeyword) {
  222. QueryWrapper<TbItem> ew = new QueryWrapper<>();
  223. ew.lambda().like(TbItem::getTypeName, itemTypeNameKeyword).like(TbItem::getItemName, itemNameKeyword);
  224. List<TbItem> list = list(ew);
  225. return list.isEmpty() ? null : list.get(0);
  226. }
  227. /**
  228. * 仅仅入场管理费可用
  229. *
  230. * @param itemName
  231. * @return
  232. */
  233. public TbItem findByName(String itemName) {
  234. QueryWrapper<TbItem> ew = new QueryWrapper<>();
  235. ew.lambda().eq(TbItem::getItemName, itemName);
  236. List<TbItem> list = list(ew);
  237. return list.isEmpty() ? null : list.get(0);
  238. }
  239. }