<?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.workitem.dao.WorkItemDao">
    
	<sql id="workItemColumns">
		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.office_id AS "officeId",
		a.company_id AS "companyId",
		a.catalogue_item_id AS "catalogueItemId",
		a.is_assets AS "isAssets",
		a.item_count AS "itemCount",
		a.state AS "state",
		a.number AS "number",
		a.end_user AS "endUser",
		a.user_date AS "userDate",
		a.user_office_id AS "userOfficeId",
		oo.top_company as "userOfficeName",
		uu.name as "endUserName",
		w.id as "workCatalogueItem.id",
		w.name as "itemName",
		w.number as "workCatalogueItem.number",
		w.type as "workCatalogueItem.type",
		w.model as "workCatalogueItem.model",
		w.primary_unit as "workCatalogueItem.primaryUnit"
	</sql>
	
	<sql id="workItemJoins">
        LEFT JOIN work_catalogue_item w ON w.id = a.catalogue_item_id
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
		LEFT JOIN sys_user u ON u.id = a.create_by
		LEFT JOIN sys_office oo ON oo.id = a.user_office_id
		LEFT JOIN sys_user uu ON uu.id = a.end_user
	</sql>
	
    
	<select id="get" resultType="WorkItem" >
		SELECT 
			<include refid="workItemColumns"/>
		FROM work_item a
		<include refid="workItemJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkItem" >
		SELECT 
			<include refid="workItemColumns"/>
		FROM work_item a
		<include refid="workItemJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="isAssets != null and isAssets != ''">
				AND a.is_assets = #{isAssets}
			</if>
			<if test="state != null and state != ''">
				AND a.state = #{state}
			</if>
			<if test="itemName != null and itemName != ''">
				AND w.name like <if test="dbName == 'mysql'">concat('%',#{itemName},'%')</if>
			</if>
			<if test="userOfficeId != null and userOfficeId != ''">
				AND a.user_office_id = #{userOfficeId}
			</if>
			<if test="number != null and number != ''">
				AND a.number like <if test="dbName == 'mysql'">concat('%',#{number},'%')</if>
			</if>
			<if test="endUserName != null and endUserName != ''">
				AND uu.name like <if test="dbName == 'mysql'">concat('%',#{endUserName},'%')</if>
			</if>
			<if test="beginDate != null and endDate != null and beginDate != '' and endDate != ''">
				AND a.user_date BETWEEN #{beginDate} AND #{endDate}
			</if>
			and a.company_id=#{companyId}
		</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="findListBybranch" resultType="WorkItem" >
		SELECT
		<include refid="workItemColumns"/>
		FROM work_item a
		<include refid="workItemJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="isAssets != null and isAssets != ''">
				AND a.is_assets = #{isAssets}
			</if>
			<if test="state != null and state != ''">
				AND a.state = #{state}
			</if>
			<if test="itemName != null and itemName != ''">
				AND w.name like <if test="dbName == 'mysql'">concat('%',#{itemName},'%')</if>
			</if>
			<if test="userOfficeId != null and userOfficeId != ''">
				AND a.user_office_id = #{userOfficeId}
			</if>
			<if test="number != null and number != ''">
				AND a.number like <if test="dbName == 'mysql'">concat('%',#{number},'%')</if>
			</if>
			<if test="endUserName != null and endUserName != ''">
				AND uu.name like <if test="dbName == 'mysql'">concat('%',#{endUserName},'%')</if>
			</if>
			<if test="beginDate != null and endDate != null and beginDate != '' and endDate != ''">
				AND a.user_date BETWEEN #{beginDate} AND #{endDate}
			</if>
			<choose>
				<when test="branchOffice !=null and branchOffice != ''">
					and  a.office_id in (select off.id from sys_office off where off.branch_office=#{branchOffice})
				</when>
				<otherwise>
					and  a.office_id in
					(select off.id from sys_office off where off.parent_ids like concat('%',#{companyId},'%') and off.type=2 and (off.branch_office is null or off.branch_office=''))
				</otherwise>
			</choose>
		</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="findListByCompanyId" resultType="WorkItem" >
		SELECT
		<include refid="workItemColumns"/>
		FROM work_item a
		<include refid="workItemJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="isAssets != null and isAssets != ''">
				AND a.is_assets = #{isAssets}
			</if>
			<if test="state != null and state != ''">
				AND a.state = #{state}
			</if>
			<if test="itemName != null and itemName != ''">
				AND w.name like <if test="dbName == 'mysql'">concat('%',#{itemName},'%')</if>
			</if>
			<if test="userOfficeId != null and userOfficeId != ''">
				AND a.user_office_id = #{userOfficeId}
			</if>
			<if test="number != null and number != ''">
				AND a.number like <if test="dbName == 'mysql'">concat('%',#{number},'%')</if>
			</if>
			<if test="endUserName != null and endUserName != ''">
				AND uu.name like <if test="dbName == 'mysql'">concat('%',#{endUserName},'%')</if>
			</if>
			<if test="beginDate != null and endDate != null and beginDate != '' and endDate != ''">
				AND a.user_date BETWEEN #{beginDate} AND #{endDate}
			</if>
			<choose>
				<when test="branchOffice !=null and branchOffice != ''">
					and  a.office_id in (select off.id from sys_office off where off.branch_office=#{branchOffice})
				</when>
				<otherwise>
					and  a.office_id in
					(select off.id from sys_office off where off.parent_ids like concat('%',#{companyId},'%') and off.type=2 and (off.branch_office is null or off.branch_office=''))
				</otherwise>
			</choose>
			and a.id not in (select b.fixed_assets_number from work_borrow_management b where b.`state` in ('1','2','3','4') and b.del_flag='0')
			and a.id not in (select s.item_id from work_scrap s where s.`status` in ('1','2','3','4') and s.del_flag='0')
		</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="findScrapList" resultType="WorkItem" >
		SELECT
		<include refid="workItemColumns"/>
		FROM work_item a
		<include refid="workItemJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL} AND a.state != '4'
			<if test="state != null and state != ''">
				AND a.state = #{state}
			</if>
			<if test="isAssets != null and isAssets != ''">
				AND a.is_assets = #{isAssets}
			</if>
			<if test="itemName != null and itemName != ''">
				AND w.name like <if test="dbName == 'mysql'">concat('%',#{itemName},'%')</if>
			</if>
			<choose>
				<when test="branchOffice !=null and branchOffice != ''">
					and  a.office_id in (select off.id from sys_office off where off.branch_office=#{branchOffice})
				</when>
				<otherwise>
			 and  a.office_id in
			(select off.id from sys_office off where off.parent_ids like concat('%',#{companyId},'%') and off.type=2 and (off.branch_office is null or off.branch_office=''))
				</otherwise>
			</choose>
			and a.id not in (select b.fixed_assets_number from work_borrow_management b where b.`state` in ('1','2','3','4') and b.del_flag='0')
			and a.id not in (select s.item_id from work_scrap s where s.`status` in ('1','2','3','4') and s.del_flag='0')
		</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="WorkItem" >
		SELECT 
			<include refid="workItemColumns"/>
		FROM work_item a
		<include refid="workItemJoins"/>
		<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(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			office_id,
			company_id,
			catalogue_item_id,
			is_assets,
			item_count,
			state,
			number,
			end_user,
			user_date,
			user_office_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{officeId},
			#{companyId},
			#{catalogueItemId},
			#{isAssets},
			#{itemCount},
			#{state},
			#{number},
			#{endUser},
			#{userDate},
			#{userOfficeId}
		)
	</insert>
	
	<update id="update">
		UPDATE work_item SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			office_id = #{officeId},
			company_id = #{companyId},
			catalogue_item_id = #{catalogueItemId},
			is_assets = #{isAssets},
			item_count = #{itemCount},
			state = #{state},
			number = #{number},
			end_user = #{endUser},
			user_date = #{userDate},
			user_office_id = #{userOfficeId}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_item
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_item SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkItem" statementType="STATEMENT">
		select * FROM work_item  where ${propertyName} = '${value}'
	</select>

	<select id="findItemList" resultType="WorkItem" >
		SELECT
		<include refid="workItemColumns"/>
		FROM work_item a
		<include refid="workItemJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			and a.state='1'
			AND a.is_assets = '1'
			<if test="itemName != null and itemName != ''">
				AND w.name like <if test="dbName == 'mysql'">concat('%',#{itemName},'%')</if>
			</if>
			and a.company_id = #{companyId}
		</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="getFindItem" resultType="WorkItem" >
		SELECT * from work_item a where a.catalogue_item_id=#{id} and a.is_assets='0' and a.del_flag='0'
	</select>
</mapper>