| package com.matrix.system.hive.statistics; | 
|   | 
|   | 
| import com.matrix.core.tools.DateUtil; | 
| import com.matrix.core.tools.LogUtil; | 
| import com.matrix.system.constance.Dictionary; | 
| import com.matrix.system.hive.bean.*; | 
| import com.matrix.system.hive.dao.*; | 
|   | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.scheduling.annotation.Scheduled; | 
| import org.springframework.stereotype.Component; | 
|   | 
| import java.math.BigDecimal; | 
| import java.util.Date; | 
| import java.util.List; | 
|   | 
| import static com.matrix.system.hive.statistics.BusinessDataTypeEnum.*; | 
|   | 
| /** | 
|  * 业绩统计报表定时任务 | 
|  * | 
|  * @author jyy | 
|  */ | 
| @Component | 
| public class StatisticsBusinessDataJob { | 
|   | 
|     @Autowired | 
|     SysShopInfoDao shopInfoDao; | 
|   | 
|     @Autowired | 
|     SysBusinessDataDao businessDataDao; | 
|   | 
|     @Autowired | 
|     SysOrderItemDao orderItemDao; | 
|   | 
|     @Autowired | 
|     SysBeauticianStateDao serviceItemDao; | 
|   | 
|     @Autowired | 
|     ShoppingGoodsAssembleDao shoppingGoodsAssembleDao; | 
|   | 
|     /** | 
|      * 备用触发 | 
|      * @return | 
|      */ | 
|     public boolean executeExt2() { | 
|   | 
|         Date today = DateUtil.stringToDate("2020-11-27",DateUtil.DATE_FORMAT_DD); | 
|         Date now=new Date(); | 
|         while (DateUtil.isBeforeDate(now,today)){ | 
|             LogUtil.info("日期:"+DateUtil.dateFormatStr(today,DateUtil.DATE_FORMAT_DD)); | 
|             SysBusinessData sysBusinessData = new SysBusinessData(); | 
|             sysBusinessData.setTime(today); | 
|             businessDataDao.deleteByModel(sysBusinessData); | 
|   | 
|             List<SysShopInfo> sysShopInfos = shopInfoDao.selectShopInfo(null); | 
|   | 
|             for (SysShopInfo shopInfo : sysShopInfos) { | 
|   | 
|                 statisticOrderInfo(shopInfo, today); | 
|   | 
|                 statisticServiceOrderInfo(shopInfo, today); | 
|   | 
|             } | 
|             today=DateUtil.getDateAfter(today,1); | 
|         } | 
|   | 
|         return true; | 
|     } | 
|   | 
|   | 
|     @Scheduled(cron = "1 0 0 1/1 * ?") | 
|     public boolean executeExt() { | 
|   | 
|         LogUtil.info("业绩统计定时任务开始运行*******************"); | 
|         Date today = new Date(); | 
|   | 
|         SysBusinessData sysBusinessData = new SysBusinessData(); | 
|         sysBusinessData.setTime(DateUtil.getDateBefore(today,1)); | 
|         businessDataDao.deleteByModel(sysBusinessData); | 
|   | 
|         List<SysShopInfo> sysShopInfos = shopInfoDao.selectShopInfo(null); | 
|   | 
|         for (SysShopInfo shopInfo : sysShopInfos) { | 
|   | 
|             statisticOrderInfo(shopInfo, today); | 
|   | 
|             statisticServiceOrderInfo(shopInfo, today); | 
|   | 
|         } | 
|         LogUtil.info("业绩统计定时任务结束*******************"); | 
|         return true; | 
|     } | 
|   | 
|     /** | 
|      * 统计服务单数据 | 
|      */ | 
|     private void statisticServiceOrderInfo(SysShopInfo shopInfo, Date today) { | 
|         List<SysBeauticianState> serviceItemList = serviceItemDao.selectItemByTime(shopInfo.getId(), today); | 
|         BusinessDataTypeEnum[] typeEnums = {FREE_CONSUME_PAY, CONSUME_PAY}; | 
|         BigDecimal[] valueArray = {BigDecimal.ZERO, BigDecimal.ZERO}; | 
|         for (SysBeauticianState orderItem : serviceItemList) { | 
|             if (Dictionary.BEATUI_STATE_FWJS.equals(orderItem.getState()) && orderItem.getProjUse() != null) { | 
|   | 
|                 BigDecimal count = new BigDecimal(orderItem.getCount().toString()); | 
|                 BigDecimal total = new BigDecimal(orderItem.getProjUse().getPrice().toString()).multiply(count); | 
|   | 
|                 if (Dictionary.TAOCAN_SOURCE_ZS.equals(orderItem.getProjUse().getSource())) { | 
|                     valueArray[0] = valueArray[0].add(total); | 
|                 } else { | 
|                     valueArray[1] = valueArray[1].add(total); | 
|                 } | 
|             } | 
|         } | 
|         clearValue(valueArray); | 
|         saveBusinessData(shopInfo, today, typeEnums, valueArray); | 
|     } | 
|   | 
|     /** | 
|      * 统计订单中的数据 | 
|      */ | 
|     private void statisticOrderInfo(SysShopInfo shopInfo, Date today) { | 
|   | 
|         List<SysOrderItem> orderItemList = orderItemDao.selectItemByTime(shopInfo.getId(), today); | 
|         BusinessDataTypeEnum[] typeEnums = {CASH_PAY, CARD_PAY, REFUND_CASH_PAY, REFUND_CARD_PAY, ARREARS_PAY}; | 
|         BigDecimal[] valueArray = {BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO}; | 
|   | 
|         for (SysOrderItem orderItem : orderItemList) { | 
|   | 
|             if (Dictionary.ORDER_STATU_YFK.equals(orderItem.getStatus()) | 
|                     || Dictionary.ORDER_STATU_QK.equals(orderItem.getStatus())) { | 
|   | 
|                 valueArray[0] = valueArray[0].add(new BigDecimal(orderItem.getCashPay() == null ? "0" : orderItem.getCashPay().toString())); | 
|                 valueArray[1] = valueArray[1].add(new BigDecimal(orderItem.getCardPay() == null ? "0" : orderItem.getCardPay().toString())); | 
|   | 
|             } else if (Dictionary.ORDER_STATU_TK.equals(orderItem.getStatus())) { | 
|   | 
|                 valueArray[2] = valueArray[2].add(new BigDecimal(orderItem.getCashPay() == null ? "0" : orderItem.getCashPay().toString())); | 
|   | 
|                 valueArray[3] = valueArray[3].add(new BigDecimal(orderItem.getCardPay() == null ? "0" : orderItem.getCardPay().toString())); | 
|             } | 
|             if (Dictionary.ORDER_STATU_QK.equals(orderItem.getStatus())) { | 
|   | 
|                 valueArray[4] = valueArray[4].add(new BigDecimal(orderItem.getArrears() == null ? "0" : orderItem.getArrears().toString())); | 
|             } | 
|         } | 
|         clearValue(valueArray); | 
|         saveBusinessData(shopInfo, today, typeEnums, valueArray); | 
|     } | 
|   | 
|     /** | 
|      * 保留2位小数 | 
|      * | 
|      * @param valueArray | 
|      */ | 
|     private void clearValue(BigDecimal[] valueArray) { | 
|         for (int i = 0; i < valueArray.length; i++) { | 
|             valueArray[i] = valueArray[i].setScale(2, BigDecimal.ROUND_HALF_DOWN); | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 根据数组中的数据类型和值数组中的值,新增统计数据 | 
|      */ | 
|     private void saveBusinessData(SysShopInfo shopInfo, Date today, BusinessDataTypeEnum[] typeEnums, Object[] valueArray) { | 
|         for (int i = 0; i < valueArray.length; i++) { | 
|             SysBusinessData sysBusinessData = new SysBusinessData(); | 
|             BusinessDataTypeEnum dataTypeEnum = typeEnums[i]; | 
|   | 
|             sysBusinessData.setCompanyId(shopInfo.getCompanyId()) | 
|                     .setShopId(shopInfo.getId()) | 
|                     .setTime(today) | 
|                     .setDataType(dataTypeEnum.getType()) | 
|                     .setCode(dataTypeEnum.getCode()) | 
|                     .setSequence(dataTypeEnum.getSequence()) | 
|                     .setValue(valueArray[i].toString()); | 
|             sysBusinessData.setCreateBy("System"); | 
|             sysBusinessData.setUpdateBy("System"); | 
|             businessDataDao.insert(sysBusinessData); | 
|         } | 
|     } | 
|   | 
| } |