From 72b971c98c46531064d74b68fc8fe8864ed0b544 Mon Sep 17 00:00:00 2001
From: guoshilong <123456>
Date: Tue, 08 Nov 2022 09:04:56 +0800
Subject: [PATCH] 大屏溯源统计次数sql修改

---
 src/main/java/org/springblade/modules/farmplant/mapper/FarmProductStockMapper.xml |  174 ++++++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 127 insertions(+), 47 deletions(-)

diff --git a/src/main/java/org/springblade/modules/farmplant/mapper/FarmProductStockMapper.xml b/src/main/java/org/springblade/modules/farmplant/mapper/FarmProductStockMapper.xml
index 83994b5..b5ab481 100644
--- a/src/main/java/org/springblade/modules/farmplant/mapper/FarmProductStockMapper.xml
+++ b/src/main/java/org/springblade/modules/farmplant/mapper/FarmProductStockMapper.xml
@@ -4,9 +4,10 @@
 
     <!--自定义查询农产品库存分页数据-->
     <select id="selectFarmProductStockPage" resultType="org.springblade.modules.farmplant.vo.FarmProductStockVO">
-        select sfps.strain_id strainId,sum(sfps.weight) weight,ss.url,ss.strain_name strainName from
+        select sfps.id,sfps.strain_id strainId,sfps.land_id,sum(sfps.weight) weight,ss.url,ss.strain_name strainName from
         sys_farm_product_stock sfps
         left join sys_strain ss on ss.id = sfps.strain_id
+        left join sys_land sl on sl.id = sfps.land_id
         where 1=1
         <if test="farmProductStock.strainId!=null and farmProductStock.strainId!=''">
             and sfps.strain_id = #{farmProductStock.strainId}
@@ -26,13 +27,62 @@
         <if test="farmProductStock.tenantId!=null and farmProductStock.tenantId!=''">
             and sfps.tenant_id = #{farmProductStock.tenantId}
         </if>
-        group by sfps.strain_id,ss.url,ss.strain_name
+        <if test="farmProductStock.farmId!=null and farmProductStock.farmId!=''">
+            and sl.farm_id = #{farmProductStock.farmId}
+        </if>
+        group by sfps.id,sfps.strain_id,ss.url,ss.strain_name,sfps.land_id
+    </select>
+
+    <select id="selectFarmProductStockPageCount" resultType="org.springblade.modules.farmplant.vo.FarmProductStockVO">
+        SELECT a.strainId,SUM(weight) weight,a.url,a.strainName FROM
+            (SELECT
+                 sfps.id,
+                 sfps.strain_id AS strainId,
+                 sfps.land_id,
+                 sum( sfps.weight ) AS weight,
+                 ss.url,
+                 ss.strain_name AS strainName
+             FROM
+                 sys_farm_product_stock sfps
+                     LEFT JOIN sys_strain ss ON ss.id = sfps.strain_id
+                     LEFT JOIN sys_land sl ON sl.id = sfps.land_id
+             WHERE
+                 1 = 1
+        <if test="farmProductStock.strainId!=null and farmProductStock.strainId!=''">
+            and sfps.strain_id = #{farmProductStock.strainId}
+        </if>
+        <if test="farmProductStock.leaves!=null and farmProductStock.leaves!=''">
+            and sfps.leaves = #{farmProductStock.leaves}
+        </if>
+        <if test="farmProductStock.startTime!=null and farmProductStock.startTime!=''">
+            and sfps.time &gt;= #{farmProductStock.startTime}
+        </if>
+        <if test="farmProductStock.endTime!=null and farmProductStock.endTime!=''">
+            and sfps.time &lt;= #{farmProductStock.endTime}
+        </if>
+        <if test="farmProductStock.deptId!=null and farmProductStock.deptId!=''">
+            and sfps.dept_id = #{farmProductStock.deptId}
+        </if>
+        <if test="farmProductStock.tenantId!=null and farmProductStock.tenantId!=''">
+            and sfps.tenant_id = #{farmProductStock.tenantId}
+        </if>
+        <if test="farmProductStock.farmId!=null and farmProductStock.farmId!=''">
+            and sl.farm_id = #{farmProductStock.farmId}
+        </if>
+             GROUP BY
+                 sfps.id,
+                 sfps.strain_id,
+                 ss.url,
+                 ss.strain_name,
+                 sfps.land_id
+            ) a WHERE weight !=0 GROUP BY strainId
     </select>
 
     <select id="selectFarmProductStockPages" resultType="org.springblade.modules.farmplant.vo.FarmProductStockVO">
         select sfps.strain_id strainId,sfps.land_id,sum(sfps.weight) weight,ss.url,ss.strain_name strainName from
         sys_farm_product_stock sfps
         left join sys_strain ss on ss.id = sfps.strain_id
+        left join sys_land sl on sl.id = sfps.land_id
         where 1=1
         <if test="farmProductStock.strainId!=null and farmProductStock.strainId!=''">
             and sfps.strain_id = #{farmProductStock.strainId}
@@ -52,15 +102,26 @@
         <if test="farmProductStock.tenantId!=null and farmProductStock.tenantId!=''">
             and sfps.tenant_id = #{farmProductStock.tenantId}
         </if>
+        <if test="farmProductStock.farmId!=null and farmProductStock.farmId!=''">
+            and sl.farm_id = #{farmProductStock.farmId}
+        </if>
         group by sfps.strain_id,ss.url,ss.strain_name,sfps.land_id
+    </select>
+
+    <!--根据农场id和品种类型查询  关系:农产品库存表关联地块 -》 地块关联农场 -》 需要联合查询    -->
+    <select id="findFarmProductStockByFarmIdAndStrainId" resultType="org.springblade.modules.farmplant.entity.FarmProductStock">
+        select a.* from sys_farm_product_stock a
+        left join sys_land b on a.land_id = b.id
+        where 1=1 and a.strain_id = #{strainId} and b.farm_id = #{farmId}
     </select>
 
 
     <!--查询农产品库存数据-->
     <select id="statisticsProduct" resultType="org.springblade.modules.farmplant.vo.FarmProductStockVO">
-        select sfps.strain_id strainId,sum(sfps.weight) weight,ss.url,ss.strain_name strainName from
+        select sfps.id,sfps.strain_id strainId,sum(sfps.weight) weight,ss.url,ss.strain_name strainName from
         sys_farm_product_stock sfps
         left join sys_strain ss on ss.id = sfps.strain_id
+        LEFT JOIN sys_land sl ON sl.id = sfps.land_id
         where 1=1
         <if test="farmProductStock.strainId!=null and farmProductStock.strainId!=''">
             and sfps.strain_id = #{farmProductStock.strainId}
@@ -80,7 +141,10 @@
         <if test="farmProductStock.tenantId!=null and farmProductStock.tenantId!=''">
             and sfps.tenant_id = #{farmProductStock.tenantId}
         </if>
-        group by sfps.strain_id,ss.url,ss.strain_name
+        <if test="farmProductStock.farmId!=null and farmProductStock.farmId!=''">
+            and sl.farm_id = #{farmProductStock.farmId}
+        </if>
+        group by sfps.id,sfps.strain_id,ss.url,ss.strain_name
     </select>
 
     <!--大屏产量每个月统计-->
@@ -93,18 +157,15 @@
 
     <!--大屏经营概况库存数量统计-->
     <select id="statisticsStock" resultType="java.util.HashMap">
-        SELECT
-        SUM( stock.weight ) AS sum ,
-        SUM( stock.recovery ) AS cssum
-        FROM
-        sys_farm_product_stock AS stock
+        SELECT SUM( stock.weight ) AS sum,SUM( stock.recovery ) AS cssum FROM sys_farm_product_stock AS stock
+        left join sys_land sl on sl.id = stock.land_id
         WHERE
         1 = 1
         <if test="year!=null and year!=''">
             and YEAR ( stock.time ) = #{year}
         </if>
-        <if test="deptId!=null and deptId!=''">
-            and stock.dept_id = #{deptId}
+        <if test="farmId!=null and farmId!=''">
+            and sl.farm_id = #{farmId}
         </if>
     </select>
     <!--按年统计产量-->
@@ -135,29 +196,30 @@
 <!--    </select>-->
         <select id="statisticsStockYear" resultType="java.util.HashMap">
             select m.id time, IFNULL(b,0) num from
-        (
-          select 1 as id union
-            select 2 union
-            select 3 union
-            select 4 union
-            select 5 union
-            select 6 union
-            select 7 union
-            select 8 union
-            select 9 union
-            select 10 union
-            select 11 union
-            select 12
-        ) m
-        left join
-        (
-            select month(ssr.time) a,sum(weight) b from sys_recovery ssr
-
-            where  year(ssr.time) = #{year}
-            GROUP BY month(ssr.time)
-        ) n
-        on m.id = n.a
-        order by id
+                (
+                  select 1 as id union
+                    select 2 union
+                    select 3 union
+                    select 4 union
+                    select 5 union
+                    select 6 union
+                    select 7 union
+                    select 8 union
+                    select 9 union
+                    select 10 union
+                    select 11 union
+                    select 12
+                ) m
+                left join
+                (
+                    select month(ssr.time) a,sum(weight) b from sys_recovery ssr
+                    left join sys_land sl on sl.id = ssr.land_id
+                    where  year(ssr.time) = #{year}
+                    and sl.farm_id = #{farmId}
+                    GROUP BY month(ssr.time)
+                ) n
+            on m.id = n.a
+            order by id
         </select>
     <!--按月份统计产量-->
     <select id="statisticsStockMonth" resultType="java.util.HashMap">
@@ -248,11 +310,13 @@
         DATE_FORMAT( recovery.time, '%Y-%c-%d' ) AS gptime
         FROM
         sys_recovery recovery
-        WHERE
-        DATE_FORMAT( recovery.time, '%Y-%m' ) = #{year}
+        left join sys_land sl on sl.id = recovery.land_id
+        WHERE DATE_FORMAT( recovery.time, '%Y-%m' ) = #{year}
+        and sl.farm_id = #{farmId}
         GROUP BY
         gptime
         ) AS righttable ON DATE_FORMAT( lefttable.date, '%Y-%c-%d' ) = righttable.gptime
+        ORDER BY time
     </select>
     <!--按日统计产量-->
     <select id="statisticsStockDay" resultType="java.util.HashMap">
@@ -274,8 +338,10 @@
         LEFT JOIN
         (
         SELECT HOUR(recovery.time) AS order_hour, SUM(weight) AS num from sys_recovery recovery
+        left join sys_land sl on sl.id = recovery.land_id
         WHERE recovery.time >= str_to_date(concat(#{year},' 00:00:00'),'%Y-%m-%d %T')
         AND recovery.time &lt;= str_to_date(concat(#{year},' 23:59:59'),'%Y-%m-%d %T')
+        and sl.farm_id = #{farmId}
         GROUP BY order_hour
         ) b ON a.order_hour=b.order_hour
         ORDER BY a.order_hour
@@ -288,15 +354,14 @@
         IFNULL( SUM( CASE WHEN LEAVES = 2 THEN weight END ), 0 ) AS '2',
         IFNULL( SUM( CASE WHEN LEAVES = 3 THEN weight END ), 0 ) AS '3',
         IFNULL( SUM( CASE WHEN LEAVES = 4 THEN weight END ), 0 ) AS '4'
-        FROM
-        sys_recovery
-        WHERE
-        1 = 1
+        FROM sys_recovery sr
+        left join sys_land sl on sr.land_id = sl.id
+        WHERE 1 = 1
         <if test="year!=null and year!=''">
-            and YEAR ( time ) = #{year}
+            and sr.time like concat('%',#{year},'%')
         </if>
-        <if test="deptId!=null and deptId!=''">
-            and dept_id = #{deptId}
+        <if test="farmId!=null and farmId!=''">
+            and sl.farm_id = #{farmId}
         </if>
     </select>
     <!--大屏经营概况年产量统计-->
@@ -322,9 +387,10 @@
 
     <!--小程序农产品库存详情数据-->
     <select id="statisticsProductx" resultType="org.springblade.modules.farmplant.vo.FarmProductStockVO">
-        select sfps.strain_id strainId,sfps.weight,ss.url,ss.strain_name strainName from
+        select sfps.strain_id strainId,SUM(sfps.weight) AS weight,ss.url,ss.strain_name strainName from
         sys_farm_product_stock sfps
         left join sys_strain ss on ss.id = sfps.strain_id
+        LEFT JOIN sys_land land ON land.id = sfps.land_id
         where 1=1
         <if test="farmProductStock.strainId!=null and farmProductStock.strainId!=''">
             and sfps.strain_id = #{farmProductStock.strainId}
@@ -344,6 +410,13 @@
         <if test="farmProductStock.tenantId!=null and farmProductStock.tenantId!=''">
             and sfps.tenant_id = #{farmProductStock.tenantId}
         </if>
+        <if test="farmProductStock.strainType !=null and farmProductStock.strainType !='' ">
+            and ss.strain_type = #{farmProductStock.strainType}
+        </if>
+        <if test="farmProductStock.farmId !=null and farmProductStock.farmId !='' ">
+            and land.farm_id = #{farmProductStock.farmId}
+        </if>
+        GROUP BY sfps.strain_id
     </select>
 
     <!--销售对比库存是否充足-->
@@ -351,18 +424,25 @@
         SELECT
         CASE
             WHEN
-                weight > #{sale} THEN
+                weight >= #{sale} THEN
                 TRUE ELSE FALSE
                 END result
         FROM
             sys_farm_product_stock
         WHERE
-            strain_id = #{id}
+            id = #{id}
     </select>
+
     <!--减去对应库存-->
     <update id="stockReduce">
         UPDATE sys_farm_product_stock SET weight = weight - #{sale}
         WHERE
-            strain_id = #{id}
+            id = #{id}
     </update>
+
+    <select id="selectBy2Id" resultType="org.springblade.modules.farmplant.entity.FarmProductStock">
+        SELECT id,strain_id,weight,leaves,time,operator,remark,
+               create_time,farm_plant_id,dept_id,tenant_id,recovery,land_id
+        FROM sys_farm_product_stock  WHERE strain_id = #{strainId} AND land_id = #{landId}
+    </select>
 </mapper>

--
Gitblit v1.9.3