package com.supersavedriving.user.modular.system.util; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.supersavedriving.user.modular.system.model.*; import com.supersavedriving.user.modular.system.service.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.Date; import java.util.List; import java.util.Objects; /** * 定时任务工具类 */ @Component public class TaskUtil { @Autowired private IUserToCouponService userToCouponService; @Resource private ICompanyFundFlowService companyFundFlowService; @Autowired private IBranchOfficeService branchOfficeService; @Autowired private IDriverService driverService; @Autowired private ISystemConfigService systemConfigService; @Autowired private IAccountChangeDetailService accountChangeDetailService; @Autowired private IRevenueService revenueService; /** * 每隔一分钟去处理的定时任务 */ @Scheduled(fixedRate = 1000 * 60) public void taskMinute(){ try { } catch (Exception e) { e.printStackTrace(); } } /** * 每天的凌晨执行的任务 */ @Scheduled(cron = "0 0 0 * * *") public void taskDay(){ try { userToCouponService.delUserCoupon(); }catch (Exception e){ e.printStackTrace(); } } /** * 每月1日凌晨执行的任务 */ @Scheduled(cron = "0 0 0 1 * *") public void taskMonth1(){ try { List drivers = driverService.selectList(new EntityWrapper().eq("status", 1)); for (Driver driver : drivers) { SystemConfig systemConfig3= systemConfigService.selectOne(new EntityWrapper().eq("type", 3) .eq("companyType",1) .eq("companyId",1) .eq("branchOfficeId",driver.getBranchOfficeId()) .last("LIMIT 1")); if(Objects.isNull(systemConfig3)){ continue; } JSONObject jsonObject3 = JSON.parseObject(systemConfig3.getContent()); // 按单分佣 Double nu2 = jsonObject3.getDouble("num2"); Double nu4 = jsonObject3.getDouble("num4"); //处理平台抽佣 if(Objects.nonNull(nu2) && nu2 > 0 && nu4 == 1){ //先平台抽佣 // num4_ = (num3 >= num4_ ? num4_ : num3); Revenue revenue = new Revenue(); revenue.setType(2); revenue.setUserType(4); revenue.setUserId(1); // revenue.setOrderId(order.getId()); revenue.setAmount(nu2); revenue.setCreateTime(new Date()); revenueService.insert(revenue); Double balance = revenueService.queryCompanyBalance(); //记录企业流水 CompanyFundFlow companyFundFlow = new CompanyFundFlow(); companyFundFlow.setType(3); companyFundFlow.setObjectType(1); companyFundFlow.setObjectId(null); companyFundFlow.setBalance(new BigDecimal(balance)); companyFundFlow.setMoney(new BigDecimal(nu2)); companyFundFlow.setCreateTime(new Date()); companyFundFlowService.insert(companyFundFlow); } SystemConfig systemConfig = systemConfigService.selectOne(new EntityWrapper().eq("type", 3) .eq("companyType",2) .eq("companyId",driver.getBranchOfficeId()) .last("LIMIT 1")); if(Objects.nonNull(systemConfig)){ JSONObject jsonObject = JSON.parseObject(systemConfig.getContent()); Double num2 = jsonObject.getDouble("num2"); Double num4 = jsonObject.getDouble("num4"); //分公司分佣 if(Objects.nonNull(num2) && num2 > 0 && num4 == 1){ Revenue revenue = new Revenue(); revenue.setType(2); revenue.setUserType(5); revenue.setUserId(driver.getBranchOfficeId()); // revenue.setOrderId(order.getId()); revenue.setAmount(num2); revenue.setCreateTime(new Date()); revenueService.insert(revenue); Double balance = revenueService.queryBranchOfficeBalance(driver.getAgentId()); //记录企业流水 CompanyFundFlow companyFundFlow = new CompanyFundFlow(); companyFundFlow.setType(3); companyFundFlow.setObjectType(3); companyFundFlow.setObjectId(driver.getBranchOfficeId()); companyFundFlow.setBalance(new BigDecimal(balance)); companyFundFlow.setMoney(new BigDecimal(num2)); companyFundFlow.setCreateTime(new Date()); companyFundFlowService.insert(companyFundFlow); Driver driver1 = driverService.selectById(driver.getId()); if (driver1.getVipEndDate()==null||new Date().after(driver1.getVipEndDate())) { AccountChangeDetail accountChangeDetail = new AccountChangeDetail(); accountChangeDetail.setCode(System.currentTimeMillis() + UUIDUtil.getNumberRandom(5)); accountChangeDetail.setUserType(2); accountChangeDetail.setUserId(driver.getId()); accountChangeDetail.setType(1); accountChangeDetail.setChangeType(9); accountChangeDetail.setOldData(new BigDecimal(driver.getBalance()).add(new BigDecimal(driver.getBackgroundBalance())).setScale(2, RoundingMode.HALF_EVEN).doubleValue()); driver.setBalance(driver.getBalance() - num2); accountChangeDetail.setNewData(new BigDecimal(driver.getBalance()).add(new BigDecimal(driver.getBackgroundBalance())).setScale(2, RoundingMode.HALF_EVEN).doubleValue()); accountChangeDetail.setExplain("分公司抽成"); accountChangeDetail.setCreateTime(new Date()); accountChangeDetailService.insert(accountChangeDetail); driverService.updateById(driver); } } } } }catch (Exception e){ e.printStackTrace(); } } /** * 每月16日凌晨执行的任务 */ @Scheduled(cron = "0 0 0 16 * *") public void taskMonth16(){ try { }catch (Exception e){ e.printStackTrace(); } } }