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
package com.ibeetl.admin.console.service;
 
 
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.ibeetl.admin.console.dao.FundManagementApiDao;
import com.ibeetl.admin.console.dao.XzxUserRedpaperInfoDao;
import com.ibeetl.admin.console.model.PartnerAccountModel;
import com.ibeetl.admin.console.model.PlatformAccountLogModel;
import com.ibeetl.admin.console.model.UserAccountModel;
import com.ibeetl.admin.console.model.XzxUserRedpaperInfoModel;
import com.ibeetl.admin.console.util.DoubleUtil;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.entity.XzxCityPartner;
import com.ibeetl.admin.core.entity.XzxUserRedpaperInfo;
import com.ibeetl.admin.core.entity.XzxUserRedpaperRule;
import com.ibeetl.admin.core.service.CoreBaseService;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.util.PlatformException;
import org.apache.commons.codec.binary.Base64;
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 java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
 
/**
 * XzxUserRedpaperInfo Service
 */
 
@Service
@Transactional
public class XzxUserRedpaperInfoService extends CoreBaseService<XzxUserRedpaperInfo>{
 
    @Autowired private XzxUserRedpaperInfoDao xzxUserRedpaperInfoDao;
    @Autowired
    XzxCityPartnerService service;
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    @Autowired
    XzxCityPartnerService cityPartnerService;
    @Autowired
    CorePlatformService platformService;
    @Autowired private FundManagementApiDao fundManagementApiDao;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    final Base64 base64 = new Base64();
    public PageQuery<XzxUserRedpaperInfo>queryByCondition(PageQuery query){
        PageQuery ret =  xzxUserRedpaperInfoDao.queryByCondition(query);
        queryListAfter(ret.getList());
        return ret;
    }
 
    public void batchDelXzxUserRedpaperInfo(List<Long> ids){
        try {
            xzxUserRedpaperInfoDao.batchDelXzxUserRedpaperInfoByIds(ids);
        } catch (Exception e) {
            throw new PlatformException("批量删除XzxUserRedpaperInfo失败", e);
        }
    }
 
    public String cancelRedPaper(XzxCityPartner partner){
        String day = sdf.format(new Date());
        Snowflake snowflake = IdUtil.getSnowflake(0, 3);
        String payOrderId = "ZF"+snowflake.nextIdStr();
        if(null!=partner){
            //合伙人退还红包
            //判断红包有效时间是否已经截止
            XzxUserRedpaperRule rule = xzxUserRedpaperInfoDao.queryRuleByDay(partner.getId(),day);
            if(null!=rule){
                //查询规则未领完的红包金额
                String redMoney = queryMoneyByRuleId(rule.getId()+"");
                BigDecimal rd = new BigDecimal(redMoney);
                if(rd.compareTo(BigDecimal.ZERO)==0){
                    return "合伙人的红包已经领完,不能退还!";
                }else{
                   PartnerAccountModel partnerAccountModel = cityPartnerService.queryAccountIdByUserId(partner.getUserId());
                   UserAccountModel userAccountModel1 = cityPartnerService.queryPartnerAccountByUserId(partner.getUserId());
                   BigDecimal hbb = new BigDecimal(partnerAccountModel.getHbb()).add(new BigDecimal(redMoney));
                   //先写日志 UserAccountModel accountModel, String type, BigDecimal money, String payOrderId
                   cityPartnerService.insertAccountLogByPartner(userAccountModel1,"5",new BigDecimal(redMoney),payOrderId);
                   cityPartnerService.insertPartnerLogByPartner(partnerAccountModel,"8",redMoney,payOrderId,partner,DoubleUtil.roundTwo(hbb.toString()));
                   //修改合伙人环保币
                   partnerAccountModel.setHbb(DoubleUtil.roundTwo(hbb.toString()));
                   cityPartnerService.updatePartnerAccount(partnerAccountModel);
                   UserAccountModel userAccountModel = new UserAccountModel();
                   userAccountModel.setUserId(partner.getUserId());
                   userAccountModel.setMoney(Double.valueOf(DoubleUtil.roundTwo(hbb.toString())));
                   cityPartnerService.updateAccountPartner(userAccountModel);
                }
            }else{
                 rule = xzxUserRedpaperInfoDao.queryRuleByDay(partner.getId(),null);
                    String endTime =rule.getEndTime();
                    if(null!=endTime&&!"".equals(endTime)){
                        String[] times = endTime.split("-");
                        return "请在红包结束时间"+times[0]+"年"+times[1]+"月"+times[2]+"日之后进行操作";
                    }else{
                        return "没有规则,无法退红包!";
                    }
            }
        }else{
            //平台管理员退还红包
            XzxUserRedpaperRule rule = xzxUserRedpaperInfoDao.queryRuleByDay(null,day);
            if(null!=rule){
                String redMoney = queryMoneyByRuleId(rule.getId()+"");
                BigDecimal rd = new BigDecimal(redMoney);
                if(rd.compareTo(BigDecimal.ZERO)==0){
                    return "平台的红包已经领完,不能退还!";
                }else{
                    String acc = cityPartnerService.queryAccountByAdminId();
                    String[] accArr = acc.split(",");
                    String oldMoney = accArr[1];
                    String oldHbb = accArr[2];
                    BigDecimal newMoney = new BigDecimal(oldMoney).add(new BigDecimal(redMoney));
                    updateAdminAccount(redMoney,"0",oldHbb,oldMoney,oldHbb,DoubleUtil.roundTwo(newMoney.toString()),null,payOrderId);
                    cityPartnerService.updatePlatformMoney(DoubleUtil.roundTwo(newMoney.toString()));
                }
            }else{
                rule = xzxUserRedpaperInfoDao.queryRuleByDay(null,null);
                String endTime =rule.getEndTime();
                if(null!=endTime&&!"".equals(endTime)){
                    String[] times = endTime.split("-");
                    return "请在红包结束时间"+times[0]+"年"+times[1]+"月"+times[2]+"日之后进行操作";
                }else{
                    return "没有规则,无法退红包!";
                }
            }
        }
        return null;
    }
    public String queryMoneyByRuleId(String ruleId){
        return xzxUserRedpaperInfoDao.queryMoneyByRuleId(ruleId);
    }
    private void updateAdminAccount(String money,String hbb,String oldHbb,String oldMoney,String newHbb,String newMoney,XzxCityPartner partner,String payOrderId){
        CoreUser user = platformService.getCurrentUser();
        PlatformAccountLogModel platformAccountLogModel = new PlatformAccountLogModel();
        platformAccountLogModel.setCreateTime(sdf.format(new Date()));
        platformAccountLogModel.setCreateUserId(user.getId()+"");
        platformAccountLogModel.setMoney(money);
        platformAccountLogModel.setHbb(hbb);
        platformAccountLogModel.setOldHbb(oldHbb);
        platformAccountLogModel.setOldMoney(oldMoney);
        platformAccountLogModel.setNewHbb(newHbb);
        platformAccountLogModel.setNewMoney(newMoney);
        if(null!=partner){
            platformAccountLogModel.setCreateUserName(partner.getPartnerName());
            platformAccountLogModel.setCreateUserMobile(partner.getMobilePhone());
        }else{
            platformAccountLogModel.setCreateUserName(user.getName());
            platformAccountLogModel.setCreateUserMobile("-");
        }
        platformAccountLogModel.setCreateUserType("2");
        platformAccountLogModel.setFlowNo(payOrderId);
        fundManagementApiDao.insertPlatFormLog(platformAccountLogModel);
    }
    public Map<String,Object> redPaperList(XzxUserRedpaperInfoModel 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);
        List<XzxUserRedpaperInfoModel> list = xzxUserRedpaperInfoDao.redPaperList(model);
        for (XzxUserRedpaperInfoModel resultModel:list) {
            String name = resultModel.getName();
            if(name!=null&&!"".equals(name)){
                try {
                    name =new String(base64.decode(name), "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            resultModel.setName(name);
        }
        Map<String,Object> totalmap = xzxUserRedpaperInfoDao.redPaperTotal(model);
        map.put("data",list);
        map.put("count",xzxUserRedpaperInfoDao.redPaperCount(model));
        map.put("code",0);
        map.put("redPaperTotalNum",totalmap.get("count"));
        if(totalmap.get("money")!=null){
            map.put("redPaperTotalMoney",totalmap.get("money"));
        }else{
            map.put("redPaperTotalMoney","0");
        }
        return map;
    }
 
    public void batchInsertPaperInfo(List<XzxUserRedpaperInfo> list){
        xzxUserRedpaperInfoDao.batchInsertPaperInfo(list);
    }
    public String subPartnerAccount(String amount){
        List<String> partnerIds = service.queryPartnerByCurrent();
        Snowflake snowflake = IdUtil.getSnowflake(0, 3);
        String payOrderId = "ZF"+snowflake.nextIdStr();
        if(null!=partnerIds&&partnerIds.size()>0){
            PartnerAccountModel partnerAccount = new PartnerAccountModel();
            XzxCityPartner partner = service.queryById(partnerIds.get(0));
            partnerAccount = service.queryAccountIdByUserId(partner.getUserId());
            BigDecimal pHbb = new BigDecimal(partnerAccount.getHbb());
            BigDecimal subHbb = pHbb.subtract(new BigDecimal(amount));
            if(subHbb.compareTo(BigDecimal.ZERO)==-1){
                return "合伙人的余额为:"+partnerAccount.getHbb()+",不足以发红包金额:"+amount+",请充值环保币!";
            }
            UserAccountModel userAccount = service.queryPartnerAccountByUserId(partner.getUserId());
            cityPartnerService.insertPartnerAccountLog(userAccount,"5",new BigDecimal(amount),payOrderId,"8");
            userAccount.setMoney(Double.valueOf(DoubleUtil.roundTwo(subHbb.toString())));
            cityPartnerService.updateAccountPartner(userAccount);
            partnerAccount.setHbb(DoubleUtil.roundTwo(subHbb.toString()));
            cityPartnerService.updatePartnerAccount(partnerAccount);
        }else{
            String acc = service.queryAccountByAdminId();
            String[] accArr = acc.split(",");
            String adminMoney = accArr[1];
            String adminhbb = accArr[2];
            //平台
            BigDecimal oldAdminMoney = new BigDecimal(adminMoney);
            BigDecimal oldAdminhbb = new BigDecimal(adminhbb);
            //平台账户处理红包
            BigDecimal newAdminhbb = oldAdminhbb.subtract(new BigDecimal(amount));
            service.updatePlatformHbb(DoubleUtil.roundTwo(newAdminhbb.toString()));
            PlatformAccountLogModel platformAccountLogModel = new PlatformAccountLogModel();
            platformAccountLogModel.setCreateTime(sdf.format(new Date()));
            platformAccountLogModel.setCreateUserId("1");
            platformAccountLogModel.setMoney(DoubleUtil.roundTwo("0"));
            platformAccountLogModel.setHbb("-" + DoubleUtil.roundTwo(amount));
            platformAccountLogModel.setOldHbb(DoubleUtil.roundTwo(adminhbb));
            platformAccountLogModel.setOldMoney(DoubleUtil.roundTwo(adminMoney));
            platformAccountLogModel.setNewHbb(DoubleUtil.roundTwo(newAdminhbb.toString()));
            platformAccountLogModel.setNewMoney(DoubleUtil.roundTwo(adminMoney));
            platformAccountLogModel.setCreateUserName("平台管理员");
            platformAccountLogModel.setCreateUserType("2");
            platformAccountLogModel.setCreateUserMobile("--");
            platformAccountLogModel.setFlowNo(payOrderId);
            service.insertPlatFormLog(platformAccountLogModel);
        }
        return null;
    }
 
}