<?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.module.coding.dal.mysql.kshzs.KshzsMapper">

    <sql id="queryInfo">
        <if test="kqid != null and kqid != ''">
            and kqid = #{kqid}
        </if>
        <if test="xzqhdm!= null and xzqhdm!=''">
            and cdbm = #{xzqhdm}
        </if>
        <if test="lspzmc!= null and lspzmc!=''">
            and lspzmc in (
                <foreach collection="lspzmcLst" item="item" separator=",">
                    #{item}
                </foreach>
            )
        </if>
        <if test="lsdjdm!= null and lsdjdm!=''">
            and lsdjbm in (
                <foreach collection="lsdjdmLst" item="item" separator=",">
                    #{item}
                </foreach>
            )
        </if>
    </sql>

    <select id="selectKqmcList" parameterType="com.unis.module.coding.controller.admin.kshzs.vo.paramVo" resultType="com.unis.module.coding.controller.admin.kshzs.vo.kqxxVo">
        select id kqid,kqmc,kqdm from DELIVERY_KQXX
        <where>
            <if test="kqmc!= null and kqmc!= ''">
                and kqmc like concat('%',#{kqmc},'%')
            </if>
        </where>
    </select>

    <select id="selectSpList" parameterType="com.unis.module.coding.controller.admin.kshzs.vo.paramVo" resultType="map">
        select jkdbm,jkdmc,url from DELIVERY_SPXX
        <where>
            <if test="kqdm!= null and kqdm!= ''">
                and kqdm = #{kqdm}
            </if>
        </where>
    </select>

    <select id="selectList" parameterType="com.unis.module.coding.controller.admin.kshzs.vo.paramVo" resultType="map">
        select round(sum(hw.jjsl/10000),2) kcsl,hw.cdbm xzqhdm,sheng.jd jd,sheng.wd wd from DELIVERY_HWK hw,DELIVERY_SHENG sheng
            where hw.cdbm = sheng.xzqhbm
        <where>
            <include refid="queryInfo"/>
        </where>
        group by hw.cdbm,hw.jjsl,sheng.jd,sheng.wd
        order by hw.cdbm
    </select>

    <select id="selectPieChart" parameterType="com.unis.module.coding.controller.admin.kshzs.vo.paramVo" resultType="map">
        <if test = "lx=='1'">
            select round(sum(jjsl/10000),2) "value",lspzmc name from DELIVERY_HWK
            <where>
                <include refid="queryInfo"/>
            </where>
            group by lspzmc order by FIELD(pzdlmc,'小麦','玉米','稻谷','大豆','其他') asc
        </if>
        <if test = "lx=='2'">
            select round(sum(jjsl/10000),2) "value",shnd name from DELIVERY_HWK
            <where>
                <include refid="queryInfo"/>
            </where>
            group by shnd
            order by shnd desc
        </if>
    </select>

    <select id="selectCardMapList" parameterType="com.unis.module.coding.controller.admin.kshzs.vo.paramVo" resultType="map">
        SELECT COUNT(ca.id) AS cfsl,NVL(ROUND(SUM(ca.sjcr), 2), 0) AS crzl
        FROM DELIVERY_CAXX ca JOIN DELIVERY_KQXX kq ON ca.kqid = kq.id
        <where>
            <include refid="queryInfo"/>
        </where>
    </select>

    <select id="queryLspzList" parameterType="com.unis.module.coding.controller.admin.kshzs.vo.paramVo" resultType="map">
        select round(sum(jjsl),2) "value",lspzmc name from DELIVERY_HWK
        <where>
            <include refid="queryInfo"/>
        </where>
        group by lspzmc order by FIELD(lspzmc,'小麦','玉米','稻谷','大豆','其他') asc
    </select>

    <select id="selectCxxList" parameterType="com.unis.module.coding.controller.admin.kshzs.vo.paramVo" resultType="com.unis.module.coding.controller.admin.kshzs.vo.cfxxVo">
        SELECT
            id AS cfid,cfmc,sjcr AS ccsl,
            CASE
            WHEN yxcf = 1 THEN '是'
            ELSE '否'
            END AS sfjgc
        FROM DELIVERY_CAXX
        <where>
            <include refid="queryInfo"/>
        </where>
    </select>

    <select id="queryKqInfo" parameterType="com.unis.module.coding.controller.admin.kshzs.vo.paramVo" resultType="com.unis.module.coding.controller.admin.kshzs.vo.kqxxVo">
        SELECT
         id kqid,kqdm,ssfrqy qymc,shxydm tyshxydm,resid ljdz from DELIVERY_KQXX
        FROM DELIVERY_KQXX
        <where>
            <if test="kqid != null and kqid != ''">
                and id = #{kqid}
            </if>
        </where>
    </select>

    <select id="selectCwxxInfo" parameterType="com.unis.module.coding.controller.admin.kshzs.vo.paramVo" resultType="com.unis.module.coding.controller.admin.kshzs.vo.cwxxVo">
        select id cwxxid,sfgykg,syndzzc,gd from DELIVERY_CWXX
        <where>
            <include refid="queryInfo"/>
        </where>
    </select>

    <select id="selectCwxxYearList" resultType="com.unis.module.coding.controller.admin.cwxxyear.vo.CwxxYearSaveReqVO">
        select top 3 cn,zfz,yysr,jlr from DELIVERY_CWXX_YEAR
        <where>
            <if test="cwxxid != null and cwxxid != ''">
                and cwxx_id = #{cwxxid}
            </if>
        </where>
        order by cn desc
    </select>

    <select id="selectCfInfo" parameterType="com.unis.module.coding.controller.admin.kshzs.vo.paramVo" resultType="com.unis.module.coding.controller.admin.kshzs.vo.cfxxVo">
        SELECT
             camc cfmc,cfbm cabh,cflx,jfsyrq,sjsynx,sjcr,cazt cfzt,khmc dwmc,khlxdh lxdh,khbh tyshxydm from DELIVERY_CAXX
        FROM DELIVERY_CAXX
        <where>
            <if test="cfid != null and cfid != ''">
                and id = #{cfid}
            </if>
        </where>
    </select>

    <select id="selectHwkInfo" parameterType="com.unis.module.coding.controller.admin.kshzs.vo.paramVo" resultType="com.unis.module.coding.controller.admin.kshzs.vo.hwkVo">
        select hwmc,ca.cfmc,bgyxm bgy,lspzmc,
                CASE
                WHEN lsdjbm = '01' THEN '一等'
                WHEN lsdjbm = '02' THEN '二等'
                WHEN lsdjbm = '03' THEN '三等'
                WHEN lsdjbm = '04' THEN '四等'
                WHEN lsdjbm = '05' THEN '五等'
                ELSE '等外'
                END AS lsdjmc
                ,shnd,cdbm lscd,sjsl,jjsl,rksj,bgfs from DELIVERY_HWK
            left join DELIVERY_CAXX ca on cf_id = ca.id
        <where>
            <if test="cfid != null and cfid != ''">
                and cf_id = #{cfid}
            </if>
        </where>
    </select>

    <select id="selectLqjcList" parameterType="com.unis.module.coding.controller.admin.kshzs.vo.paramVo" resultType="com.unis.module.coding.controller.admin.kshzs.vo.lqjcVo">
        select hw.hwmc camc,hw.lspzmc,hw.jjsl kcsl,
                CASE
                WHEN hw.lsdjbm = '01' THEN '一等'
                WHEN hw.lsdjbm = '02' THEN '二等'
                WHEN hw.lsdjbm = '03' THEN '三等'
                WHEN hw.lsdjbm = '04' THEN '四等'
                WHEN hw.lsdjbm = '05' THEN '五等'
                ELSE '等外'
                END AS lsdjmc,lqjc.CWW cfww,lqjc.CWS cfws,lqjc.CNW cfnw,lqjc.CNS cfns,lqjc.LSZGW lszgw,lqjc.LSZDW lszdw,lqjc.LSPJW lspjw,lqjc.FILE_IDS fileIds
        from DELIVERY_LQJCXX lqjc
            left join DELIVERY_HWK hw on lqjc.hwk_id = hw.id
        <where>
            <if test="cfid != null and cfid != ''">
                and lqjc.cf_id = #{cfid}
            </if>
        </where>
    </select>

    <sql id="queryShengInfo">
        <if test="xzqhdm!= null and xzqhdm!=''">
            and kq.sheng = #{xzqhdm}
        </if>
        <if test="lspzmc != null and lspzmc != ''">
            and kq.id in (
               select hw.kqid from DELIVERY_HWK hw
               where hw.lspzmc in (
                   <foreach collection="lspzmcLst" item="item" separator=",">
                        #{item}
                   </foreach>
                )
            )
        </if>
        <if test="lsdjdm != null and lsdjdm != ''">
            and kq.id in (
                select hw.kqid from DELIVERY_HWK hw
                where hw.lsdjdm in (
                    <foreach collection="lsdjdmLst" item="item" separator=",">
                        #{item}
                    </foreach>
                )
            )
        </if>
    </sql>

    <select id="queryList" parameterType="com.unis.module.coding.controller.admin.kshzs.vo.paramVo" resultType="map">
        select kq.id,kq.kqmc,kq.kqdm,kq.jd,kq.wd from DELIVERY_KQXX kq
        <where>
            <include refid="queryShengInfo"/>
        </where>
    </select>

    <select id="selectCardList" parameterType="com.unis.module.coding.controller.admin.kshzs.vo.paramVo" resultType="map">
        SELECT
                (SELECT COUNT(kq.id) FROM DELIVERY_KQXX kq
                    <where>
                        <include refid="queryShengInfo"/>
                    </where>) AS kdsl,
                (SELECT COUNT(ca.id) AS cfsl
                 FROM DELIVERY_CAXX ca
                          JOIN DELIVERY_KQXX kq ON ca.kqid = kq.id
                <where>
                    <include refid="queryShengInfo"/>
                </where>) AS cfsl,
                (SELECT NVL(ROUND(SUM(ca.sjcr), 2), 0) AS crzl
                 FROM DELIVERY_CAXX ca
                          JOIN DELIVERY_KQXX kq ON ca.kqid = kq.id
                <where>
                    <include refid="queryShengInfo"/>
                </where>) AS crzl,
                (SELECT NVL(ROUND(SUM(cr.jz), 2), 0) AS cksl
                 FROM DELIVERY_CRKXX cr JOIN DELIVERY_KQXX kq ON cr.kqid = kq.id
                <where>
                    cr.crklx = 0
                    <include refid="queryShengInfo"/>
                </where>) AS cksl,
                (SELECT NVL(ROUND(SUM(cr.jz), 2), 0) AS rksl
                 FROM DELIVERY_CRKXX cr JOIN DELIVERY_KQXX kq ON cr.kqid = kq.id
                <where>
                    cr.crklx = 1
                    <include refid="queryShengInfo"/>
                </where>) AS rksl,
                (SELECT NVL(ROUND(SUM(hw.jjsl) / 10000, 2), 0) AS kcsl
                 FROM DELIVERY_HWK hw
                JOIN DELIVERY_KQXX kq ON hw.kqid = kq.id
                <where>
                    <include refid="queryShengInfo"/>
                </where>
                ) AS kcsl
        FROM dual;
    </select>

</mapper>