chenjiahe
2024-01-19 d83d729f80576bb4c7e73bcfff5c403a23f23b82
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
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<String,Object> 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<MarketSystemProject> marketSystemProjectList = commonService.selectList(MarketSystemProjectMapper.class,sqlSentence);
 
        //指定活动项目编号匹配存储
        Set<String> 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<ProjectTypeRelation> 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<OrderItem> orderItemList = commonService.selectList(OrderItemMapper.class,sqlSentence);
 
        //已经算了疗程数的项目编号
        Set<String> mateSkuNoSet = new HashSet<>();
        //总疗程数,所有的项目疗程数总和
        int cureNum = 0;
 
        //存储卡项所有的子项,k值:权益标识
        Map<String,List<CardItemInfo>> cardItemInfoMapList;
        List<OrderItemSon> orderItemSonList;
        List<CardEquity> cardEquityList;
        List<CardItemInfo> 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<OrderItemSon> getOrderItemSonList(String orderItemId,String goodsType,CommonService commonService){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> 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<CardEquity> getCardEquityList(String cardItemId, CommonService commonService){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> 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<String,List<CardItemInfo>> getCardItemInfoList(String cardItemId, CommonService commonService){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> 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<CardItemInfo> cardItemInfoList = commonService.selectList(CardItemInfoMapper.class,sqlSentence);
 
        Map<String,List<CardItemInfo>> cardItemInfoMapList = new HashMap<>();
        List<CardItemInfo> 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<String,Object> 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);
    }
 
 
 
}