Pārlūkot izejas kodu

重构用餐报表

王先生 2 gadi atpakaļ
vecāks
revīzija
5833c375bd

+ 35 - 0
eladmin-system/src/main/java/me/zhengjie/modules/dm/statement/repository/DmHaveDinnerRepository.java

@@ -4,8 +4,12 @@ import me.zhengjie.modules.dm.statement.domain.DmHaveDinner;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.jpa.repository.*;
+import org.springframework.data.repository.query.Param;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.util.List;
+import java.util.Map;
+
 public interface DmHaveDinnerRepository extends JpaRepository<DmHaveDinner,Integer>, JpaSpecificationExecutor<DmHaveDinner> {
     /**
      * todo 是否存在视图
@@ -199,4 +203,35 @@ public interface DmHaveDinnerRepository extends JpaRepository<DmHaveDinner,Integ
                     "WHERE 1> (SELECT COUNT(*) from zdweek b WHERE a.time = b.time and a.zd_num > b.zd_num)" +
                     ")a ",nativeQuery = true)
     Page<DmHaveDinner> selectByZdWeek(Pageable pageable);
+
+    /**
+     * 判断视图是否存在
+     * @param viewName
+     * @return
+     */
+    @Query(nativeQuery = true,value = "select information_schema.VIEWS.TABLE_NAME from information_schema.VIEWS where (information_schema.VIEWS.TABLE_SCHEMA='dm_restaurant') and information_schema.VIEWS.TABLE_NAME = :viewName")
+    String judgeView(@Param("viewName") String viewName);
+
+    /**
+     * 删除视图
+     * @param viewName
+     */
+    @Modifying
+    @Transactional
+    @Query(nativeQuery = true,value = "drop view `DAY_DINING_STATEMENTS`")
+    void dropDayView(@Param("viewName") String viewName);
+
+    /**
+     * 创建视图
+     * @param startTime //开始时间
+     * @param nowTime  //当前时间
+     * @param viewName //视图名称
+     */
+    @Modifying
+    @Transactional
+    @Query(nativeQuery = true,value = "create view DAY_DINING_STATEMENTS as SELECT item.pid,item.name,COUNT(item.pid) as countPid,item.price,rec.create_date,item.seq FROM dm_order_record rec LEFT JOIN  dm_order_item item on rec.seq = item.seq WHERE rec.create_date BETWEEN ?1  AND ?2  GROUP BY item.pid ")
+    void createDayView(@Param("startTime") String startTime,@Param("nowTime") String nowTime,@Param("viewName") String viewName);
+
+    @Query(nativeQuery = true,value = "SELECT * FROM `DAY_DINING_STATEMENTS`")
+    List<Map<String,Object>> selectDayView();
 }

+ 66 - 0
eladmin-system/src/main/java/me/zhengjie/modules/quartz/task/DiningStatementsTask.java

@@ -0,0 +1,66 @@
+/**
+ * Copyright (C), 2022-08-02
+ * FileName: DiningStatements
+ * Author:   wanglongda
+ * Date:     2022/8/2 15:10
+ * Description: 用餐报表
+ */
+package me.zhengjie.modules.quartz.task;/**
+ * Created Name: wanglongda
+ * Created Time: 2022/8/2 15:10
+ * Description: iot-zhcy
+ */
+
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import me.zhengjie.modules.dm.statement.repository.DmHaveDinnerRepository;
+import me.zhengjie.modules.dm.statement.service.mapstruct.DmHaveDinnerMapper;
+import me.zhengjie.utils.StringUtils;
+import org.springframework.stereotype.Component;
+
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+
+/**
+ * <功能简要> <br>
+ * <用餐报表>
+ *
+ * @Author wanglongda
+ * @createTime 2022/8/2 15:10
+ * @Version 1.0.0
+ */
+
+@Slf4j
+@Component
+@RequiredArgsConstructor
+public class DiningStatementsTask {
+
+    private final DmHaveDinnerRepository dmHaveDinnerRepository;
+
+    /**
+     * 定时生成用餐日报  DAY_DINING_STATEMENTS
+     */
+    public void dayDiningStatements(){
+        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
+
+        Calendar instance = Calendar.getInstance();
+        //当前时间
+        String nowTime = simpleDateFormat.format(instance.getTime()) + " 23:59:59";
+        String StartTime = simpleDateFormat.format(instance.getTime()) + " 00:00:00";
+        //第一步判断是否有Day_Dining_Statements视图
+       String viewName = dmHaveDinnerRepository.judgeView("DAY_DINING_STATEMENTS");
+       //由于是一天一生成 那就再新增之前就要删除之前的视图
+       if (!StringUtils.isEmpty(viewName)){
+           //删除视图
+           dmHaveDinnerRepository.dropDayView("DAY_DINING_STATEMENTS");
+           //创建视图
+           dmHaveDinnerRepository.createDayView(StartTime,nowTime,"DAY_DINING_STATEMENTS");
+
+       }else {
+           //创建视图
+           dmHaveDinnerRepository.createDayView(StartTime,nowTime,"DAY_DINING_STATEMENTS");
+       }
+
+    }
+
+}

+ 10 - 1
eladmin-system/src/main/java/me/zhengjie/modules/thirdparty/v1/OpenApiController.java

@@ -34,6 +34,7 @@ import me.zhengjie.modules.dm.order.service.dto.DmOrderRecordQueryCriteria;
 import me.zhengjie.modules.dm.service.domain.DmServicePj;
 import me.zhengjie.modules.dm.service.repository.DmServicePjRepository;
 import me.zhengjie.modules.dm.service.service.DmServicePjService;
+import me.zhengjie.modules.dm.statement.repository.DmHaveDinnerRepository;
 import me.zhengjie.modules.dm.userInformation.domain.DmUserInformation;
 import me.zhengjie.modules.dm.userInformation.service.DmUserInformationService;
 import me.zhengjie.modules.dm.userInformation.service.dto.DmUserInformationDto;
@@ -73,7 +74,7 @@ public class OpenApiController {
     private final DmOrderRecordService dmOrderRecordService;
     private final DmInformationService dmInformationService;
     private final DmUserInformationService dmUserInformationService;
-
+    private final DmHaveDinnerRepository dmHaveDinnerRepository;
     @Log("获取今日菜品-App")
     @ApiOperation("获取今日菜品-App")
     @AnonymousPostMapping(value = "/foodRepository")
@@ -340,4 +341,12 @@ public class OpenApiController {
         jsonObject.put("countTheTopThree",countTheTopThree);
         return new BaseResponse<>(jsonObject);
     }
+
+    @AnonymousGetMapping(value = "/getDiningStatements")
+    @Log("获取用餐日报")
+    @ApiOperation("获取用餐日报")
+    public BaseResponse<Object> getDiningStatements(){
+       List<Map<String,Object>> maps = dmHaveDinnerRepository.selectDayView();
+        return new BaseResponse<>(maps);
+    }
 }