<?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.cplbusiness.mapper.DataKcglCplkcswDefaultMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.unis.crk.cplbusiness.entity.DataKcglCplkcswDefault">
        <id column="dataId" property="dataId" />
        <result column="unitid" property="unitid" />
        <result column="zdmc" property="zdmc" />
        <result column="zdbm" property="zdbm" />
        <result column="ch" property="ch" />
        <result column="hwh" property="hwh" />
        <result column="chmc" property="chmc" />
        <result column="hwmc" property="hwmc" />
        <result column="builder" property="builder" />
        <result column="cjsj" property="cjsj" />
        <result column="editor" property="editor" />
        <result column="modifyDate" property="modifyDate" />
        <result column="updateTime" property="updateTime" />
        <result column="scrq" property="scrq" />
        <result column="bzq" property="bzq" />
        <result column="xz" property="xz" />
        <result column="deptId" property="deptId" />
        <result column="sjjlpzmc" property="sjjlpzmc" />
        <result column="sjjlpzbh" property="sjjlpzbh" />
        <result column="sjjlppmc" property="sjjlppmc" />
        <result column="sjjlppbh" property="sjjlppbh" />
        <result column="sjjldjbh" property="sjjldjbh" />
        <result column="sjjldjmc" property="sjjldjmc" />
        <result column="sjjlspmc" property="sjjlspmc" />
        <result column="sjjlspmcbh" property="sjjlspmcbh" />
        <result column="kcsl" property="kcsl" />
        <result column="scpc" property="scpc" />
        <result column="gg" property="gg" />
		<result column="jldw" property="jldw" />
        <result column="zje" property="zje" />
        <result column="js" property="js" />
        <result column="scnf" property="scnf" />
        <result column="cd" property="cd" />
        <result column="rcrq" property="rcrq" />
    </resultMap>


    <select id="selectMyMapsPage" resultType="java.util.Map">
		SELECT DISTINCT
		sjjlppmc_enum.enumName AS sjjlppmc,
		sjjlpzmc_enum.enumName AS sjjlpzmc,
		sjjlspmc_enum.sjjlspmc AS sjjlspmc,
		jldw_enum.enumName AS jldwmc,
		a.unitid,
		a.xz,
		a.sjjlpzbh,
		a.sjjlppbh,
		a.sjjlspmcbh,
		a.gg,
		a.jldw,
		SUM(a.kcsl) as kcsl,
		SUM(a.js) as js
		FROM
		data_kcgl_cplkcsw_default a
		LEFT JOIN basic_enum sjjlppmc_enum ON sjjlppmc_enum.enumId = a.sjjlppbh
		LEFT JOIN basic_enum sjjlpzmc_enum ON sjjlpzmc_enum.enumId = a.sjjlpzbh
		LEFT JOIN basic_enum jldw_enum ON jldw_enum.enumId = a.jldw
		LEFT JOIN data_crkyw_cplsp_default sjjlspmc_enum ON sjjlspmc_enum.sjjlspmcbh = a.sjjlspmcbh
		and  sjjlspmc_enum.unitId = a.unitId
		LEFT JOIN basic_enum sjjldjmc_enum ON sjjldjmc_enum.enumId = a.sjjldjbh

		<if test="ew.sqlSegment != null and ew.sqlSegment != ''">
            where  ${ew.sqlSegment}
        </if>
		GROUP BY
		sjjlppmc_enum.enumName ,
		sjjlpzmc_enum.enumName ,
		sjjlspmc_enum.sjjlspmc ,
		jldw_enum.enumName ,
		a.unitid,
		a.xz,
		a.sjjlpzbh,
		a.sjjlppbh,
		a.sjjlspmcbh,
		a.gg,
		a.jldw
    </select>
    <insert id="addStoreByXckc">
        INSERT INTO data_kcgl_cplkcsw_default(
	dataId, -- 1、数据id
	unitid, -- 2、单位id
	zdmc,   -- 3.站点名称
	zdbm,		-- 4.站点编码
	ch,			-- 5.仓房
	hwh,		-- 6.货位
	chmc,   -- 7.仓房名称
	hwmc,		-- 8.货位名称
	builder, -- 9、创建人
	cjsj,    -- 10.创建时间
	scrq,    -- 11.生产日期
	bzq,		 -- 11.保质期
	xz,			 -- 12.性质
	sjjlpzmc, -- 13.实际军粮品种名称
	sjjlpzbh,	-- 14.实际军粮品种编号
	sjjlppmc,	-- 15.实际军粮品牌名称
	sjjlppbh,	-- 16.实际军粮品牌编号
	sjjldjbh,	-- 17.实际军粮等级编号
	sjjlspmc,	-- 18.实际军粮商品名称
	sjjlspmcbh,	-- 19.实际军粮商品名称编号
	scpc,				-- 20.生产批次
	gg,					-- 30.规格
	rcrq,				-- 31.(入仓日期)业务时间
	zje,				-- 32.总金额
    js,			  -- 33.件数
	kcsl, 			-- 34.库存数量
	scnf, 			-- 35.生产数量
	jldw,			-- 36.计量单位
	cd		      -- 37.产地
)
SELECT
	dataId, -- 1、数据id
	unitid, -- 2、单位id
	zdmc,   -- 3.站点名称
	zdbm,		-- 4.站点编码
	ch,			-- 5.仓房
	hwh,		-- 6.货位
	chmc,   -- 7.仓房名称
	hwmc,		-- 8.货位名称
	builder, -- 9、创建人
	cjsj,    -- 10.创建时间
	scrq,    -- 11.生产日期
	bzq,		 -- 11.保质期
	xz,			 -- 12.性质
	sjjlpzmc, -- 13.实际军粮品种名称
	sjjlpzbh,	-- 14.实际军粮品种编号
	sjjlppmc,	-- 15.实际军粮品牌名称
	sjjlppbh,	-- 16.实际军粮品牌编号
	sjjldjbh,	-- 17.实际军粮等级编号
	sjjlspmc,	-- 18.实际军粮商品名称
	sjjlspmcbh,	-- 19.实际军粮商品名称编号
	scpc,				-- 20.生产批次
	gg,					-- 30.规格
	ywsj,				-- 31.(入仓日期)业务时间
	${zje},				-- 32.总金额
     js,			-- 33.件数
	${kcsl}, 			-- 34.库存数量
	scnf, 			-- 35.生产数量
	jldw,			-- 36.计量单位
	cd		      -- 37.产地
FROM
	data_kcgl_cplxckc_default
where dataId = #{dataId}
    </insert>
	<select id="getKcsw" resultType="java.util.Map">
		SELECT
			a.dataId, -- 1、数据id
			a.unitid, -- 2、单位id
			a.zdmc,   -- 3.站点名称
			a.zdbm,		-- 4.站点编码
			a.ch,			-- 5.仓房
			a.hwh,		-- 6.货位
			a.chmc,   -- 7.仓房名称
			a.scrq,    -- 11.生产日期
			a.bzq,		 -- 11.保质期
			a.xz,			 -- 12.性质
		    a.sjjlppbh,
			sjjlppmc_enum.enumName as sjjlppmc,
			a.sjjlpzbh,
			sjjlpzmc_enum.enumName as sjjlpzmc,
			a.sjjlspmcbh,
			sjjlspmc_enum.sjjlspmc as sjjlspmc,
			a.sjjldjbh,
			sjjldjmc_enum.enumName as sjjldjmc,
			a.sjjlspmcbh,	-- 19.实际军粮商品名称编号
			a.scpc,				-- 20.生产批次
			a.gg,					-- 30.规格
			a.rcrq,				-- 31.(入仓日期)业务时间
			a.zje,				-- 32.总金额
			a.js,			-- 33.件数
			a.kcsl, 			-- 34.库存数量
			a.scnf, 			-- 35.生产年份
			a.jldw,			-- 36.计量单位
			a.zje/a.js as dj ,-- 单价
			jldw_enum.enumName as jldwmc,
			a.cd		      -- 37.产地
		FROM
			data_kcgl_cplkcsw_default a
				LEFT JOIN basic_enum sjjlppmc_enum ON sjjlppmc_enum.enumId = a.sjjlppbh
				LEFT JOIN basic_enum sjjlpzmc_enum ON sjjlpzmc_enum.enumId = a.sjjlpzbh
				LEFT JOIN basic_enum jldw_enum ON jldw_enum.enumId = a.jldw
				LEFT JOIN data_crkyw_cplsp_default sjjlspmc_enum ON sjjlspmc_enum.sjjlspmcbh = a.sjjlspmcbh
				and  sjjlspmc_enum.unitId = a.unitId
				LEFT JOIN basic_enum sjjldjmc_enum ON sjjldjmc_enum.enumId = a.sjjldjbh
		WHERE
			ch = #{ch}
		  AND hwh = #{hwh}
		  and  	a.unitid=#{unitid}
	</select>
	<select id="getKcswInfoByHwh" resultType="java.util.Map">
				SELECT
		DISTINCT
			a.ch,			-- 5.仓房
			a.hwh,		-- 6.货位
			a.xz,
			a.scrq,
			a.bzq,
		 a.sjjlppbh,
			sjjlppmc_enum.enumName as sjjlppmc,
			a.sjjlpzbh,
			sjjlpzmc_enum.enumName as sjjlpzmc,
			a.sjjlspmcbh,
			sjjlspmc_enum.sjjlspmc as sjjlspmc,
			a.sjjldjbh,
			sjjldjmc_enum.enumName as sjjldjmc,
			a.scpc,				-- 20.生产批次
			a.gg,					-- 30.规格
			a.scnf, 			-- 35.生产年份
			a.jldw,			-- 36.计量单位
			jldw_enum.enumName as jldwmc,
			a.kcsl,
			a.kcsl as "bdsl",
			a.zje/a.js as dj ,-- 单价
			a.js,
			a.cd		      -- 37.产地
		FROM
			data_kcgl_cplkcsw_default a
				LEFT JOIN basic_enum sjjlppmc_enum ON sjjlppmc_enum.enumId = a.sjjlppbh
				LEFT JOIN basic_enum sjjlpzmc_enum ON sjjlpzmc_enum.enumId = a.sjjlpzbh
				LEFT JOIN basic_enum jldw_enum ON jldw_enum.enumId = a.jldw
				LEFT JOIN data_crkyw_cplsp_default sjjlspmc_enum ON sjjlspmc_enum.sjjlspmcbh = a.sjjlspmcbh
				and  sjjlspmc_enum.unitId = a.unitId
				LEFT JOIN basic_enum sjjldjmc_enum ON sjjldjmc_enum.enumId = a.sjjldjbh
		WHERE
			 a.hwh = #{hwh}
ORDER BY a.scpc
	</select>
	<select id="getKcswInfoByCh" resultType="java.util.Map">
				SELECT
		DISTINCT
			a.ch,			-- 5.仓房
			a.hwh,		-- 6.货位
			a.xz,
			a.scrq,
			a.bzq,
		 a.sjjlppbh,
			sjjlppmc_enum.enumName as sjjlppmc,
			a.sjjlpzbh,
			sjjlpzmc_enum.enumName as sjjlpzmc,
			a.sjjlspmcbh,
			sjjlspmc_enum.sjjlspmc as sjjlspmc,
			a.sjjldjbh,
			sjjldjmc_enum.enumName as sjjldjmc,
			a.scpc,				-- 20.生产批次
			a.gg,					-- 30.规格
			a.scnf, 			-- 35.生产年份
			a.jldw,			-- 36.计量单位
			jldw_enum.enumName as jldwmc,
			a.kcsl,
			a.kcsl as "bdsl",
			a.zje/a.js as dj ,-- 单价
			a.js,
			a.cd		      -- 37.产地
		FROM
			data_kcgl_cplkcsw_default a
				LEFT JOIN basic_enum sjjlppmc_enum ON sjjlppmc_enum.enumId = a.sjjlppbh
				LEFT JOIN basic_enum sjjlpzmc_enum ON sjjlpzmc_enum.enumId = a.sjjlpzbh
				LEFT JOIN basic_enum jldw_enum ON jldw_enum.enumId = a.jldw
				LEFT JOIN data_crkyw_cplsp_default sjjlspmc_enum ON sjjlspmc_enum.sjjlspmcbh = a.sjjlspmcbh
				and  sjjlspmc_enum.unitId = a.unitId
				LEFT JOIN basic_enum sjjldjmc_enum ON sjjldjmc_enum.enumId = a.sjjldjbh
				LEFT JOIN basic_enum cclx_enum ON sjjldjmc_enum.enumId = a.sjjldjbh
		WHERE
			 a.ch = #{ch}
ORDER BY a.hwh,a.scpc
	</select>
	<select id="getKcswInfoBySpbh" resultType="java.util.Map">
		SELECT
		DISTINCT
			a.ch,			-- 5.仓房
			a.hwh,		-- 6.货位
			a.scrq,    -- 11.生产日期
			a.bzq,		 -- 11.保质期
			a.xz,			 -- 12.性质
			a.bzq,
			a.js,
			a.kcsl,
			 a.sjjlppbh,
			sjjlppmc_enum.enumName as sjjlppmc,
			a.sjjlpzbh,
			sjjlpzmc_enum.enumName as sjjlpzmc,
			a.sjjlspmcbh,
			sjjlspmc_enum.sjjlspmc as sjjlspmc,
			a.sjjldjbh,
			sjjldjmc_enum.enumName as sjjldjmc,
			a.scpc,				-- 20.生产批次
			a.gg,					-- 30.规格
			a.scnf, 			-- 35.生产年份
			a.jldw,			-- 36.计量单位
			jldw_enum.enumName as jldwmc,
			a.cd		      -- 37.产地
		FROM
			data_kcgl_cplkcsw_default a
			LEFT JOIN basic_enum sjjlppmc_enum ON sjjlppmc_enum.enumId = a.sjjlppbh
			LEFT JOIN basic_enum sjjlpzmc_enum ON sjjlpzmc_enum.enumId = a.sjjlpzbh
			LEFT JOIN basic_enum jldw_enum ON jldw_enum.enumId = a.jldw
			LEFT JOIN data_crkyw_cplsp_default sjjlspmc_enum ON sjjlspmc_enum.sjjlspmcbh = a.sjjlspmcbh
			and  sjjlspmc_enum.unitId = a.unitId
			LEFT JOIN basic_enum sjjldjmc_enum ON sjjldjmc_enum.enumId = a.sjjldjbh
		WHERE
			 a.hwh = #{hwh}
			and a.sjjlspmcbh = #{spbh}
			and a.gg = #{gg}
			ORDER BY a.scpc
	</select>
	<select id="getChInfo" resultType="java.util.Map">
	SELECT DISTINCT
			b.storehouse_name AS chmc,
			c.warehouse_name AS hwmc,
			SUM(kcsl) AS kcsl,
			SUM(js) AS js
		FROM
			data_kcgl_cplkcsw_default a
		LEFT JOIN basic_storehouse b ON a.ch = b.storehouse_id
		LEFT JOIN basic_warehouse c ON a.hwh = c.warehouse_id
		WHERE
		a.unitid = #{unitid}
		and	a.sjjlppbh = #{ppbh}
		AND a.sjjlpzbh = #{pzbh}
		AND a.sjjlspmcbh = #{spbh}
		AND a.gg = #{gg}
		GROUP BY
			b.storehouse_name,
			c.warehouse_name
	</select>
	<select id="getChSumInfo" resultType="java.util.Map">
		SELECT DISTINCT
			b.storehouse_name AS chmc,
			b.storehouse_id AS ch,
			IFNULL(SUM(kcsl),0) AS kcsl,
			IFNULL(SUM(js),0) AS js
			FROM
			basic_storehouse b
			LEFT JOIN data_kcgl_cplkcsw_default a ON a.ch = b.storehouse_id
			WHERE
			b.org_id = #{unitid}
			GROUP BY
			b.storehouse_id
			ORDER BY b.storehouse_code
	</select>
	<select id="findPageKcyj" resultType="java.util.Map">
		SELECT
		cf.storehouse_name AS chmc,
		hw.warehouse_name AS hwmc,
		sp.sjjlspmc AS sjjlspmc,
		pz.enumName AS sjjlpzmc,
		pp.enumName AS sjjlppmc,
		kc.modifyDate,
		kc.cjsj,
		kc.js,
		kc.scrq,
		jl.enumName,
		kc.gg,
		sp.kcyjs,
		sp.kcyjx
		FROM
		data_kcgl_cplkcsw_default kc
		LEFT JOIN basic_storehouse cf ON cf.storehouse_id = kc.ch
		LEFT JOIN basic_warehouse hw ON hw.warehouse_id = kc.hwh
		LEFT JOIN data_crkyw_cplsp_default sp ON sp.sjjlspmcbh = kc.sjjlspmcbh
		LEFT JOIN basic_enum pz ON pz.enumId = kc.sjjlpzbh
		LEFT JOIN basic_enum pp ON pp.enumId = kc.sjjlppbh
		LEFT JOIN basic_enum jl ON jl.enumId = kc.jldw
		where 	kc.js&gt;sp.kcyjs or kc.js&lt;sp.kcyjx
		<if test="ew.sqlSegment != null and ew.sqlSegment != ''">
			and   ${ew.sqlSegment}
		</if>
	</select>
</mapper>
