<?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.crk.business.qcrk.mapper.DataCrkywTzdxxDefaultMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.unis.crk.business.qcrk.entity.DataCrkywTzdxxDefault">
        <id column="DataID" property="DataID" />
        <result column="UnitID" property="UnitID" />
        <result column="PeriodID" property="PeriodID" />
        <result column="RoundID" property="RoundID" />
        <result column="builder" property="builder" />
        <result column="UPDATE_TIME" property="updateTime" />
        <result column="SytelInfoID" property="SytelInfoID" />
        <result column="BuildDate" property="BuildDate" />
        <result column="editor" property="editor" />
        <result column="ModifyDate" property="ModifyDate" />
        <result column="DeptID" property="DeptID" />
        <result column="tzdID" property="tzdID" />
        <result column="kfmc" property="kfmc" />
        <result column="khID" property="khID" />
        <result column="htID" property="htID" />
        <result column="htbh" property="htbh" />
        <result column="lspz" property="lspz" />
        <result column="lsxz" property="lsxz" />
        <result column="mxpz" property="mxpz" />
        <result column="rksj" property="rksj" />
        <result column="sl" property="sl" />
        <result column="sldx" property="sldx" />
        <result column="dj" property="dj" />
        <result column="lsnq" property="lsnq" />
        <result column="dcfy" property="dcfy" />
        <result column="je" property="je" />
        <result column="lsdj" property="lsdj" />
        <result column="bz" property="bz" />
        <result column="rz" property="rz" />
        <result column="sf" property="sf" />
        <result column="zz" property="zz" />
        <result column="bwsl" property="bwsl" />
        <result column="kddw" property="kddw" />
        <result column="kdr" property="kdr" />
        <result column="kdsj" property="kdsj" />
        <result column="jhdID" property="jhdID" />
        <result column="jhdbh" property="jhdbh" />
        <result column="cczyfl" property="cczyfl" />
        <result column="tzdzt" property="tzdzt" />
        <result column="tzdbh" property="tzdbh" />
        <result column="thdbh" property="thdbh" />
        <result column="sysl" property="sysl" />
        <result column="scnf" property="scnf" />
        <result column="cd" property="cd" />
        <result column="zxbm" property="zxbm" />
        <result column="zrbgy" property="zrbgy" />
        <result column="wcsl" property="wcsl" />
        <result column="rukushijian" property="rukushijian" />
        <result column="slrdh" property="slrdh" />
        <result column="slrsfzh" property="slrsfzh" />
        <result column="slrdz" property="slrdz" />
        <result column="slr" property="slr" />
        <result column="hhyhmc" property="hhyhmc" />
        <result column="hfyhhb" property="hfyhhb" />
        <result column="cfmc" property="cfmc" />
        <result column="hwmc" property="hwmc" />
        <result column="yhkh" property="yhkh" />
        <result column="jhfl" property="jhfl" />
        <result column="jhkh" property="jhkh" />
        <result column="ysfs" property="ysfs" />
        <result column="sfrglr" property="sfrglr" />
        <result column="skr" property="skr" />
        <result column="jhapbh" property="jhapbh" />
        <result column="zssl" property="zssl" />
        <result column="zylx" property="zylx" />
        <result column="FLOW_ID" property="flowId" />
        <result column="FLOW_STATUS" property="flowStatus" />
    </resultMap>

    <select id="selectMyMapsPage" resultType="java.util.Map">
        SELECT DISTINCT
        DATA_CRKYW_TZDXX_DEFAULT.ModifyDate AS "ModifyDate",
        DATA_CRKYW_TZDXX_DEFAULT.FLOW_ID AS FLOWID,
        DATA_CRKYW_TZDXX_DEFAULT.FLOW_STATUS AS FLOWSTATUS,
        DATA_CRKYW_TZDXX_DEFAULT.sl AS "sl",
        DATA_CRKYW_TZDXX_DEFAULT.lspz AS "lspz",
        b_1.enumName AS "lspz_value",
        DATA_CRKYW_TZDXX_DEFAULT.kdsj AS "kdsj",
        DATA_CRKYW_TZDXX_DEFAULT.sysl AS "sysl",
        DATA_CRKYW_TZDXX_DEFAULT.htbh AS "htbh",
        DATA_CRKYW_TZDXX_DEFAULT.tzdbh AS "tzdbh",
        DATA_CRKYW_TZDXX_DEFAULT.jhdbh AS "jhdbh",
        DATA_CRKYW_TZDXX_DEFAULT.slr AS "slr",
        DATA_CRKYW_TZDXX_DEFAULT.DataID AS DataID
        FROM
        DATA_CRKYW_TZDXX_DEFAULT
        LEFT JOIN basic_enum b_1 ON DATA_CRKYW_TZDXX_DEFAULT.lspz = b_1.enumId
        WHERE
        ifnull(
        DATA_CRKYW_TZDXX_DEFAULT.SytelInfoID,
        ''
        ) = '2c9180856f7a0b21016f7e7c54e21cc1'
        AND DATA_CRKYW_TZDXX_DEFAULT.PeriodID = '11'
        AND DATA_CRKYW_TZDXX_DEFAULT.RoundId = 0
        <if test="ew.sqlSegment != null and ew.sqlSegment != ''">
            and ${ew.sqlSegment}
        </if>
    </select>
    <select id="queryQcInTzdInf" resultType="java.util.Map">
          SELECT
        ifnull(xzklhjz, 0.0) AS "zl",
        ifnull(c.sl, 0) AS "sl",
        ifnull(c.sysl, 0.0) AS "sysl",
        ifnull(c.wcsl, 0.0) AS "wcsl",
        ifnull(c.DataID, '') AS "dataId",
        ifnull(b.tzdcbid, '') AS "tzdcbid",
        ifnull(b.tzdbh, '') AS "tzdh"
    FROM
        data_crkyw_cpz_default a
    LEFT JOIN data_crkyw_rmdj_default b ON a.BizNo = b.BizNo
    LEFT JOIN data_crkyw_tzdxx_default c ON b.tzdbh = c.tzdbh
    WHERE
        a.dataid =#{dataId}
    </select>
    <select id="queryQcInTzdInfByDepot" resultType="java.util.Map">
          SELECT
        ifnull(xzklhjz, 0.0) AS "zl",
        ifnull(c.shiping_count, 0.0) AS "sl",
        ifnull(c.remain_quantity, 0.0) AS "sysl",
        ifnull(c.complete_quantity, 0.0) AS "wcsl",
        ifnull(c.id, '') AS "dataId",
        ifnull(b.tzdcbid, '') AS "tzdcbid",
        ifnull(b.tzdbh, '') AS "tzdh"
    FROM
        data_crkyw_cpz_default a
    LEFT JOIN data_crkyw_rmdj_default b ON a.BizNo = b.BizNo
    LEFT JOIN business_delivery_storage_notice c ON b.tzdbh = c.bill_number
    WHERE
        a.dataid =#{dataId}
    </select>
    <select id="queryQCOutTzdInf" resultType="java.util.Map">
         SELECT
        ifnull(klhjz, 0.0) AS "zl",
        ifnull(c.sl, 0) AS "sl",
        ifnull(c.sysl, 0.0) AS "sysl",
        ifnull(c.wcsl, 0.0) AS "wcsl",
        ifnull(c.DataID, '') AS "dataId",
        ifnull(b.tzdcbid, '') AS "tzdcbid",
        ifnull(b.tzdbh, '') AS "tzdh"
    FROM
        data_crkyw_ckcmz_default a
    LEFT JOIN data_crkyw_ckrmdj_default b ON a.BizNo = b.BizNo
    LEFT JOIN data_crkyw_tzdxx_default c ON b.tzdbh = c.tzdbh
    WHERE
        a.dataid =#{dataId}
    </select>
    <select id="queryQCOutTzdInfByDepot" resultType="java.util.Map">
            SELECT
        ifnull(klhjz, 0.0) AS "zl",
        ifnull(c.shiping_count, 0.0) AS "sl",
        ifnull(c.remain_quantity, 0.0) AS "sysl",
        ifnull(c.complete_quantity, 0.0) AS "wcsl",
        ifnull(c.id, '') AS "dataId",
        ifnull(b.tzdcbid, '') AS "tzdcbid",
        ifnull(b.tzdbh, '') AS "tzdh"
    FROM
        data_crkyw_ckcmz_default a
    LEFT JOIN data_crkyw_ckrmdj_default b ON a.BizNo = b.BizNo
     LEFT JOIN business_delivery_storage_notice c ON b.tzdbh = c.bill_number
    WHERE
        a.dataid =#{dataId}
    </select>
    <select id="getTzdList" resultType="java.util.Map">
        SELECT DISTINCT
	DATA_CRKYW_TZDXX_FLT_001.DataID AS "fltDataId",
	DATA_CRKYW_TZDXX_DEFAULT.dataid AS "dataid",
	DATE_FORMAT(
		DATA_CRKYW_TZDXX_FLT_001.rcsj,
		'%Y-%m-%d %T'
	) AS rcsj,
	DATA_CRKYW_TZDXX_DEFAULT.htbh AS htbh,
	DATA_CRKYW_TZDXX_DEFAULT.slrdz AS slrdz,
	DATA_CRKYW_TZDXX_FLT_001.sysl AS sysl,
	cd_enum.enumName AS "cd_value",
	DATA_CRKYW_TZDXX_DEFAULT.cd AS "cd",
	tzscnf_enum.enumName AS "tzscnf_value",
	DATA_CRKYW_TZDXX_DEFAULT.scnf AS "scnf",
	DATA_CRKYW_TZDXX_DEFAULT.sl AS sl,
	DATA_CRKYW_TZDXX_DEFAULT.skr AS skr,
	DATA_CRKYW_TZDXX_FLT_001.hwhmc AS hwhmc,
	DATA_CRKYW_TZDXX_DEFAULT.slrdh AS slrdh,
	DATA_CRKYW_TZDXX_FLT_001.hwID AS hwID,
	grain_detail_kind_enum.enumName AS "mxpz_value",
	DATA_CRKYW_TZDXX_FLT_001.grain_detail_kind AS "mxpz",
	DATA_CRKYW_TZDXX_DEFAULT.tzdID AS tzdID,
	DATA_CRKYW_TZDXX_DEFAULT.slr AS slr,
	grain_attribute_enum.enumName AS "lsxz_value",
	DATA_CRKYW_TZDXX_FLT_001.grain_attribute AS "lsxz",
	grain_grade_enum.enumName AS "lydj_value",
	DATA_CRKYW_TZDXX_FLT_001.grain_grade AS "lydj",
	DATA_CRKYW_TZDXX_FLT_001.chmc AS chmc,
	DATA_CRKYW_TZDXX_DEFAULT.jhdbh AS jhdbh,
	grain_kind_enum.enumName AS "lspz_value",
	DATA_CRKYW_TZDXX_FLT_001.grain_kind AS "lspz",
	DATA_CRKYW_TZDXX_DEFAULT.jhfl AS jhfl,
	DATA_CRKYW_TZDXX_DEFAULT.slrsfzh AS slrsfzh,
	DATA_CRKYW_TZDXX_DEFAULT.tzdbh AS tzdbh,
	DATA_CRKYW_TZDXX_FLT_001.chID AS chID,
	DATA_CRKYW_TZDXX_DEFAULT.FLOW_ID AS FLOWID
FROM
	DATA_CRKYW_TZDXX_DEFAULT
LEFT JOIN DATA_CRKYW_TZDXX_FLT_001 ON DATA_CRKYW_TZDXX_FLT_001.MainDataID = DATA_CRKYW_TZDXX_DEFAULT.DataID
LEFT JOIN basic_enum lsxz_enum ON lsxz_enum.enumId = DATA_CRKYW_TZDXX_DEFAULT.lsxz
 LEFT JOIN basic_enum tzscnf_enum ON tzscnf_enum.enumId = DATA_CRKYW_TZDXX_DEFAULT.scnf
LEFT JOIN basic_enum cd_enum ON cd_enum.enumId = DATA_CRKYW_TZDXX_DEFAULT.cd
LEFT JOIN basic_enum grain_detail_kind_enum ON grain_detail_kind_enum.enumId = DATA_CRKYW_TZDXX_FLT_001.grain_detail_kind
LEFT JOIN basic_enum grain_attribute_enum ON  grain_attribute_enum.enumId = DATA_CRKYW_TZDXX_FLT_001.grain_attribute
LEFT JOIN basic_enum grain_grade_enum ON grain_grade_enum.enumId = DATA_CRKYW_TZDXX_FLT_001.grain_grade
LEFT JOIN basic_enum grain_kind_enum ON  grain_kind_enum.enumId = DATA_CRKYW_TZDXX_FLT_001.grain_kind
 WHERE
        DATA_CRKYW_TZDXX_DEFAULT.UnitID = #{unitId}
    AND DATA_CRKYW_TZDXX_DEFAULT.FLOW_ID = #{flowId}
    AND DATA_CRKYW_TZDXX_DEFAULT.sysl > 0 and DATA_CRKYW_TZDXX_FLT_001.sysl > 0
    ORDER BY tzdbh desc
    </select>
    <select id="getTzdListByDepot" resultType="java.util.Map">
        SELECT
        detail.id AS "fltDataId", -- 从表id
        notice.id AS "dataid",  -- 主表ID
        notice.id AS tzdID, -- 通知单id
        notice.bill_number AS tzdbh, -- 通知单编号,
        shiping_count  as sl, -- 通知单数量
        IF(detail.in_count > 0,in_count,out_count)-IFNULL(detail.complete_quantity,0) AS sysl, -- 剩余数量
        IF(detail.in_count > 0,'A7B1FDB54DE7B32D6F8067FEFE2BF52F','6FD0418CD28A2B3F3E7E9EC0D7429461') as FLOWID, -- 流程Id
        DATE_FORMAT(detail.input_time,'%Y-%m-%d %T') AS rcsj -- 入仓时间
        ,notice.contract AS htbh -- 合同编号
        ,notice.contract_bid AS htID -- 合同id
        ,plan.plan_number AS jhdbh -- 计划编号
        ,notice.plan_bid AS jhdID -- 计划id
        ,enum.enumName  as jhfl -- 计划分类
        ,customer.name as slr -- <!-- 客户名称 -->
        ,customer.address as slrdz, -- <!-- 地址 -->
        IFNULL(customer.social_credit_code,customer.identification) as slrsfzh,-- <!-- 企业信用代码或者身份证号 -->
        customer.mobile as slrdh,-- <!-- 手机号 -->
        customer.account_number yhkh, -- 开户账号
        customer.bank_name khyhmc, -- 开户银行名称
        customer.account_name khyhhb, --  开户银行行别
        customer.administrative_division skr,-- <!-- 收款人 -->
        customer.business_regist_num jhkh, -- <!-- 计划客户 -->

        grain_detail_kind_enum.enumName AS "mxpz_value",  -- 明细品种名称
        detail.grain_detail_kind AS "mxpz",

        grain_attribute_enum.enumName AS "lsxz_value", -- 性质名称
        detail.grain_attribute AS "lsxz",

        grain_grade_enum.enumName AS "lydj_value", -- 等级名称
        detail.grain_grade AS "lydj",

        grain_kind_enum.enumName AS "lspz_value", -- 品种名称
        detail.grain_kind AS "lspz",

        cd_enum.enumName AS "cd_value",  -- 产地名称
        detail.grain_producing_area AS "cd",

        tzscnf_enum.enumName AS "tzscnf_value", -- 生产年份名称
        detail.grain_annual AS "scnf",

        detail.house_id as chID, -- 仓房id
        detail.warehouse_id as hwID, -- 货位id
        detail.house_name as chmc, -- 仓房名称
        detail.warehouse_name as hwhmc -- 货位名称
        FROM
        business_delivery_storage_notice notice
        inner JOIN business_store_ware_detail detail on notice.id = detail.zid
        left join business_customer customer on customer.id=notice.customer_bid
        left join business_plan plan on notice.plan_bid = plan.id
        left join basic_enum enum on  plan.execute_type = enum.enumId

        LEFT JOIN basic_enum lsxz_enum ON lsxz_enum.enumId = detail.grain_attribute
        LEFT JOIN basic_enum tzscnf_enum ON tzscnf_enum.enumId = detail.grain_annual
        LEFT JOIN basic_enum cd_enum ON cd_enum.enumId = detail.grain_producing_area

        LEFT JOIN basic_enum grain_attribute_enum ON  grain_attribute_enum.enumId = detail.grain_attribute
        LEFT JOIN basic_enum grain_grade_enum ON grain_grade_enum.enumId = detail.grain_grade
        LEFT JOIN basic_enum grain_kind_enum ON  grain_kind_enum.enumId = detail.grain_kind
        LEFT JOIN basic_enum grain_detail_kind_enum ON grain_detail_kind_enum.enumId = detail.grain_detail_kind

        WHERE ((notice.audit_state=2) or (notice.bill_type=2)) -- <!-- 审批通过的,中转通知单没有审批 -->
        and notice.in_application = 1
        AND notice.house_id = #{unitId}   -- <!--用house_id 区分是哪个库的 house_id是代储库 orgId 是主库 -->
        and IF(detail.in_count > 0,'A7B1FDB54DE7B32D6F8067FEFE2BF52F','6FD0418CD28A2B3F3E7E9EC0D7429461') = #{flowId}
        AND IF(detail.in_count > 0,in_count,out_count)-IFNULL(detail.complete_quantity,0) >0
        ORDER BY notice.bill_number
    </select>
</mapper>