<?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.DataKcglFcbgzDefaultMapper">

	<!-- 通用查询映射结果 -->
	<resultMap id="BaseResultMap" type="com.unis.crk.business.kcgl.entity.DataKcglFcbgzDefault">
		<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="BuildDate" property="BuildDate" />
		<result column="editor" property="editor" />
		<result column="ModifyDate" property="ModifyDate" />
		<result column="DeptID" property="DeptID" />
		<result column="SytelInfoID" property="SytelInfoID" />
		<result column="lkmc" property="lkmc" />
		<result column="ch" property="ch" />
		<result column="hwh" property="hwh" />
		<result column="pz" property="pz" />
		<result column="rq" property="rq" />
		<result column="wjh" property="wjh" />
		<result column="srsl" property="srsl" />
		<result column="zcsl" property="zcsl" />
		<result column="srsjsl" property="srsjsl" />
		<result column="zcsjsl" property="zcsjsl" />
		<result column="kcsjsl" property="kcsjsl" />
		<result column="kcsl" property="kcsl" />
		<result column="jzr" property="jzr" />
		<result column="lkbm" property="lkbm" />
		<result column="mxpz" property="mxpz" />
		<result column="dj" property="dj" />
		<result column="cjsj" property="cjsj" />
		<result column="hwxz" property="hwxz" />
		<result column="zy" property="zy" />
		<result column="OldDataID" property="OldDataID" />
		<result column="StockDataID" property="StockDataID" />
		<result column="Direction" property="Direction" />
		<result column="hwcd" property="hwcd" />
		<result column="scnf" property="scnf" />
		<result column="jjdh" property="jjdh" />
		<result column="hcph" property="hcph" />
		<result column="ljsrsjsl" property="ljsrsjsl" />
		<result column="ljsrsl" property="ljsrsl" />
		<result column="ljzcsjsl" property="ljzcsjsl" />
		<result column="ljzcsl" property="ljzcsl" />
		<result column="sssh" property="sssh" />
		<result column="bpsf" property="bpsf" />
		<result column="bpzz" property="bpzz" />
		<result column="pjsf" property="pjsf" />
		<result column="pjzz" property="pjzz" />
		<result column="zbszsl" property="zbszsl" />
		<result column="bz" property="bz" />
	</resultMap>

    <select id="selectMyMapsPage" resultType="java.util.Map">
        SELECT *   FROM  data_kcgl_fcbgz_default
        <if test="ew.sqlSegment != null and ew.sqlSegment != ''">
            where  ${ew.sqlSegment}
        </if>
    </select>
    <select id="queryFcbgzByid"  resultType="java.util.Map">
           SELECT
		DATE_FORMAT(rq, '%Y-%m-%d %T') AS "rq",
		ifnull(kcsl, 0) AS "kcsl",
		ifnull(srsl, 0) AS "srsl",
		ifnull(zcsl, 0) AS "zcsl",
		ifnull(kcsjsl, 0) AS "kcsjsl",
		ifnull(srsjsl, 0) AS "srsjsl",
		ifnull(zcsjsl, 0) AS "zcsjsl",
		ifnull(ljsrsl, 0) AS "ljsrsl",
		ifnull(ljzcsl, 0) AS "ljzcsl",
		ifnull(ljsrsjsl, 0) AS "ljsrsjsl",
		ifnull(ljzcsjsl, 0) AS "ljzcsjsl"
	FROM
		data_kcgl_fcbgz_default
	WHERE dataid = #{dataId}
	</select>
    <update id="updateFcbgzByWrapper">
    update  data_kcgl_fcbgz_default  set UPDATE_TIME= null,kcsl =kcsl+(${sl})	WHERE unitId=#{unitId}  AND ifnull(ch,'')=#{ch} AND ifnull(hwh,'')=#{hwh} AND ifnull(pz,'')=#{pz} AND ifnull(hwxz,'')=#{hwxz}
     and rq >= #{rq}   AND DataID != #{dataId}
  </update>
    <insert id="addFfcbgz">
 		INSERT INTO data_kcgl_fcbgz_default(
				DataID, 		-- 1.'数据记录ID',
				UnitID, 		-- 2.'单位ID',
				PeriodID, 		-- 3.'期间ID',
				RoundID, 		-- 4.'舍位ID',
				ModifyDate, 	-- 5.'最后一次编辑时间',
				builder, 		-- 6.'条目创建人',
				lkmc, 			-- 7.'粮库名称',
				ch, 			-- 8.'仓号',
				hwh, 			-- 9.'货位号',
				pz, 			-- 10.'品种',
				rq, 			-- 11.'日期',
				wjh, 			-- 12.'文件号',
				srsl, 			-- 13.'收入数量',
				zcsl, 			-- 14.'支出数量',
				kcsl, 			-- 15.'库存数量',
				jzr, 			-- 16.'记账人',
				lkbm, 			-- 17.'粮库编码',
				mxpz, 			-- 18.'明细品种',
				dj, 			-- 19.'等级',`
				cjsj, 			-- 20.'创建时间',
				hwxz, 			-- 21.'货物性质',
				zy, 			-- 22.'摘要',
				StockDataID,	-- 23.对应的库存明细数据id
				OldDataID,		-- 24.对应冲抵的数据id
				Direction,		-- 25.数据方向
				hwcd, 			-- 26、货物产地
				scnf, 			-- 27、生产年份
				jjdh,			-- 28、检斤单号
				hcph,			-- 29、火车皮号
				ljsrsl,			-- 30、累计收入数量
				ljzcsl,			-- 31、累计支出数量
				srsjsl,			-- 32、收入实际数量
				zcsjsl,			-- 33、支出实际数量
				kcsjsl,			-- 34、库存实际数量
				ljsrsjsl,		-- 35、累计收入实际数量
				ljzcsjsl,		-- 36、累计支出实际数量
				DeptID			-- 37、可以用于区分是否补录
			)
			SELECT
				A.DataID, 	-- 1.
				A.UnitID, 					-- 2.
				A.PeriodID, 				-- 3.
				A.RoundID, 					-- 4.
				NOW(), 						-- 5.
				#{userName}, 						-- 6.
				A.lkmc, 					-- 7.
				A.sjcf, 					-- 8.
				A.sjhw, 					-- 9.
				A.pz, 						-- 10.
				A.rcrq, 					-- 11.
				A.kybgh, 					-- 12.可研报告号
			  ${inNum}, 						-- 13.
				${outNum}, 						-- 14.
				${kcsl}, 					-- 15.
				#{userName}, 						-- 16.
				A.lkbh, 					-- 17.
				A.mxpz,						-- 18.
				A.dj, 						-- 19.
				NOW(), 						-- 20.
				hwxz, 						-- 21.
				'', 						-- 22.
				A.DataID,					-- 23.
				'',							-- 24.
				1,							-- 25.
				A.hwcd,						-- 26.
				A.scnf,						-- 27.
				A.jjdh,						-- 28.
				A.hcph,						-- 29、火车皮号
			  ${ljsrsl},			-- 30、累计收入数量
				${ljzcsl},			-- 31、累计支出数量
				${srsjsl},			-- 32、收入实际数量
				${zcsjsl},			-- 33、支出实际数量
				${kcsjsl},			-- 34、库存实际数量
				${ljsrsjsl},		-- 35、累计收入实际数量
				${ljzcsjsl},		-- 36、累计支出实际数量
				A.DeptID
			FROM data_kcgl_xckc_default	A		-- 1.库存明细表
				WHERE A.DataID=#{dataId};
    </insert>
</mapper>