package com.doumee.task; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.doumee.biz.system.SystemDictDataBiz; import com.doumee.core.utils.Constants; import com.doumee.core.wx.WxPayProperties; import com.doumee.dao.business.model.MonthlySettlement; import com.doumee.dao.system.model.SystemDictData; import com.doumee.service.business.*; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.io.IOException; import java.math.BigDecimal; import java.net.URI; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Map; import java.util.Objects; /** * 定时任务 * @author jiangping * @date 2021-10-10 14:40:35 * https://www.bejson.com/othertools/cron/ cron 表达式生成地址 */ @Component @EnableScheduling public class ScheduleTool { @Autowired private AreasService areasService; @Autowired private AftersaleService aftersaleService; @Autowired private GoodsorderService goodsorderService; @Autowired private WxPayProperties wxPayProperties; @Autowired private SystemDictDataBiz systemDictDataBiz; @Autowired private ActivitySignService activitySignService; @Autowired private MonthlySettlementService monthlySettlementService; @Autowired private IntegralService integralService; /** * 是否开发者 */ @Value("${timing}") private Boolean timing; /** * 缓存省市区 * @throws Exception */ @Scheduled(fixedDelay=24*60*60*1000) public void cacheCampus() throws Exception { areasService.cacheData(); } /** * 定时刷新数据code缓存信 * @throws Exception */ @Scheduled(fixedDelay=24*60*60*1000) public void refreshOrderCodes() throws Exception { goodsorderService.refreshOrderCodes(); System.out.println("定时刷新数据code缓存信息--------Begin------"); goodsorderService.refreshOrderCodes(); System.out.println("定时刷新数据code缓存信息--------end------"); } /** * 每天凌晨重置所有订单code类初始值 * @throws Exception */ @Scheduled(cron="0 0 0 * * ?") public void resetOrderCodes() throws Exception { System.out.println("定时加载数据code缓存信息--------Begin------"); goodsorderService.resetOrderCodes(); System.out.println("定时加载数据code缓存信息--------end------"); } /** * 更新微信 ACCESS_TOKEN */ // @Scheduled(fixedDelay = 1000L * 60L * 90L) public void updAccessToken(){ String appId = wxPayProperties.getAppId(); String appSecret = wxPayProperties.getAppSecret(); //生成微信token String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+appSecret; String response = doGet(url,null); JSONObject json = JSONObject.parseObject(response); SystemDictData systemDictData = new SystemDictData(); systemDictData.setId(106); systemDictData.setCode(json.getString("access_token")); systemDictDataBiz.updateById(systemDictData); } /** * 定时取消 未支付订单信息 */ // @Scheduled(fixedDelay = 1000L * 10L ) public void cancelGoodsOrder(){ goodsorderService.autoCancel(); } /** * 定时完成售后单状态 */ // @Scheduled(fixedDelay = 1000L * 10L ) public void doneAfterSales(){ aftersaleService.autoDone(); } /** * 定时确认 */ // @Scheduled(fixedDelay = 1000L * 10L ) public void autoAffirm(){ goodsorderService.autoAffirm(); } /** * 定时取消 未支付活动参与记录 */ // @Scheduled(fixedDelay = 1000L * 10L ) public void autoCancelSign(){ activitySignService.autoCancelSign(); } /** * 发送get请求 * @param url 请求URL * @param param 请求参数 key:value url携带参数 或者无参可不填 * @return */ public String doGet(String url, Map param) { // 创建Httpclient对象 CloseableHttpClient httpclient = HttpClients.createDefault(); String resultString = ""; CloseableHttpResponse response = null; try { // 创建uri URIBuilder builder = new URIBuilder(url); if (param != null) { for (String key : param.keySet()) { builder.addParameter(key, param.get(key)); } } URI uri = builder.build(); // 创建http GET请求 HttpGet httpGet = new HttpGet(uri); // 执行请求 response = httpclient.execute(httpGet); // 判断返回状态是否为200 if (response.getStatusLine().getStatusCode() == 200) { resultString = EntityUtils.toString(response.getEntity(), "UTF-8"); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (response != null) { response.close(); } httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return resultString; } /** * 更新 月结费用 * 每月一号处理数据 */ @Scheduled(cron = "0 1 0 1 * ?") public void monthlySettlement(){ if(timing) { //定义日期显示格式 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); Calendar c = Calendar.getInstance(); Integer nowmonth=c.get(Calendar.MONTH)+1;//当前月份 Integer lastmonth=c.get(Calendar.MONTH) ;//上个月 //获取上月的月末金额 MonthlySettlement lastMonthlySettlement = monthlySettlementService.findOne(new MonthlySettlement()); //获取期初收支金额(获取上个月份的总余额) BigDecimal nowMonthIn = integralService.getMonthlyMoney(); if( Objects.nonNull(lastMonthlySettlement)){ if(Constants.equalsInteger(lastMonthlySettlement.getMonth(),lastmonth)){ MonthlySettlement updatemonth=new MonthlySettlement(); updatemonth.setId(lastMonthlySettlement.getId()); updatemonth.setEndMoney(nowMonthIn); monthlySettlementService.update(updatemonth); //加入当前月份的数据 MonthlySettlement monthlySettlement=new MonthlySettlement(); monthlySettlement.setCreateTime(new Date()); monthlySettlement.setYear(c.get(Calendar.YEAR)); monthlySettlement.setMonth( c.get(Calendar.MONTH) + 1); monthlySettlement.setBeginMoney(nowMonthIn); monthlySettlementService.intsert(monthlySettlement); }else if(Constants.equalsInteger(lastMonthlySettlement.getMonth(),nowmonth)){ MonthlySettlement monthlySettlement=new MonthlySettlement(); monthlySettlement.setId(lastMonthlySettlement.getId()); monthlySettlement.setBeginMoney(nowMonthIn); monthlySettlementService.update(monthlySettlement); }else{ MonthlySettlement monthlySettlement=new MonthlySettlement(); monthlySettlement.setCreateTime(new Date()); monthlySettlement.setYear(c.get(Calendar.YEAR)); monthlySettlement.setMonth( c.get(Calendar.MONTH) + 1); monthlySettlement.setBeginMoney(nowMonthIn); monthlySettlementService.intsert(monthlySettlement); } }else{ MonthlySettlement monthlySettlement=new MonthlySettlement(); monthlySettlement.setCreateTime(new Date()); monthlySettlement.setYear(c.get(Calendar.YEAR)); monthlySettlement.setMonth( c.get(Calendar.MONTH) + 1); monthlySettlement.setBeginMoney(nowMonthIn); monthlySettlementService.intsert(monthlySettlement); } } } }