package com.pj.project.tb_item; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.pj.constants.business.CarEnum; import com.pj.constants.business.SyncTypeEnum; import com.pj.project.relation_type_item.RelationTypeItem; import com.pj.project.relation_type_item.RelationTypeItemService; import com.pj.project.sync.HelpService; import com.pj.project.sync.bo.ItemTypeBO; import com.pj.project.sync.response.item.IExpenseItem; import com.pj.project.sync.response.item.IrulesItem; import com.pj.project.tb_costomer.TbCostomer; import com.pj.project.tb_fee_item.TbFeeItem; import com.pj.project.tb_fee_item.TbFeeItemService; import com.pj.project.tb_item_fac.TbItemFac; import com.pj.project.tb_item_fac.TbItemFacService; import com.pj.project.tb_item_type.TbItemType; import com.pj.project.tb_item_type.TbItemTypeService; import com.pj.utils.so.SoMap; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.Date; import java.util.List; /** * Service: tb_item -- 作业配置项 * * @author qzy */ @Service @Slf4j @Transactional public class TbItemService extends ServiceImpl implements IService { /** * 底层 Mapper 对象 */ @Autowired TbItemMapper tbItemMapper; @Resource private HelpService helpService; @Resource RelationTypeItemService relationTypeItemService; @Resource private TbItemTypeService tbItemTypeService; @Resource private TbItemFacService tbItemFacService; @Resource private TbFeeItemService tbFeeItemService; /** * 删 */ void delete(Long id) { this.removeById(id); } /** * 查集合 - 根据条件(参数为空时代表忽略指定条件) */ List getList(SoMap so) { return tbItemMapper.getList(so); } public List getTypeItemList(SoMap soMap) { return tbItemMapper.getTypeItemList(soMap); } public List getItemTypeById(String typeId) { return tbItemMapper.getItemTypeById(typeId); } public TbItem findByCode(String code) { QueryWrapper ew = new QueryWrapper<>(); ew.eq("item_code", code); List list = list(ew); return list.isEmpty() ? null : list.get(0); } /** * 费项详情 * * @param request */ public void receive(String request) { IExpenseItem iExpenseItem = helpService.deCrypte(request, IExpenseItem.class); TbItemType tbItemType = tbItemTypeService.findByCode(iExpenseItem.getBizTypeNo()); log.info("detail:{}", JSONUtil.toJsonStr(iExpenseItem)); if (tbItemType == null) { log.error("类型不存在"); return; } String uniqueExpenseId = iExpenseItem.getUniqueExpenseId(); String syncType = iExpenseItem.getSyncType(); if (StrUtil.equals(syncType, SyncTypeEnum.DELETE.getCode())) { TbItem db = findByCode(uniqueExpenseId); if (db != null) { this.removeById(db.getId()); relationTypeItemService.removeByItemId(db.getId()); tbItemFacService.removeByItemId(db.getId()); } } else { TbItem tbItem = handlerItem(tbItemType, iExpenseItem, new Date()); //多规格费项因子 if (StrUtil.equals(iExpenseItem.getPattern(), "RULES")) { handlerFac(iExpenseItem, tbItem, iExpenseItem.getUniqueExpenseId()); } } } /** * 处理费项详情 * * @param tbItemType * @param item * @param now * @return */ private TbItem handlerItem(TbItemType tbItemType, IExpenseItem item, Date now) { String name = item.getExpenseName(); String code = item.getUniqueExpenseId(); TbItem tbItem = this.findByCode(code.trim()); if (tbItem == null) { tbItem = new TbItem(); } String payType = tbItemType.getPayType(); tbItem.setPayType(Integer.parseInt(payType)) .setPayTypeName(tbItemType.getPayTypeName()); String typeId = tbItemType.getId(); tbItem.setItemCode(code).setItemName(name).setUnit(item.getExpenseUnit()).setStatus(1) .setInc(1).setNeedRemark(0).setPrice(item.getFixedPrice()).setMinLength(0).setCarLength(25) .setMinWeight(0).setMaxWeight(999999).setTaxRate(item.getTaxRate().doubleValue()) .setTypeId(typeId) .setTypeName(tbItemType.getName()) .setItemType(CarEnum.CarTypeEnum.EMPTY_TYPE.getDesc() + "," + CarEnum.CarTypeEnum.WEIGHT_TYPE.getDesc()); String businessType = tbItemType.getBusinessType(); if (StrUtil.isEmpty(businessType) || StrUtil.equals("0", businessType)) { tbItem.setBusinessType(TbCostomer.CustomerEnum.BUSINESS_TYPE.getType()) .setBusinessTypeName("默认"); } else { tbItem.setBusinessType(businessType).setBusinessTypeName(tbItemType.getPayTypeName()); } this.saveOrUpdate(tbItem); RelationTypeItem relationTypeItem = relationTypeItemService.findByTypeIdAndItemId(typeId, tbItem.getId()); if (relationTypeItem == null) { relationTypeItem = new RelationTypeItem(); relationTypeItem.setInc(1).setNeedRemark(0).setMustRemark(0); } relationTypeItem.setTypeId(typeId).setItemId(tbItem.getId()) .setTypeName(tbItemType.getName()); relationTypeItemService.saveOrUpdate(relationTypeItem); return tbItem; } /** * 费项因子 * * @param item * @param tbItem */ private void handlerFac(IExpenseItem item, TbItem tbItem, String uniqueExpenseId) { List rules = item.getRules(); doHanlder(rules, tbItem, uniqueExpenseId); } private void doHanlder(List rules, TbItem tbItem, String uniqueExpenseId) { rules.forEach(irulesItem -> { Long facId = irulesItem.getFacId(); TbItemFac tbItemFac = tbItemFacService.getByFaceId(facId); if (tbItemFac == null) { tbItemFac = new TbItemFac(); } String rangeVal = irulesItem.getPriceFactor().getRangeVal(); List rangeVals = StrUtil.splitTrim(rangeVal, "$$"); String minVal = rangeVals.get(0); if (NumberUtil.isNumber(minVal)) { tbItemFac.setMinRange(Double.valueOf(minVal)); } String maxVal = rangeVals.get(1); if (NumberUtil.isNumber(maxVal)) { tbItemFac.setMaxRange(Double.valueOf(maxVal)); } tbItemFac.setName(irulesItem.getPriceFactor().getFacName()).setOperator(irulesItem.getPriceFactor().getAliasName()) .setTextType(irulesItem.getPriceFactor().getTextType()) .setFacId(facId).setRangeVal(rangeVal).setUniqueExpenseId(uniqueExpenseId) .setUnit(irulesItem.getPriceFactor().getUnit()); tbItemFac.setItemId(tbItem.getId()); tbItemFacService.saveOrUpdate(tbItemFac); List children = irulesItem.getChildren(); if (children != null) { doHanlder(children, tbItem, uniqueExpenseId); } }); } /** * 停车费规则 * * @return */ public TbItem getPartItem() { QueryWrapper ew = new QueryWrapper<>(); ew.lambda().like(TbItem::getItemName, "停车费"); List list = this.list(ew); return list.isEmpty() ? null : list.get(0); } /** * 根据code删除 * * @param uniqueExpenseId */ private void removeByCode(String uniqueExpenseId) { QueryWrapper ew = new QueryWrapper<>(); ew.lambda().eq(TbItem::getItemCode, uniqueExpenseId); this.remove(ew); } /** * 查找停车费项 * * @param itemTypeNameKeyword * @param itemNameKeyword * @return */ public TbItem findByTypeNameAndItemName(String itemTypeNameKeyword, String itemNameKeyword) { QueryWrapper ew = new QueryWrapper<>(); ew.lambda().like(TbItem::getTypeName, itemTypeNameKeyword).like(TbItem::getItemName, itemNameKeyword); List list = list(ew); return list.isEmpty() ? null : list.get(0); } /** * 仅仅入场管理费可用 * * @param itemName * @return */ public TbItem findByName(String itemName) { QueryWrapper ew = new QueryWrapper<>(); ew.lambda().eq(TbItem::getItemName, itemName); List list = list(ew); return list.isEmpty() ? null : list.get(0); } }