Pu Zhibing
4 天以前 25cf266e99714ac5d428e66b40ce272325c91dd8
ManagementQYTTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/TCompanyServiceImpl.java
@@ -1,38 +1,20 @@
package com.stylefeng.guns.modular.system.service.impl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
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.cloudPayment.req.AllocationReq;
import com.stylefeng.guns.modular.system.controller.util.Base64Util;
import com.stylefeng.guns.modular.system.dao.IncomeMapper;
import com.stylefeng.guns.modular.system.dao.TEnterpriseWithdrawalMapper;
import com.stylefeng.guns.modular.system.model.*;
import com.stylefeng.guns.modular.system.dao.TCompanyMapper;
import com.stylefeng.guns.modular.system.service.*;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.system.dao.TCompanyMapper;
import com.stylefeng.guns.modular.system.model.TCompany;
import com.stylefeng.guns.modular.system.service.ITCompanyService;
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.usermodel.CellType;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
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.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * <p>
@@ -262,238 +244,5 @@
    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;
    @Override
    public void updateMoney() {
        // 平台
        TCompany company1 = baseMapper.selectList(new EntityWrapper<TCompany>().eq("type", 1)).get(0);
        TEnterpriseWithdrawal enterpriseWithdrawal = enterpriseWithdrawalMapper.selectList(new EntityWrapper<TEnterpriseWithdrawal>().eq("companyId", company1.getId())).get(0);
        // 转出 电子账簿id
        String balanceAcctId = enterpriseWithdrawal.getBalanceAcctId();
        String code = enterpriseWithdrawal.getTransactionAuthorizationCode();
         code = Base64Util.decode(code);
        // 获取分公司分账时间
        // 获取所有分公司
        List<TCompany> companies = baseMapper.selectList(new EntityWrapper<TCompany>().eq("type", 2));
        // 获取所有未分账的信息
        List<Income> state = incomeService.selectList(new EntityWrapper<Income>().eq("state", 0));
        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<TEnterpriseWithdrawal> enterpriseWithdrawal1 = enterpriseWithdrawalMapper.selectList(new EntityWrapper<TEnterpriseWithdrawal>().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);
                    try {
                        AllocationResp allocationResp = AllocationExample.create(allocationReq);
                        System.out.println(company.getId()+"::"+company.getName()+"-----分账状态"+allocationResp.getStatus()+"--------------");
                        if("processing".equals(allocationResp.getStatus())){
                            List<Income> collect = state.stream().filter(e -> e.getUserType() == 1 && company.getId().equals(e.getObjectId())).collect(Collectors.toList());
                            collect.stream().forEach(e->e.setAllNo(allocationResp.getAllocationId()));
                            incomeService.updateBatchById(collect);
                        }
                        if("succeeded".equals(allocationResp.getStatus())){
                            List<Income> collect = state.stream().filter(e -> e.getUserType() == 1 && company.getId().equals(e.getObjectId())).collect(Collectors.toList());
                            collect.stream().forEach(e->e.setState(1));
                            incomeService.updateBatchById(collect);
                        }
                    } catch (UpyztException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        // 司机分账
        List<String> collect2 = userWithdrawalService.selectList(new EntityWrapper<UserWithdrawal>().eq("applicationStatus", "succeeded")).stream().map(e -> e.getPhone()).collect(Collectors.toList());
        if(collect2.size()==0){
            collect2.add("0");
        }
        // 获取所有司机
        List<TDriver> tDrivers = driverService.selectList(new EntityWrapper<TDriver>().in("phone",collect2));
        // 所有司机公司的分账时间
        List<TDriverPayDays> tDriverPayDays = driverPayDaysService.selectList(null);
        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<TDriver> collect = tDrivers.stream().filter(e -> e.getCompanyId() == tDriverPayDay.getCompanyId()).collect(Collectors.toList());
                for (TDriver tDriver : collect) {
                    // 获取司机的电子账簿
                    List<UserWithdrawal> userWithdrawals = userWithdrawalService.selectList(new EntityWrapper<UserWithdrawal>().eq("phone", tDriver.getPhone()).eq("applicationStatus", "succeeded"));
                    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;
                        }
                        Double v = sum * 100;
                        int money = v.intValue();
                        UserWithdrawal userWithdrawal = userWithdrawals.get(0);
                        String balanceAcctId1 = userWithdrawal.getBalanceAcctId();
                        AllocationReq allocationReq = new AllocationReq();
                        allocationReq.setOutOrderNo(ToolUtil.getRandomString(32));
                        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 {
                            AllocationResp allocationResp = AllocationExample.create(allocationReq);
                            System.out.println(tDriver.getId()+"::"+tDriver.getName()+"-----分账状态"+allocationResp.getStatus()+"--------------");
                            if("processing".equals(allocationResp.getStatus())){
                                List<Income> collect1 = state.stream().filter(e -> e.getUserType() == 2 && tDriver.getId().equals(e.getObjectId()) ).collect(Collectors.toList());
                                collect1.stream().forEach(e->e.setAllNo(allocationResp.getAllocationId()));
                                incomeService.updateBatchById(collect1);
                            }
                            if("succeeded".equals(allocationResp.getStatus())){
                                List<Income> collect1 = state.stream().filter(e -> e.getUserType() == 2 && tDriver.getId().equals(e.getObjectId()) ).collect(Collectors.toList());
                                collect1.stream().forEach(e->e.setState(1));
                                incomeService.updateBatchById(collect1);
                            }
                        } catch (UpyztException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
    @Override
    public void aaa() throws UpyztException {
        AllocationResp allocationResp = AllocationExample.retrieveById("3008203473415955501");
        System.out.println(allocationResp);
    }
    @Override
    public void taskDayFenzhang() {
        List<Income> state = incomeService.selectList(new EntityWrapper<Income>().eq("state", 0).isNotNull("allNo"));
        List<String> collect = state.stream().map(Income::getAllNo).distinct().collect(Collectors.toList());
        for (String s : collect) {
            try {
                AllocationResp allocationResp = AllocationExample.retrieveById(s);
                System.out.println("分账定时查询:"+allocationResp);
                if("succeeded".equals(allocationResp.getStatus())){
                    companyMapper.updateState(s);
                }
            } catch (UpyztException e) {
                e.printStackTrace();
            }
        }
    }
    @Autowired
    private ITCompanyWithdrawService tCompanyWithdrawService;
    @Autowired
    private ITPubWithdrawalService tPubWithdrawalService;
    @Override
    public void taskTixian() {
        // 获取公司提现信息
        List<TCompanyWithdraw> tCompanyWithdraws = tCompanyWithdrawService.selectList(new EntityWrapper<TCompanyWithdraw>().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<TPubWithdrawal> tPubWithdrawals = tPubWithdrawalService.selectList(new EntityWrapper<TPubWithdrawal>().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;
}