<?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.mobileInfo.modular.crk.mapper.CrkMapper">

    <select id="getData" parameterType="com.unis.mobileInfo.modular.crk.param.CrkParam" resultType="java.util.Map">
      <!-- ywlx, 1入库 2出库-->
        <if test="ywlx == 1">
            SELECT
            a.BizNo BizNo,
            a.UnitID,
            d.org_name orgName,
            '入库' ywlx,
            b.xzklhjz jz,
            a.lspz,
            e.enumname lspzmc,
            DATE_FORMAT(c.cmsj, '%Y-%m-%d %H:%i:%s') cmsj
            FROM
            depot_qh.data_crkyw_rmdj_default a
            INNER JOIN depot_qh.data_crkyw_cpz_default b ON a.BizNo = b.BizNo
            INNER JOIN depot_qh.data_crkyw_rkcm_default c ON a.BizNo = c.BizNo
            INNER JOIN depot_qh.org_info d ON d.org_id = a.UnitID
            INNER JOIN depot_qh.basic_enum e ON e.enumId = a.lspz
        </if>
        <if test="ywlx == 2">
            SELECT
            a.BizNo BizNo,
            a.UnitID,
            d.org_name orgName,
            '出库' ywlx,
            b.klhjz jz,
            a.lspz,
            e.enumname lspzmc,
            DATE_FORMAT(c.cmsj, '%Y-%m-%d %H:%i:%s') cmsj
            FROM
            depot_qh.data_crkyw_ckrmdj_default a
            INNER JOIN depot_qh.data_crkyw_ckcmz_default b ON a.BizNo = b.BizNo
            INNER JOIN depot_qh.data_crkyw_ckcm_default c ON a.BizNo = c.BizNo
            INNER JOIN depot_qh.org_info d ON d.org_id = a.UnitID
            INNER JOIN depot_qh.basic_enum e ON e.enumId = a.lspz
        </if>
         where 1=1
         <if test="orgId != null">
             and a.UnitID = #{orgId}
         </if>
        <if test="startCrksj != null">
            and c.cmsj &gt;= #{startCrksj}
        </if>
        <if test="endCrksj != null">
            and c.cmsj &lt;= #{endCrksj}
        </if>
    </select>

    <select id="getDetail" parameterType="com.unis.mobileInfo.modular.crk.param.CrkParam" resultType="java.util.Map">
        <!-- ywlx, 1入库  2出库-->
        <if test="ywlx == 1">
            SELECT
            a.BizNo BizNo,
            a.UnitID,
            d.org_name orgName,
            '入库' ywlx,
            b.xzklhjz,
            a.lspz,
            e.enumname lspzmc,
            c.cmsj,
            a.cph,
            a.cyr,
            a.lsxz,
            e1.enumname lsxzmc,
            a.lydj,
            e2.enumname lsdjmc,
            DATE_FORMAT(a.djsj, '%Y-%m-%d %H:%i:%s') rmsj,
            b.xzklhjz jz,
            b.pz,
            f.mz,
            b.sjchID,
            b.sjhwID,
            g.storehouse_name cfmc,
            h.warehouse_name hwmc,
            b.bgy
            FROM
            depot_qh.data_crkyw_rmdj_default a
            INNER JOIN depot_qh.data_crkyw_cpz_default b ON a.BizNo = b.BizNo
            INNER JOIN depot_qh.data_crkyw_rkcm_default c ON a.BizNo = c.BizNo
            INNER JOIN depot_qh.data_crkyw_cmz_default f ON a.BizNo = f.BizNo
            INNER JOIN depot_qh.org_info d ON d.org_id = a.UnitID
            INNER JOIN depot_qh.basic_enum e ON e.enumId = a.lspz
            INNER JOIN depot_qh.basic_enum e1 ON e1.enumId = a.lsxz
            INNER JOIN depot_qh.basic_enum e2 ON e2.enumId = a.lydj
            INNER JOIN depot_qh.basic_storehouse g ON g.storehouse_id = b.sjchID
            INNER JOIN depot_qh.basic_warehouse h ON h.warehouse_id = b.sjhwID
            where a.BizNo = #{bizNo}
        </if>
        <if test="ywlx == 2">
            SELECT
            a.BizNo BizNo,
            a.UnitID,
            d.org_name orgName,
            '出库' ywlx,
            b.klhjz jz,
            a.lspz,
            e.enumname lspzmc,
            DATE_FORMAT(c.cmsj, '%Y-%m-%d %H:%i:%s') cmsj,
            a.cph,
            a.cyr,
            a.lyxz,
            e1.enumname lsxzmc,
            a.lydj,
            e2.enumname lsdjmc,
            DATE_FORMAT(a.djsj, '%Y-%m-%d %H:%i:%s') rmsj,
            b.klhjz jz,
            w.pz,
            b.mz,
            a.chID,
            a.hwID,
            g.storehouse_name cfmc,
            h.warehouse_name hwmc,
            b.bgy
            FROM
            depot_qh.data_crkyw_ckrmdj_default a
            INNER JOIN depot_qh.data_crkyw_ckcmz_default b ON a.BizNo = b.BizNo
            INNER JOIN depot_qh.data_crkyw_ckcm_default c ON a.BizNo = c.BizNo
            INNER JOIN depot_qh.data_crkyw_ckcpz_default w ON a.BizNo = w.BizNo
            INNER JOIN depot_qh.org_info d ON d.org_id = a.UnitID
            INNER JOIN depot_qh.basic_enum e ON e.enumId = a.lspz
            INNER JOIN depot_qh.basic_enum e1 ON e1.enumId = a.lyxz
            INNER JOIN depot_qh.basic_enum e2 ON e2.enumId = a.lydj
            INNER JOIN depot_qh.basic_storehouse g ON g.storehouse_id = a.chID
            INNER JOIN depot_qh.basic_warehouse h ON h.warehouse_id = a.hwID
            where a.BizNo = #{bizNo}
        </if>
    </select>

    <select id="businessRecord" parameterType="com.unis.mobileInfo.modular.crk.param.BusinessRecordParam" resultType="java.util.Map">
        select * from(
        SELECT
        a.BizNo BizNo,
        a.UnitID,
        d.org_name orgName,
        '入库' ywlx,
        a.lspz lspz,
        e.enumname lspzmc,
        a.cph cph,
        a.lsxz lsxz,
        e1.enumname lsxzmc,
        DATE_FORMAT(
        b.zykssj,
        '%Y-%m-%d %H:%i:%s'
        ) zykssj,
        b.pz pz,
        f.mz mz
        FROM
        depot_qh.data_crkyw_rmdj_default a
        INNER JOIN depot_qh.data_crkyw_cpz_default b ON a.BizNo = b.BizNo
        INNER JOIN depot_qh.data_crkyw_rkcm_default c ON a.BizNo = c.BizNo
        INNER JOIN depot_qh.data_crkyw_cmz_default f ON a.BizNo = f.BizNo
        INNER JOIN depot_qh.org_info d ON d.org_id = a.UnitID
        INNER JOIN depot_qh.basic_enum e ON e.enumId = a.lspz
        INNER JOIN depot_qh.basic_enum e1 ON e1.enumId = a.lsxz
        UNION
        SELECT
        a.BizNo BizNo,
        a.UnitID,
        d.org_name orgName,
        '出库' ywlx,
        a.lspz lspz,
        e.enumname lspzmc,
        a.cph cph,
        a.lyxz lyxz,
        e1.enumname lsxzmc,
        DATE_FORMAT(
        b.zykssj,
        '%Y-%m-%d %H:%i:%s'
        ) zykssj,
        w.pz pz,
        b.mz mz
        FROM
        depot_qh.data_crkyw_ckrmdj_default a
        INNER JOIN depot_qh.data_crkyw_ckcmz_default b ON a.BizNo = b.BizNo
        INNER JOIN depot_qh.data_crkyw_ckcm_default c ON a.BizNo = c.BizNo
        INNER JOIN depot_qh.data_crkyw_ckcpz_default w ON a.BizNo = w.BizNo
        INNER JOIN depot_qh.org_info d ON d.org_id = a.UnitID
        INNER JOIN depot_qh.basic_enum e ON e.enumId = a.lspz
        INNER JOIN depot_qh.basic_enum e1 ON e1.enumId = a.lyxz
        )a
        where 1=1
        <if test="businessRecordParam.orgName != null">
            and orgName like CONCAT('%', #{businessRecordParam.orgName}, '%')
        </if>
        <if test="businessRecordParam.bizNo != null">
            and BizNo = #{businessRecordParam.bizNo}
        </if>
        <if test="businessRecordParam.cph != null">
            and cph = #{businessRecordParam.cph}
        </if>
        <if test="businessRecordParam.startCrksj != null">
            and zykssj &gt;= #{businessRecordParam.startCrksj}
        </if>
        <if test="businessRecordParam.endCrksj != null">
            and zykssj &lt;= #{businessRecordParam.endCrksj}
        </if>
        <if test="businessRecordParam.orgId != null">
            and UnitID = #{businessRecordParam.orgId}
        </if>
    </select>

    <select id="getBusinessRecordDetail" parameterType="com.unis.mobileInfo.modular.crk.param.BusinessRecordParam" resultType="java.util.Map">
        <!-- 1入库 2出库-->
        <if test="businessRecordParam.ywlx == 1 ">
        select
            a.BizNo BizNo,
            -- 单据号
            a.UnitID,
            d.org_name orgName,
            -- 库区名称
            '入库' ywlx,
            b.xzklhjz,
            a.lspz,
            e.enumname lspzmc,
            -- 粮食品种
            c.cmsj,
            a.cph,
            -- 车辆编号
            a.lsxz,
            e1.enumname lsxzmc,
            -- 粮食性质
            a.lydj,
            e2.enumname lsdjmc,
            -- 等级名称
            DATE_FORMAT(a.djsj, '%Y-%m-%d %H:%i:%s') rmsj,
            b.xzklhjz jz,
            b.pz,
            -- 皮重
            f.mz,
            -- 毛重
            b.sjchID,
            b.sjhwID,
            g.storehouse_name cfmc,
            -- 仓房名称
            h.warehouse_name hwmc,
            b.bgy,
            DATE_FORMAT(
            b.zykssj,
            '%Y-%m-%d %H:%i:%s'
            ) zykssj,
            -- 业务时间
            a.jhdbh jhdbh,
            -- 计划单号
            a.htbh htbh,
            -- 合同编号
            e6.enumname scnf,
            -- 生产年份
            a.slr khmc,
            -- 客户名称
            a.cyr cyr,
            -- 承运人
            a.cyrlxdh cyrlxdh,
            -- 承运人电话
            a.cyrlxdz cyrlxdz,
            -- 承运人地址
            a.cyrsfzh cyrsfzh,
            -- 承运人证照号
            e3.enumname cllx,
            -- 车辆类型
            a.slrdh khlxdh,
            -- 联系电话
            e4.enumname cd,
            -- 产地
            a.slrsfzh slrsfzh,
            -- 身份证
            a.slrdz slrdz,
            -- 地址
            e5.enumname cphlx,
            -- 车牌号类型
            a.gch gch,
            -- 挂车号
            a.djr djr,
            -- 登记人
            qy.qydh qydh,
            -- 扦样单号
            qy.qydw qydw,
            -- 扦样点位
            qy.qyqy qyqy,
            -- 扦样区域
            qy.ypdbsl ypdbsl,
            -- 样品代表数量
            DATE_FORMAT(
            qy.qysj,
            '%Y-%m-%d %H:%i:%s'
            ) qysj,
            -- 扦样时间
            qy.qyr qyr,
            -- 扦样人
            jy.jydh jydh,
            -- 化验单号
            case jy.jyjg when '3145' then '不合格' when '3146' then '合格' end jyjg ,
            -- 检验结果
            jy.jyr jyr,
            -- 检验人
            jy.dj dj,
            -- 单价
            DATE_FORMAT(
            jy.jysj,
            '%Y-%m-%d %H:%i:%s'
            ) jysj,
            -- 检验时间
            b.jjdh jjdh,
            -- 检斤单号
            DATE_FORMAT(
            b.cpzsj,
            '%Y-%m-%d %H:%i:%s'
            ) cpzsj,
            -- 称皮时间
            DATE_FORMAT(
            f.cmzsj,
            '%Y-%m-%d %H:%i:%s'
            ) cmzsj,
            -- 称毛时间
            f.mjy mjy,
            -- 毛检员
            b.pjy pjy,
            -- 皮检员
            b.xzzl xzzl,
            -- 卸载重量
            b.jpy jpy,
            -- 监磅员
            b.jpyyj jpyyj,
            -- 监磅员意见
            b.xckz xckz,
            -- 现场扣杂
            b.xcks xcks,
            -- 现场扣水
            b.zyjssj zykssj,
            -- 作业开始时间
            b.zyjssj zyjssj,
            -- 作业结束时间
            js.hhyhmc hhyhmc,
            -- 开户银行名称
            js.hfyhhb hfyhhb,
            -- 开户行行别
            js.khhh khhh -- 开户行号
            FROM
            depot_qh.data_crkyw_rmdj_default a
            INNER JOIN depot_qh.data_crkyw_cpz_default b ON a.BizNo = b.BizNo
            INNER JOIN depot_qh.data_crkyw_rkcm_default c ON a.BizNo = c.BizNo
            INNER JOIN depot_qh.data_crkyw_cmz_default f ON a.BizNo = f.BizNo
            INNER JOIN depot_qh.org_info d ON d.org_id = a.UnitID
            INNER JOIN depot_qh.basic_enum e ON e.enumId = a.lspz
            INNER JOIN depot_qh.basic_enum e1 ON e1.enumId = a.lsxz
            INNER JOIN depot_qh.basic_enum e2 ON e2.enumId = a.lydj
            INNER JOIN depot_qh.basic_enum e3 ON e3.enumId = b.cllx
            INNER JOIN depot_qh.basic_enum e4 ON e4.enumId = a.cd
            INNER JOIN depot_qh.basic_enum e5 ON e5.enumId = a.cchlx
            INNER JOIN depot_qh.basic_enum e6 ON e6.enumId = a.scnf
            INNER JOIN depot_qh.basic_storehouse g ON g.storehouse_id = b.sjchID
            INNER JOIN depot_qh.basic_warehouse h ON h.warehouse_id = b.sjhwID
            INNER JOIN depot_qh.data_crkyw_qyxx_default qy ON qy.BizNo = a.BizNo
            INNER JOIN depot_qh.data_crkyw_jyxx_default jy ON jy.BizNo = a.BizNo
            INNER JOIN depot_qh.data_crkyw_jsxx_default js ON js.BizNo = a.BizNo
            where a.BizNo = #{businessRecordParam.bizNo}
        </if>

        <if test="businessRecordParam.ywlx == 2">
            SELECT
            a.BizNo BizNo,
            a.UnitID,
            d.org_name orgName,
            '出库' ywlx,
            b.klhjz jz,
            a.lspz,
            e.enumname lspzmc,
            DATE_FORMAT(c.cmsj, '%Y-%m-%d %H:%i:%s') cmsj,
            a.cph,
            a.cyr,
            a.lyxz,
            e1.enumname lsxzmc,
            a.lydj,
            e2.enumname lsdjmc,
            DATE_FORMAT(a.djsj, '%Y-%m-%d %H:%i:%s') rmsj,
            b.klhjz jz,
            w.pz,
            b.mz,
            a.chID,
            a.hwID,
            g.storehouse_name cfmc,
            h.warehouse_name hwmc,
            b.bgy,
            DATE_FORMAT(
            b.zykssj,
            '%Y-%m-%d %H:%i:%s'
            ) zykssj,
            -- 业务时间
            a.jhdbh jhdbh,
            -- 计划单号
            a.htbh htbh,
            -- 合同编号
            e6.enumname scnf,
            -- 生产年份
            a.kfmc khmc,
            -- 客户名称
            a.cyr cyr,
            -- 承运人
            w.cyrdh cyrlxdh,
            -- 承运人电话
            a.cyrdz cyrlxdz,
            -- 承运人地址
            c.cyrsfzh cyrsfzh,
            -- 承运人证照号
            e3.enumname cllx,
            -- 车辆类型
            a.slrdh khlxdh,
            -- 联系电话
            e4.enumname cd,
            -- 产地
            a.slrsfzh slrsfzh,
            -- 身份证
            a.slrdz slrdz,
            -- 地址
            e5.enumname cphlx,
            -- 车牌号类型
            a.gch gch,
            -- 挂车号
            a.djr djr,
            -- 登记人
            -- 检验时间
            b.jjdh jjdh,
            -- 检斤单号
            DATE_FORMAT(w.jpsj, '%Y-%m-%d %H:%i:%s') cpzsj,
            -- 称皮时间
            DATE_FORMAT(b.jmsj, '%Y-%m-%d %H:%i:%s') cmzsj,
            -- 称毛时间
            b.mjy mjy,
            -- 毛检员
            w.pjy pjy,
            -- 皮检员
            b.klhjz xzzl,
            -- 卸载重量
            b.jpy jpy,
            -- 监磅员
            b.jpyyj jpyyj,
            -- 监磅员意见
            -- b.xckz xckz,
            -- 现场扣杂
            -- b.xcks xcks,
            -- 现场扣水
            b.zyjssj zyjssj,
            -- 作业开始时间
            b.zyjssj zyjssj -- 作业结束时间
            FROM
            depot_qh.data_crkyw_ckrmdj_default a
            INNER JOIN depot_qh.data_crkyw_ckcmz_default b ON a.BizNo = b.BizNo
            INNER JOIN depot_qh.data_crkyw_ckcm_default c ON a.BizNo = c.BizNo
            INNER JOIN depot_qh.data_crkyw_ckcpz_default w ON a.BizNo = w.BizNo
            INNER JOIN depot_qh.org_info d ON d.org_id = a.UnitID
            INNER JOIN depot_qh.basic_enum e ON e.enumId = a.lspz
            INNER JOIN depot_qh.basic_enum e1 ON e1.enumId = a.lyxz
            INNER JOIN depot_qh.basic_enum e2 ON e2.enumId = a.lydj
            INNER JOIN depot_qh.basic_enum e3 ON e3.enumId = w.cllx
            INNER JOIN depot_qh.basic_enum e4 ON e4.enumId = a.cd
            INNER JOIN depot_qh.basic_enum e5 ON e5.enumId = a.cchlx
            INNER JOIN depot_qh.basic_enum e6 ON e6.enumId = a.scnf
            INNER JOIN depot_qh.basic_storehouse g ON g.storehouse_id = a.chID
            INNER JOIN depot_qh.basic_warehouse h ON h.warehouse_id = a.hwID
            where a.BizNo = #{businessRecordParam.bizNo}
        </if>
    </select>

    <select id="getRkhy" parameterType="java.lang.String" resultType="java.util.Map">
        select a.zbxmc,a.jyz,a.jykjje,a.zklsl,b.enumname from data_crkyw_jyxx_flt_001  a
        inner JOIN basic_enum b on a.zbxmc = b.enumId
        where a.MainDataID in (
            select DataID from data_crkyw_jyxx_default where BizNo = #{bizNo}
        )
    </select>

    <select id="getTp" parameterType="java.lang.String" resultType="java.util.Map">
        select FileName,ExtensionName,URL from attaches a
        where a.ReferencesID in (
            select DataID from data_crkyw_ckcmz_default where BizNo = #{bizNo}
        )
        UNION
        select FileName,ExtensionName,URL from attaches a
        where a.ReferencesID in (
            select DataID from data_crkyw_cmz_default where BizNo = #{bizNo}
        )
    </select>
</mapper>