<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.unis.reserveLayout.modular.dataKcglKcswDefault.mapper.KcswDefaultMapper">

    <select id="findJyxxPage" resultType="com.unis.reserveLayout.modular.dataKcglKcswDefault.entity.VO.CrkJyxxDataVO">
        SELECT DISTINCT
        DATA_CRKYW_JYXX_DEFAULT.ModifyDate AS "ModifyDate",
        DATA_CRKYW_RMDJ_DEFAULT.lsxz AS "lsxz",
        b_0.enumName AS "lsxzName",
        DATA_CRKYW_RMDJ_DEFAULT.cph AS "cph",
        DATA_CRKYW_RMDJ_DEFAULT.lspz AS "lspz",
        b_2.enumName AS "lspzName",
        DATA_CRKYW_JYXX_DEFAULT.jydh AS "jydh",
        DATA_CRKYW_JYXX_DEFAULT.jyr AS "jyr",
        DATA_CRKYW_JYXX_DEFAULT.jyjg AS "jyjg",
        b_5.enumName AS "jyjgName",
        DATA_CRKYW_JYXX_DEFAULT.lsdj AS "lsdj",
        b_6.enumName AS "lsdjName",
        DATA_CRKYW_JYXX_DEFAULT.jysj AS "jysj",
        DATA_CRKYW_JYXX_DEFAULT.BizNo AS "bizNo",
        DATA_CRKYW_JYXX_DEFAULT.DataID AS "dataId"
        FROM
        depot_qh.DATA_CRKYW_RMDJ_DEFAULT
        LEFT JOIN depot_qh.DATA_CRKYW_JYXX_DEFAULT ON DATA_CRKYW_RMDJ_DEFAULT.BizNo = DATA_CRKYW_JYXX_DEFAULT.BizNo
        LEFT JOIN depot_qh.basic_enum b_0 ON DATA_CRKYW_RMDJ_DEFAULT.lsxz = b_0.enumId
        LEFT JOIN depot_qh.basic_enum b_2 ON DATA_CRKYW_RMDJ_DEFAULT.lspz = b_2.enumId
        LEFT JOIN depot_qh.basic_enum b_5 ON DATA_CRKYW_JYXX_DEFAULT.jyjg = b_5.enumId
        LEFT JOIN depot_qh.basic_enum b_6 ON DATA_CRKYW_JYXX_DEFAULT.lsdj = b_6.enumId
        WHERE
        ifnull(
        DATA_CRKYW_JYXX_DEFAULT.SytelInfoID,
        ''
        ) = '402881476395411d0163964a2b57000b'
        AND DATA_CRKYW_JYXX_DEFAULT.PeriodID = '11'
        AND DATA_CRKYW_JYXX_DEFAULT.RoundId = 0
        <if test="ew.sqlSegment != null and ew.sqlSegment != ''">
            and ${ew.sqlSegment}
        </if>
        ORDER BY DATA_CRKYW_RMDJ_DEFAULT.BizNo
    </select>
    <select id="getCrkChartDataByPz" resultType="com.unis.reserveLayout.modular.dataKcglKcswDefault.entity.VO.CrkChartDataVO">
        SELECT * FROM (SELECT
        a.lspz,
        b0_enum.enumname AS lspzName,
        '3' as ywlx,
        SUM(b.xzklhjz / 1000) AS sumZl
        FROM
        depot_qh.data_crkyw_rmdj_default a
        INNER JOIN depot_qh.data_crkyw_cpz_default b ON a.BizNo = b.BizNo
        LEFT JOIN depot_qh.basic_enum b0_enum ON b0_enum.enumId = a.lspz
        GROUP BY
        a.lspz,
        b0_enum.enumname
        union ALL
        SELECT
        a.lspz,
        b0_enum.enumname AS lspzName,
        '1' as ywlx,
        SUM(b.klhjz / 1000) AS sumZl
        FROM
        depot_qh.data_crkyw_ckrmdj_default a
        INNER JOIN depot_qh.data_crkyw_ckcmz_default b ON a.BizNo = b.BizNo
        LEFT JOIN depot_qh.basic_enum b0_enum ON b0_enum.enumId = a.lspz
        GROUP BY
        a.lspz,
        b0_enum.enumname
        ) x

        <if test="crkCharParam.lspzName != null and crkCharParam.lspzName != ''">
            where lspzName LIKE  CONCAT('%',#{crkCharParam.lspzName},'%')
        </if>
        ORDER BY ywlx
    </select>
    <select id="getCrkChartDataByDj" resultType="com.unis.reserveLayout.modular.dataKcglKcswDefault.entity.VO.CrkChartDataVO">
        SELECT
        a.dj as lsdj,
        b0_enum.enumname AS lsdjName,
        SUM(a.kcsl / 1000) AS sumZl
        FROM
        depot_qh.data_kcgl_kcsw_default a
        LEFT JOIN depot_qh.basic_enum b0_enum ON b0_enum.enumId = a.dj

        <if test="crkCharParam.lsdjName != null and crkCharParam.lsdjName != ''">
            where b0_enum.enumname LIKE  CONCAT('%',#{crkCharParam.lsdjName},'%')
        </if>
        GROUP BY
        a.dj,
        b0_enum.enumname
        ORDER BY a.dj
    </select>
    <select id="getXzList" resultType="com.unis.reserveLayout.modular.dataKcglKcswDefault.entity.VO.CrkKcswXzDataVO">
        SELECT
    orgId,orgName,
    MAX(CASE WHEN lspzName = '小麦及混合小麦' THEN sumZl ELSE 0 END) AS 'xm',
     MAX(CASE WHEN lspzName = '青稞' THEN sumZl ELSE 0 END) AS 'qk',
     MAX(CASE WHEN lspzName = '稻谷' THEN sumZl ELSE 0 END) AS 'dg'
    FROM (SELECT
        b.org_id orgId,
        b.org_name orgName,
        a.pz,
        b1_enum.enumname AS lspzName,
        SUM(a.kcsl / 1000) AS sumZl
    FROM
        depot_qh.data_kcgl_kcsw_default a
    LEFT JOIN depot_qh.basic_enum b1_enum ON b1_enum.enumId = a.pz
    LEFT JOIN depot_qh.org_info b on a.UnitID=b.org_id
    WHERE
        a.hwxz IN (${xz}) and a.pz in (3164,3166,8000)
        <if test="orgId != null and orgId != ''">
            and a.UnitID = #{orgId}
        </if>
    GROUP BY
        a.unitid,a.pz
    ) x
    GROUP BY
        orgId,orgName
    ORDER BY
        orgId,orgName
    </select>

    <select id="getJyxxDetail" resultType="com.unis.reserveLayout.modular.dataKcglKcswDefault.entity.VO.CrkJyxxDataVO">
        SELECT
        a.lsxz AS "lsxz",
        b_2.enumName as "lspzName",
        a.lspz AS "lspz",
        mxpz_enum.enumName AS "mxpzName",
        a.mxpz AS "mxpz",
        b.jysj AS "jysj",
        b.BizNo AS "BizNo",
        b.jydh AS "jydh",
        b.jyyj AS "jyyj",
        b.jyr AS "jyr",
        b.jyjg AS "jyjg",
        b_5.enumName AS "jyjgName",
        b.lsdj AS "lsdj",
        b_6.enumName AS "lsdjName",
        b.dj AS "dj",
        b.jybz AS "jybz",
        b.DataID AS "dataId"
    FROM
        DATA_CRKYW_RMDJ_DEFAULT a
    inner JOIN DATA_CRKYW_JYXX_DEFAULT b ON a.BizNo = b.BizNo
    LEFT JOIN basic_enum b_2 ON a.lspz = b_2.enumId
    LEFT JOIN basic_enum b_5 ON b.jyjg = b_5.enumId
    LEFT JOIN basic_enum b_6 ON b.lsdj = b_6.enumId
    LEFT JOIN basic_enum mxpz_enum ON mxpz_enum.enumId = a.mxpz
    where b.DataID = #{dataId}
    </select>
    <select id="getJyxxMxDetail" resultType="com.unis.reserveLayout.modular.dataKcglKcswDefault.entity.VO.CrkJyxxMxDataVO">
        SELECT
	a.zbxmc AS "zbxmc",
	d.enumName AS "zbxmcName",
	a.jykl AS "jykl",
	a.jykj AS "jykj",
	a.jyz AS "jyz",
	a.jykjje AS "jykjje",
	a.zklsl AS "zklsl",
	a.DataID AS "dataId",
	d.enumCode
FROM
	DATA_CRKYW_JYXX_FLT_001 a
inner JOIN DATA_CRKYW_JYXX_DEFAULT b ON b.DataID = a.MainDataId
LEFT JOIN BASIC_ENUM d ON a.zbxmc = d.enumId
WHERE
	b.DataID = #{dataId}
	ORDER BY d.enumCode asc
    </select>
</mapper>