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.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;
|
|
/**
|
* <p>
|
* 公司信息表 服务实现类
|
* </p>
|
*
|
* @author 吕雪
|
* @since 2020-06-06
|
*/
|
@Service
|
public class TCompanyServiceImpl extends ServiceImpl<TCompanyMapper, TCompany> implements ITCompanyService {
|
|
|
@Resource
|
private TCompanyMapper companyMapper;
|
|
|
|
|
@Override
|
public List<Map<String, Object>> getCompanyList(Page<Map<String, Object>> 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<Map<String, Object>> getCompanyScopeById(Integer id) {
|
return this.baseMapper.getCompanyScopeById(id);
|
}
|
|
@Override
|
public List<Map<String, Object>> getFranchiseeList(Page<Map<String, Object>> 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<String, Object> map = new HashMap<>();
|
TCompany companyInfo = this.selectById(id);
|
/*Address address = addressService.selectById((String) companyInfo.getDetailAddress());
|
companyInfo.setAddressId(address);
|
Map<String, List<Region>> regions = null;
|
if(null != address){
|
//查询注册地址对应的行政数据
|
regions = regionService.getRegions(address.getProvince(), address.getCity());
|
}
|
//查询企业的超级管理员
|
EntityWrapper<User> 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<OperateRegion> 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<String, Object> queryOperationalData(Integer type, String start, String end, Integer companyId, Integer offset, Integer limit) throws Exception {
|
Map<String, Object> map = new HashMap<>();
|
List<Map<String, Object>> 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<Map<String, Object>> list = companyMapper.queryOperationalData(type, start, end, companyId, null, null);
|
|
List<List<String>> lists = new ArrayList<>();
|
List<String> 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<List<List<String>>> lists1 = new ArrayList<>();
|
List<List<String>> lists2 = new ArrayList<>();
|
for(Map<String, Object> map : list){
|
List<String> 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<String> 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<Map<String, Object>> getCompanyIncomeList(Page<Map<String, Object>> page, String beginTime, String endTime, String userId) {
|
return this.baseMapper.getCompanyIncomeList(page,beginTime,endTime,userId);
|
}
|
|
@Override
|
public List<Map<String, Object>> 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;
|
|
@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;
|
|
}
|