package com.hx.phip.tool.club; import com.hx.common.service.CommonService; import com.hx.exception.TipsException; import com.hx.mybatisTool.SqlSentence; import com.hx.phiappt.common.OrderItemConstants; import com.hx.phiappt.common.OrderTotalConstants; import com.hx.phiappt.constants.enums.GroupTypeEnum; import com.hx.phiappt.constants.enums.ProjectRelationTypeEnum; import com.hx.phiappt.dao.mapper.ProjectTypeRelationMapper; import com.hx.phiappt.model.cardItem.CardEquity; import com.hx.phiappt.model.cardItem.CardItemInfo; import com.hx.phiappt.model.market.system.MarketSystem; import com.hx.phiappt.model.market.system.MarketSystemProject; 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.project.ProjectTypeRelation; import com.hx.phiappt.model.user.club.UserClubMember; import com.hx.phip.dao.mapper.*; import com.hx.util.DateUtil; import java.util.*; /**用户俱乐部会员 * @author CJH */ public class UserCluebMemberTool { /**订单下单结账校验是否符合会员条件*/ public static void orderChekMember(String orderId,Integer marketType, CommonService commonService){ OrdersTotal ordersTotal = commonService.selectOneByKey(OrdersTotalMapper.class,orderId); if(ordersTotal == null){ throw new TipsException("订单标识错误!"); } //充值单跳过 if(OrderTotalConstants.TYPE_RECHARGE.equals(ordersTotal.getType())){ return; } //未支付跳过 if(ordersTotal.getPayStatus() != OrderTotalConstants.PAY_STATUS_SUC){ return; } //查询活动配置 SqlSentence sqlSentence = new SqlSentence(); Map values = new HashMap<>(); values.put("type",marketType); sqlSentence.sqlSentence("SELECT * FROM market_system WHERE isDel = 0 AND isUp = 1 AND type = #{m.type}",values); MarketSystem marketSystem = commonService.selectOneByKey(MarketSystemMapper.class,sqlSentence); if(marketSystem == null){ return; } //---判断是否在活动时间内 if(marketSystem.getStartTime() != null){ if(ordersTotal.getPayTime().getTime() < marketSystem.getStartTime().getTime()){ return; } } if(marketSystem.getEndTime() != null){ if(ordersTotal.getPayTime().getTime() > marketSystem.getEndTime().getTime()){ return; } } //----获取指定的项目配置 values.clear(); values.put("marketSystemId",marketSystem.getId()); sqlSentence.sqlSentence("SELECT type,typeCode,typeId FROM market_system_project WHERE isDel = 0 AND marketSystemId = #{m.marketSystemId}",values); List marketSystemProjectList = commonService.selectList(MarketSystemProjectMapper.class,sqlSentence); //指定活动项目编号匹配存储 Set skuNoSet = new HashSet<>(); StringBuilder itemNameSql = new StringBuilder(); for(MarketSystemProject marketSystemProject:marketSystemProjectList){ if(marketSystemProject.getType() == MarketSystemProject.TYPE_PROJECT_MAPPER){ if(itemNameSql.length() > 0){ itemNameSql.append(","); } itemNameSql.append("'").append(marketSystemProject.getTypeCode()).append("'"); }else{ skuNoSet.add(marketSystemProject.getTypeCode()); } } if(itemNameSql.length() > 0){ values.clear(); values.put("moduleType", ProjectRelationTypeEnum.MODULE_TYPE_ZS_PROJECT.getCode()); values.put("itemNameList",itemNameSql.toString()); sqlSentence.sqlSentence("SELECT skuNo FROM project_type_relation WHERE isDel = 0 AND moduleType = 7 AND itemName IN(${m.itemNameList})",values); List projectTypeRelationList = commonService.selectList(ProjectTypeRelationMapper.class,sqlSentence); for(ProjectTypeRelation projectTypeRelation:projectTypeRelationList){ skuNoSet.add(projectTypeRelation.getSkuNo()); } } //---获取购买的子单 values.clear(); values.put("orderId",ordersTotal.getId()); sqlSentence.sqlSentence("SELECT id,type,commonId,goodsNo,goodsName,usedTotal,hasReNum FROM order_item WHERE isDel = 0 AND orderId = #{m.orderId}",values); List orderItemList = commonService.selectList(OrderItemMapper.class,sqlSentence); //已经算了疗程数的项目编号 Set mateSkuNoSet = new HashSet<>(); //总疗程数,所有的项目疗程数总和 int cureNum = 0; //存储卡项所有的子项,k值:权益标识 Map> cardItemInfoMapList; List orderItemSonList; List cardEquityList; List cardItemInfoList; int cureItemMax; int cureItemNum; int deductionSurp; int deductionNum; for(OrderItem orderItem:orderItemList){ if(OrderItemConstants.TYPE_PROJECT.equals(orderItem.getType())){ //项目,没有包含 if(skuNoSet.contains(orderItem.getGoodsNo())){ //疗程次数叠加,退款的也需要减去 cureNum = cureNum+orderItem.getUsedTotal()-orderItem.getHasReNum(); } }else if(OrderItemConstants.TYPE_PROMOTION.equals(orderItem.getType())){ //促销 orderItemSonList = getOrderItemSonList(orderItem.getId(),OrderItemConstants.TYPE_PROJECT,commonService); for(OrderItemSon orderItemSon:orderItemSonList){ if(skuNoSet.contains(orderItemSon.getGoodsNo())){ //疗程次数叠加,退款的也需要减去 cureNum = cureNum+orderItem.getUsedTotal()-orderItem.getHasReNum(); } } }else if(OrderItemConstants.TYPE_CARD.equals(orderItem.getType())){ //卡项,注意,不是卡包 //TODO 退款没有减去 cardEquityList = getCardEquityList(orderItem.getCommonId(),commonService); cardItemInfoMapList = getCardItemInfoList(orderItem.getCommonId(),commonService); for(CardEquity cardEquity:cardEquityList){ cardItemInfoList = cardItemInfoMapList.get(cardEquity.getId()); if(cardItemInfoList==null || cardItemInfoList.size()==0){ continue; } //判断权益类型 if(cardEquity.getEquityType().equals(CardEquity.EQUITY_TYPE_PROJECT)){ for(CardItemInfo cardItemInfo:cardItemInfoList){ //固定项目类型 if(cardItemInfo.getUserNum() != null){ cureNum = cureNum+cardItemInfo.getUserNum()*cardItemInfo.getMaxNum()*orderItem.getBuyNum(); } } }else if(cardEquity.getEquityType().equals(CardEquity.EQUITY_TYPE_TIME_CARD)){ //TODO 这里还没有做完 //只存储最大的一个疗程数 cureItemMax = 0; //剩余抵扣次数 deductionSurp = cardEquity.getValue(); //次卡 for(CardItemInfo cardItemInfo:cardItemInfoList){ if(cardItemInfo.getUserNum() != null){ deductionNum = cardEquity.getValue()/cardItemInfo.getEveryDrawNum(); cureItemNum = cardItemInfo.getUserNum()*deductionNum*orderItem.getBuyNum(); if(cureItemNum>cureItemMax){ cureItemMax = cureItemMax; } } } } } } } //判断是否符合规则 if(cureNum >= marketSystem.getCureNum()){ //生成俱乐部会员 createClubMember(UserClubMember.TYPE_REGENERATION,ordersTotal,marketSystem,commonService); } } /**获取二级子订单*/ public static List getOrderItemSonList(String orderItemId,String goodsType,CommonService commonService){ SqlSentence sqlSentence = new SqlSentence(); Map values = new HashMap<>(); values.put("orderItemId",orderItemId); values.put("goodsType",goodsType); sqlSentence.sqlSentence("SELECT id,type,goodsId,goodsNo,goodsName,usedTotal,hasReNum FROM order_item_source WHERE isDel = 0 AND orderItemId = #{m.orderItemId} AND type = #{m.type}",values); return commonService.selectList(OrderItemSonMapper.class,sqlSentence); } /**获取所有的权益*/ public static List getCardEquityList(String cardItemId, CommonService commonService){ SqlSentence sqlSentence = new SqlSentence(); Map values = new HashMap<>(); values.put("cardItemId",cardItemId); sqlSentence.sqlSentence("SELECT cii.id,cii.equityType,cii.value,cii.shareMoney" + " FROM card_equity cii WHERE cii.isDel = 0 AND cii.cardItemId = #{m.cardItemId}",values); return commonService.selectList(CardEquityMapper.class,sqlSentence); } /**获取卡项所有的项目*/ public static Map> getCardItemInfoList(String cardItemId, CommonService commonService){ SqlSentence sqlSentence = new SqlSentence(); Map values = new HashMap<>(); values.put("cardItemId",cardItemId); sqlSentence.sqlSentence("SELECT cii.id,cii.cardEquityId,cii.commonCode,cii.commonId,cii.commonName,cii.commonType,cii.maxNum,cii.everyDrawNum,p.treatmentItemsNum AS userNum" + " LEFT JOIN project p ON p.id = cii.commonId AND p.isExecutable = 1" + " FROM card_item_info cii WHERE cii.isDel = 0 AND cii.cardItemId = #{m.cardItemId} AND cii.commonType IN('project')",values); List cardItemInfoList = commonService.selectList(CardItemInfoMapper.class,sqlSentence); Map> cardItemInfoMapList = new HashMap<>(); List cardItemInfoList1; for(CardItemInfo cardItemInfo:cardItemInfoList){ cardItemInfoList1 = cardItemInfoMapList.computeIfAbsent(cardItemInfo.getCardEquityId(),k->new ArrayList<>()); cardItemInfoList1.add(cardItemInfo); } return cardItemInfoMapList; } /**订单生成再生俱乐部会员*/ public static void createClubMember(Integer type,OrdersTotal ordersTotal,MarketSystem marketSystem,CommonService commonService){ Integer durationType = UserClubMember.DURATION_TYPE_PARAGRAPH; Date startTime = null; Date endTime = null; if(marketSystem.getDurationType() == MarketSystem.DURATION_TYPE_ALL){ durationType = UserClubMember.DURATION_TYPE_ALL; }else if(marketSystem.getDurationType() == MarketSystem.DURATION_TYPE_PARAGRAPH){ startTime = marketSystem.getDurationStart(); endTime = marketSystem.getDurationEnd(); }else{ startTime = ordersTotal.getPayTime(); endTime = startTime; if(marketSystem.getDurationYear() != null){ endTime = DateUtil.addYear(endTime,marketSystem.getDurationYear()); } if(marketSystem.getDurationMonth() != null){ endTime = DateUtil.addMonth(endTime,marketSystem.getDurationMonth()); } if(marketSystem.getDurationDay() != null){ endTime = DateUtil.addDay(endTime,marketSystem.getDurationDay()); } //时间精确到当天最后一秒 endTime = DateUtil.dayToEndDateNoMillisecond(endTime); } insertClubMember(type,durationType,startTime,endTime,UserClubMember.SOURCE_TYPE_ORDER,ordersTotal.getId(),marketSystem.getId(),ordersTotal.getUserId(),commonService); } /**保存俱乐部会员*/ public static void insertClubMember(Integer type,Integer durationType,Date startTime,Date endTime,Integer sourceType,String sourceId,String sourceOtherId ,String userId,CommonService commonService){ SqlSentence sqlSentence = new SqlSentence(); Map values = new HashMap<>(); //有绑定的全部进行取消 values.put("type",type); values.put("status",UserClubMember.STATUS_NORMAL); values.put("userId",userId); sqlSentence.sqlUpdate(" isDel = 2 WHERE isDel = 0 AND type = #{m.type} AND status = #{m.status} AND userId = #{m.userId}",values); commonService.updateWhere(UserClubMemberMapper.class,sqlSentence); UserClubMember userClubMember = new UserClubMember(); userClubMember.setType(type); userClubMember.setStatus(UserClubMember.STATUS_NORMAL); userClubMember.setDurationType(durationType); userClubMember.setStartTime(startTime); userClubMember.setEndTime(endTime); userClubMember.setSourceType(sourceType); userClubMember.setSourceId(sourceId); userClubMember.setSourceOtherId(sourceOtherId); userClubMember.setUserId(userId); commonService.insert(UserClubMemberMapper.class,userClubMember); } }