<?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_ids AS "shelvesIds",
		a.warehouse_ids AS "warehouseIds",
		a.code AS "code",
		a.amount AS "amount",
		a.num AS "num",
		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",
		type.amount AS "type.amount",
		type.unit AS "type.unit",



		qr.url AS "qr.url"

	</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_qr qr ON a.qr_id = qr.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="shelvesIds != null and shelvesIds != ''">
				AND a.shelves_ids LIKE
				<if test="dbName == 'oracle'">'%'||#{shelvesIds}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{shelvesIds}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{shelvesIds},'%')</if>
			</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_ids,
			warehouse_ids,
			code,
			amount,
			num,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag
		) VALUES (
			#{id},
			#{name},
			#{type.id},
			#{model},
			#{batchNum},
			#{ledger.id},
			#{goodsInfo.id},
			#{qr.id},
			#{shelvesIds},
			#{warehouseIds},
			#{code},
			#{amount},
			#{num},
			#{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_ids = #{shelvesIds},
			warehouse_ids = #{warehouseIds},
			code = #{code},
			amount = #{amount},
			num = #{num},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks}
		WHERE id = #{id}
	</update>

	<update id="updateNum">
		UPDATE t_wh_ledger_info SET
			num = #{num}
		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>
	<update id="updateByLedgerInfo">
		UPDATE t_wh_ledger_info SET
			del_flag = '0'
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="LedgerInfo" statementType="STATEMENT">
		select * FROM t_wh_ledger_info  where ${propertyName} = '${value}'
	</select>
	<select id="findListByQrIdAndCode" 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="code != null and code != ''">
				AND a.code = #{code}
			</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>
</mapper>