<?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.jeeplus.modules.warehouse.ledger.mapper.LedgerInfoMapper">
    
	<sql id="ledgerInfoColumns">
		a.id AS "id",
		a.name AS "name",
		a.type_id AS "type.id",
		a.model AS "model",
		a.batch_num AS "batchNum",
		a.ledger_id AS "ledger.id",
		a.goods_info_id AS "goodsInfo.id",
		a.qr_id AS "qr.id",
		a.shelves_id AS "shelves.id",
		a.code AS "code",
		a.amount AS "amount",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
				
				
		type.code AS "type.code",
		type.name AS "type.name",
				
				
				
				
				
				
		shelves.name AS "shelves.name"
	</sql>
	
	<sql id="ledgerInfoJoins">
		
		LEFT JOIN t_wh_material_type type ON type.id = a.type_id
			LEFT JOIN t_wh_ledger b ON b.id = a.ledger_id
		LEFT JOIN t_wh_shelves shelves ON shelves.id = a.shelves_id
	</sql>
	
    
	<select id="get" resultType="LedgerInfo" >
		SELECT 
			<include refid="ledgerInfoColumns"/>
		FROM t_wh_ledger_info a
		<include refid="ledgerInfoJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="LedgerInfo" >
		SELECT 
			<include refid="ledgerInfoColumns"/>
		FROM t_wh_ledger_info a
		<include refid="ledgerInfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			${dataScope}
			<if test="name != null and name != ''">
				AND a.name LIKE 
					<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
			</if>
			<if test="type != null and type.id != null and type.id != ''">
				AND a.type_id = #{type.id}
			</if>
			<if test="model != null and model != ''">
				AND a.model LIKE 
					<if test="dbName == 'oracle'">'%'||#{model}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{model}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{model},'%')</if>
			</if>
			<if test="batchNum != null and batchNum != ''">
				AND a.batch_num LIKE 
					<if test="dbName == 'oracle'">'%'||#{batchNum}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{batchNum}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{batchNum},'%')</if>
			</if>
			<if test="ledger != null and ledger.id != null and ledger.id != ''">
				AND a.ledger_id = #{ledger.id}
			</if>
			<if test="shelves != null and shelves.id != null and shelves.id != ''">
				AND a.shelves_id = #{shelves.id}
			</if>
			<if test="code != null and code != ''">
				AND a.code LIKE 
					<if test="dbName == 'oracle'">'%'||#{code}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{code}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{code},'%')</if>
			</if>
			<if test="qr != null and qr.id != null and qr.id != ''">
				AND a.qr_id = #{qr.id}
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.create_date ASC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="LedgerInfo" >
		SELECT 
			<include refid="ledgerInfoColumns"/>
		FROM t_wh_ledger_info a
		<include refid="ledgerInfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			${dataScope}
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.create_date ASC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO t_wh_ledger_info(
			id,
			name,
			type_id,
			model,
			batch_num,
			ledger_id,
			goods_info_id,
			qr_id,
			shelves_id,
			code,
			amount,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag
		) VALUES (
			#{id},
			#{name},
			#{type.id},
			#{model},
			#{batchNum},
			#{ledger.id},
			#{goodsInfo.id},
			#{qr.id},
			#{shelves.id},
			#{code},
			#{amount},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag}
		)
	</insert>
	
	<update id="update">
		UPDATE t_wh_ledger_info SET 	
			name = #{name},
			type_id = #{type.id},
			model = #{model},
			batch_num = #{batchNum},
			ledger_id = #{ledger.id},
			goods_info_id = #{goodsInfo.id},
			qr_id = #{qr.id},
			shelves_id = #{shelves.id},
			code = #{code},
			amount = #{amount},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM t_wh_ledger_info
		<choose>
			<when test="id !=null and id != ''">
				WHERE id = #{id}
			</when>
			<otherwise>
				WHERE ledger_id = #{ledger.id}
			</otherwise>
		</choose>
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE t_wh_ledger_info SET 
			del_flag = #{DEL_FLAG_DELETE}
		<choose>
			<when test="id !=null and id != ''">
				WHERE id = #{id}
			</when>
			<otherwise>
				WHERE ledger_id = #{ledger.id}
			</otherwise>
		</choose>
	</update>
	<!--逻辑删除-->
	<update id="deleteByLedgerInfo">
		UPDATE t_wh_ledger_info SET
		del_flag = '1'
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="LedgerInfo" statementType="STATEMENT">
		select * FROM t_wh_ledger_info  where ${propertyName} = '${value}'
	</select>

	<select id="findCliList" resultType="com.jeeplus.modules.warehouse.ledger.dto.CountLedgerInfo" >
		SELECT
		a.ledger_id AS "ledger.id",
		count(a.id) AS "num",
		a.type_id AS "type.id",
		a.model AS "model",
		a.batch_num AS "batchNum",
		a.shelves_id AS "shelves.id",
		sum(a.amount) AS "sum",
		type.name AS "type.name",
		s.name AS "shelves.name",
		w.id AS "shelves.warehouse.id",
		w.name AS "shelves.warehouse.name"
		FROM t_wh_ledger_info a
		LEFT JOIN t_wh_material_type type on type.id = a.type_id
		LEFT JOIN t_wh_shelves s on s.id = a.shelves_id
		LEFT JOIN t_wh_warehouse w on w.id = s.warehouse_id
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND a.ledger_id = #{ledger.id}
		</where>
		GROUP BY a.ledger_id,a.batch_num,a.shelves_id
	</select>
</mapper>