package com.stylefeng.guns.modular.system.service.impl; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.plugins.Page; import com.baomidou.mybatisplus.service.impl.ServiceImpl; import com.stylefeng.guns.core.util.ToolUtil; import com.stylefeng.guns.modular.cloudPayment.example.AllocationExample; import com.stylefeng.guns.modular.cloudPayment.example.WithdrawalExample; import com.stylefeng.guns.modular.huifu.biz.FenZhangUtil; import com.stylefeng.guns.modular.system.controller.util.Base64Util; import com.stylefeng.guns.modular.system.dao.TCompanyMapper; import com.stylefeng.guns.modular.system.dao.TEnterpriseWithdrawalMapper; import com.stylefeng.guns.modular.system.model.*; import com.stylefeng.guns.modular.system.service.*; import com.stylefeng.guns.modular.system.util.RedisUtil; import com.stylefeng.guns.modular.system.util.ResultUtil; import com.unionpay.upyzt.exception.UpyztException; import com.unionpay.upyzt.resp.AllocationResp; import com.unionpay.upyzt.resp.WithdrawalResp; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.ZoneId; import java.time.temporal.ChronoUnit; import java.util.*; import java.util.stream.Collectors; /** *

* 公司信息表 服务实现类 *

* * @author 吕雪 * @since 2020-06-06 */ @Service public class TCompanyServiceImpl extends ServiceImpl implements ITCompanyService { @Resource private TCompanyMapper companyMapper; @Override public List> getCompanyList(Page> page, String beginTime, String endTime, String name, String principalName, String principalPhone, String adminName, String adminPhone, String serviceStr, Integer state) { return this.baseMapper.getCompanyList(page, beginTime, endTime, name, principalName, principalPhone, adminName, adminPhone, serviceStr, state); } @Override public List> getCompanyScopeById(Integer id) { return this.baseMapper.getCompanyScopeById(id); } @Override public List> getFranchiseeList(Page> page, String beginTime, String endTime, String name, String account, String principalName, String principalPhone, String serviceStr, Integer state, Integer roleType, Integer nowUserId) { return this.baseMapper.getFranchiseeList(page, beginTime, endTime, name, account, principalName, principalPhone, serviceStr, state, roleType, nowUserId); } @Override public ResultUtil selectCompanyInfoById(String id) throws Exception { Map map = new HashMap<>(); TCompany companyInfo = this.selectById(id); /*Address address = addressService.selectById((String) companyInfo.getDetailAddress()); companyInfo.setAddressId(address); Map> regions = null; if(null != address){ //查询注册地址对应的行政数据 regions = regionService.getRegions(address.getProvince(), address.getCity()); } //查询企业的超级管理员 EntityWrapper entityWrapper = new EntityWrapper<>(); entityWrapper.eq("is_admin", "0"); entityWrapper.eq("company_info_id", companyInfo.getCompanyId()); entityWrapper.ne("status", 3); User user = userService.selectOne(entityWrapper); //查询企业的经营范围 List OperateRegions = operateRegionService.getDataByCompanyInfoId(companyInfo.getCompanyId()); map.put("companyInfo", companyInfo); map.put("region", regions); map.put("OperateRegion", OperateRegions); map.put("admin", user);*/ return ResultUtil.success(map); } /** * 获取运营汇总数据 * @param type * @param start * @param end * @param companyId * @param offset * @param limit * @return * @throws Exception */ @Override public Map queryOperationalData(Integer type, String start, String end, Integer companyId, Integer offset, Integer limit) throws Exception { Map map = new HashMap<>(); List> list = companyMapper.queryOperationalData(type, start, end, companyId, offset, limit); int i = companyMapper.queryOperationalDataCount(type, start, end, companyId); map.put("rows", list); map.put("total", i); return map; } /** * 下载运营汇总数据 * @param type * @param companyId * @return * @throws Exception */ @Override public HSSFWorkbook downloadOperationalData(Integer type, String start, String end, Integer companyId) throws Exception { List> list = companyMapper.queryOperationalData(type, start, end, companyId, null, null); List> lists = new ArrayList<>(); List list1 = new ArrayList<>(); list1.add("类别:"); list1.add(type == 1 ? "快车" : type == 2 ? "出租车" : type == 3 ? "城际" : "小件物流"); list1.add(""); list1.add(""); list1.add("起止时间:"); list1.add(null == start || "".equals(start) ? "" : start + " ~ " + end); list1.add(""); list1.add(""); list1.add("运营商:"); String company = ""; if (null != companyId) { company = companyMapper.selectById(companyId).getName(); } list1.add(company); list1.add(""); list1.add(""); lists.add(list1); list1 = new ArrayList<>(); list1.add("日期"); list1.add("用户注册数量"); list1.add("在线司机数"); list1.add("订单情况"); list1.add(""); list1.add(""); list1.add("支付情况"); list1.add(""); list1.add(""); list1.add(""); list1.add(""); list1.add("投诉数量"); lists.add(list1); list1 = new ArrayList<>(); list1.add(""); list1.add(""); list1.add(""); list1.add("下单数量"); list1.add("乘客取消数量"); list1.add("取消占比"); list1.add("通过平台支付(单)"); list1.add("通过其它支付(单)"); list1.add("合计"); list1.add("平台支付占比%"); list1.add("金额"); list1.add(""); lists.add(list1); List>> lists1 = new ArrayList<>(); List> lists2 = new ArrayList<>(); for (Map map : list) { List list2 = new ArrayList<>(); list2.add(null != map.get("time") ? map.get("time").toString() : ""); list2.add(null != map.get("register") ? map.get("register").toString() : ""); list2.add(null != map.get("online") ? map.get("online").toString() : ""); list2.add(null != map.get("orderNum") ? map.get("orderNum").toString() : ""); list2.add(null != map.get("cancelNum") ? map.get("cancelNum").toString() : ""); list2.add(null != map.get("cancelProportion") ? map.get("cancelProportion").toString() : ""); list2.add(null != map.get("onlinePay") ? map.get("onlinePay").toString() : ""); list2.add(null != map.get("offlinePay") ? map.get("offlinePay").toString() : ""); list2.add(null != map.get("total") ? map.get("total").toString() : ""); list2.add(null != map.get("payProportion") ? map.get("payProportion").toString() : ""); list2.add(null != map.get("money") ? map.get("money").toString() : ""); list2.add(null != map.get("complaint") ? map.get("complaint").toString() : ""); lists2.add(list2); } lists1.add(lists2); HSSFWorkbook hssfWorkbook = new HSSFWorkbook(); HSSFSheet hssfSheet = hssfWorkbook.createSheet(); hssfSheet.setColumnWidth(0, 6 * 256); hssfSheet.setDefaultRowHeightInPoints(20f); for (int i = 0; i < lists.size(); i++) { HSSFRow hssfRow = hssfSheet.createRow(i);//设置第一行数据(标题) HSSFCellStyle style = hssfWorkbook.createCellStyle(); HSSFFont font = hssfWorkbook.createFont(); // font.setBold(true); style.setFont(font); // style.setAlignment(HorizontalAlignment.CENTER); for (int l = 0; l < lists.get(i).size(); l++) { HSSFCell hssfCell = hssfRow.createCell(l); // hssfCell.setCellType(CellType.STRING);//设置表格类型 hssfCell.setCellValue(lists.get(i).get(l)); hssfCell.setCellStyle(style); if (l > 0) { hssfSheet.setColumnWidth(l, 20 * 256); } } } //这个就是合并单元格 //参数说明:1:开始行 2:结束行 3:开始列 4:结束列 //比如我要合并 第二行到第四行的 第六列到第八列 sheet.addMergedRegion(new CellRangeAddress(1,3,5,7)); hssfSheet.addMergedRegion(new CellRangeAddress(0, 0, 1, 3)); hssfSheet.addMergedRegion(new CellRangeAddress(0, 0, 5, 7)); hssfSheet.addMergedRegion(new CellRangeAddress(0, 0, 9, 10)); hssfSheet.addMergedRegion(new CellRangeAddress(1, 2, 0, 0)); hssfSheet.addMergedRegion(new CellRangeAddress(1, 2, 1, 1)); hssfSheet.addMergedRegion(new CellRangeAddress(1, 2, 2, 2)); hssfSheet.addMergedRegion(new CellRangeAddress(1, 1, 3, 5)); hssfSheet.addMergedRegion(new CellRangeAddress(1, 1, 6, 10)); hssfSheet.addMergedRegion(new CellRangeAddress(1, 2, 11, 11)); for (int i = 0; i < lists1.size(); i++) { //将数据添加到表格中 List data = null; for (int l = 0; l < lists1.get(i).size(); l++) { HSSFRow row = hssfSheet.createRow(l + 3); data = lists1.get(i).get(l); for (int j = 0; j < data.size(); j++) { HSSFCell hssfCell = row.createCell(j); // hssfCell.setCellType(CellType.STRING);//设置表格类型 hssfCell.setCellValue(data.get(j)); } } } return hssfWorkbook; } @Override public List> getCompanyIncomeList(Page> page, String beginTime, String endTime, String userId) { return this.baseMapper.getCompanyIncomeList(page, beginTime, endTime, userId); } @Override public List> getCompanyIncomeListEx(String beginTime, String endTime, String userId) { return this.baseMapper.getCompanyIncomeListEx(beginTime, endTime, userId); } @Override public Double getSumCompanyIncomeMoney(String beginTime, String endTime, String userId) { return this.baseMapper.getSumCompanyIncomeMoney(beginTime, endTime, userId); } @Resource private TEnterpriseWithdrawalMapper enterpriseWithdrawalMapper; @Resource private IIncomeService incomeService; @Resource private ITDriverService driverService; @Resource private IUserWithdrawalService userWithdrawalService; @Resource private ITDriverPayDaysService driverPayDaysService; @Resource private RedisUtil redisUtil; @Override public void updateMoney() { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); // 平台 // TCompany company1 = baseMapper.selectList(new EntityWrapper().eq("type", 1)).get(0); // TEnterpriseWithdrawal enterpriseWithdrawal = enterpriseWithdrawalMapper.selectList(new EntityWrapper().eq("companyId", company1.getId())).get(0); // 转出 电子账簿id // String balanceAcctId = enterpriseWithdrawal.getBalanceAcctId(); // String code = enterpriseWithdrawal.getTransactionAuthorizationCode(); // code = Base64Util.decode(code); // 获取分公司分账时间 // 获取所有分公司 List companies = baseMapper.selectList(new EntityWrapper().eq("type", 2)); // 获取所有未分账的信息 List state = incomeService.selectList(new EntityWrapper().eq("state", 0).eq("category",1)); for (TCompany company : companies) { Integer paymentDays = company.getPaymentDays(); LocalDate localDate = new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); System.out.println(localDate); LocalDate localDate1 = company.getUpdateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); System.out.println(localDate1); long between = ChronoUnit.DAYS.between(localDate, localDate1); System.out.println(between); if (between % paymentDays == 0) { // 开始分账 System.out.println("-----------分公司开始分账----------"); // 获取当前收入电子账簿id List enterpriseWithdrawal1 = enterpriseWithdrawalMapper.selectList(new EntityWrapper().eq("type",1).eq("companyId", company.getId())); if (enterpriseWithdrawal1.size() > 0) { // 获取当前公司未分账的金额 Double sum = state.stream().filter(e -> e.getUserType() == 1 && company.getId().equals(e.getObjectId())).mapToDouble(Income::getMoney).sum(); System.out.println("金额 分:" + sum); System.out.println("公司id:" + company.getId()); if (sum == null || sum == 0) { continue; } // Double v = sum * 100; // int money = v.intValue(); TEnterpriseWithdrawal enterpriseWithdrawal2 = enterpriseWithdrawal1.get(0); // String balanceAcctId1 = enterpriseWithdrawal2.getBalanceAcctId(); // AllocationReq allocationReq = new AllocationReq(); // allocationReq.setOutOrderNo(ToolUtil.getRandomString(32)); // allocationReq.setPayBalanceAcctId(balanceAcctId); // allocationReq.setRecvBalanceAcctId(balanceAcctId1); // allocationReq.setAmount(money); // allocationReq.setPassword(code); // allocationReq.setProductCount(1); // allocationReq.setProductName("分公司分账"); // allocationReq.setOrderNo(ToolUtil.getRandomString(32)); // allocationReq.setOrderAmount(1l); String req_seq_id = ToolUtil.getRandomString(32); String seq_date = simpleDateFormat.format(new Date()); try { // AllocationResp allocationResp = AllocationExample.create(allocationReq); BigDecimal sum1 = new BigDecimal(sum).setScale(2, RoundingMode.HALF_UP); Map fenzhang = FenZhangUtil.fenzhang(req_seq_id, seq_date, String.valueOf(sum1), enterpriseWithdrawal2.getHuifuId()); String resp_code = fenzhang.get("resp_code").toString(); if(!"00000000".equals(resp_code)){ continue; } System.out.println(company.getId() + "::" + company.getName() + "-----分账状态" + fenzhang.toString() + "--------------"); if (!"S".equals(fenzhang.get("trans_stat").toString())) { List collect = state.stream().filter(e -> e.getUserType() == 1 && company.getId().equals(e.getObjectId())).collect(Collectors.toList()); collect.stream().forEach(e -> { e.setReqSeqId(req_seq_id); e.setReqDate(seq_date); }); incomeService.updateBatchById(collect); } if ("S".equals(fenzhang.get("trans_stat").toString())) { List collect = state.stream().filter(e -> e.getUserType() == 1 && company.getId().equals(e.getObjectId())).collect(Collectors.toList()); collect.stream().forEach(e -> { e.setState(1); e.setReqDate(seq_date); }); incomeService.updateBatchById(collect); } } catch (Exception e) { e.printStackTrace(); } } } } // 司机分账 List collect2 = userWithdrawalService.selectList(new EntityWrapper().eq("type", 1)).stream().map(e -> e.getUserId()).collect(Collectors.toList()); if (collect2.size() == 0) { collect2.add(0); } // 获取所有司机 List tDrivers = driverService.selectList(new EntityWrapper().in("id", collect2)); // 所有司机公司的分账时间 List tDriverPayDays = driverPayDaysService.selectList(null); redisUtil.setStrValue("获取的司机:" + new Date(), tDriverPayDays.toString()); for (TDriverPayDays tDriverPayDay : tDriverPayDays) { Integer day = tDriverPayDay.getDay(); LocalDate localDate = new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); LocalDate localDate1 = tDriverPayDay.getUpdateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); long between = ChronoUnit.DAYS.between(localDate, localDate1); if (between % day == 0) { // 当前公司的司机 List collect = tDrivers.stream().filter(e -> e.getCompanyId() == tDriverPayDay.getCompanyId()).collect(Collectors.toList()); for (TDriver tDriver : collect) { // 获取司机的电子账簿 List userWithdrawals = userWithdrawalService.selectList(new EntityWrapper() .eq("userId",tDriver.getId()) .eq("userType",2).eq("type",1)); if (userWithdrawals.size() > 0) { // 当前司机分账金额 Double sum = state.stream().filter(e -> (e.getUserType() == 2 && tDriver.getId().equals(e.getObjectId()))).mapToDouble(Income::getMoney).sum(); if (sum == null || sum == 0) { continue; } String req_seq_id = ToolUtil.getRandomString(32); String seq_date = simpleDateFormat.format(new Date()); UserWithdrawal userWithdrawal = userWithdrawals.get(0); // String balanceAcctId1 = userWithdrawal.getBalanceAcctId(); // AllocationReq allocationReq = new AllocationReq(); // allocationReq.setOutOrderNo(); // allocationReq.setPayBalanceAcctId(balanceAcctId); // allocationReq.setRecvBalanceAcctId(balanceAcctId1); // allocationReq.setAmount(money); // allocationReq.setProductName("司机分账"); // allocationReq.setProductCount(1); // allocationReq.setOrderNo(ToolUtil.getRandomString(32)); // allocationReq.setOrderAmount(1l); // allocationReq.setPassword(code); try { Map fenzhang = FenZhangUtil.fenzhang(req_seq_id, seq_date, String.valueOf(sum), userWithdrawal.getHuifuId()); // AllocationResp allocationResp = AllocationExample.create(allocationReq); System.out.println(tDriver.getId() + "::" + tDriver.getName() + "-----分账状态" + fenzhang.toString() + "--------------"); String resp_code = fenzhang.get("resp_code").toString(); if(!"00000000".equals(resp_code)){ continue; } if (!"S".equals(fenzhang.get("trans_stat").toString())) { List collect1 = state.stream().filter(e -> e.getUserType() == 2 && tDriver.getId().equals(e.getObjectId())).collect(Collectors.toList()); collect1.stream().forEach(e -> { e.setReqSeqId(req_seq_id); e.setReqDate(seq_date); }); incomeService.updateBatchById(collect1); } if ("S".equals(fenzhang.get("trans_stat").toString())) { List collect1 = state.stream().filter(e -> e.getUserType() == 2 && tDriver.getId().equals(e.getObjectId())).collect(Collectors.toList()); collect1.stream().forEach(e -> { e.setState(1); e.setReqDate(seq_date); }); incomeService.updateBatchById(collect1); } } catch (Exception e) { e.printStackTrace(); } } } } } } @Override public void aaa() throws UpyztException { AllocationResp allocationResp = AllocationExample.retrieveById("3008203473415955501"); System.out.println(allocationResp); } @Override public void taskDayFenzhang() { List state = incomeService.selectList(new EntityWrapper().eq("state", 0).isNotNull("reqSeqId")); // 分组 Set collect = state.stream().map(Income::getReqSeqId).collect(Collectors.toSet()); for (String s : collect) { try { Income income = state.stream().filter(e -> e.getReqSeqId().equals(s)).findFirst().orElse(null); if (income != null) { Map map = FenZhangUtil.queryFenzhang(income.getReqSeqId(), income.getReqDate()); System.out.println("分账定时查询:" + map.toString()); if ("S".equals(map.get("trans_stat").toString())) { companyMapper.updateState(s); } } // AllocationResp allocationResp = AllocationExample.retrieveById(s); // // if("succeeded".equals(allocationResp.getStatus())){ // companyMapper.updateState(s); // } } catch (Exception e) { e.printStackTrace(); } } } @Autowired private ITCompanyWithdrawService tCompanyWithdrawService; @Autowired private ITPubWithdrawalService tPubWithdrawalService; @Override public void taskTixian() { // 获取公司提现信息 List tCompanyWithdraws = tCompanyWithdrawService.selectList(new EntityWrapper().eq("status", 0).isNotNull("withdrawId")); for (TCompanyWithdraw tCompanyWithdraw : tCompanyWithdraws) { try { WithdrawalResp withdrawalResp = WithdrawalExample.retrieveById(tCompanyWithdraw.getWithdrawId()); if ("succeeded".equals(withdrawalResp.getStatus())) { tCompanyWithdraw.setStatus(1); } else if ("failed".equals(withdrawalResp.getStatus())) { tCompanyWithdraw.setStatus(2); tCompanyWithdraw.setRemark(withdrawalResp.getReason()); } tCompanyWithdrawService.updateById(tCompanyWithdraw); } catch (UpyztException e) { e.printStackTrace(); } } List tPubWithdrawals = tPubWithdrawalService.selectList(new EntityWrapper().eq("state", 1).isNotNull("withdrawId")); for (TPubWithdrawal tPubWithdrawal : tPubWithdrawals) { try { WithdrawalResp withdrawalResp = WithdrawalExample.retrieveById(tPubWithdrawal.getWithdrawId()); if ("failed".equals(withdrawalResp.getStatus())) { tPubWithdrawal.setState(3); tPubWithdrawal.setRemark(withdrawalResp.getReason()); if (tPubWithdrawal.getUserType() == 1) { TUser tUser = userService.selectById(tPubWithdrawal.getUserId()); tUser.setBalance(tUser.getBalance().add(tPubWithdrawal.getMoney())); userService.updateById(tUser); } else { TDriver driver = driverService.selectById(tPubWithdrawal.getUserId()); driver.setLaveBusinessMoney(new BigDecimal(driver.getLaveBusinessMoney()).add(tPubWithdrawal.getMoney()).doubleValue()); driverService.updateById(driver); } } else if ("succeeded".equals(withdrawalResp.getStatus())) { tPubWithdrawal.setState(2); tPubWithdrawal.setSuccessTime(new Date()); } tPubWithdrawalService.updateById(tPubWithdrawal); } catch (UpyztException e) { e.printStackTrace(); } } } @Autowired private ITUserService userService; }