package com.hx.phip.his.service.impl; import com.hx.exception.TipsException; import com.hx.mybatisTool.SqlSentence; import com.hx.phiappt.common.*; import com.hx.phiappt.model.*; import com.hx.phiappt.model.consumables.Consumables; import com.hx.phiappt.model.deduction.*; import com.hx.phiappt.model.order.OrderItem; import com.hx.phiappt.model.order.OrderItemSon; import com.hx.phiappt.model.order.OrdersTotal; import com.hx.phiappt.model.promotion.Promotion; import com.hx.phiappt.model.user.UserProject; import com.hx.phiappt.model.user.UserProjectItem; import com.hx.phiappt.model.warehouse.ShopWarehouse; import com.hx.phiappt.model.warehouse.ShopWarehouseItem; import com.hx.phip.his.config.SystemConfig; import com.hx.phiappt.dao.mapper.*; import com.hx.phip.dao.mapper.syn.HisOrderItemRecordMapper; import com.hx.phip.his.service.SyncExecutionRecordNewService; import com.hx.phip.his.service.SyncProjectTreeService; import com.hx.phip.his.service.SyncTransactioalService; import com.hx.phip.model.HisOrderItemRecord; import com.hx.phip.uti.CreateNo; import com.hx.resultTool.ResponseCode; import com.hx.resultTool.Result; import com.hx.util.StringUtils; import com.mongodb.BasicDBObject; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.math.BigDecimal; import java.util.*; /** * @Author */ @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class) @Service public class SyncExecutionRecordNewServiceImpl implements SyncExecutionRecordNewService { private static Logger logger = LoggerFactory.getLogger(SyncExecutionRecordNewServiceImpl.class); @Resource private SystemConfig systemConfig; @Resource private UserMapper userMapper; @Resource private OrderItemMapper orderItemMapper; @Resource private OrderItemSonMapper orderItemSonMapper; @Resource private DeductionProjectMapper deductionProjectMapper; @Resource private DeductionDrugsMapper deductionDrugsMapper; @Resource private ConsumablesMapper consumablesMapper; @Resource private ShopWarehouseMapper shopWarehouseMapper; @Resource private DeductionJoinMapper deductionJoinMapper; @Resource private EmployeeRoleMapper employeeRoleMapper; @Resource private OrdersTotalMapper ordersTotalMapper; @Resource private ProjectMapper projectMapper; @Resource private UserProjectMapper userProjectMapper; @Resource private UserProjectItemMapper userProjectItemMapper; @Resource private SystemParameterMapper systemParameterMapper; @Resource private FamiliesRoomMapper familiesRoomMapper; @Resource private DeductionSingleMapper deductionSingleMapper; @Resource private EmployeeMapper employeeMapper; @Resource private ShopWarehouseItemMapper shopWarehouseItemMapper; @Resource private ShopMapper shopMapper; @Resource private PromotionMapper promotionMapper; @Resource private DeductionDeviceParameterMapper deductionDeviceParameterMapper; @Resource private DeviceMapper deviceMapper; @Resource private DeviceParameterMapper deviceParameterMapper; // @Autowired private MongoTemplate mongoTemplate; @Autowired private SyncProjectTreeService syncProjectTreeService; @Resource private SyncTransactioalService syncTransactioalService; /** * 处理同步可执行记录 * @param obj */ @Override public void handleexecutableRecord(JSONObject obj) { if (obj != null) { SqlSentence sqlSentence = new SqlSentence(); Map map = new HashMap<>(); sqlSentence.setM(map); String itemId = obj.optString("id");//执行条目id sqlSentence.setSqlSentence("select * from user_project_item where itemId = #{m.itemId} and isDel = 0 "); map.put("itemId", itemId); UserProjectItem userProjectItem = userProjectItemMapper.selectOne(sqlSentence); Project p = null; User user =null; UserProject userProject =null; UserProject userProjectCopy =null; OrdersTotal ordersTotal=null; String orderId = obj.optString("orderId");//订单号 JSONObject item = obj.optJSONObject("item");//条目信息 JSONObject skuInfo = obj.optJSONObject("skuInfo");//订单行SKU String amount = obj.optString("amount");//执行总金额 String remain = obj.optString("remain");//未执行划扣金额 sqlSentence.setSqlSentence("select * from orders_total where hisOrderId = #{m.hisOrderId} and isDel = 0 "); map.put("hisOrderId", orderId); ordersTotal = ordersTotalMapper.selectOne(sqlSentence); String shopId=""; String shopName=""; boolean isTransfer=false; if (ordersTotal == null) { String transferId=obj.optString("transferId");//可能这条划扣记录是转增过来的,根据转增id查到对应的id if (StringUtils.isEmpty(transferId)){ logger.error("订单找不到对应的,转增id也为空,执行条目id:{}", itemId); return; } isTransfer=true; Query query = new Query(); Criteria criteria=new Criteria(); criteria.and("_id").is(transferId); query.addCriteria(criteria); BasicDBObject basicDBObject = mongoTemplate.findOne(query,BasicDBObject.class,"his-transfer"); if(basicDBObject==null) { logger.error("找不到对应的转增id,转增id:{},执行条目id:{}",transferId, itemId); return; } map.put("hisOrderId", basicDBObject.getString("orderId")); ordersTotal = ordersTotalMapper.selectOne(sqlSentence); if (ordersTotal == null) { logger.error("通过转增id查询的订单在phis找不到,转增id:{},执行条目id:{}",transferId, itemId); return; } JSONObject receiverOrganization=JSONObject.fromObject(basicDBObject.get("receiverOrganization")); Shop shop = shopMapper.selectOneByApiId(receiverOrganization.optString("_id")); if(shop==null){ shopId=ordersTotal.getShopId(); shopName=ordersTotal.getShopName(); }else { shopId=shop.getId(); shopName=shop.getName(); } }else { shopId=ordersTotal.getShopId(); shopName=ordersTotal.getShopName(); } //2022-12-06 荣爷说未执行门店已领建数据为主 JSONObject clinic= obj.optJSONObject("clinic");//执行门店 if(clinic!=null){ Shop shop = shopMapper.selectOneByApiId(clinic.optString("id")); if(shop!=null){ shopId=shop.getId(); shopName=shop.getName(); } } if (item != null) { p = projectMapper.selectOneByApiId(item.optString("id")); if (p == null) { logger.error("请先同步项目,项目apild:" + item.optString("id")); return; } } JSONObject customerJson = obj.optJSONObject("customer");//客户信息 if (customerJson != null) { user = userMapper.selectOneByApiId(customerJson.optString("id")); if (user == null) { logger.error("请先同步用户,用户apild:" + customerJson.optString("id")); return; } } int totalTimes = obj.optInt("totalTimes");//可执行总次数 int leftTimes = obj.optInt("leftTimes");//剩余可执行次数 String status = obj.optString("status");//状态 sqlSentence.setSqlSentence("select * from user_project where goodsId = #{m.goodsId} and userId = #{m.userId} and isDel = 0 "); map.put("goodsId", p.getId()); map.put("userId", user.getId()); userProject= userProjectMapper.selectOne(sqlSentence); Calendar calendar=Calendar.getInstance(); if(userProjectItem==null){ //因为pis新版本的开单是会处理未划扣数据,所有发现数据是新的的话,订单appid不是领建就不新增 //荣爷说crm也要处理 2022-12-12 10.05 if(!systemConfig.getApiAppId().equals(ordersTotal.getAppId()) && !isTransfer){ return; } /* // 09-26 开会的时候说,同步中心只处理领健创建的订单的数据 if(!systemConfig.getApiAppId().equals(ordersTotal.getAppId())){ // logger.info("订单不是领健创建的,由phis自行处理订单id:{}",ordersTotal.getHisOrderId()); return; } */ userProjectItem=new UserProjectItem(); // 处理用户拥有的项目子项 String itId = item.optString("id"); String skuInfoId = skuInfo.optString("id"); String type = obj.optString("type");//项目类型 service 项目, promotion 套餐项目, card-service 卡项项目 if(userProject==null){ userProject=new UserProject(); //判断这个用户第一次使用这个项目 userProject.setType(OrderGoodsConstants.TYPE_PROJECT); userProject.setGoodsId(p.getId()); userProject.setGoodsName(p.getName()); userProject.setGoodsNo(p.getCoding()); userProject.setUserId(user.getId()); userProject.setCreateTime(new Date(obj.optLong("createdAt"))); userProject.setUsedTotal(totalTimes); userProject.setNotUsedNum(leftTimes); userProject.setUsedNum(totalTimes - leftTimes); userProject.setOverdueNum(0); userProject.setTransferNum(0); userProject.setCancelNum(0); userProject.setInBygNum(0); userProject.setEffectiveStatus(UserProjectConstants.EFF_STATUS_YES);//待改变 userProjectCopy= userProjectMapper.selectOne(sqlSentence); if(userProjectCopy==null){ userProject.setIsSpa(syncProjectTreeService.projectSpa(p.getProjectTypeId())?1:0); userProjectMapper.insert(userProject); }else { userProjectCopy.setUsedTotal(userProjectCopy.getUsedTotal()+totalTimes); userProjectCopy.setNotUsedNum(userProjectCopy.getNotUsedNum()+leftTimes); userProjectCopy.setUsedNum(userProjectCopy.getUsedNum()+totalTimes - leftTimes); userProjectMapper.updateAll(userProjectCopy); } }else { //判断这个用户第n次使用这个项目 userProject.setUsedTotal(userProject.getUsedTotal()+totalTimes); userProject.setNotUsedNum(userProject.getNotUsedNum()+leftTimes); userProject.setUsedNum(userProject.getUsedNum()+totalTimes - leftTimes); userProjectMapper.updateAll(userProject); } if ("service".equals(type)) { //项目类型划扣 sqlSentence.setSqlSentence("select * from order_item where orderId = #{m.orderId} and commonId=#{m.commonId} and isDel = 0 "); map.put("orderId", ordersTotal.getId()); map.put("commonId", p.getId()); List list=orderItemMapper.selectList(sqlSentence); if(list!=null && list.size()>=1) { //普通订单 OrderItem items = list.get(0); userProjectItem.setTotal(items.getTotal()); userProjectItem.setActualTotal(items.getActualTotal()); userProjectItem.setOriPrice(items.getOriPrice()); userProjectItem.setCurPrice(items.getCurPrice()); userProjectItem.setBuyNum(items.getBuyNum()); userProjectItem.setUsedTotal(totalTimes); userProjectItem.setNotUsedNum(leftTimes); if("cancelled".equals(status)){ userProjectItem.setUsedNum(0); }else { userProjectItem.setUsedNum(totalTimes - leftTimes); } userProjectItem.setShopId(shopId); userProjectItem.setShopName(shopName); userProjectItem.setDepartmentCode(ordersTotal.getDepartmentCode()); userProjectItem.setDepartmentName(ordersTotal.getDepartmentName()); userProjectItem.setTimeLimit(0); userProjectItem.setLimitStartTime(new Date()); calendar.setTime(new Date()); calendar.add(Calendar.YEAR,100); userProjectItem.setLimitEndTime(calendar.getTime()); userProjectItem.setCommonType(OrderSourceConstans.TYPE_PROJECT); userProjectItem.setCommonId(items.getId()); userProjectItem.setUsedOne(items.getSingle()); userProjectItem.setUserProjectId(userProject.getId()); userProjectItem.setUserId(userProject.getUserId()); userProjectItem.setOrderItemId(items.getId()); userProjectItem.setItemId(itemId); userProjectItem.setEffectiveStatus(transformationStatus(status)); userProjectItem.setNoDeductionAmount(new BigDecimal(remain)); userProjectItem.setDeductionTotalAmount(new BigDecimal(amount)); userProjectItem.setIsSpa(syncProjectTreeService.projectSpa(p.getProjectTypeId())?1:0); userProjectItem.setOrdersTotalId(ordersTotal.getId()); userProjectItem.setOrdersTotalNumber(ordersTotal.getOrderNo()); userProjectItem.setGoodsId(p.getId()); userProjectItem.setGoodsName(p.getName()); userProjectItem.setSpecs(items.getSpecs()); userProjectItemMapper.insert(userProjectItem); }else { //普通订单找不到就肯定是phis开的卡包订单 sqlSentence.setSqlSentence(" SELECT ois.* FROM order_item_source ois INNER JOIN order_item oi ON oi.id=ois.orderItemId " + " where ois.orderId = #{m.orderId} and ois.goodsId=#{m.commonId}"); map.put("orderId", ordersTotal.getId()); map.put("commonId", p.getId()); List sonListlist=orderItemSonMapper.selectList(sqlSentence); if(sonListlist==null || sonListlist.size()==0) { throw new RuntimeException("未找到未执行记录的订单记录!") ; } OrderItemSon orderItemSon = sonListlist.get(0); userProjectItem.setTotal(orderItemSon.getTotal()); userProjectItem.setActualTotal(orderItemSon.getActualTotal()); userProjectItem.setOriPrice(orderItemSon.getOriPrice()); userProjectItem.setCurPrice(orderItemSon.getCurPrice()); userProjectItem.setBuyNum(orderItemSon.getBuyNum()); userProjectItem.setUsedTotal(totalTimes); userProjectItem.setNotUsedNum(leftTimes); if("cancelled".equals(status)){ userProjectItem.setUsedNum(0); }else { userProjectItem.setUsedNum(totalTimes - leftTimes); } userProjectItem.setShopId(shopId); userProjectItem.setShopName(shopName); userProjectItem.setDepartmentCode(ordersTotal.getDepartmentCode()); userProjectItem.setDepartmentName(ordersTotal.getDepartmentName()); userProjectItem.setTimeLimit(0); userProjectItem.setLimitStartTime(new Date()); calendar.setTime(new Date()); calendar.add(Calendar.YEAR,100); userProjectItem.setLimitEndTime(calendar.getTime()); userProjectItem.setCommonType(OrderSourceConstans.TYPE_RETAIL); userProjectItem.setCommonId(orderItemSon.getId()); userProjectItem.setUsedOne(orderItemSon.getUsedOne()); userProjectItem.setUserProjectId(userProject.getId()); userProjectItem.setUserId(userProject.getUserId()); userProjectItem.setOrderItemId(orderItemSon.getId()); userProjectItem.setItemId(itemId); userProjectItem.setEffectiveStatus(transformationStatus(status)); userProjectItem.setNoDeductionAmount(new BigDecimal(remain)); userProjectItem.setDeductionTotalAmount(new BigDecimal(amount)); userProjectItem.setIsSpa(syncProjectTreeService.projectSpa(p.getProjectTypeId())?1:0); userProjectItem.setOrdersTotalId(ordersTotal.getId()); userProjectItem.setOrdersTotalNumber(ordersTotal.getOrderNo()); userProjectItem.setGoodsId(p.getId()); userProjectItem.setGoodsName(p.getName()); userProjectItem.setSpecs(orderItemSon.getSpecs()); userProjectItemMapper.insert(userProjectItem); } }else if("card-service".equals(type)){ //普通订单找不到就肯定是phis开的卡包订单 sqlSentence.setSqlSentence(" SELECT ois.* FROM order_item_source ois INNER JOIN order_item oi ON oi.id=ois.orderItemId " + " where ois.orderId = #{m.orderId} and ois.goodsId=#{m.commonId}"); map.put("orderId", ordersTotal.getId()); map.put("commonId", p.getId()); List sonListlist=orderItemSonMapper.selectList(sqlSentence); if(sonListlist==null || sonListlist.size()==0) throw new RuntimeException("未找到未执行记录的订单记录!") ; OrderItemSon orderItemSon = sonListlist.get(0); userProjectItem.setTotal(orderItemSon.getTotal()); userProjectItem.setActualTotal(orderItemSon.getActualTotal()); userProjectItem.setOriPrice(orderItemSon.getOriPrice()); userProjectItem.setCurPrice(orderItemSon.getCurPrice()); userProjectItem.setBuyNum(orderItemSon.getBuyNum()); userProjectItem.setUsedTotal(totalTimes); userProjectItem.setNotUsedNum(leftTimes); if("cancelled".equals(status)){ userProjectItem.setUsedNum(0); }else { userProjectItem.setUsedNum(totalTimes - leftTimes); } userProjectItem.setShopId(shopId); userProjectItem.setShopName(shopName); userProjectItem.setDepartmentCode(ordersTotal.getDepartmentCode()); userProjectItem.setDepartmentName(ordersTotal.getDepartmentName()); userProjectItem.setTimeLimit(0); userProjectItem.setLimitStartTime(new Date()); calendar.setTime(new Date()); calendar.add(Calendar.YEAR,100); userProjectItem.setLimitEndTime(calendar.getTime()); userProjectItem.setCommonType(OrderSourceConstans.TYPE_RETAIL); userProjectItem.setCommonId(orderItemSon.getId()); userProjectItem.setUsedOne(orderItemSon.getUsedOne()); userProjectItem.setUserProjectId(userProject.getId()); userProjectItem.setUserId(userProject.getUserId()); userProjectItem.setOrderItemId(orderItemSon.getId()); userProjectItem.setItemId(itemId); userProjectItem.setEffectiveStatus(transformationStatus(status)); userProjectItem.setNoDeductionAmount(new BigDecimal(remain)); userProjectItem.setDeductionTotalAmount(new BigDecimal(amount)); userProjectItem.setIsSpa(syncProjectTreeService.projectSpa(p.getProjectTypeId())?1:0); userProjectItem.setOrdersTotalId(ordersTotal.getId()); userProjectItem.setOrdersTotalNumber(ordersTotal.getOrderNo()); userProjectItem.setGoodsId(p.getId()); userProjectItem.setGoodsName(p.getName()); userProjectItem.setSpecs(orderItemSon.getSpecs()); userProjectItemMapper.insert(userProjectItem); }else { //卡项待商量 sqlSentence.setSqlSentence("select * from promotion where apiId = #{m.skuInfoId} and isDel = 0"); map.put("skuInfoId", skuInfoId); Promotion promotion=promotionMapper.selectOne(sqlSentence); if(promotion==null){ // logger.info("促销没找到itemID:{},促销id:{}",itemId,skuInfoId); throw new RuntimeException("促销没找到itemID!") ; } map.put("promotionId", promotion.getId()); sqlSentence.setSqlSentence(" SELECT ois.* FROM order_item_source ois INNER JOIN order_item oi ON oi.id=ois.orderItemId " + " where ois.orderId = #{m.orderId} and ois.goodsId=#{m.commonId} and oi.commonId=#{m.promotionId} "); map.put("orderId", ordersTotal.getId()); map.put("commonId", p.getId()); List list=orderItemSonMapper.selectList(sqlSentence); if(list==null || list.size()==0) throw new RuntimeException("未找到未执行记录的订单记录!") ;; OrderItemSon orderItemSon = list.get(0); userProjectItem.setTotal(orderItemSon.getTotal()); userProjectItem.setActualTotal(orderItemSon.getActualTotal()); userProjectItem.setOriPrice(orderItemSon.getOriPrice()); userProjectItem.setCurPrice(orderItemSon.getCurPrice()); userProjectItem.setBuyNum(orderItemSon.getBuyNum()); userProjectItem.setUsedTotal(totalTimes); userProjectItem.setNotUsedNum(leftTimes); if("cancelled".equals(status)){ userProjectItem.setUsedNum(0); }else { userProjectItem.setUsedNum(totalTimes - leftTimes); } userProjectItem.setShopId(shopId); userProjectItem.setShopName(shopName); userProjectItem.setDepartmentCode(ordersTotal.getDepartmentCode()); userProjectItem.setDepartmentName(ordersTotal.getDepartmentName()); userProjectItem.setTimeLimit(0); userProjectItem.setLimitStartTime(new Date()); calendar.setTime(new Date()); calendar.add(Calendar.YEAR,100); userProjectItem.setLimitEndTime(calendar.getTime()); userProjectItem.setCommonType(OrderSourceConstans.TYPE_RETAIL); userProjectItem.setCommonId(orderItemSon.getId()); userProjectItem.setUsedOne(orderItemSon.getUsedOne()); userProjectItem.setUserProjectId(userProject.getId()); userProjectItem.setUserId(userProject.getUserId()); userProjectItem.setOrderItemId(orderItemSon.getId()); userProjectItem.setItemId(itemId); userProjectItem.setEffectiveStatus(transformationStatus(status)); userProjectItem.setNoDeductionAmount(new BigDecimal(remain)); userProjectItem.setDeductionTotalAmount(new BigDecimal(amount)); userProjectItem.setIsSpa(syncProjectTreeService.projectSpa(p.getProjectTypeId())?1:0); userProjectItem.setOrdersTotalId(ordersTotal.getId()); userProjectItem.setOrdersTotalNumber(ordersTotal.getOrderNo()); userProjectItem.setGoodsId(p.getId()); userProjectItem.setGoodsName(p.getName()); userProjectItem.setSpecs(orderItemSon.getSpecs()); userProjectItemMapper.insert(userProjectItem); } } //2023-01-16 屏蔽掉修改用户项目,因为现在划扣只是再phis操作,没同步领建,修改会导致数量错误 else { int notUsedNum=userProjectItem.getNotUsedNum(); // 修改用户拥有的项目子项 userProjectItem.setNotUsedNum(leftTimes); userProjectItem.setUsedTotal(totalTimes); if("cancelled".equals(status)){ userProjectItem.setUsedNum(0); }else { userProjectItem.setUsedNum(totalTimes - leftTimes); } userProjectItem.setEffectiveStatus(transformationStatus(status)); userProjectItem.setNoDeductionAmount(new BigDecimal(remain)); userProjectItem.setDeductionTotalAmount(new BigDecimal(amount)); userProjectItem.setOrdersTotalId(ordersTotal.getId()); userProjectItem.setOrdersTotalNumber(ordersTotal.getOrderNo()); userProjectItemMapper.updateAll(userProjectItem); //修改用户拥有的项目 userProject=userProjectMapper.selectOneByKey(userProjectItem.getUserProjectId()); if(notUsedNum>leftTimes){ leftTimes=notUsedNum - leftTimes; userProject.setNotUsedNum(userProject.getNotUsedNum()-leftTimes); userProject.setUsedNum(userProject.getUsedNum()+leftTimes); }else if(notUsedNum map = new HashMap<>(); sqlSentence.setM(map); String itemId = obj.optString("id");//执行条目id sqlSentence.setSqlSentence("select * from user_project_item where itemId = #{m.itemId} and isDel = 0 "); map.put("itemId", itemId); UserProjectItem userProjectItem = userProjectItemMapper.selectOne(sqlSentence); //存在就不处理 if(userProjectItem != null){ return; } Project p = null; User user =null; UserProject userProject =null; UserProject userProjectCopy =null; OrdersTotal ordersTotal=null; String orderId = obj.optString("orderId");//订单号 JSONObject item = obj.optJSONObject("item");//条目信息 JSONObject skuInfo = obj.optJSONObject("skuInfo");//订单行SKU String amount = obj.optString("amount");//执行总金额 String remain = obj.optString("remain");//未执行划扣金额 sqlSentence.setSqlSentence("select * from orders_total where hisOrderId = #{m.hisOrderId} and isDel = 0 "); map.put("hisOrderId", orderId); ordersTotal = ordersTotalMapper.selectOne(sqlSentence); String shopId=""; String shopName=""; boolean isTransfer=false; if (ordersTotal == null) { String transferId=obj.optString("transferId");//可能这条划扣记录是转增过来的,根据转增id查到对应的id if (StringUtils.isEmpty(transferId)){ logger.error("订单找不到对应的,转增id也为空,执行条目id:{}", itemId); return; } isTransfer=true; Query query = new Query(); Criteria criteria=new Criteria(); criteria.and("_id").is(transferId); query.addCriteria(criteria); BasicDBObject basicDBObject = mongoTemplate.findOne(query,BasicDBObject.class,"his-transfer"); if(basicDBObject==null) { logger.error("找不到对应的转增id,转增id:{},执行条目id:{}",transferId, itemId); return; } map.put("hisOrderId", basicDBObject.getString("orderId")); ordersTotal = ordersTotalMapper.selectOne(sqlSentence); if (ordersTotal == null) { logger.error("通过转增id查询的订单在phis找不到,转增id:{},执行条目id:{}",transferId, itemId); return; } JSONObject receiverOrganization=JSONObject.fromObject(basicDBObject.get("receiverOrganization")); Shop shop = shopMapper.selectOneByApiId(receiverOrganization.optString("_id")); if(shop==null){ shopId=ordersTotal.getShopId(); shopName=ordersTotal.getShopName(); }else { shopId=shop.getId(); shopName=shop.getName(); } }else { shopId=ordersTotal.getShopId(); shopName=ordersTotal.getShopName(); } //2022-12-06 荣爷说未执行门店已领建数据为主 JSONObject clinic= obj.optJSONObject("clinic");//执行门店 if(clinic!=null){ Shop shop = shopMapper.selectOneByApiId(clinic.optString("id")); if(shop!=null){ shopId=shop.getId(); shopName=shop.getName(); } } if (item != null) { p = projectMapper.selectOneByApiId(item.optString("id")); if (p == null) { logger.info("请先同步项目,项目apild:" + item.optString("id")); return; } } JSONObject customerJson = obj.optJSONObject("customer");//客户信息 if (customerJson != null) { user = userMapper.selectOneByApiId(customerJson.optString("id")); if (user == null) { logger.info("请先同步用户,用户apild:" + customerJson.optString("id")); return; } } int totalTimes = obj.optInt("totalTimes");//可执行总次数 int leftTimes = obj.optInt("leftTimes");//剩余可执行次数 String status = obj.optString("status");//状态 String transferId = obj.optString("transferId");//转增标识 sqlSentence.setSqlSentence("select * from user_project where goodsId = #{m.goodsId} and userId = #{m.userId} and isDel = 0 "); map.put("goodsId", p.getId()); map.put("userId", user.getId()); userProject= userProjectMapper.selectOne(sqlSentence); Calendar calendar=Calendar.getInstance(); if(userProjectItem==null){ //因为pis新版本的开单是会处理未划扣数据,所有发现数据是新的的话,订单appid不是领建就不新增 //荣爷说crm也要处理 2022-12-12 10.05 /*if(!systemConfig.getApiAppId().equals(ordersTotal.getAppId()) && !isTransfer){ return; }*/ userProjectItem=new UserProjectItem(); // 处理用户拥有的项目子项 String itId = item.optString("id"); String skuInfoId = skuInfo.optString("id"); String type = obj.optString("type");//项目类型 service 项目, promotion 套餐项目, card-service 卡项项目 if(userProject==null){ userProject=new UserProject(); //判断这个用户第一次使用这个项目 userProject.setType(OrderGoodsConstants.TYPE_PROJECT); userProject.setGoodsId(p.getId()); userProject.setGoodsName(p.getName()); userProject.setGoodsNo(p.getCoding()); userProject.setUserId(user.getId()); userProject.setCreateTime(new Date(obj.optLong("createdAt"))); userProject.setUsedTotal(totalTimes); userProject.setNotUsedNum(leftTimes); userProject.setUsedNum(totalTimes - leftTimes); userProject.setOverdueNum(0); userProject.setTransferNum(0); userProject.setCancelNum(0); userProject.setInBygNum(0); userProject.setEffectiveStatus(UserProjectConstants.EFF_STATUS_YES);//待改变 userProjectCopy= userProjectMapper.selectOne(sqlSentence); if(userProjectCopy==null){ userProject.setIsSpa(syncProjectTreeService.projectSpa(p.getProjectTypeId())?1:0); userProjectMapper.insert(userProject); }else { userProjectCopy.setUsedTotal(userProjectCopy.getUsedTotal()+totalTimes); userProjectCopy.setNotUsedNum(userProjectCopy.getNotUsedNum()+leftTimes); userProjectCopy.setUsedNum(userProjectCopy.getUsedNum()+totalTimes - leftTimes); userProjectMapper.updateAll(userProjectCopy); } }else { //判断这个用户第n次使用这个项目 userProject.setUsedTotal(userProject.getUsedTotal()+totalTimes); userProject.setNotUsedNum(userProject.getNotUsedNum()+leftTimes); userProject.setUsedNum(userProject.getUsedNum()+totalTimes - leftTimes); userProjectMapper.updateAll(userProject); } if ("service".equals(type)) { //项目类型划扣 sqlSentence.setSqlSentence("select * from order_item where orderId = #{m.orderId} and commonId=#{m.commonId} and isDel = 0 "); map.put("orderId", ordersTotal.getId()); map.put("commonId", p.getId()); List list=orderItemMapper.selectList(sqlSentence); if(list!=null && list.size()>=1) { //普通订单 OrderItem items = list.get(0); userProjectItem.setTotal(items.getTotal()); userProjectItem.setActualTotal(items.getActualTotal()); userProjectItem.setOriPrice(items.getOriPrice()); userProjectItem.setCurPrice(items.getCurPrice()); userProjectItem.setBuyNum(items.getBuyNum()); userProjectItem.setUsedTotal(totalTimes); userProjectItem.setNotUsedNum(leftTimes); if("cancelled".equals(status)){ userProjectItem.setUsedNum(0); }else { userProjectItem.setUsedNum(totalTimes - leftTimes); } userProjectItem.setShopId(shopId); userProjectItem.setShopName(shopName); userProjectItem.setDepartmentCode(ordersTotal.getDepartmentCode()); userProjectItem.setDepartmentName(ordersTotal.getDepartmentName()); userProjectItem.setTimeLimit(0); userProjectItem.setLimitStartTime(new Date()); calendar.setTime(new Date()); calendar.add(Calendar.YEAR,100); userProjectItem.setLimitEndTime(calendar.getTime()); userProjectItem.setCommonType(OrderSourceConstans.TYPE_PROJECT); userProjectItem.setCommonId(items.getId()); userProjectItem.setUsedOne(items.getSingle()); userProjectItem.setUserProjectId(userProject.getId()); userProjectItem.setUserId(userProject.getUserId()); userProjectItem.setOrderItemId(items.getId()); userProjectItem.setItemId(itemId); userProjectItem.setEffectiveStatus(transformationStatus(status)); userProjectItem.setNoDeductionAmount(new BigDecimal(remain)); userProjectItem.setDeductionTotalAmount(new BigDecimal(amount)); userProjectItem.setIsSpa(syncProjectTreeService.projectSpa(p.getProjectTypeId())?1:0); userProjectItem.setOrdersTotalId(ordersTotal.getId()); userProjectItem.setOrdersTotalNumber(ordersTotal.getOrderNo()); userProjectItem.setGoodsId(p.getId()); userProjectItem.setGoodsName(p.getName()); userProjectItem.setSpecs(items.getSpecs()); if(StringUtils.noNull(transferId)){ userProjectItem.setIsTransfer(UserProjectItem.YES); } userProjectItemMapper.insert(userProjectItem); }else { //普通订单找不到就肯定是phis开的卡包订单 sqlSentence.setSqlSentence(" SELECT ois.* FROM order_item_source ois INNER JOIN order_item oi ON oi.id=ois.orderItemId " + " where ois.orderId = #{m.orderId} and ois.goodsId=#{m.commonId}"); map.put("orderId", ordersTotal.getId()); map.put("commonId", p.getId()); List sonListlist=orderItemSonMapper.selectList(sqlSentence); if(sonListlist==null || sonListlist.size()==0) { throw new RuntimeException("未找到未执行记录的订单记录!") ; } OrderItemSon orderItemSon = sonListlist.get(0); userProjectItem.setTotal(orderItemSon.getTotal()); userProjectItem.setActualTotal(orderItemSon.getActualTotal()); userProjectItem.setOriPrice(orderItemSon.getOriPrice()); userProjectItem.setCurPrice(orderItemSon.getCurPrice()); userProjectItem.setBuyNum(orderItemSon.getBuyNum()); userProjectItem.setUsedTotal(totalTimes); userProjectItem.setNotUsedNum(leftTimes); if("cancelled".equals(status)){ userProjectItem.setUsedNum(0); }else { userProjectItem.setUsedNum(totalTimes - leftTimes); } userProjectItem.setShopId(shopId); userProjectItem.setShopName(shopName); userProjectItem.setDepartmentCode(ordersTotal.getDepartmentCode()); userProjectItem.setDepartmentName(ordersTotal.getDepartmentName()); userProjectItem.setTimeLimit(0); userProjectItem.setLimitStartTime(new Date()); calendar.setTime(new Date()); calendar.add(Calendar.YEAR,100); userProjectItem.setLimitEndTime(calendar.getTime()); userProjectItem.setCommonType(OrderSourceConstans.TYPE_RETAIL); userProjectItem.setCommonId(orderItemSon.getId()); userProjectItem.setUsedOne(orderItemSon.getUsedOne()); userProjectItem.setUserProjectId(userProject.getId()); userProjectItem.setUserId(userProject.getUserId()); userProjectItem.setOrderItemId(orderItemSon.getId()); userProjectItem.setItemId(itemId); userProjectItem.setEffectiveStatus(transformationStatus(status)); userProjectItem.setNoDeductionAmount(new BigDecimal(remain)); userProjectItem.setDeductionTotalAmount(new BigDecimal(amount)); userProjectItem.setIsSpa(syncProjectTreeService.projectSpa(p.getProjectTypeId())?1:0); userProjectItem.setOrdersTotalId(ordersTotal.getId()); userProjectItem.setOrdersTotalNumber(ordersTotal.getOrderNo()); userProjectItem.setGoodsId(p.getId()); userProjectItem.setGoodsName(p.getName()); userProjectItem.setSpecs(orderItemSon.getSpecs()); if(StringUtils.noNull(transferId)){ userProjectItem.setIsTransfer(UserProjectItem.YES); } userProjectItemMapper.insert(userProjectItem); } }else if("card-service".equals(type)){ //普通订单找不到就肯定是phis开的卡包订单 sqlSentence.setSqlSentence(" SELECT ois.* FROM order_item_source ois INNER JOIN order_item oi ON oi.id=ois.orderItemId " + " where ois.orderId = #{m.orderId} and ois.goodsId=#{m.commonId}"); map.put("orderId", ordersTotal.getId()); map.put("commonId", p.getId()); List sonListlist=orderItemSonMapper.selectList(sqlSentence); if(sonListlist==null || sonListlist.size()==0) throw new RuntimeException("未找到未执行记录的订单记录!") ; OrderItemSon orderItemSon = sonListlist.get(0); userProjectItem.setTotal(orderItemSon.getTotal()); userProjectItem.setActualTotal(orderItemSon.getActualTotal()); userProjectItem.setOriPrice(orderItemSon.getOriPrice()); userProjectItem.setCurPrice(orderItemSon.getCurPrice()); userProjectItem.setBuyNum(orderItemSon.getBuyNum()); userProjectItem.setUsedTotal(totalTimes); userProjectItem.setNotUsedNum(leftTimes); if("cancelled".equals(status)){ userProjectItem.setUsedNum(0); }else { userProjectItem.setUsedNum(totalTimes - leftTimes); } userProjectItem.setShopId(shopId); userProjectItem.setShopName(shopName); userProjectItem.setDepartmentCode(ordersTotal.getDepartmentCode()); userProjectItem.setDepartmentName(ordersTotal.getDepartmentName()); userProjectItem.setTimeLimit(0); userProjectItem.setLimitStartTime(new Date()); calendar.setTime(new Date()); calendar.add(Calendar.YEAR,100); userProjectItem.setLimitEndTime(calendar.getTime()); userProjectItem.setCommonType(OrderSourceConstans.TYPE_RETAIL); userProjectItem.setCommonId(orderItemSon.getId()); userProjectItem.setUsedOne(orderItemSon.getUsedOne()); userProjectItem.setUserProjectId(userProject.getId()); userProjectItem.setUserId(userProject.getUserId()); userProjectItem.setOrderItemId(orderItemSon.getId()); userProjectItem.setItemId(itemId); userProjectItem.setEffectiveStatus(transformationStatus(status)); userProjectItem.setNoDeductionAmount(new BigDecimal(remain)); userProjectItem.setDeductionTotalAmount(new BigDecimal(amount)); userProjectItem.setIsSpa(syncProjectTreeService.projectSpa(p.getProjectTypeId())?1:0); userProjectItem.setOrdersTotalId(ordersTotal.getId()); userProjectItem.setOrdersTotalNumber(ordersTotal.getOrderNo()); userProjectItem.setGoodsId(p.getId()); userProjectItem.setGoodsName(p.getName()); userProjectItem.setSpecs(orderItemSon.getSpecs()); if(StringUtils.noNull(transferId)){ userProjectItem.setIsTransfer(UserProjectItem.YES); } userProjectItemMapper.insert(userProjectItem); }else { //卡项待商量 sqlSentence.setSqlSentence("select * from promotion where apiId = #{m.skuInfoId} and isDel = 0"); map.put("skuInfoId", skuInfoId); Promotion promotion=promotionMapper.selectOne(sqlSentence); if(promotion==null){ // logger.info("促销没找到itemID:{},促销id:{}",itemId,skuInfoId); throw new RuntimeException("促销没找到itemID!") ; } map.put("promotionId", promotion.getId()); sqlSentence.setSqlSentence(" SELECT ois.* FROM order_item_source ois INNER JOIN order_item oi ON oi.id=ois.orderItemId " + " where ois.orderId = #{m.orderId} and ois.goodsId=#{m.commonId} and oi.commonId=#{m.promotionId} "); map.put("orderId", ordersTotal.getId()); map.put("commonId", p.getId()); List list=orderItemSonMapper.selectList(sqlSentence); if(list==null || list.size()==0) { throw new RuntimeException("未找到未执行记录的订单记录!") ; } OrderItemSon orderItemSon = list.get(0); userProjectItem.setTotal(orderItemSon.getTotal()); userProjectItem.setActualTotal(orderItemSon.getActualTotal()); userProjectItem.setOriPrice(orderItemSon.getOriPrice()); userProjectItem.setCurPrice(orderItemSon.getCurPrice()); userProjectItem.setBuyNum(orderItemSon.getBuyNum()); userProjectItem.setUsedTotal(totalTimes); userProjectItem.setNotUsedNum(leftTimes); if("cancelled".equals(status)){ userProjectItem.setUsedNum(0); }else { userProjectItem.setUsedNum(totalTimes - leftTimes); } userProjectItem.setShopId(shopId); userProjectItem.setShopName(shopName); userProjectItem.setDepartmentCode(ordersTotal.getDepartmentCode()); userProjectItem.setDepartmentName(ordersTotal.getDepartmentName()); userProjectItem.setTimeLimit(0); userProjectItem.setLimitStartTime(new Date()); calendar.setTime(new Date()); calendar.add(Calendar.YEAR,100); userProjectItem.setLimitEndTime(calendar.getTime()); userProjectItem.setCommonType(OrderSourceConstans.TYPE_RETAIL); userProjectItem.setCommonId(orderItemSon.getId()); userProjectItem.setUsedOne(orderItemSon.getUsedOne()); userProjectItem.setUserProjectId(userProject.getId()); userProjectItem.setUserId(userProject.getUserId()); userProjectItem.setOrderItemId(orderItemSon.getId()); userProjectItem.setItemId(itemId); userProjectItem.setEffectiveStatus(transformationStatus(status)); userProjectItem.setNoDeductionAmount(new BigDecimal(remain)); userProjectItem.setDeductionTotalAmount(new BigDecimal(amount)); userProjectItem.setIsSpa(syncProjectTreeService.projectSpa(p.getProjectTypeId())?1:0); userProjectItem.setOrdersTotalId(ordersTotal.getId()); userProjectItem.setOrdersTotalNumber(ordersTotal.getOrderNo()); userProjectItem.setGoodsId(p.getId()); userProjectItem.setGoodsName(p.getName()); userProjectItem.setSpecs(orderItemSon.getSpecs()); if(StringUtils.noNull(transferId)){ userProjectItem.setIsTransfer(UserProjectItem.YES); } userProjectItemMapper.insert(userProjectItem); } } } public static int transformationStatus(String status){ switch (status) { case "cancelled": return UserProjectConstants.EFF_STATUS_CANCEL; case "completion": return UserProjectConstants.EFF_STATUS_DONE; case "overTime": return UserProjectConstants.EFF_STATUS_EXPIRE; default: return UserProjectConstants.EFF_STATUS_YES; // cannot be } } /** * 处理同步已执行记录 * @param obj */ @Override public void handleExecutionRecord(JSONObject obj) { if (obj != null) { SqlSentence sqlSentence = new SqlSentence(); Map map = new HashMap<>(); sqlSentence.setM(map); String number= obj.optString("number");//编号 String id = obj.optString("id");//唯一ID String amount = obj.optString("amount");//划扣金额 sqlSentence.setSqlSentence("SELECT * FROM deduction_single WHERE hisId = #{m.hisId}"); map.put("hisId", id); // 处理划扣项目清单 DeductionSingle deductionSingle=deductionSingleMapper.selectOne(sqlSentence); DeductionProject deductionProject=null; if(deductionSingle==null){ deductionSingle=new DeductionSingle(); if(!StringUtils.isEmpty(obj.optString("executedDate"))) { deductionSingle.setCreateTime(new Date(Long.parseLong(obj.optString("executedDate"))));// 开单时间 } deductionSingle.setHisId(obj.optString("id"));//hisid String itemId = obj.optString("itemId");//可执行条目ID sqlSentence.setSqlSentence("SELECT up.* FROM user_project up INNER JOIN user_project_item upi on up.id=upi.userProjectId WHERE up.isDel = 0 and upi.itemId = #{m.itemId} "); map.put("itemId", itemId); UserProject userProject =userProjectMapper.selectOne(sqlSentence); if(userProject==null){ logger.info("请先同步可执行条目信息,执行条目信息apild:" + itemId); //可能是同步可执行条目信息的时候,门店his查不到 throw new RuntimeException("请先同步可执行条目信息!"); } deductionSingle.setUserId(userProject.getUserId()); deductionSingle.setSourceCode(id); deductionSingle.setSourceName(id); deductionSingle.setStatus(DeductionSingleConstants.STATUS_DONE_EXECUTE); deductionSingle.setRemarkInfo(obj.optString("note")); deductionSingle.setType(DeductionSingleConstants.TYPE_SELF_ADD); JSONObject clinic= obj.optJSONObject("clinic");//执行门店 if(clinic!=null){ Shop shop = shopMapper.selectOneByApiId(clinic.optString("id")); if(shop==null){ deductionSingle.setShopId("找不到对应的操作门店了:"+clinic.optString("id")); deductionSingle.setShopName(clinic.optString("name")); }else { deductionSingle.setShopId(shop.getId()); deductionSingle.setShopName(shop.getName()); } } deductionSingle.setIsDel(obj.optBoolean("cancelled")?1:0); if(!StringUtils.isEmpty(obj.optString("cancelledDate")) && !"null".equals(obj.optString("cancelledDate"))) { deductionSingle.setCancelTime(new Date(Long.parseLong(obj.optString("cancelledDate"))));// 作废时间 } JSONObject creator= obj.optJSONObject("creator");//创建人 if(creator!=null){ Employee employee = employeeMapper.selectByApiId(creator.optString("id")); if(employee==null){ deductionSingle.setOperatorId("找不到对应的创建人了:"+creator.optString("id")); deductionSingle.setOperatorName(creator.optString("name")); }else { deductionSingle.setOperatorId(employee.getId()); deductionSingle.setOperatorName(employee.getCnName()); } } String recordNo; if(StringUtils.isEmpty(number) || "null".equals(number)){ recordNo = CreateNo.createTimeNo(systemParameterMapper, "SYNCD", 8, "yyyyMMdd"); }else { recordNo = "SYNCD"+number; } deductionSingle.setRecordNo(recordNo); deductionSingleMapper.insert(deductionSingle); // 校验项目信息是否存在 Project project =projectMapper.selectOneByKey(userProject.getGoodsId()); if(project == null){ logger.info("未找到你要执行项目的项目信息!项目id:" + userProject.getGoodsId()); throw new RuntimeException("未找到你要执行项目的项目信息!"); // throw new ServiceException("未找到你要执行项目的项目信息!"); } // 处理划扣项目 deductionProject=new DeductionProject(); deductionProject.setCreateTime(deductionSingle.getCreateTime()); Integer quantity = obj.optInt("quantity");//执行数量 deductionProject.setNum(quantity); deductionProject.setDeductionSingleId(deductionSingle.getId()); deductionProject.setUserProjectId(userProject.getId()); deductionProject.setProjectId(userProject.getGoodsId()); deductionProject.setProjectNo(userProject.getGoodsNo()); deductionProject.setProjectName(project.getName()); deductionProject.setPrice(project.getPrice()); deductionProject.setDeductionAmount(new BigDecimal(amount)); sqlSentence.setSqlSentence("SELECT * FROM user_project_item WHERE itemId = #{m.itemId} and isDel = 0 "); map.put("itemId", itemId); UserProjectItem userProjectItem = userProjectItemMapper.selectOne(sqlSentence); if(userProjectItem == null){ logger.info("未找到当前用户的划扣项目详细信息!执行条目id:" + itemId); return; // throw new ServiceException("未找到你的项目详细信息!"); } deductionProject.setUserProjectId(userProjectItem.getUserProjectId()); deductionProject.setUserProjectItemId(userProjectItem.getId()); deductionProject.setShopId(deductionSingle.getShopId()); deductionProject.setShopName(deductionSingle.getShopName()); JSONObject medicalDepartment = obj.optJSONObject("medicalDepartment");//医疗科室 if(medicalDepartment==null){ deductionProject.setDepartmentCode(null); deductionProject.setDepartmentName(null); }else { // 科室编号为空默认科室 sqlSentence.setSqlSentence("SELECT * FROM families_room WHERE hisId = #{m.hisId} and isDel = 0 "); map.put("hisId", medicalDepartment.optString("id")); FamiliesRoom familiesRoom= familiesRoomMapper.selectOne(sqlSentence); if(familiesRoom==null){ deductionProject.setDepartmentCode("找不到科室了"); deductionProject.setDepartmentName("找不到科室了"); }else { deductionProject.setDepartmentCode(familiesRoom.getFamiliesRoomCode()); deductionProject.setDepartmentName(familiesRoom.getFamiliesRoomName()); } } deductionProject.setSpecification(project.getSpecification()); deductionProject.setUseDuration(project.getUseDuration()); deductionProject.setPalsyDuration(project.getPalsyDuration()); deductionProjectMapper.insert(deductionProject); //2022-11-30 因为设计到金博的仓库和耗材,所以只有在新增的适合才处理参与人和耗用品 //处理参与人信息 handleDeductionJoin(obj,deductionProject,deductionSingle); //处理耗用品 handleDeductionDrugs(obj,deductionProject,deductionSingle); //处理划扣设备参数 handleDeductionDeviceParameter(obj,deductionProject,deductionSingle); }/*else { JSONObject clinic= obj.optJSONObject("clinic");//执行门店 if(clinic!=null){ Shop shop = shopMapper.selectOneByApiId(clinic.optString("id")); if(shop==null){ deductionSingle.setShopId("找不到对应的操作门店了:"+clinic.optString("id")); deductionSingle.setShopName(clinic.optString("name")); }else { deductionSingle.setShopId(shop.getId()); deductionSingle.setShopName(shop.getName()); } } deductionSingle.setIsDel(obj.optBoolean("cancelled")?1:0); if(!StringUtils.isEmpty(obj.optString("cancelledDate")) && !"null".equals(obj.optString("cancelledDate"))) { deductionSingle.setCancelTime(new Date(Long.parseLong(obj.optString("cancelledDate"))));// 作废时间 } JSONObject creator= obj.optJSONObject("creator");//创建人 if(creator!=null){ Employee employee = employeeMapper.selectByApiId(creator.optString("id")); if(employee==null){ deductionSingle.setOperatorId("找不到对应的创建人了:"+creator.optString("id")); deductionSingle.setOperatorName(clinic.optString("name")); }else { deductionSingle.setOperatorId(employee.getId()); deductionSingle.setOperatorName(employee.getCnName()); } } deductionSingleMapper.updateAll(deductionSingle); sqlSentence.setSqlSentence("SELECT * FROM deduction_project WHERE deductionSingleId = #{m.deductionSingleId} and isDel = 0 "); map.put("deductionSingleId", deductionSingle.getId()); deductionProject=deductionProjectMapper.selectOne(sqlSentence); if(deductionProject==null){ return; } if(!StringUtils.isEmpty(obj.optString("executedDate"))) { deductionProject.setCreateTime(new Date(Long.parseLong(obj.optString("executedDate"))));// 开单时间 } JSONObject medicalDepartment = obj.optJSONObject("medicalDepartment");//医疗科室 deductionProject.setDeductionAmount(new BigDecimal(amount)); if(medicalDepartment==null){ deductionProject.setDepartmentCode(null); deductionProject.setDepartmentName(null); }else { // 科室编号为空默认科室 sqlSentence.setSqlSentence("SELECT * FROM families_room WHERE hisId = #{m.hisId} and isDel = 0 "); map.put("hisId", medicalDepartment.optString("id")); FamiliesRoom familiesRoom= familiesRoomMapper.selectOne(sqlSentence); if(familiesRoom==null){ deductionProject.setDepartmentCode("找不到科室了"); deductionProject.setDepartmentName("找不到科室了"); }else { deductionProject.setDepartmentCode(familiesRoom.getFamiliesRoomCode()); deductionProject.setDepartmentName(familiesRoom.getFamiliesRoomName()); } } deductionProjectMapper.updateAll(deductionProject); }*/ } } /** * 处理同步已执行记录,划扣金额保存 * @param obj */ @Override public void handleExecutionRecordAmount(JSONObject obj) { if (obj == null) { return; } SqlSentence sqlSentence = new SqlSentence(); Map map = new HashMap<>(); sqlSentence.setM(map); String id = obj.optString("id");//唯一ID sqlSentence.setSqlSentence("SELECT * FROM deduction_single WHERE hisId = #{m.hisId}"); map.put("hisId", id); // 处理划扣项目清单 DeductionSingle deductionSingle=deductionSingleMapper.selectOne(sqlSentence); //不存在就不处理 if(deductionSingle==null){ return; } String amount = obj.optString("amount");//划扣金额 String number= obj.optString("number");//编号 map.put("deductionSingleId",deductionSingle.getId()); map.put("deductionAmount",amount==null?"0":amount); map.put("status",obj.optBoolean("cancelled")?DeductionSingleConstants.STATUS_RESCINDED:DeductionSingleConstants.STATUS_DONE_EXECUTE); sqlSentence.sqlUpdate("deductionAmount = #{m.deductionAmount},isDel = 0" + " WHERE deductionSingleId = #{m.deductionSingleId}",map); if(deductionProjectMapper.updateWhere(sqlSentence)!=1){ logger.info("同步划扣金额失败1:{}",obj); throw new TipsException("同步划扣金额失败1"); } sqlSentence.sqlUpdate("status = #{m.status},isDel = 0" + " WHERE id = #{m.deductionSingleId}",map); if(deductionSingleMapper.updateWhere(sqlSentence)!=1){ logger.info("同步划扣金额失败2:{}",obj); throw new TipsException("同步划扣金额失败2"); } } //处理参与人信息 public void handleDeductionJoin(JSONObject obj,DeductionProject deductionProject,DeductionSingle deductionSingle){ //处理参与人信息 SqlSentence sqlSentence = new SqlSentence(); Map map = new HashMap<>(); sqlSentence.setM(map); sqlSentence.setSqlSentence("SELECT * FROM deduction_join WHERE deductionSingleId = #{m.deductionSingleId} and deductionProjectId = #{m.deductionProjectId} and isDel = 0 "); map.put("deductionProjectId",deductionProject.getId()); map.put("deductionSingleId",deductionSingle.getId()); List list=deductionJoinMapper.selectList(sqlSentence); if(list !=null && list.size()>1){ sqlSentence.setSqlSentence("deductionSingleId = #{m.deductionSingleId} and deductionProjectId = #{m.deductionProjectId} "); deductionJoinMapper.deleteWhere(sqlSentence); } JSONArray participants = obj.optJSONArray("participants"); DeductionJoin deductionJoin=null; JSONObject json=null; JSONObject role=null; Employee employee = null; EmployeeRole employeeRole=null; sqlSentence.setSqlSentence("SELECT er.* FROM employee_role er\n" + "INNER JOIN employee_role_type ert ON er.roleTypeId = ert.id\n" + "WHERE er.employeeId = #{m.employeeId} AND er.shopId = #{m.shopId} AND er.isDel = 0 AND er.roleUniqueStr=#{m.roleUniqueStr}"); map.put("shopId", deductionProject.getShopId()); if (participants != null && participants.size() > 0) { for (int i = 0; i < participants.size(); i++) { json= participants.optJSONObject(i); employee = employeeMapper.selectByApiId(json.optString("id")); deductionJoin=new DeductionJoin(); deductionJoin.setCreateTime(deductionSingle.getCreateTime()); if(employee==null){ //throw new ServiceException("未找到员工信息!员工名称:"+json.optString("name")); deductionJoin.setRoleId("未找到员工信息"); deductionJoin.setRoleName("未找到员工信息"); deductionJoin.setEmployeeId("未找到员工信息"); deductionJoin.setEmployeeName(json.optString("name")); deductionJoin.setDeductionSingleId(deductionSingle.getId()); deductionJoin.setDeductionProjectId(deductionProject.getId()); deductionJoin.setUseDuration(json.optInt("durationInMin")); }else { role=json.optJSONObject("role"); map.put("employeeId", employee.getId()); map.put("roleUniqueStr", handleRoleType(role.toString())); List employeeRoleList=employeeRoleMapper.selectList(sqlSentence); if(employeeRoleList==null || employeeRoleList.size()==0){ employeeRole=null; }else { employeeRole=employeeRoleList.get(0); } if(employeeRole==null){ //throw new ServiceException("未找到员工信息!员工名称:"+json.optString("name")); deductionJoin.setRoleId("未找到员工角色信息"); deductionJoin.setRoleName(role.optString("name")); deductionJoin.setEmployeeId(employee.getId()); deductionJoin.setEmployeeName(employee.getCnName()); deductionJoin.setDeductionSingleId(deductionSingle.getId()); deductionJoin.setDeductionProjectId(deductionProject.getId()); deductionJoin.setUseDuration(json.optInt("durationInMin")); }else { deductionJoin.setRoleId(employeeRole.getId()); deductionJoin.setRoleName(role.optString("name")); deductionJoin.setEmployeeId(employee.getId()); deductionJoin.setEmployeeName(employee.getCnName()); deductionJoin.setDeductionSingleId(deductionSingle.getId()); deductionJoin.setDeductionProjectId(deductionProject.getId()); deductionJoin.setUseDuration(json.optInt("durationInMin")); } } deductionJoinMapper.insert(deductionJoin); } } } //处理耗用品 public void handleDeductionDrugs(JSONObject obj,DeductionProject deductionProject,DeductionSingle deductionSingle){ //处理耗用品 SqlSentence sqlSentence = new SqlSentence(); Map map = new HashMap<>(); sqlSentence.setM(map); sqlSentence.setSqlSentence("SELECT * FROM deduction_drugs WHERE deductionSingleId = #{m.deductionSingleId} and deductionProjectId = #{m.deductionProjectId} and isDel = 0 "); map.put("deductionProjectId",deductionProject.getId()); map.put("deductionSingleId",deductionSingle.getId()); List list=deductionDrugsMapper.selectList(sqlSentence); if(list !=null && list.size()>0){ sqlSentence.setSqlSentence("deductionSingleId = #{m.deductionSingleId} and deductionProjectId = #{m.deductionProjectId} "); deductionDrugsMapper.deleteWhere(sqlSentence); } JSONArray costItems = obj.optJSONArray("costItems"); JSONObject costItem=null; DeductionDrugs deductionDrugs=null; List shopWarehouseList=null; ShopWarehouse shopWarehouse=null; List consumablesList=null; Consumables consumables=null; List ShopWarehouseItemList=null; if (costItems != null && costItems.size() > 0) { for (int i = 0; i < costItems.size(); i++) { costItem= costItems.optJSONObject(i); deductionDrugs=new DeductionDrugs(); deductionDrugs.setCreateTime(deductionSingle.getCreateTime()); deductionDrugs.setDrugsNo(costItem.optString("batchNo")); deductionDrugs.setDrugsName(costItem.optString("name")); deductionDrugs.setSpecs(costItem.optString("spec")); deductionDrugs.setNum(costItem.optInt("quantity")); deductionDrugs.setUnitName(costItem.optString("unit")); sqlSentence.setSqlSentence("SELECT * FROM shop_warehouse WHERE hisId = #{m.hisId} "); map.put("hisId", costItem.optJSONObject("warehouse").optString("id")); shopWarehouseList=shopWarehouseMapper.selectList(sqlSentence); if(shopWarehouseList==null || shopWarehouseList.size()==0){ shopWarehouse=null; }else { shopWarehouse=shopWarehouseList.get(0); } if(shopWarehouse==null){ deductionDrugs.setWarehouseId("HIS-"+costItem.optJSONObject("warehouse").optString("id")); }else { deductionDrugs.setWarehouseId(shopWarehouse.getId()); } deductionDrugs.setBatchNo(costItem.optString("batchNo")); sqlSentence.setSqlSentence("SELECT * FROM consumables WHERE hisId = #{m.hisId}"); map.put("hisId", costItem.optString("id")); consumablesList=consumablesMapper.selectList(sqlSentence); if(consumablesList==null || consumablesList.size()==0){ consumables=null; }else { consumables=consumablesList.get(0); } if(consumables==null){ deductionDrugs.setConsumablesId("HIS-"+costItem.optString("id")); }else { deductionDrugs.setConsumablesId(consumables.getId()); } if(shopWarehouse==null || consumables==null){ deductionDrugs.setPrice(new BigDecimal(0)); }else { sqlSentence.setSqlSentence("SELECT * FROM shop_warehouse_item WHERE shopWarehouseId = #{m.shopWarehouseId} and goodId = #{m.goodId} and batchNo = #{m.batchNo} and isDel = 0 "); map.put("shopWarehouseId", shopWarehouse.getId()); map.put("goodId", consumables.getId()); map.put("batchNo", costItem.optString("batchNo")); ShopWarehouseItemList= shopWarehouseItemMapper.selectList(sqlSentence); if (ShopWarehouseItemList==null || ShopWarehouseItemList.size()==0){ deductionDrugs.setPrice(new BigDecimal(0)); }else { deductionDrugs.setPrice(ShopWarehouseItemList.get(0).getPrice()); deductionDrugs.setExpiryDate(ShopWarehouseItemList.get(0).getExpiryDate()); } } deductionDrugs.setDeductionProjectId(deductionProject.getId()); deductionDrugs.setDeductionSingleId(deductionSingle.getId()); deductionDrugsMapper.insert(deductionDrugs); } } } //处理划扣设备参数 public void handleDeductionDeviceParameter(JSONObject obj,DeductionProject deductionProject,DeductionSingle deductionSingle){ //处理划扣设备参数 SqlSentence sqlSentence = new SqlSentence(); Map map = new HashMap<>(); sqlSentence.setM(map); sqlSentence.setSqlSentence("SELECT * FROM deduction_device_parameter WHERE deductionSingleId = #{m.deductionSingleId} and deductionProjectId = #{m.deductionProjectId} and isDel = 0 "); map.put("deductionProjectId",deductionProject.getId()); map.put("deductionSingleId",deductionSingle.getId()); List list=deductionDeviceParameterMapper.selectList(sqlSentence); if(list !=null && list.size()>0){ sqlSentence.setSqlSentence("deductionSingleId = #{m.deductionSingleId} and deductionProjectId = #{m.deductionProjectId} "); deductionDeviceParameterMapper.deleteWhere(sqlSentence); } JSONArray devices = obj.optJSONArray("devices"); if (devices==null || devices.size()==0){ return; } JSONObject deviceJson; DeductionDeviceParameter deductionDeviceParameter; Device device; for (Object o : devices) { deviceJson= (JSONObject) o; deductionDeviceParameter=new DeductionDeviceParameter(); deductionDeviceParameter.setCreateTime(deductionSingle.getCreateTime()); deductionDeviceParameter.setDeviceName(deviceJson.optJSONObject("device").optString("name")); deductionDeviceParameter.setParameterName(deviceJson.optString("parameterName")); deductionDeviceParameter.setParameterUntil(deviceJson.optString("unit")); deductionDeviceParameter.setReference(deviceJson.optString("reference")); deductionDeviceParameter.setOptionalValue(deviceJson.optString("actual")); sqlSentence.setSqlSentence("SELECT * FROM device WHERE hisId = #{m.hisId} LIMIT 1 "); map.put("hisId",deviceJson.optJSONObject("device").optString("_id")); device=deviceMapper.selectOne(sqlSentence); if(device==null){ device=new Device(); device.setName(deviceJson.optJSONObject("device").optString("name")); device.setHisId(deviceJson.optJSONObject("device").optString("_id")); device.setIsDel(BaseEntity.YES);//伪造的数据全部逻辑删除 deviceMapper.insert(device); } deductionDeviceParameter.setDeviceId(device.getId()); sqlSentence.setSqlSentence("SELECT * FROM device_parameter WHERE deviceId = #{m.deviceId} and parameterName=#{m.parameterName}"); map.put("parameterName",deviceJson.optString("parameterName")); map.put("deviceId",device.getId()); DeviceParameter deviceParameter=deviceParameterMapper.selectOne(sqlSentence); if(deviceParameter==null){ deviceParameter=new DeviceParameter(); deviceParameter.setDeviceId(device.getId()); deviceParameter.setParameterName(deviceJson.optString("parameterName")); deviceParameter.setParameterUntil(deviceJson.optString("unit")); deviceParameter.setReference(deviceJson.optString("reference")); deviceParameter.setOptionalValue("同步默认"); deviceParameter.setIsDel(BaseEntity.YES);//伪造的数据全部逻辑删除 deviceParameterMapper.insert(deviceParameter); } deductionDeviceParameter.setDeviceParameterId(deviceParameter.getId()); deductionDeviceParameter.setDeductionSingleId(deductionSingle.getId()); deductionDeviceParameter.setDeductionProjectId(deductionProject.getId()); deductionDeviceParameterMapper.insert(deductionDeviceParameter); } } public String handleRoleType(String roleName){ //顾问 if (roleName.indexOf("咨询主管") != -1 || roleName.indexOf("咨询师") != -1 ) { return RoleType.UNIQUE_STR_ADVISER_LEADER; } //顾问助理 if (roleName.indexOf("咨询助理") != -1) { return RoleType.UNIQUE_STR_ADVISER_ASSISTANT; } //TMK顾问 if (roleName.indexOf("电网咨询师") != -1 || roleName.indexOf("客服") != -1) { return RoleType.UNIQUE_STR_TMK_ADVISER; } //医生 if (roleName.indexOf("医生") != -1 || roleName.indexOf("兼职医生权限") != -1) { return RoleType.UNIQUE_STR_DOCTOR; } //医生助理 if (roleName.indexOf("医助") != -1) { return RoleType.UNIQUE_STR_DOCTOR_ASSISTANT; } //护士 if (roleName.indexOf("护士") != -1 || roleName.indexOf("护士长") != -1 || roleName.indexOf("美容师") != -1) { return RoleType.UNIQUE_STR_NURSE; } //护士助理 if (roleName.indexOf("助理护士长") != -1) { return RoleType.UNIQUE_STR_NURSE_ASSISTANT; } //前台 if (roleName.indexOf("前台") != -1) { return RoleType.UNIQUE_STR_RECEPTION; } //店长 if (roleName.indexOf("店长") != -1) { return RoleType.UNIQUE_STR_SHOPOWNER; } return RoleType.UNIQUE_STR_MIC; } /** * 转换同步可执行记录 * @param obj */ @Override public void conversionExecutableRecord(JSONObject obj,String hisOrderItemId) { if (obj != null) { SqlSentence sqlSentence = new SqlSentence(); Map map = new HashMap<>(); sqlSentence.setM(map); String itemId = obj.optString("id");//执行条目id sqlSentence.setSqlSentence("select * from user_project_item where itemId = #{m.itemId} LIMIT 1 "); map.put("itemId", itemId); UserProjectItem userProjectItem = userProjectItemMapper.selectOne(sqlSentence); Project p = null; User user =null; UserProject userProject =null; UserProject userProjectCopy =null; OrdersTotal ordersTotal=null; String orderId = obj.optString("orderId");//订单号 JSONObject item = obj.optJSONObject("item");//条目信息 JSONObject skuInfo = obj.optJSONObject("skuInfo");//订单行SKU String amount = obj.optString("amount");//执行总金额 String remain = obj.optString("remain");//未执行划扣金额 sqlSentence.setSqlSentence("select * from orders_total where hisOrderId = #{m.hisOrderId} LIMIT 1"); map.put("hisOrderId", orderId); ordersTotal = ordersTotalMapper.selectOne(sqlSentence); String shopId=""; String shopName=""; boolean isTransfer=false; if (ordersTotal == null) { String transferId=obj.optString("transferId");//可能这条划扣记录是转增过来的,根据转增id查到对应的id if (StringUtils.isEmpty(transferId)){ // logger.error("订单找不到对应的,转增id也为空,执行条目id:{}", itemId); throw new RuntimeException("订单找不到对应的,转增id也为空,执行条目id:"+itemId); } isTransfer=true; Query query = new Query(); Criteria criteria=new Criteria(); criteria.and("_id").is(transferId); query.addCriteria(criteria); BasicDBObject basicDBObject = mongoTemplate.findOne(query,BasicDBObject.class,"his-transfer"); if(basicDBObject==null) { // logger.error("找不到对应的转增id,转增id:{},执行条目id:{}",transferId, itemId); throw new RuntimeException("找不到对应的转增id,转增id:"+transferId+",执行条目id:"+itemId); } map.put("hisOrderId", basicDBObject.getString("orderId")); ordersTotal = ordersTotalMapper.selectOne(sqlSentence); if (ordersTotal == null) { // logger.error("通过转增id查询的订单在phis找不到,转增id:{},执行条目id:{}",transferId, itemId); throw new RuntimeException("通过转增id查询的订单在phis找不到,转增id:"+transferId+",执行条目id:"+itemId); } JSONObject receiverOrganization=JSONObject.fromObject(basicDBObject.get("receiverOrganization")); Shop shop = shopMapper.selectOneByApiId(receiverOrganization.optString("_id")); if(shop==null){ shopId=ordersTotal.getShopId(); shopName=ordersTotal.getShopName(); }else { shopId=shop.getId(); shopName=shop.getName(); } }else { shopId=ordersTotal.getShopId(); shopName=ordersTotal.getShopName(); } //2022-12-06 荣爷说未执行门店已领建数据为主 JSONObject clinic= obj.optJSONObject("clinic");//执行门店 if(clinic!=null){ sqlSentence.setSqlSentence("select * from shop where apiId = #{m.apiId} LIMIT 1"); map.put("apiId", clinic.optString("id")); Shop shop = shopMapper.selectOne(sqlSentence); if(shop==null){ shop=new Shop(); shop.setCode(shopCode()); shop.setName(clinic.optString("name")); shop.setApiId(clinic.optString("id")); shopMapper.insert(shop); } shopId=shop.getId(); shopName=shop.getName(); } if (item != null) { sqlSentence.setSqlSentence("select * from project WHERE apiId = #{m.apiId} LIMIT 1"); map.put("apiId", item.optString("id")); p = projectMapper.selectOne(sqlSentence); if (p == null) { p=new Project(); p.setApiId(item.optString("id")); p.setName(item.optString("name")); p.setIsDel(BaseEntity.YES); p.setRemark("初始化同步未执行项目查不到项目,新增一个"); projectMapper.insert(p); logger.info("新增项目:{},id:{}",item.optString("name"),p.getId()); /* logger.error("请先同步项目,项目apild:" + item.optString("id")); return;*/ } } JSONObject customerJson = obj.optJSONObject("customer");//客户信息 if (customerJson != null) { sqlSentence.setSqlSentence("select * from user WHERE apiId = #{m.apiId} LIMIT 1"); map.put("apiId", customerJson.optString("id")); user = userMapper.selectOne(sqlSentence); if (user == null) { user=new User(); user.setIsDel(BaseEntity.YES); user.setName(customerJson.optString("id")); user.setApiId(customerJson.optString("name")); userMapper.insert(user); /* logger.error("请先同步用户,用户apild:" + customerJson.optString("id")); return;*/ } } int totalTimes = obj.optInt("totalTimes");//可执行总次数 int leftTimes = obj.optInt("leftTimes");//剩余可执行次数 int executedNumber = obj.optInt("executedNumber");//已执行次数 int refunded = obj.optInt("refunded");//退款次数 String status = obj.optString("status");//状态 sqlSentence.setSqlSentence("select * from user_project where goodsId = #{m.goodsId} and userId = #{m.userId} and isDel = 0 LIMIT 1 "); map.put("goodsId", p.getId()); map.put("userId", user.getId()); userProject= userProjectMapper.selectOne(sqlSentence); Calendar calendar=Calendar.getInstance(); if(userProjectItem==null){ //因为pis新版本的开单是会处理未划扣数据,所有发现数据是新的的话,订单appid不是领建就不新增 //荣爷说crm也要处理 2022-12-12 10.05 if(!systemConfig.getApiAppId().equals(ordersTotal.getAppId()) && !isTransfer){ return; } userProjectItem=new UserProjectItem(); // 处理用户拥有的项目子项 String itId = item.optString("id"); String skuInfoId = skuInfo.optString("id"); String type = obj.optString("type");//项目类型 service 项目, promotion 套餐项目, card-service 卡项项目 if(userProject==null){ userProject=new UserProject(); //判断这个用户第一次使用这个项目 userProject.setType(OrderGoodsConstants.TYPE_PROJECT); userProject.setGoodsId(p.getId()); userProject.setGoodsName(p.getName()); userProject.setGoodsNo(p.getCoding()); userProject.setUserId(user.getId()); userProject.setCreateTime(new Date(obj.optLong("createdAt"))); userProject.setUsedTotal(totalTimes); userProject.setNotUsedNum(leftTimes); userProject.setUsedNum(executedNumber); userProject.setCancelNum(refunded); userProject.setOverdueNum(0); userProject.setTransferNum(0); userProject.setCancelNum(0); userProject.setInBygNum(0); userProject.setEffectiveStatus(UserProjectConstants.EFF_STATUS_YES);//待改变 userProjectCopy= userProjectMapper.selectOne(sqlSentence); if(userProjectCopy==null){ userProject.setIsSpa(syncProjectTreeService.projectSpa(p.getProjectTypeId())?1:0); userProjectMapper.insert(userProject); }else { userProjectCopy.setUsedTotal(userProjectCopy.getUsedTotal()+totalTimes); userProjectCopy.setNotUsedNum(userProjectCopy.getNotUsedNum()+leftTimes); userProjectCopy.setUsedNum(userProjectCopy.getUsedNum()+executedNumber); userProjectCopy.setCancelNum(userProjectCopy.getUsedNum()+refunded); userProjectMapper.updateAll(userProjectCopy); } }else { //判断这个用户第n次使用这个项目 userProject.setUsedTotal(userProject.getUsedTotal()+totalTimes); userProject.setNotUsedNum(userProject.getNotUsedNum()+leftTimes); userProject.setUsedNum(userProject.getUsedNum()+executedNumber); userProject.setCancelNum(userProject.getUsedNum()+refunded); userProjectMapper.updateAll(userProject); } if ("service".equals(type)) { //项目类型划扣 sqlSentence.setSqlSentence("select * from order_item where orderId = #{m.orderId} and commonId=#{m.commonId} and isDel = 0 "); map.put("orderId", ordersTotal.getId()); map.put("commonId", p.getId()); List list=orderItemMapper.selectList(sqlSentence); if(list!=null && list.size()>=1) { //普通订单 OrderItem items = list.get(0); userProjectItem.setTotal(items.getTotal()); userProjectItem.setActualTotal(items.getActualTotal()); userProjectItem.setOriPrice(items.getOriPrice()); userProjectItem.setCurPrice(items.getCurPrice()); userProjectItem.setBuyNum(items.getBuyNum()); userProjectItem.setUsedTotal(totalTimes); userProjectItem.setNotUsedNum(leftTimes); userProjectItem.setUsedNum(executedNumber); userProjectItem.setCancelNum(refunded); userProjectItem.setShopId(shopId); userProjectItem.setShopName(shopName); userProjectItem.setDepartmentCode(ordersTotal.getDepartmentCode()); userProjectItem.setDepartmentName(ordersTotal.getDepartmentName()); userProjectItem.setTimeLimit(0); userProjectItem.setLimitStartTime(new Date()); calendar.setTime(new Date()); calendar.add(Calendar.YEAR,100); userProjectItem.setLimitEndTime(calendar.getTime()); userProjectItem.setCommonType(OrderSourceConstans.TYPE_PROJECT); userProjectItem.setCommonId(items.getId()); userProjectItem.setUsedOne(items.getSingle()); userProjectItem.setUserProjectId(userProject.getId()); userProjectItem.setUserId(userProject.getUserId()); userProjectItem.setOrderItemId(items.getId()); userProjectItem.setItemId(itemId); userProjectItem.setEffectiveStatus(transformationStatus(status)); userProjectItem.setNoDeductionAmount(new BigDecimal(remain)); userProjectItem.setDeductionTotalAmount(new BigDecimal(amount)); userProjectItem.setIsSpa(syncProjectTreeService.projectSpa(p.getProjectTypeId())?1:0); userProjectItem.setOrdersTotalId(ordersTotal.getId()); userProjectItem.setOrdersTotalNumber(ordersTotal.getOrderNo()); userProjectItem.setGoodsId(p.getId()); userProjectItem.setGoodsName(p.getName()); userProjectItem.setSpecs(items.getSpecs()); userProjectItem.setCreateTime(new Date(obj.optLong("createdAt"))); userProjectItemMapper.insert(userProjectItem); }else { //普通订单找不到就肯定是phis开的卡包订单 sqlSentence.setSqlSentence(" SELECT ois.* FROM order_item_source ois INNER JOIN order_item oi ON oi.id=ois.orderItemId " + " where ois.orderId = #{m.orderId} and ois.goodsId=#{m.commonId}"); map.put("orderId", ordersTotal.getId()); map.put("commonId", p.getId()); List sonListlist=orderItemSonMapper.selectList(sqlSentence); if(sonListlist==null || sonListlist.size()==0) return; OrderItemSon orderItemSon = sonListlist.get(0); userProjectItem.setTotal(orderItemSon.getTotal()); userProjectItem.setActualTotal(orderItemSon.getActualTotal()); userProjectItem.setOriPrice(orderItemSon.getOriPrice()); userProjectItem.setCurPrice(orderItemSon.getCurPrice()); userProjectItem.setBuyNum(orderItemSon.getBuyNum()); userProjectItem.setUsedTotal(totalTimes); userProjectItem.setNotUsedNum(leftTimes); userProjectItem.setUsedNum(executedNumber); userProjectItem.setCancelNum(refunded); userProjectItem.setShopId(shopId); userProjectItem.setShopName(shopName); userProjectItem.setDepartmentCode(ordersTotal.getDepartmentCode()); userProjectItem.setDepartmentName(ordersTotal.getDepartmentName()); userProjectItem.setTimeLimit(0); userProjectItem.setLimitStartTime(new Date()); calendar.setTime(new Date()); calendar.add(Calendar.YEAR,100); userProjectItem.setLimitEndTime(calendar.getTime()); userProjectItem.setCommonType(OrderSourceConstans.TYPE_RETAIL); userProjectItem.setCommonId(orderItemSon.getId()); userProjectItem.setUsedOne(orderItemSon.getUsedOne()); userProjectItem.setUserProjectId(userProject.getId()); userProjectItem.setUserId(userProject.getUserId()); userProjectItem.setOrderItemId(orderItemSon.getId()); userProjectItem.setItemId(itemId); userProjectItem.setEffectiveStatus(transformationStatus(status)); userProjectItem.setNoDeductionAmount(new BigDecimal(remain)); userProjectItem.setDeductionTotalAmount(new BigDecimal(amount)); userProjectItem.setIsSpa(syncProjectTreeService.projectSpa(p.getProjectTypeId())?1:0); userProjectItem.setOrdersTotalId(ordersTotal.getId()); userProjectItem.setOrdersTotalNumber(ordersTotal.getOrderNo()); userProjectItem.setGoodsId(p.getId()); userProjectItem.setGoodsName(p.getName()); userProjectItem.setSpecs(orderItemSon.getSpecs()); userProjectItem.setCreateTime(new Date(obj.optLong("createdAt"))); userProjectItemMapper.insert(userProjectItem); } }else if("card-service".equals(type)){ //普通订单找不到就肯定是phis开的卡包订单 sqlSentence.setSqlSentence(" SELECT ois.* FROM order_item_source ois INNER JOIN order_item oi ON oi.id=ois.orderItemId " + " where ois.orderId = #{m.orderId} and ois.goodsId=#{m.commonId}"); map.put("orderId", ordersTotal.getId()); map.put("commonId", p.getId()); List sonListlist=orderItemSonMapper.selectList(sqlSentence); if(sonListlist==null || sonListlist.size()==0) return; OrderItemSon orderItemSon = sonListlist.get(0); userProjectItem.setTotal(orderItemSon.getTotal()); userProjectItem.setActualTotal(orderItemSon.getActualTotal()); userProjectItem.setOriPrice(orderItemSon.getOriPrice()); userProjectItem.setCurPrice(orderItemSon.getCurPrice()); userProjectItem.setBuyNum(orderItemSon.getBuyNum()); userProjectItem.setUsedTotal(totalTimes); userProjectItem.setNotUsedNum(leftTimes); userProjectItem.setUsedNum(executedNumber); userProjectItem.setCancelNum(refunded); userProjectItem.setShopId(shopId); userProjectItem.setShopName(shopName); userProjectItem.setDepartmentCode(ordersTotal.getDepartmentCode()); userProjectItem.setDepartmentName(ordersTotal.getDepartmentName()); userProjectItem.setTimeLimit(0); userProjectItem.setLimitStartTime(new Date()); calendar.setTime(new Date()); calendar.add(Calendar.YEAR,100); userProjectItem.setLimitEndTime(calendar.getTime()); userProjectItem.setCommonType(OrderSourceConstans.TYPE_RETAIL); userProjectItem.setCommonId(orderItemSon.getId()); userProjectItem.setUsedOne(orderItemSon.getUsedOne()); userProjectItem.setUserProjectId(userProject.getId()); userProjectItem.setUserId(userProject.getUserId()); userProjectItem.setOrderItemId(orderItemSon.getId()); userProjectItem.setItemId(itemId); userProjectItem.setEffectiveStatus(transformationStatus(status)); userProjectItem.setNoDeductionAmount(new BigDecimal(remain)); userProjectItem.setDeductionTotalAmount(new BigDecimal(amount)); userProjectItem.setIsSpa(syncProjectTreeService.projectSpa(p.getProjectTypeId())?1:0); userProjectItem.setOrdersTotalId(ordersTotal.getId()); userProjectItem.setOrdersTotalNumber(ordersTotal.getOrderNo()); userProjectItem.setGoodsId(p.getId()); userProjectItem.setGoodsName(p.getName()); userProjectItem.setSpecs(orderItemSon.getSpecs()); userProjectItem.setCreateTime(new Date(obj.optLong("createdAt"))); userProjectItemMapper.insert(userProjectItem); }else { //因为pis新版本的开单是会处理未划扣数据,所有发现数据是新的的话,订单appid不是领建就不新增 //荣爷说crm也要处理 2022-12-12 10.05 if(!systemConfig.getApiAppId().equals(ordersTotal.getAppId()) && !isTransfer){ return; } //卡项待商量 sqlSentence.setSqlSentence("select * from promotion where apiId = #{m.skuInfoId} and isDel = 0"); map.put("skuInfoId", skuInfoId); Promotion promotion=promotionMapper.selectOne(sqlSentence); if(promotion==null){ // logger.info("促销都没得玩鸡巴啊itemID:{},促销id:{}",itemId,skuInfoId); throw new RuntimeException("促销都没得itemID:"+itemId+",促销id:"+skuInfoId); } map.put("promotionId", promotion.getId()); sqlSentence.setSqlSentence(" SELECT ois.* FROM order_item_source ois INNER JOIN order_item oi ON oi.id=ois.orderItemId " + " where ois.orderId = #{m.orderId} and ois.goodsId=#{m.commonId} and oi.commonId=#{m.promotionId} "); map.put("orderId", ordersTotal.getId()); map.put("commonId", p.getId()); List list=orderItemSonMapper.selectList(sqlSentence); if(list==null || list.size()==0) return; OrderItemSon orderItemSon = list.get(0); userProjectItem.setTotal(orderItemSon.getTotal()); userProjectItem.setActualTotal(orderItemSon.getActualTotal()); userProjectItem.setOriPrice(orderItemSon.getOriPrice()); userProjectItem.setCurPrice(orderItemSon.getCurPrice()); userProjectItem.setBuyNum(orderItemSon.getBuyNum()); userProjectItem.setUsedTotal(totalTimes); userProjectItem.setNotUsedNum(leftTimes); userProjectItem.setUsedNum(executedNumber); userProjectItem.setCancelNum(refunded); userProjectItem.setShopId(shopId); userProjectItem.setShopName(shopName); userProjectItem.setDepartmentCode(ordersTotal.getDepartmentCode()); userProjectItem.setDepartmentName(ordersTotal.getDepartmentName()); userProjectItem.setTimeLimit(0); userProjectItem.setLimitStartTime(new Date()); calendar.setTime(new Date()); calendar.add(Calendar.YEAR,100); userProjectItem.setLimitEndTime(calendar.getTime()); userProjectItem.setCommonType(OrderSourceConstans.TYPE_RETAIL); userProjectItem.setCommonId(orderItemSon.getId()); userProjectItem.setUsedOne(orderItemSon.getUsedOne()); userProjectItem.setUserProjectId(userProject.getId()); userProjectItem.setUserId(userProject.getUserId()); userProjectItem.setOrderItemId(orderItemSon.getId()); userProjectItem.setItemId(itemId); userProjectItem.setEffectiveStatus(transformationStatus(status)); userProjectItem.setNoDeductionAmount(new BigDecimal(remain)); userProjectItem.setDeductionTotalAmount(new BigDecimal(amount)); userProjectItem.setIsSpa(syncProjectTreeService.projectSpa(p.getProjectTypeId())?1:0); userProjectItem.setOrdersTotalId(ordersTotal.getId()); userProjectItem.setOrdersTotalNumber(ordersTotal.getOrderNo()); userProjectItem.setGoodsId(p.getId()); userProjectItem.setGoodsName(p.getName()); userProjectItem.setSpecs(orderItemSon.getSpecs()); userProjectItem.setCreateTime(new Date(obj.optLong("createdAt"))); userProjectItemMapper.insert(userProjectItem); } //三少说初始化数据新增要保留一条数据 HisOrderItemRecord hisOrderItemRecord=new HisOrderItemRecord(itemId,hisOrderItemId,user.getId(),BaseEntity.YES); syncTransactioalService.insert(HisOrderItemRecordMapper.class,hisOrderItemRecord); }else { //修改订单号 String skuInfoId = skuInfo.optString("id"); String type = obj.optString("type");//项目类型 service 项目, promotion 套餐项目, card-service 卡项项目 if ("service".equals(type)) { //项目类型划扣 sqlSentence.setSqlSentence("select * from order_item where orderId = #{m.orderId} and commonId=#{m.commonId} and isDel = 0 "); map.put("orderId", ordersTotal.getId()); map.put("commonId", p.getId()); List list=orderItemMapper.selectList(sqlSentence); if(list!=null && list.size()>=1) { //普通订单 OrderItem items = list.get(0); userProjectItem.setCommonType(OrderSourceConstans.TYPE_PROJECT); userProjectItem.setCommonId(items.getId()); userProjectItem.setOrderItemId(items.getId()); userProjectItem.setItemId(itemId); userProjectItem.setOrdersTotalId(ordersTotal.getId()); userProjectItem.setOrdersTotalNumber(ordersTotal.getOrderNo()); }else { //普通订单找不到就肯定是phis开的卡包订单 sqlSentence.setSqlSentence(" SELECT ois.* FROM order_item_source ois INNER JOIN order_item oi ON oi.id=ois.orderItemId " + " where ois.orderId = #{m.orderId} and ois.goodsId=#{m.commonId}"); map.put("orderId", ordersTotal.getId()); map.put("commonId", p.getId()); List sonListlist=orderItemSonMapper.selectList(sqlSentence); if(sonListlist==null || sonListlist.size()==0) return; OrderItemSon orderItemSon = sonListlist.get(0); userProjectItem.setCommonType(OrderSourceConstans.TYPE_RETAIL); userProjectItem.setCommonId(orderItemSon.getId()); userProjectItem.setOrderItemId(orderItemSon.getId()); userProjectItem.setOrdersTotalId(ordersTotal.getId()); userProjectItem.setOrdersTotalNumber(ordersTotal.getOrderNo()); } }else if("card-service".equals(type)){ //普通订单找不到就肯定是phis开的卡包订单 sqlSentence.setSqlSentence(" SELECT ois.* FROM order_item_source ois INNER JOIN order_item oi ON oi.id=ois.orderItemId " + " where ois.orderId = #{m.orderId} and ois.goodsId=#{m.commonId}"); map.put("orderId", ordersTotal.getId()); map.put("commonId", p.getId()); List sonListlist=orderItemSonMapper.selectList(sqlSentence); if(sonListlist==null || sonListlist.size()==0) return; OrderItemSon orderItemSon = sonListlist.get(0); userProjectItem.setCommonType(OrderSourceConstans.TYPE_RETAIL); userProjectItem.setCommonId(orderItemSon.getId()); userProjectItem.setOrderItemId(orderItemSon.getId()); userProjectItem.setOrdersTotalId(ordersTotal.getId()); userProjectItem.setOrdersTotalNumber(ordersTotal.getOrderNo()); }else { //卡项待商量 sqlSentence.setSqlSentence("select * from promotion where apiId = #{m.skuInfoId} and isDel = 0"); map.put("skuInfoId", skuInfoId); Promotion promotion=promotionMapper.selectOne(sqlSentence); if(promotion==null){ // logger.info("促销都没得玩鸡巴啊itemID:{},促销id:{}",itemId,skuInfoId); throw new RuntimeException("促销都没得itemID:"+itemId+",促销id:"+skuInfoId); } map.put("promotionId", promotion.getId()); sqlSentence.setSqlSentence(" SELECT ois.* FROM order_item_source ois INNER JOIN order_item oi ON oi.id=ois.orderItemId " + " where ois.orderId = #{m.orderId} and ois.goodsId=#{m.commonId} and oi.commonId=#{m.promotionId} "); map.put("orderId", ordersTotal.getId()); map.put("commonId", p.getId()); List list=orderItemSonMapper.selectList(sqlSentence); if(list==null || list.size()==0) return; OrderItemSon orderItemSon = list.get(0); userProjectItem.setCommonType(OrderSourceConstans.TYPE_RETAIL); userProjectItem.setCommonId(orderItemSon.getId()); userProjectItem.setOrderItemId(orderItemSon.getId()); userProjectItem.setItemId(itemId); userProjectItem.setOrdersTotalId(ordersTotal.getId()); userProjectItem.setOrdersTotalNumber(ordersTotal.getOrderNo()); } map.put("commonType", userProjectItem.getCommonType()); map.put("commonId", userProjectItem.getCommonId()); map.put("orderItemId", userProjectItem.getOrderItemId()); map.put("ordersTotalId", userProjectItem.getOrdersTotalId()); map.put("ordersTotalNumber", userProjectItem.getOrdersTotalNumber()); map.put("id", userProjectItem.getId()); sqlSentence.setSqlSentence(" commonType = #{m.commonType},commonId = #{m.commonId},orderItemId = #{m.orderItemId},ordersTotalId = #{m.ordersTotalId}, " + " ordersTotalNumber = #{m.ordersTotalNumber} WHERE id = #{m.id} "); userProjectItemMapper.updateWhere(sqlSentence); } } } public String shopCode(){ SqlSentence sqlSentence = new SqlSentence(); Map map = new HashMap<>(); sqlSentence.setM(map); sqlSentence.setSqlSentence("select * FROM shop ORDER BY `code` DESC LIMIT 1 "); Shop shop= shopMapper.selectOne(sqlSentence); String code=shop.getCode(); if(StringUtils.isEmpty(code)) { code="000"; } code= String.valueOf(Integer.parseInt(code)+1); if(code.length()<3){ int le= code.length(); for (int i = 0; i < 3 -le; i++) { code="0"+code; } } return code; } @Override public void conversionExecutionRecord(JSONObject obj) { } @Override public void initializationExecutionRecord(String id, String deductionSingleId) { SqlSentence sqlSentence = new SqlSentence(); Map map = new HashMap<>(); sqlSentence.setM(map); sqlSentence.setSqlSentence("SELECT * FROM deduction_single WHERE id = #{m.deductionSingleId}"); map.put("deductionSingleId", deductionSingleId); // 处理划扣项目清单 DeductionSingle deductionSingle=deductionSingleMapper.selectOne(sqlSentence); if(deductionSingle==null)return; String hisId=deductionSingle.getHisId(); if(StringUtils.isEmpty(hisId))return; Query up = new Query(Criteria.where("_id").is(hisId)); Map maps = mongoTemplate.findOne(up, Map.class, "his-execution-record"); if(maps==null || maps.size()==0){ return; } JSONObject item= (JSONObject) JSONArray.fromObject(maps.get("items")).get(0); String itemId=item.optString("itemId"); sqlSentence.setSqlSentence("select * from user_project_item where itemId = #{m.itemId} LIMIT 1 "); map.put("itemId", itemId); UserProjectItem userProjectItem = userProjectItemMapper.selectOne(sqlSentence); if(userProjectItem==null)return; sqlSentence.setSqlSentence(" userProjectItemId = #{m.userProjectItemId} WHERE id = #{m.id}"); map.put("userProjectItemId", userProjectItem.getId()); map.put("id", id); deductionProjectMapper.updateWhere(sqlSentence); } @Override public void updateDeductionAmount(String id, Integer usedTotal, BigDecimal deductionTotalAmount) { SqlSentence sqlSentence = new SqlSentence(); Map map = new HashMap<>(); sqlSentence.setM(map); sqlSentence.setSqlSentence("SELECT ds.recordNo as projectNo,dp.id,dp.num,dp.deductionAmount FROM deduction_project " + " dp INNER JOIN deduction_single ds on ds.id=dp.deductionSingleId WHERE dp.userProjectItemId = #{m.userProjectItemId} ORDER BY ds.createTime "); map.put("userProjectItemId", id); List deductionProjectList=deductionProjectMapper.selectList(sqlSentence); BigDecimal singleAmount=deductionTotalAmount.divide(new BigDecimal(usedTotal),10,BigDecimal.ROUND_HALF_UP);//单个金额 Integer num; BigDecimal deductionAmount; sqlSentence.setSqlSentence(" deductionAmount=#{m.deductionAmount} WHERE id = #{m.id} "); for (DeductionProject deductionProject : deductionProjectList) { num=deductionProject.getNum(); usedTotal=usedTotal-num; if(deductionProject.getProjectNo().startsWith("D")){ if(usedTotal<=0){ deductionAmount=deductionTotalAmount; }else { deductionAmount=singleAmount.multiply(BigDecimal.valueOf(num)).setScale(2,BigDecimal.ROUND_HALF_UP); } }else { deductionAmount=deductionProject.getDeductionAmount(); } deductionTotalAmount=deductionTotalAmount.subtract(deductionAmount); map.put("id", deductionProject.getId()); map.put("deductionAmount", deductionAmount); deductionProjectMapper.updateWhere(sqlSentence); if(usedTotal<=0){ break; } } } }