From 28cf328a098334b51a3e9d2d56f983fb8c862211 Mon Sep 17 00:00:00 2001 From: yujian <yujian@163.com> Date: 星期六, 23 五月 2020 09:54:38 +0800 Subject: [PATCH] 足迹、收藏订单兼容新需求 --- fanli/src/main/java/com/yeshi/fanli/service/impl/money/extract/ExtractServiceImpl.java | 706 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 667 insertions(+), 39 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/money/extract/ExtractServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/money/extract/ExtractServiceImpl.java index 4e598b4..f824710 100644 --- a/fanli/src/main/java/com/yeshi/fanli/service/impl/money/extract/ExtractServiceImpl.java +++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/money/extract/ExtractServiceImpl.java @@ -1,16 +1,28 @@ package com.yeshi.fanli.service.impl.money.extract; +import java.io.IOException; +import java.io.InputStream; import java.math.BigDecimal; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; +import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.UUID; import javax.annotation.Resource; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.DocumentHelper; +import org.dom4j.Element; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.yeshi.utils.DateUtil; +import org.yeshi.utils.entity.wx.RedPackParams; +import org.yeshi.utils.entity.wx.RedPackRecord; +import org.yeshi.utils.wx.WXPayUtil; import com.alipay.api.AlipayApiException; import com.alipay.api.AlipayClient; @@ -33,6 +45,7 @@ import com.yeshi.fanli.entity.bus.user.Extract; import com.yeshi.fanli.entity.bus.user.ExtractAuditRecord; import com.yeshi.fanli.entity.bus.user.ExtractRecord; +import com.yeshi.fanli.entity.bus.user.ExtractWeiXinRecord; import com.yeshi.fanli.entity.bus.user.HongBaoV2; import com.yeshi.fanli.entity.bus.user.PayInfo; import com.yeshi.fanli.entity.bus.user.UserAccountBindingHistory; @@ -40,6 +53,7 @@ import com.yeshi.fanli.entity.common.AdminUser; import com.yeshi.fanli.entity.money.UserMoneyDetail; import com.yeshi.fanli.entity.money.UserMoneyDetail.UserMoneyDetailTypeEnum; +import com.yeshi.fanli.entity.system.ConfigKeyEnum; import com.yeshi.fanli.exception.ExtractException; import com.yeshi.fanli.exception.NotExistObjectException; import com.yeshi.fanli.exception.ObjectStateException; @@ -52,7 +66,9 @@ import com.yeshi.fanli.service.inter.money.UserMoneyService; import com.yeshi.fanli.service.inter.money.extract.ExtractRecordService; import com.yeshi.fanli.service.inter.money.extract.ExtractService; +import com.yeshi.fanli.service.inter.money.extract.ExtractWeiXinRecordService; import com.yeshi.fanli.service.inter.money.msg.UserMoneyMsgNotificationService; +import com.yeshi.fanli.service.inter.order.CommonOrderCountService; import com.yeshi.fanli.service.inter.order.HongBaoV2Service; import com.yeshi.fanli.service.inter.order.OrderUserStatisticService; import com.yeshi.fanli.service.inter.push.PushService; @@ -62,6 +78,9 @@ import com.yeshi.fanli.util.Constant; import com.yeshi.fanli.util.GsonUtil; import com.yeshi.fanli.util.MoneyBigDecimalUtil; +import com.yeshi.fanli.util.StringUtil; +import com.yeshi.fanli.util.ThreadUtil; +import com.yeshi.fanli.util.TimeUtil; import com.yeshi.fanli.util.factory.UserMoneyDetailFactory; import net.sf.json.JSONObject; @@ -132,6 +151,12 @@ @Resource private OrderUserStatisticService orderUserStatisticService; + @Resource + private ExtractWeiXinRecordService extractWeiXinRecordService; + + @Resource + private CommonOrderCountService commonOrderCountService; + @Transactional public Integer addExtract(Extract extract) { Integer integer = extract(extract); @@ -153,12 +178,22 @@ updateExtract.setId(id); updateExtract.setState(Extract.STATE_PROCESSING); extractMapper.updateByPrimaryKeySelective(updateExtract); + // 鏀瑰彉璧勯噾璁板綍鐘舵�� + UserMoneyDetail detail = userMoneyDetailService.selectByTypeAndUidAndIdentifyCode( + UserMoneyDetailTypeEnum.extract, find.getUserInfo().getId(), find.getId()); + if (detail != null) { + UserMoneyDetail update = new UserMoneyDetail(detail.getId()); + update.setShow(true); + update.setUpdateTime(new Date()); + userMoneyDetailMapper.updateByPrimaryKeySelective(update); + } + transfer(find, admin); // 鎻愮幇閫氳繃锛屾敮浠樺疂杞处 return null; } - @Transactional + @Transactional(rollbackFor = Exception.class) public synchronized void rejectExtract(long id, String reason, AdminUser admin) throws ObjectStateException, NotExistObjectException { Extract find = extractMapper.selectByPrimaryKey(id); @@ -238,9 +273,9 @@ && (System.currentTimeMillis() - history.getCreateTime().getTime()) < 1000 * 60 * 60 * 24 * 7L) return 111; - final String autoExtract = configService.get("extract_way"); // 鏄惁鑷姩杞处 - final String maxCount = configService.get("extract_count_day"); - final String maxMoney = configService.get("extract_money_day"); + final String autoExtract = configService.get(ConfigKeyEnum.extractWay.getKey()); // 鏄惁鑷姩杞处 + final String maxCount = configService.get(ConfigKeyEnum.extractDayCount.getKey()); + final String maxMoney = configService.get(ConfigKeyEnum.extractMoneyDay.getKey()); UserInfo user = userInfoMapper.selectByPrimaryKeyForUpdate(extract.getUserInfo().getId()); // 浣欓涓嶈冻 @@ -261,25 +296,37 @@ extractMapper.insertSelective(extract); - ExtractAuditRecord auditRecord = new ExtractAuditRecord(); + final ExtractAuditRecord auditRecord = new ExtractAuditRecord(); auditRecord.setBeforeMoney(user.getMyHongBao()); auditRecord.setExtract(extract); Date minTime = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 180L); - // 缁熻鏁版嵁 - int orderCount50More = orderUserStatisticService.countOrderCount100MoreByUidAndMinTime(user.getId(), minTime); - BigDecimal weiQuanOrderFanAmount = orderUserStatisticService.sumWeiQuanOrderFanAmountByUid(user.getId(), - minTime); - int weiQuanOrderCount = orderUserStatisticService.countWeiQuanOrderByUid(user.getId(), minTime); - auditRecord.setExtraInfoStr(new Gson() - .toJson(new ExtractOrderStatisticDTO(orderCount50More, weiQuanOrderCount, weiQuanOrderFanAmount, 0))); extractAuditRecordMapper.insertSelective(auditRecord); + + ThreadUtil.run(new Runnable() { + + @Override + public void run() { + // 缁熻鏁版嵁 + int less10Count = commonOrderCountService.countOderByUidAndLess10(user.getId()); + ExtractAuditRecord update = new ExtractAuditRecord(); + update.setId(auditRecord.getId()); + int orderCount50More = orderUserStatisticService.countOrderCount100MoreByUidAndMinTime(user.getId(), + minTime); + BigDecimal weiQuanOrderFanAmount = orderUserStatisticService.sumWeiQuanOrderFanAmountByUid(user.getId(), + minTime); + int weiQuanOrderCount = orderUserStatisticService.countWeiQuanOrderByUid(user.getId(), minTime); + update.setExtraInfoStr(new Gson().toJson(new ExtractOrderStatisticDTO(orderCount50More, + weiQuanOrderCount, weiQuanOrderFanAmount, 0, less10Count))); + extractAuditRecordMapper.updateByPrimaryKeySelective(update); + } + }); // 鏂扮増璧勯噾璇︽儏 UserMoneyDetail userMoneyDetail = null; try { - userMoneyDetail = UserMoneyDetailFactory.createExtract(extract); + userMoneyDetail = UserMoneyDetailFactory.createExtract(extract, null); } catch (UserMoneyDetailException e2) { try { LogHelper.errorDetailInfo(e2); @@ -298,7 +345,6 @@ LogHelper.userInfo("uid=" + user.getId() + ",鎻愪氦杞处鐢宠!"); } else {// 鏆傛椂涓嶈嚜鍔ㄦ彁鐜� // transfer(extract, null); - ; } return null; } @@ -374,39 +420,39 @@ * @param extract * @param session */ - //TODO 杞处閫昏緫闇�瑕佹敼鍙� + // TODO 杞处閫昏緫闇�瑕佹敼鍙橈紝鍥哄畾IP @Transactional private void extractByZhiFuBao(Extract extract, AdminUser adminUser) { - LogHelper.userErrorInfo("鎻愮幇:寮�濮嬮�氳繃鎻愮幇-"+extract.getId()); + LogHelper.userErrorInfo("鎻愮幇:寮�濮嬮�氳繃鎻愮幇-" + extract.getId()); String privateKey = Constant.alipayConfig.getPrivateKey(); AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", Constant.alipayConfig.getAppId(), privateKey, "json", "gbk", null, "RSA2"); AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest(); - String uuid = UUID.randomUUID().toString().replace("-", ""); String appName = Constant.systemCommonConfig.getProjectChineseName(); JSONObject json = new JSONObject(); - json.put("out_biz_no", uuid);//鏈�闀块暱搴�64浣嶏紝鏀寔鑻辨枃锛屼笅鍒掔嚎锛屾暟瀛� + json.put("out_biz_no", "widthdraw_" + extract.getId());// 鏈�闀块暱搴�64浣嶏紝鏀寔鑻辨枃锛屼笅鍒掔嚎锛屾暟瀛� json.put("payee_type", "ALIPAY_LOGONID"); json.put("payee_account", extract.getAccount()); json.put("amount", extract.getMoney()); - json.put("payer_show_name", appName + "App" + "鎻愮幇"); + json.put("payer_show_name", appName); json.put("payee_real_name", extract.getName()); - json.put("remark", "鏉ヨ嚜" + appName + "App" + "鐨勬彁鐜�"); + json.put("remark", "鏉ヨ嚜閲嶅簡蹇渷绉戞妧鏈夐檺鍏徃"); request.setBizContent(json.toString()); AlipayFundTransToaccountTransferResponse response = null; try { response = alipayClient.execute(request); - LogHelper.userErrorInfo("鎻愮幇:鏀粯瀹濋�氳繃鎻愮幇鎴愬姛-"+extract.getId()); + LogHelper.userErrorInfo("鎻愮幇:鏀粯瀹濋�氳繃鎻愮幇鎴愬姛-" + extract.getId()); } catch (AlipayApiException e) { - e.printStackTrace(); + LogHelper.errorDetailInfo(e, "鏀粯瀹濊浆璐﹀紓甯�:" + extract.getId(), ""); LogHelper.userErrorInfo("鎻愮幇:鏀粯瀹濇彁鐜板紓甯革細" + response + ",鎻愮幇淇℃伅" + GsonUtil.toJson(extract)); } try { CMQManager.getInstance().addExtractResultMsg(extract, response, adminUser); - LogHelper.userErrorInfo("鎻愮幇:娣诲姞澶勭悊闃熷垪鎴愬姛-"+extract.getId()); + LogHelper.userErrorInfo("鎻愮幇:娣诲姞澶勭悊闃熷垪鎴愬姛-" + extract.getId()); } catch (Exception e) { - LogHelper.userErrorInfo("鎻愮幇:鏀粯瀹濇彁鐜癈MQ寮傚父锛�" + new Gson().toJson(response) + ",鎻愮幇淇℃伅" + GsonUtil.toJson(extract)); + LogHelper + .userErrorInfo("鎻愮幇:鏀粯瀹濇彁鐜癈MQ寮傚父锛�" + new Gson().toJson(response) + ",鎻愮幇淇℃伅" + GsonUtil.toJson(extract)); } } @@ -477,7 +523,7 @@ * @param session */ @Transactional - private void extractSuccess(Extract extract) { + private void extractSuccess(Extract extract,String alipayNo) { // 鑰佺増鏈姛鑳� // List<MoneyRecord> list = moneyRecordDao.list("from MoneyRecord mr // where mr.extract.id = " + extract.getId()); @@ -500,8 +546,27 @@ } catch (Exception e) { e.printStackTrace(); } + + UserMoneyDetail detailNew = null; + try { + detailNew = UserMoneyDetailFactory.createExtract(extract, alipayNo); + } catch (UserMoneyDetailException e) { + e.printStackTrace(); + } + + // 澶栨樉璐︽埛鏄庣粏 + UserMoneyDetail userMoneyDetail = userMoneyDetailMapper.selectByTypeAndUidAndIdentifyCode( + UserMoneyDetailTypeEnum.extractNew, extract.getUserInfo().getId(), extract.getId()); + if (userMoneyDetail != null) { + UserMoneyDetail detail = new UserMoneyDetail(userMoneyDetail.getId()); + detail.setShow(true); + detail.setUpdateTime(new Date()); + if (detailNew != null) + detail.setDescInfo(detailNew.getDescInfo()); + userMoneyDetailMapper.updateByPrimaryKeySelective(detail); + } // 鏂扮増閫氱煡 - userMoneyMsgNotificationService.extractSuccess(extract); + userMoneyMsgNotificationService.extractSuccess(extract, alipayNo); } /** @@ -512,7 +577,7 @@ return extractMapper.selectByPrimaryKey(id); } - @Transactional + @Transactional(rollbackFor = Exception.class) @Override public void checkExtract(Long uid) throws ExtractException { BigDecimal compensateMoney = extractMapper.computeCompensateByUid(uid); @@ -534,8 +599,10 @@ // 鑾峰彇鎻愮幇锛岃幏鍙栨彁鐜伴獙璇� List<UserMoneyDetailTypeEnum> typeList = new ArrayList<>(); typeList.add(UserMoneyDetailTypeEnum.extract); + typeList.add(UserMoneyDetailTypeEnum.extractNew); typeList.add(UserMoneyDetailTypeEnum.extractReject); typeList.add(UserMoneyDetailTypeEnum.extractVerify); + typeList.add(UserMoneyDetailTypeEnum.extractVerifyNew); BigDecimal extractMoney = userMoneyDetailService.statisticUserTypeMoneyWithDate(uid, typeList, new Date(0), now); extractMoney = extractMoney.abs(); @@ -555,7 +622,7 @@ BigDecimal after = hongBaoMoney.subtract(extractMoney.add(weiqaunMoney).add(balance).add(compensateMoney)); if (after.compareTo(new BigDecimal("0")) != 0) - throw new ExtractException(2, "缁撶畻璧勯噾寮傚父"); + throw new ExtractException(2, "缁撶畻璧勯噾寮傚父锛�" + after); } @Override @@ -604,7 +671,7 @@ if (response.isSuccess()) { String code = response.getCode(); if ("10000".equals(code)) { - extractSuccess(extract); + extractSuccess(extract,response.getOrderId()); } else { extractFail(extract, response.getSubMsg()); } @@ -664,7 +731,6 @@ // 鏇存柊鎻愮幇璁板綍瀹℃牳琛� extractAuditRecordMapper.updateByPrimaryKeySelective(auditRecord); - } } @@ -673,7 +739,7 @@ @Override public void testExtractSuccess(Long id) { Extract extract = extractMapper.selectByPrimaryKey(id); - extractSuccess(extract); + extractSuccess(extract,null); Long uid = extract.getUserInfo().getId(); // 鏇存柊瀹℃牳璁板綍 @@ -718,19 +784,581 @@ } @Override - public BigDecimal sumTodayApplyMoney(Long uid,String day) { - return extractMapper.sumToadyApplyByUid(uid,day); + public BigDecimal sumTodayApplyMoney(Long uid, String day) { + return extractMapper.sumToadyApplyByUid(uid, day); } @Override public BigDecimal sumVerifyingMoney(Long uid) { - List<Integer> stateList=new ArrayList<>(); - stateList.add(Extract.STATE_NOT_PROCESS); - stateList.add(Extract.STATE_PROCESSING); - BigDecimal money= extractMapper.sumMoneyByUidAndState(uid, stateList); - if(money==null) + List<Integer> stateList = new ArrayList<>(); + stateList.add(Extract.STATE_NOT_PROCESS); + stateList.add(Extract.STATE_PROCESSING); + BigDecimal money = extractMapper.sumMoneyByUidAndState(uid, stateList); + if (money == null) return new BigDecimal(0); else return money; } + + @Transactional(rollbackFor = Exception.class) + @Override + public void autoExtract(UserInfo user) { + if (user == null || user.getWxOpenId() == null || user.getWxUnionId() == null + || user.getState() != UserInfo.STATE_NORMAL) + return; + + Date date = new Date(); + BigDecimal money = new BigDecimal(Constant.AUTO_EXTRACT_MONEY); + + // 1銆佹彃鍏ヨ褰� + ExtractWeiXinRecord extractRecord = new ExtractWeiXinRecord(); + extractRecord.setUid(user.getId()); + extractRecord.setOpenId(user.getWxOpenId()); + extractRecord.setUnionId(user.getWxUnionId()); + extractRecord.setMoney(money); + extractRecord.setCreateTime(date); + extractRecord.setUpdateTime(date); + extractWeiXinRecordService.insertSelective(extractRecord); + + // 浜ゆ槗鍙� + Long recordId = extractRecord.getId(); + String billno = "BanLiExtract" + recordId; + + // 鏇存柊璁板綍 + ExtractWeiXinRecord updeteRecord = null; + + String mchId = "1520950211"; + String appId = "wx5c0d167c6e3ad726"; + String key = "XYJkJ2018FAfaodCCx899mLl138rfGVd"; + + // 2銆佸彂鏀惧井淇$孩鍖� + RedPackParams redPackParams = new RedPackParams(); + redPackParams.setBillno(billno); + redPackParams.setMchId(mchId); + redPackParams.setWxappId(appId); + redPackParams.setSendName("鏉挎牀蹇渷"); + redPackParams.setOpenid(user.getWxOpenId()); + redPackParams.setMoney(money); + redPackParams.setTotalNum(1); + redPackParams.setWishing("璐︽埛涓墿浣欎綑棰濊鍦ㄦ湰App鍐呭畬鎴愭彁鐜�"); + redPackParams.setClientIp("106.80.118.163"); + redPackParams.setActName("鑷姩鎻愮幇"); + redPackParams.setKey(key); + redPackParams.setRemark("寰俊鑷姩鎻愮幇"); + // redPackParams.setSceneId(null); + // redPackParams.setRiskInfo(null); + + // 鍙戠敓绾㈠寘鐘舵�� + boolean stateRedPack = false; + + String pwd = "1520950211"; + InputStream cert = ExtractServiceImpl.class.getClassLoader() + .getResourceAsStream("certificate/apiclient_cert.p12"); // 璇诲彇.p12鏂囦欢 + // 鎵ц鍙戞斁绾㈠寘 + try { + String result = WXPayUtil.redPackToOpenId(redPackParams, pwd, cert); + if (!StringUtil.isNullOrEmpty(result)) { + stateRedPack = parseSendResult(result); + } + + if (!stateRedPack) { + String redPackRecord = WXPayUtil.getRedPackRecord(billno, mchId, appId, key, pwd, cert); + if (!StringUtil.isNullOrEmpty(redPackRecord)) { + updeteRecord = parseDTO(redPackRecord); + } + } + } catch (Exception e) { + String redPackRecord = WXPayUtil.getRedPackRecord(billno, mchId, appId, key, pwd, cert); + if (!StringUtil.isNullOrEmpty(redPackRecord)) { + updeteRecord = parseDTO(redPackRecord); + } + LogHelper.errorDetailInfo(e); + } + try { + cert.close(); + } catch (IOException e1) { + e1.printStackTrace(); + } + + // 鏇存柊淇℃伅 + if (updeteRecord == null) { + updeteRecord = new ExtractWeiXinRecord(); + } else { + stateRedPack = true; + } + + updeteRecord.setId(recordId); + updeteRecord.setStatus(ExtractWeiXinRecord.SENDING); + updeteRecord.setBillno(billno); + extractWeiXinRecordService.updateByPrimaryKeySelective(updeteRecord); + + // 绾㈠寘鍙戞斁澶辫触 + if (!stateRedPack) + return; + + // 3銆佸姞鍏ヨ祫閲戞槑缁� + UserMoneyDetail detail = new UserMoneyDetail(); + detail.setCreateTime(new Date()); + detail.setIdentifyCode(StringUtil.Md5(UserMoneyDetailTypeEnum.extractAutoWX.name() + ":" + recordId)); + detail.setMoney(money); + detail.setTitle(UserMoneyDetailTypeEnum.extractAutoWX.getDesc()); + detail.setType(UserMoneyDetailTypeEnum.extractAutoWX); + detail.setUpdateTime(new Date()); + detail.setUserInfo(user); + try { + userMoneyDetailService.addUserMoneyDetail(detail); + } catch (UserMoneyDetailException e) { + LogHelper.errorDetailInfo(e); + } + + } + + private boolean parseSendResult(String result) { + try { + Document document = DocumentHelper.parseText(result); + // 鑾峰彇鏍硅妭鐐瑰厓绱犲璞� + Element root = document.getRootElement(); + Element returnCode = root.element("return_code"); + + if (!"SUCCESS".equalsIgnoreCase(returnCode.getText())) { + Element returnMsg = root.element("return_msg"); + System.out.println(returnMsg.getText()); + return false; + } + + Element resultCode = root.element("result_code"); + if (!"SUCCESS".equalsIgnoreCase(resultCode.getText())) { + Element errCode = root.element("err_code"); + Element errCodeDes = root.element("err_code_des"); + System.out.println(errCode.getText() + ":" + errCodeDes.getText()); + return false; + } + return true; + } catch (DocumentException e) { + e.printStackTrace(); + LogHelper.errorDetailInfo(e); + } + return false; + } + + private ExtractWeiXinRecord parseDTO(String result) { + try { + + Document document = DocumentHelper.parseText(result); + // 鑾峰彇鏍硅妭鐐瑰厓绱犲璞� + Element root = document.getRootElement(); + Element returnCode = root.element("return_code"); + + if (!"SUCCESS".equalsIgnoreCase(returnCode.getText())) { + Element returnMsg = root.element("return_msg"); + System.out.println(returnMsg.getText()); + return null; + } + + Element resultCode = root.element("result_code"); + if (!"SUCCESS".equalsIgnoreCase(resultCode.getText())) { + Element errCode = root.element("err_code"); + Element errCodeDes = root.element("err_code_des"); + System.out.println(errCode.getText() + ":" + errCodeDes.getText()); + } + + ExtractWeiXinRecord resultRecord = new ExtractWeiXinRecord(); + resultRecord.setBillno(root.element("result_code").getText()); + resultRecord.setDetailno(root.element("detail_id").getText()); + resultRecord.setStatus(root.element("status").getText()); + resultRecord.setSendType(root.element("send_type").getText()); + resultRecord.setHbType(root.element("result_code").getText()); + + String totalNum = root.element("total_num").getText(); + resultRecord.setTotalNum(Integer.parseInt(totalNum)); + + String sendTime = root.element("send_time").getText(); + resultRecord.setSendTime(new Date(TimeUtil.convertDateToTemp2(sendTime))); + + Element element = root.element("hblist"); + Iterator<?> iterator = element.elementIterator(); + while (iterator.hasNext()) { + Element childElement = (Element) iterator.next(); + Element rcvTime = childElement.element("rcv_time"); + resultRecord.setRcvTime(new Date(TimeUtil.convertDateToTemp2(rcvTime.getText()))); + break; + } + return resultRecord; + + } catch (DocumentException e) { + e.printStackTrace(); + LogHelper.errorDetailInfo(e); + } + return null; + } + + @Override + public List<UserInfo> preAutoUser() throws Exception { + int page = 0; + int pageSize = 100; + int maxSize = 300; + Date nowDate = new Date(); + // 涓�銆佽嚜鍔ㄦ彁鐜伴拡瀵圭殑鐢ㄦ埛缇わ細 鍓嶆彁鏉′欢:鐢ㄦ埛鍦ㄨ窛浠�60澶╁埌90澶╁唴鏈椿璺冭繃鐨勭敤鎴� + String beganDate = DateUtil.reduceDayTostring(90, nowDate); + String endDate = DateUtil.reduceDayTostring(60, nowDate); + // 鏉′欢3锛氳窛绂讳笂涓�娆℃垚鍔熼鍙栧井淇$孩鍖呭凡缁忚秴杩囦簡30澶╂椂闂达紝鍙湪绗�31澶╁啀娆′笅鍙戯紱 + String receivedDate = DateUtil.reduceDayTostring(30, nowDate); + // 鏉′欢4锛氳窛绂讳笂涓�娆℃湭鎴愬姛棰嗗彇寰俊绾㈠寘宸茬粡瓒呰繃浜�15澶╂椂闂达紝鍙湪绗�16澶╁啀娆′笅鍙戯紱 + String refundDate = DateUtil.reduceDayTostring(15, nowDate); + + // 浣欓鏈�浣庨檺鍒� + BigDecimal minSurplus = BigDecimal.valueOf(Constant.AUTO_EXTRACT_MIN_SURPLUS); + + List<UserInfo> list = new ArrayList<UserInfo>(); + + while (true) { + // 鏌ヨ婊¤冻鏉′欢 1銆�2 鐨剈id + List<UserInfo> listUser = userInfoService.getAutoExtractUser(page * pageSize, pageSize, minSurplus, + beganDate, endDate); + if (listUser == null || listUser.isEmpty()) + break; + + List<Long> listId = new ArrayList<Long>(); + for (UserInfo userInfo : listUser) { + listId.add(userInfo.getId()); + } + + // 鏌ヨ璁板綍鏄惁婊¤冻 + List<Long> listValid = extractWeiXinRecordService.getValidUsers(listId, receivedDate, refundDate); + if (listValid == null || listValid.isEmpty()) { + page++; + continue; + } + + for (Long uid : listValid) { + if (list.size() >= maxSize) + break; + for (UserInfo userInfo : listUser) { + if (list.size() >= maxSize) + break; + + if (userInfo.getId().longValue() == uid.longValue()) { + list.add(userInfo); + break; + } + } + } + + if (list.size() >= maxSize) + break; + + page++; + } + return list; + } + + @Override + public List<String> getAutoExtractOpenIds() throws Exception { + int page = 0; + int pageSize = 100; + int maxSize = 301; // appId + 瀹為檯openid300涓� + Date nowDate = new Date(); + // 涓�銆佽嚜鍔ㄦ彁鐜伴拡瀵圭殑鐢ㄦ埛缇わ細 鍓嶆彁鏉′欢:鐢ㄦ埛鍦ㄨ窛浠�60澶╁埌90澶╁唴鏈椿璺冭繃鐨勭敤鎴� + String beganDate = DateUtil.reduceDayTostring(90, nowDate); + String endDate = DateUtil.reduceDayTostring(60, nowDate); + // 鏉′欢3锛氳窛绂讳笂涓�娆℃垚鍔熼鍙栧井淇$孩鍖呭凡缁忚秴杩囦簡30澶╂椂闂达紝鍙湪绗�31澶╁啀娆′笅鍙戯紱 + String receivedDate = DateUtil.reduceDayTostring(30, nowDate); + // 鏉′欢4锛氳窛绂讳笂涓�娆℃湭鎴愬姛棰嗗彇寰俊绾㈠寘宸茬粡瓒呰繃浜�15澶╂椂闂达紝鍙湪绗�16澶╁啀娆′笅鍙戯紱 + String refundDate = DateUtil.reduceDayTostring(15, nowDate); + + // 鎻愮幇閲戦 + BigDecimal money = BigDecimal.valueOf(Constant.AUTO_EXTRACT_MONEY); + // 浣欓鏈�浣庨檺鍒� + BigDecimal minSurplus = BigDecimal.valueOf(Constant.AUTO_EXTRACT_MIN_SURPLUS); + + List<String> listOpendIDs = new ArrayList<String>(); + // 棣栬appID + listOpendIDs.add(Constant.getWXAccount(null, null).getAppId()); + + while (true) { + // 鏌ヨ婊¤冻鏉′欢 1銆�2 鐨剈id + List<UserInfo> listUser = userInfoService.getAutoExtractUser(page * pageSize, pageSize, minSurplus, + beganDate, endDate); + if (listUser == null || listUser.isEmpty()) + break; + + List<Long> listId = new ArrayList<Long>(); + for (UserInfo userInfo : listUser) { + listId.add(userInfo.getId()); + } + + // 鏌ヨ璁板綍鏄惁婊¤冻 + List<Long> listValid = extractWeiXinRecordService.getValidUsers(listId, receivedDate, refundDate); + if (listValid == null || listValid.isEmpty()) { + page++; + continue; + } + + for (Long uid : listValid) { + UserInfo userInfo = subHongBaoByUid(uid, money, minSurplus); + if (userInfo != null) { + listOpendIDs.add(userInfo.getWxOpenId()); + if (listOpendIDs.size() >= maxSize) + break; + } + } + + if (listOpendIDs.size() >= maxSize) + break; + + page++; + } + return listOpendIDs; + } + + @Override + public List<UserInfo> preAutoUserTo1212() throws Exception { + int page = 0; + int pageSize = 100; + int maxSize = 300; + Date nowDate = new Date(); + // 鏉′欢3锛氳窛绂讳笂涓�娆℃垚鍔熼鍙栧井淇$孩鍖呭凡缁忚秴杩囦簡30澶╂椂闂达紝鍙湪绗�31澶╁啀娆′笅鍙戯紱 + String receivedDate = DateUtil.reduceDayTostring(30, nowDate); + // 鏉′欢4锛氳窛绂讳笂涓�娆℃湭鎴愬姛棰嗗彇寰俊绾㈠寘宸茬粡瓒呰繃浜�15澶╂椂闂达紝鍙湪绗�16澶╁啀娆′笅鍙戯紱 + String refundDate = DateUtil.reduceDayTostring(15, nowDate); + // 浣欓鏈�浣庨檺鍒� + List<UserInfo> list = new ArrayList<UserInfo>(); + while (true) { + // 鏌ヨ婊¤冻鏉′欢 1銆�2 鐨剈id + List<UserInfo> listUser = userInfoService.getAutoExtractUserTo1212(page * pageSize, pageSize); + if (listUser == null || listUser.isEmpty()) + break; + + List<Long> listId = new ArrayList<Long>(); + for (UserInfo userInfo : listUser) { + listId.add(userInfo.getId()); + } + + // 鏌ヨ璁板綍鏄惁婊¤冻 + List<Long> listValid = extractWeiXinRecordService.getValidUsers(listId, receivedDate, refundDate); + if (listValid == null || listValid.isEmpty()) { + page++; + continue; + } + + for (Long uid : listValid) { + if (list.size() >= maxSize) + break; + for (UserInfo userInfo : listUser) { + if (list.size() >= maxSize) + break; + + if (userInfo.getId().longValue() == uid.longValue()) { + list.add(userInfo); + break; + } + } + } + + if (list.size() >= maxSize) + break; + + page++; + } + return list; + } + + @Override + public List<String> getAutoExtractOpenIdsTo1212() throws Exception { + int page = 0; + int pageSize = 100; + int maxSize = 301; // appId + 瀹為檯openid300涓� + Date nowDate = new Date(); + // 鏉′欢3锛氳窛绂讳笂涓�娆℃垚鍔熼鍙栧井淇$孩鍖呭凡缁忚秴杩囦簡30澶╂椂闂达紝鍙湪绗�31澶╁啀娆′笅鍙戯紱 + String receivedDate = DateUtil.reduceDayTostring(30, nowDate); + // 鏉′欢4锛氳窛绂讳笂涓�娆℃湭鎴愬姛棰嗗彇寰俊绾㈠寘宸茬粡瓒呰繃浜�15澶╂椂闂达紝鍙湪绗�16澶╁啀娆′笅鍙戯紱 + String refundDate = DateUtil.reduceDayTostring(15, nowDate); + + // 鎻愮幇閲戦 + BigDecimal money = BigDecimal.valueOf(1); + + List<String> listOpendIDs = new ArrayList<String>(); + // 棣栬appID + listOpendIDs.add(Constant.getWXAccount(null, null).getAppId()); + + while (true) { + // 鏌ヨ婊¤冻鏉′欢 1銆�2 鐨剈id + List<UserInfo> listUser = userInfoService.getAutoExtractUserTo1212(page * pageSize, pageSize); + if (listUser == null || listUser.isEmpty()) + break; + + List<Long> listId = new ArrayList<Long>(); + for (UserInfo userInfo : listUser) { + listId.add(userInfo.getId()); + } + + // 鏌ヨ璁板綍鏄惁婊¤冻 + List<Long> listValid = extractWeiXinRecordService.getValidUsers(listId, receivedDate, refundDate); + if (listValid == null || listValid.isEmpty()) { + page++; + continue; + } + + for (Long uid : listValid) { + UserInfo userInfo = subHongBaoByUid(uid, money, money); + if (userInfo != null) { + listOpendIDs.add(userInfo.getWxOpenId()); + if (listOpendIDs.size() >= maxSize) + break; + } + } + + if (listOpendIDs.size() >= maxSize) + break; + + page++; + } + return listOpendIDs; + } + + @Transactional(rollbackFor = Exception.class) + private UserInfo subHongBaoByUid(Long uid, BigDecimal money, BigDecimal minSurplus) { + UserInfo userInfo = userInfoMapper.selectByPrimaryKeyForUpdate(uid); + if (userInfo != null && !StringUtil.isNullOrEmpty(userInfo.getWxOpenId()) + && !StringUtil.isNullOrEmpty(userInfo.getWxUnionId()) + && userInfo.getMyHongBao().compareTo(minSurplus) >= 0) { + // 寰俊鎻愮幇璁板綍 + ExtractWeiXinRecord extractRecord = new ExtractWeiXinRecord(); + extractRecord.setUid(uid); + extractRecord.setOpenId(userInfo.getWxOpenId()); + extractRecord.setUnionId(userInfo.getWxUnionId()); + extractRecord.setMoney(money); + extractRecord.setStatus(ExtractWeiXinRecord.SENDING); + extractRecord.setType(ExtractWeiXinRecord.TYPE_MANUAL); + extractRecord.setCreateTime(new Date()); + extractRecord.setUpdateTime(new Date()); + extractWeiXinRecordService.insertSelective(extractRecord); + // 璁板綍id + Long recordId = extractRecord.getId(); + + // 璧勯噾鏄庣粏 + UserMoneyDetail detail = new UserMoneyDetail(); + detail.setCreateTime(new Date()); + detail.setIdentifyCode(StringUtil.Md5(UserMoneyDetailTypeEnum.extractAutoWX.name() + ":" + recordId)); + detail.setMoney(new BigDecimal("-" + money.toString())); + detail.setTitle(UserMoneyDetailTypeEnum.extractAutoWX.getDesc()); + detail.setType(UserMoneyDetailTypeEnum.extractAutoWX); + detail.setUpdateTime(new Date()); + detail.setUserInfo(userInfo); + + // 璧勯噾璁$畻 + userMoneyService.subUserMoney(userInfo.getId(), money, detail); + + // 婊¤冻鏉′欢杩斿洖淇℃伅 + return userInfo; + } + return null; + } + + @Override + public void updateManualExtractRecord(List<RedPackRecord> list) throws ExtractException { + if (list == null || list.isEmpty()) + throw new ExtractException(1, "绾㈠寘璁板綍淇℃伅涓虹┖"); + + for (RedPackRecord redPackRecord : list) { + updateWeiXinRecord(redPackRecord); + } + } + + @Transactional(rollbackFor = Exception.class) + private void updateWeiXinRecord(RedPackRecord redPackRecord) { + if (StringUtil.isNullOrEmpty(redPackRecord.getOpenId())) + return; + + ExtractWeiXinRecord record = extractWeiXinRecordService.getByOpenIdAndType(redPackRecord.getOpenId(), + ExtractWeiXinRecord.TYPE_MANUAL); + if (record == null) + return; + + // 鐘舵�佽繃婊� + String statusOld = record.getStatus(); + if (StringUtil.isNullOrEmpty(statusOld) || ExtractWeiXinRecord.REFUND.equals(statusOld) + || ExtractWeiXinRecord.RECEIVED.equals(statusOld)) + return; + + ExtractWeiXinRecord updeteRecord = new ExtractWeiXinRecord(); + updeteRecord.setId(record.getId()); + updeteRecord.setUpdateTime(new Date()); + + String billno = redPackRecord.getBillno(); + if (!StringUtil.isNullOrEmpty(billno)) { + updeteRecord.setBillno(billno.replace("`", "").replace("'", "")); + } + + String detailno = redPackRecord.getDetailno(); + if (!StringUtil.isNullOrEmpty(detailno)) { + updeteRecord.setDetailno(detailno.replace("`", "").replace("'", "")); + } + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); + String rcvTime = redPackRecord.getRcvTime(); + if (!StringUtil.isNullOrEmpty(rcvTime)) { + try { + updeteRecord.setRcvTime(sdf.parse(rcvTime)); + } catch (ParseException e) { + e.printStackTrace(); + } + } + + String sendTime = redPackRecord.getSendTime(); + if (!StringUtil.isNullOrEmpty(sendTime)) { + try { + updeteRecord.setSendTime(sdf.parse(sendTime)); + } catch (ParseException e) { + e.printStackTrace(); + } + } + + String status = redPackRecord.getStatus(); + if (!StringUtil.isNullOrEmpty(status)) { + switch (status) { + case "宸插彂鏀惧緟棰嗗彇": + status = ExtractWeiXinRecord.SENT; + break; + case "杩囨湡鏈閫�娆�": + status = ExtractWeiXinRecord.REFUND; + break; + case "宸查鍙�": + status = ExtractWeiXinRecord.RECEIVED; + break; + default: + status = null; + break; + } + } + updeteRecord.setStatus(status); + extractWeiXinRecordService.updateByPrimaryKeySelective(updeteRecord); + + if (ExtractWeiXinRecord.RECEIVED.equals(status)) { + } else if (ExtractWeiXinRecord.REFUND.equals(status)) { + // 璧勯噾鏄庣粏 + UserMoneyDetail detail = new UserMoneyDetail(); + detail.setCreateTime(new Date()); + detail.setIdentifyCode( + StringUtil.Md5(UserMoneyDetailTypeEnum.extractAutoWXRefund.name() + ":" + record.getId())); + detail.setMoney(record.getMoney()); + detail.setTitle(UserMoneyDetailTypeEnum.extractAutoWXRefund.getDesc()); + detail.setType(UserMoneyDetailTypeEnum.extractAutoWXRefund); + detail.setUpdateTime(new Date()); + detail.setUserInfo(new UserInfo(record.getUid())); + // 璧勯噾璁$畻 + userMoneyService.addUserMoney(record.getUid(), record.getMoney(), detail); + } + } + + + @Override + public List<Extract> getExtractSucceedRecord(int page, int pageSize, Long uid) { + return extractMapper.getExtractSucceedRecord((page-1)*pageSize, pageSize, uid); + } + + @Override + public long countExtractSucceedRecord(Long uid) { + Long count = extractMapper.countExtractSucceedRecord(uid); + return count== null? 0 : count; + } } -- Gitblit v1.8.0