<?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.kcgl.mapper.DataKcglKcswDefaultMapper">

	<!-- 通用查询映射结果 -->
	<resultMap id="BaseResultMap" type="com.unis.crk.business.kcgl.entity.DataKcglKcswDefault">
		<id column="DataID" property="DataID" />
		<result column="UnitID" property="UnitID" />
		<result column="PeriodID" property="PeriodID" />
		<result column="RoundID" property="RoundID" />
		<result column="ModifyDate" property="ModifyDate" />
		<result column="builder" property="builder" />
		<result column="UPDATE_TIME" property="updateTime" />
		<result column="DeptID" property="DeptID" />
		<result column="SytelInfoID" property="SytelInfoID" />
		<result column="BuildDate" property="BuildDate" />
		<result column="editor" property="editor" />
		<result column="lkmc" property="lkmc" />
		<result column="lkbm" property="lkbm" />
		<result column="hwxz" property="hwxz" />
		<result column="ch" property="ch" />
		<result column="hwh" property="hwh" />
		<result column="pz" property="pz" />
		<result column="mxpz" property="mxpz" />
		<result column="dj" property="dj" />
		<result column="rq" property="rq" />
		<result column="wjh" property="wjh" />
		<result column="kcsl" property="kcsl" />
		<result column="kcsjsl" property="kcsjsl" />
		<result column="cjsj" property="cjsj" />
		<result column="hcph" property="hcph" />
		<result column="gb" property="gb" />
		<result column="scdd" property="scdd" />
		<result column="bz" property="bz" />
		<result column="scnf" property="scnf" />
	</resultMap>

    <select id="selectMyMapsPage" resultType="java.util.Map">
        SELECT *   FROM  data_kcgl_kcsw_default
        <if test="ew.sqlSegment != null and ew.sqlSegment != ''">
            where  ${ew.sqlSegment}
        </if>
    </select>
    <select id="queryStockDetail"  resultType="java.util.Map">
           select unitId, bizno,ifnull(kybgh,'') as kybgh,ifnull(lkbh,'') as lkbh,ifnull(lkmc, '') as lkmc, sjcf,sjhw,
			ifnull(pz,'') as pz, ifnull(mxpz,'') AS mxpz, ifnull(hwxz,'') AS lsxz, rcrq,
			(case cazylx when 1 THEN ifnull(xxklhjz,0) ELSE 0 END) AS IN_NUM,
			(case cazylx when 3 THEN ifnull(xxklhjz,0) ELSE 0 END) AS OUT_NUM
		FROM data_kcgl_xckc_default where DataID=#{dataId}
	</select>
    <select id="queryStockSum"  resultType="java.util.Map">
          SELECT dataid as "dataid",ifnull(kcsl,0.0) as "kcsl",ifnull(kcsjsl,0.0) as "kcsjsl" FROM data_kcgl_kcsw_default
		WHERE unitId=#{unitId}
		AND ifnull(ch,'')=#{ch} AND ifnull(hwh,'')=#{hwh} AND ifnull(pz,'')=#{pz} AND ifnull(hwxz,'')=#{hwxz}
	</select>
    <update id="updateStoreByBiz">
        UPDATE data_kcgl_kcsw_default SET kcsl=kcsl + (${stockNum}),ModifyDate=NOW(),UPDATE_TIME=null
		WHERE  unitId=#{unitId}
		AND ifnull(ch,'')=#{ch} AND ifnull(hwh,'')=#{hwh} AND ifnull(pz,'')=#{pz} AND ifnull(hwxz,'')=#{hwxz}
  </update>
    <insert id="addStoreByXckc">
INSERT INTO data_kcgl_kcsw_default(
			DataID, 		-- 1.数据记录ID,
			UnitID, 		-- 2.单位ID,
			PeriodID, 		-- 3.期间ID,
			RoundID, 		-- 4.舍位ID,
			ModifyDate, 	-- 5.最后一次编辑时间,
			builder, 		-- 6.条目创建人,
			lkmc, 			-- 7.粮库名称,
			lkbm, 			-- 8.粮库编码,
			hwxz, 			-- 9.货物性质,
			ch, 			-- 10.仓号,
			hwh, 			-- 11.货位号,
			pz, 			-- 12.品种,
			mxpz, 			-- 13.明细品种,
			dj, 			-- 14.等级,
			rq, 			-- 15.日期,
			wjh, 			-- 16.文件号,
			kcsl, 			-- 17.库存数量,
			cjsj, 			-- 18.创建时间
			hcph,			-- 19、火车皮号
			gb,     		-- 20、国别
			scdd, 			-- 21 收储地点 默认1 1:库内 2：库外
			kcsjsl,	 		-- 22 库存实际数量
			scnf   -- 23 生产年份
		)
		SELECT
			#{dataId}, -- 1.
			UnitID, -- 2.
			PeriodID, -- 3.
			RoundID, -- 4.
			NOW(), -- 5.
			builder, -- 6.
			lkmc, -- 7.
			lkbh, -- 8.
			hwxz, -- 9.
			sjcf, -- 10.
			sjhw, -- 11.
			pz, -- 12.
			mxpz, -- 13.
			dj, -- 14.
			rcrq, -- 15.
			kybgh, -- 16.
			${kcsl}, -- 17.
			rcrq, -- 18 防止期初数据时间不正确
			hcph,		-- 19、火车皮号
			hwcd,     -- 20、产地/国别
			'1' as "scdd", 		-- 21收储地点 默认1 1:库内 2：库外
			${kcsl}, 					-- 22.
			scnf   -- 23 生产年份
		FROM data_kcgl_xckc_default
		WHERE DataID=#{dataId};
    </insert>
</mapper>