Helius
2021-06-16 5728be2af515b2200e782aa201ca5d4d67d9ea47
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package com.ibeetl.admin.console.service;
import com.ibeetl.admin.console.dao.FundManagementApiDao;
import com.ibeetl.admin.console.dao.XzxUserRedpaperRuleDao;
import com.ibeetl.admin.console.dao.XzxUserShareInfoDao;
import com.ibeetl.admin.console.model.CommissionModel;
import com.ibeetl.admin.console.model.RebateWeightPriceModel;
import com.ibeetl.admin.console.model.XzxPlatformCapitalInfoModel;
import com.ibeetl.admin.console.model.XzxUserRedpaperPriceModel;
import com.ibeetl.admin.console.util.DoubleUtil;
import com.ibeetl.admin.core.entity.XzxCityPartner;
import com.ibeetl.admin.core.entity.XzxRebateRulePrice;
import com.ibeetl.admin.core.entity.XzxUserRedpaperRule;
import com.ibeetl.admin.core.service.CoreBaseService;
import com.ibeetl.admin.core.util.PlatformException;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ibeetl.admin.console.model.XzxUserRebateRuleModel;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Service
@Transactional
public class XzxUserRedpaperRuleService extends CoreBaseService<XzxUserRedpaperRule>{
 
    @Autowired private XzxUserRedpaperRuleDao xzxUserRedpaperRuleDao;
    @Autowired private FundManagementApiDao fundManagementApiDao;
    @Autowired
    private XzxUserShareInfoDao xzxUserShareInfoDao;
    @Autowired
    XzxUserShareInfoService xzxUserShareInfoService;
    @Autowired
    XzxCityPartnerService cityPartnerService;
    final Base64 base64 = new Base64();
 
    public Map<String,Object> queryByCondition(PageQuery query, String ruleType){
        PageQuery ret =  xzxUserRedpaperRuleDao.queryByCondition(query);
        List<XzxUserRedpaperPriceModel> priceList = new ArrayList<>();
        List<XzxUserRedpaperRule> list = ret.getList();
 
        if(ruleType.equals("3")){
            for (XzxUserRedpaperRule rule:list) {
                XzxUserRedpaperPriceModel model = new XzxUserRedpaperPriceModel(rule);
                List<XzxRebateRulePrice> rulePriceList =xzxUserRedpaperRuleDao.queryRebateRulePriceList(String.valueOf(rule.getId()));
                model.setRulePriceList(rulePriceList);
                model.setApplicableObject(rule.getApplicableObject());
                //文案文字返回
                String sharingProfitType = rule.getSharingProfitType();
                if(sharingProfitType!=null){
                    String[] types = sharingProfitType.split(",");
                    StringBuffer typeDesc = new StringBuffer();
                    String aa="";
                    String demo="";
                    for (String type:types) {
                        String desc =fundManagementApiDao.queryTypeDesc(type);
                        int a=1;
                        for (XzxRebateRulePrice p:rulePriceList) {
                            if(typeDesc.toString()!=null&&!"".equals(typeDesc.toString())){
                                if(type.equals("REBATE_TWO")){
                                    if(a>=2){
                                        typeDesc.append(";").append(a+"、"+demo).append(p.getWeight()).append("kg,").append(desc).append(p.getRate()).append("%");
                                    }else{
                                        typeDesc.append(desc).append(p.getRate()).append("%");
                                    }
 
                                }else if(type.equals("REBATE_THREE")){
                                    aa=desc;
                                }
                                a=a+1;
                            }else{
                                demo=desc;
                                typeDesc.append(a+"、"+desc).append(p.getWeight()).append("kg,");
                            }
 
                            //break;
                        }
 
                    }
 
                    typeDesc.append(",").append((rulePriceList.size()+1)+"、").append(aa).append(model.getConversionPerUnit()).append("kg");
                    model.setTypeDesc(typeDesc.toString());
 
                }
                priceList.add(model);
 
            }
        }else if(ruleType.equals("8")){
            for (XzxUserRedpaperRule rule:list) {
                XzxUserRedpaperPriceModel model = new XzxUserRedpaperPriceModel(rule);
 
                StringBuffer typeDesc = new StringBuffer();
               BigDecimal radio = new BigDecimal(rule.getShareRatio());
                BigDecimal showRadio =  radio.multiply(new BigDecimal("100"));
               typeDesc.append("城市合伙人每笔订单成交,总部收取每笔订单回收流水的").append(DoubleUtil.subZeroAndDot(showRadio.toString()));
                typeDesc.append("%,做为平台服务费。");
                model.setTypeDesc(typeDesc.toString());
                model.setShareRatio(showRadio.toString());
                priceList.add(model);
 
            }
        }else{
            for (XzxUserRedpaperRule rule:list) {
                XzxUserRedpaperPriceModel model = new XzxUserRedpaperPriceModel(rule);
                //文案文字返回
                String sharingProfitType = rule.getSharingProfitType();
                model.setApplicableObject(rule.getApplicableObject());
                if(ruleType.equals("2")){
                            if(sharingProfitType!=null){
                                String[] types = sharingProfitType.split(",");
                                StringBuffer typeDesc = new StringBuffer();
                                for (String type:types) {
                                    String desc =fundManagementApiDao.queryTypeDesc(type);
                                    if(typeDesc.toString()!=null&&!"".equals(typeDesc.toString())){
                                        typeDesc.append(",").append(desc);
                                    }else{
                                        if(type.equals("SHARE_RATIO")){
                                            typeDesc.append(desc).append(model.getShareRatio()).append("%");
                                        }else{
                                            typeDesc.append(desc);
                                        }
                                    }
                                }
                                model.setTypeDesc(typeDesc.toString());
                          }
                }else{
                    if(ruleType.equals("5")){
                        StringBuffer str = new StringBuffer();
                        String productType="";
                        String rate="";
                        List<XzxRebateRulePrice> wl = fundManagementApiDao.queryWtList(rule.getId()+"");
                        model.setProductType(rule.getProductType());
                        String[] types = rule.getProductType().split(",");
                        List<XzxRebateRulePrice> rulePriceList  = new ArrayList<>();
                        for (int i=0;i<wl.size();i++) {
                            XzxRebateRulePrice ww = wl.get(i);
                            rulePriceList.add(ww);
                            str.append(i+1).append("、用户每次下单超过").append(ww.getWeight()).append("kg,返利").append(ww.getRate().split(",")[0]+"%;");
                        }
                        model.setRulePriceList(rulePriceList);
                        str.append((wl.size()+1));
                        str.append("、适用物品包括:");
                        for (String t:types) {
                            if(t.equals("1")){
                                str.append("废纸").append(",");
                            }else if(t.equals("2")){
                                str.append("杂纸").append(",");
                            }
                        }
                        model.setTypeDesc(str.toString());
                    }else if(ruleType.equals("7")){
                        StringBuffer str = new StringBuffer();
                        String packageId = rule.getPackageId();
                        XzxCityPartner partner =cityPartnerService.queryById(packageId);
                        str.append(partner.getPartnerName()).append("的发放规则");
                        str.append("1、回收员每次成交,合伙人给回收员发放提成,提成为");
                        List<RebateWeightPriceModel> itemList = xzxUserShareInfoService.queryOldItemList(rule.getId()+"");
                        for (RebateWeightPriceModel mm:itemList) {
                            String title = xzxUserShareInfoDao.queryGoodsById(mm.getProductType());
                                mm.setProductName(title);
                                str.append(title).append(":").append(mm.getMoney()).append("元/公斤,");
 
                        }
                        model.setRoyaltyRuleList(itemList);
                        String rType = rule.getProductType();
                        if(null!=rType){
                            String[]  itemType = rType.split(",");
                            if(itemType[0].equals("3")){
                                str.append("2、可以人工结算提成;");
                            }
                            if(itemType[0].equals("4")){
                                String timming = rule.getTiming();
                                String[]  trs = timming.split(",");
                                 str.append("3、每周").append(trs[0]).append(" ").append(trs[1]).append("点系统自动结算提成");
                            }
                         }
                        model.setTypeDesc(str.toString());
                    }else{
                        StringBuffer str = new StringBuffer();
                        if(model.getIssuanceRules().equals("0")){
                            double totald = Double.parseDouble(model.getAmount())*100;
                            long total =new Double(totald).longValue();
                            int count = Integer.parseInt(model.getRedEnvelopesNum());
                            long average = total / count;
                            String[] pri = model.getRandomAmountAverage().split(",");
                            str.append("1、发放金额随机,金额范围为").append(model.getRandomAmountAverage()).append("元;平均为").append(average).append("元");
                            str.append("总额为").append(model.getAmount()).append("元,随机分配;最高").append(pri[1]).append("元/个");
                        }else{
                            str.append("发放固定金额,红包为").append(model.getRandomAmountAverage()).append(",总额为").append(model.getAmount());
                        }
                        model.setTypeDesc(str.toString());
                    }
 
                }
                priceList.add(model);
 
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("data",priceList);
        map.put("pageNumber",ret.getPageNumber());
        map.put("pageSize",ret.getPageSize());
        map.put("totalPage",ret.getTotalPage());
        map.put("count",ret.getTotalRow());
        //queryListAfter(priceList);
 
        return map;
    }
 
    public void batchDelXzxUserRedpaperRule(List<Long> ids){
        try {
            xzxUserRedpaperRuleDao.batchDelXzxUserRedpaperRuleByIds(ids);
        } catch (Exception e) {
            throw new PlatformException("批量删除XzxUserRedpaperRule失败", e);
        }
    }
    public void deleteByRuleId(Integer ruleId){
         xzxUserRedpaperRuleDao.deleteByRuleId(ruleId);
    }
    public Map<String,Object> queryRebateList(XzxPlatformCapitalInfoModel model){
 
        List<String> partnerIds = cityPartnerService.queryPartnerByCurrent();
        model.setPartnerIds(partnerIds);
        Map<String,Object> map = new HashMap<>();
        int p = model.getPage();
        p = (p - 1) * model.getLimit();
        model.setPage(p);
        String mobilePhone = model.getMobilePhone();
        if(!StringUtils.isEmpty(model.getMobilePhone())){
            model.setMobilePhone(mobilePhone);
            model.setNickName(new String(Base64.encodeBase64(mobilePhone.getBytes())));
 
        }
        List<XzxPlatformCapitalInfoModel> list = xzxUserRedpaperRuleDao.queryRebateList(model);
        for (XzxPlatformCapitalInfoModel info:list) {
            if(info.getUserId()!=null&&!"".equals(info.getUserId())){
                //转昵称
                String nickName = info.getNickName();
                if(nickName!=null&&!"".equals(nickName)){
                    try {
                        nickName =new String(base64.decode(nickName), "UTF-8");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
                info.setNickName(nickName);
                //查询用户此单之前的累计重量
                String afterWeight = "0" ;
                String userId = info.getUserId();
                String completeTime = info.getCreateTime();
                List<String> orderIds = xzxUserRedpaperRuleDao.queryUserOrderIds(completeTime,userId);
                if(orderIds!=null&&orderIds.size()>0){
                    afterWeight = xzxUserRedpaperRuleDao.queryAfterWeightByOrderIds(orderIds);
                }
                info.setAfterWeight(DoubleUtil.roundThree(afterWeight));
                info.setDesignatedWeight(DoubleUtil.roundThree(info.getDesignatedWeight()));
            }
 
        }
        //如果没有用户Id,则不显示返利列表
        if(list.size()==0){
            list = new ArrayList<>();
        }
        int count = xzxUserRedpaperRuleDao.queryRebateCount(model);
        String totalAmount =xzxUserRedpaperRuleDao.queryTotalAmountByParam(model);
 
        map.put("data",list);
        map.put("count",count);
        map.put("code",0);
        map.put("orderNum",count);
        if(null!=totalAmount&&!"".equals(totalAmount)){
            map.put("totalAmount",DoubleUtil.roundTwo(totalAmount));
 
        }else{
            map.put("totalAmount","0");
        }
        return map;
    }
 
    public List<XzxUserRedpaperRule> queryRedpaperList(String startTime){
        return xzxUserRedpaperRuleDao.queryRedpaperList(startTime);
    }
    public List<XzxUserRedpaperRule> queryRedpaperListStartAndEnd(String startTime,String endTime){
        return xzxUserRedpaperRuleDao.queryRedpaperListStartAndEnd(startTime,endTime);
    }
 
    public List<XzxUserRedpaperRule> queryShareListByTime(String startTime,String endTime){
        return xzxUserRedpaperRuleDao.queryShareListByTime(startTime,endTime);
    }
    public List<XzxUserRedpaperRule> queryCommListByTime(String startTime,String endTime,String id){
        return xzxUserRedpaperRuleDao.queryCommListByTime(startTime,endTime,id);
    }
    public Map<String,Object> queryAccountList(CommissionModel model){
        Map<String,Object> map = new HashMap<>();
        int p = model.getPage();
        p = (p - 1) * model.getLimit();
        model.setPage(p);
        String partnerName = model.getPartnerName();
        if(!StringUtils.isEmpty(partnerName)){
            model.setPartnerName(partnerName);
            model.setMobile(partnerName);
        }
        int count = 0;
        String c = xzxUserRedpaperRuleDao.queryAccountListCount(model);
        List<CommissionModel>  list = xzxUserRedpaperRuleDao.queryAccountList(model);
        if(null!=c&&!"".equals(c)){
            count = Integer.parseInt(c);
        }
        //已提佣次数
        map.put("commissionNum",count);
        //总提佣金额
        String totalMoney =xzxUserRedpaperRuleDao.queryTotalCommissionMoney(model);
        if(null!=totalMoney&&!"".equals(totalMoney)){
            map.put("totalMoney",DoubleUtil.roundTwo(totalMoney));
        }else{
            map.put("totalMoney","0.00");
        }
        map.put("code",0);
        map.put("data",list);
        map.put("count",count);
        return map;
    }
    public List<XzxUserRedpaperRule> queryRebateRuleList(XzxUserRebateRuleModel model){
        return xzxUserRedpaperRuleDao.queryRebateRuleList(model);
    }
    public List<XzxUserRedpaperRule> queryCommissioList(){
        return xzxUserRedpaperRuleDao.queryCommissioList();
    }
 
    public List<XzxUserRedpaperRule> queryWeightRuleList(XzxUserRebateRuleModel model){
        return xzxUserRedpaperRuleDao.queryWeightRuleList(model);
    }
 
    public List<XzxUserRedpaperRule> queryRoyalRuleList(String partnerId,String packageId){
        return xzxUserRedpaperRuleDao.queryRoyalRuleList(partnerId,packageId);
    }
 
}