<?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.LedgerMapper">
    
	<sql id="ledgerColumns">
		a.id AS "id",
		a.name AS "name",
		a.type_id AS "type.id",
		a.model AS "model",
		a.shelvesids AS "shelvesIds",
		a.num AS "num",
		a.sum AS "sum",
		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.name AS "type.name",
		type.code AS "type.code"
	</sql>
	
	<sql id="ledgerJoins">
		
		LEFT JOIN t_wh_material_type type ON type.id = a.type_id
	</sql>
	
    
	<select id="get" resultType="Ledger" >
		SELECT 
			<include refid="ledgerColumns"/>
		FROM t_wh_ledger a
		<include refid="ledgerJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="Ledger" >
		SELECT 
			<include refid="ledgerColumns"/>
		FROM t_wh_ledger a
		<include refid="ledgerJoins"/>
		<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="shelvesIds != null and shelvesIds != ''">
				AND a.shelvesids LIKE 
					<if test="dbName == 'oracle'">'%'||#{shelvesIds}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{shelvesIds}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{shelvesIds},'%')</if>
			</if>
			<if test="num != null and num != ''">
				AND a.num >= #{num}
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="Ledger" >
		SELECT 
			<include refid="ledgerColumns"/>
		FROM t_wh_ledger a
		<include refid="ledgerJoins"/>
		<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.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO t_wh_ledger(
			id,
			name,
			type_id,
			model,
			shelvesids,
			num,
			sum,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag
		) VALUES (
			#{id},
			#{name},
			#{type.id},
			#{model},
			#{shelvesIds},
			#{num},
			#{sum},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag}
		)
	</insert>
	
	<update id="update">
		UPDATE t_wh_ledger SET 	
			name = #{name},
			type_id = #{type.id},
			model = #{model},
			shelvesids = #{shelvesIds},
			num = #{num},
			sum = #{sum},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM t_wh_ledger
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE t_wh_ledger SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="Ledger" statementType="STATEMENT">
		select * FROM t_wh_ledger  where ${propertyName} = '${value}'
	</select>

	<select id="findByTypeAndModel" resultType="Ledger" >
		SELECT
		<include refid="ledgerColumns"/>
		FROM t_wh_ledger a
		<include refid="ledgerJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND a.type_id = #{type.id}
			AND a.model = #{model}
			AND a.name = #{name}
		</where>
	</select>
	<select id="findShelvesListByLedgerId" resultType="java.lang.String" parameterType="java.lang.String">
		select
		shelves_id
		from
		t_wh_ledger_info li
		where li.ledger_id = #{id}
		and li.del_flag = '0'
	</select>
</mapper>