<?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.workitemstock.dao.WorkItemStockDao">
    
	<sql id="workItemStockColumns">
		a.id AS "id",
		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",
		a.stock_id AS "stockId",
		a.catalogue_item_id AS "catalogueItemId",
		a.count AS "count",
		a.price AS "price",
		a.total_price AS "totalPrice",
		a.contract_id AS "contractId",
		w.name AS "contractName",
		a.supplier_name AS "supplierName",
		a.is_assets AS "isAssets",
		a.number AS "number",
		a.name AS "name",
		a.type AS "type",
		a.model AS "model",
		a.assets_number AS "assetsNumber",
		a.sign AS "sign",
		a.primaryunit AS "primaryUnit"
	</sql>
	
	<sql id="workItemStockJoins">
		left join work_stock_contract w on w.id = a.contract_id
	</sql>
	
    
	<select id="get" resultType="WorkItemStock" >
		SELECT 
			<include refid="workItemStockColumns"/>
		FROM work_item_stock a
		<include refid="workItemStockJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkItemStock" >
		SELECT 
			<include refid="workItemStockColumns"/>
		FROM work_item_stock a
		<include refid="workItemStockJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL} and (a.sign is null or a.sign = '')
			<if test="stockId!= null and stockId!= ''">
				AND a.stock_id = #{stockId}
			</if>
			<if test="isAssets!= null and isAssets!= ''">
				AND a.is_assets = #{isAssets}
			</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="findLists" resultType="WorkItemStock" >
		SELECT
		<include refid="workItemStockColumns"/>
		FROM work_item_stock a
		<include refid="workItemStockJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="stockId!= null and stockId!= ''">
				AND a.stock_id = #{stockId}
			</if>
			<if test="sign!= null and sign!= ''">
				AND a.sign = #{sign}
			</if>
			<if test="isAssets!= null and isAssets!= ''">
				AND a.is_assets = #{isAssets}
			</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="WorkItemStock" >
		SELECT 
			<include refid="workItemStockColumns"/>
		FROM work_item_stock a
		<include refid="workItemStockJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</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 work_item_stock(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			stock_id,
			catalogue_item_id,
			count,
			price,
			total_price,
			contract_id,
			supplier_name,
			is_assets,
			number,
			name,
			type,
			model,
			assets_number,
			sign,
			primaryunit
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{stockId},
			#{catalogueItemId},
			#{count},
			#{price},
			#{totalPrice},
			#{contractId},
			#{supplierName},
			#{isAssets},
			#{number},
			#{name},
			#{type},
			#{model},
			#{assetsNumber},
			#{sign},
			#{primaryUnit}
		)
	</insert>
	
	<update id="update">
		UPDATE work_item_stock SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			stock_id = #{stockId},
			catalogue_item_id = #{catalogueItemId},
			count = #{count},
			price = #{price},
			total_price = #{totalPrice},
			contract_id = #{contractId},
			supplier_name = #{supplierName},
			is_assets = #{isAssets},
			number = #{number},
			name = #{name},
			type = #{type},
			model = #{model},
			assets_number = #{assetsNumber},
			sign = #{sign},
			primaryunit = #{primaryUnit}
		WHERE id = #{id}
	</update>

	<update id="updateNumber">
		UPDATE work_item_stock SET
		assets_number = #{assetsNumber}
		WHERE id = #{id}
	</update>
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_item_stock
		WHERE id = #{id}
	</update>

	<!--物理删除-->
	<update id="deleteByStock">
		DELETE FROM work_item_stock
		WHERE stock_id = #{stockId}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_item_stock SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkItemStock" statementType="STATEMENT">
		select * FROM work_item_stock  where ${propertyName} = '${value}'
	</select>


	<select id="findListByContract" resultType="WorkItemStock" >
		SELECT
		a.id AS "id",
		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",
		a.stock_id AS "stockId",
		a.catalogue_item_id AS "catalogueItemId",
		a.count AS "count",
		a.price AS "price",
		a.total_price AS "totalPrice",
		a.contract_id AS "contractId",
		w.name AS "contractName",
		a.supplier_name AS "supplierName",
		a.is_assets AS "isAssets",
		a.number AS "number",
		a.name AS "name",
		a.type AS "type",
		a.model AS "model",
		a.assets_number AS "assetsNumber",
		a.sign AS "sign",
		a.primaryunit AS "primaryUnit"
		FROM work_item_stock a
			left join work_stock_contract w on w.id = a.contract_id
			left join work_stock w2 on w2.id = a.stock_id
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL} and w2.status = '5' and (a.sign='1' or a.is_assets = '0')
			<if test="stockId!= null and stockId!= ''">
				AND a.stock_id = #{stockId}
			</if>
			<if test="contractId!= null and contractId!= ''">
				AND a.contract_id = #{contractId}
			</if>
			<if test="isAssets!= null and isAssets!= ''">
				AND a.is_assets = #{isAssets}
			</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>
	
</mapper>