<?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.workcatalogueitem.dao.WorkCatalogueItemDao">
    
	<sql id="workCatalogueItemColumns">
		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.name AS "name",
		a.number AS "number",
		a.type AS "type",
		a.model AS "model",
		a.primary_unit AS "primaryUnit",
		a.is_assets AS "isAssets",
		a.remove_flat AS "removeFlat",
		a.office_id AS "officeId",
		a.company_id AS "companyId"
	</sql>
	
	<sql id="workCatalogueItemJoins">
		LEFT JOIN sys_user u on u.id = a.create_by
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
	</sql>
	
    
	<select id="get" resultType="WorkCatalogueItem" >
		SELECT 
			<include refid="workCatalogueItemColumns"/>
		FROM work_catalogue_item a
		<include refid="workCatalogueItemJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkCatalogueItem" >
		SELECT 
			<include refid="workCatalogueItemColumns"/>
		FROM work_catalogue_item a
		<include refid="workCatalogueItemJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="name != null and name != ''">
				AND a.name like
				<if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
			</if>
			<if test="type != null and type != ''">
				AND a.type = #{type}
			</if>
			<if test="removeFlat != null and removeFlat != ''">
				AND a.remove_flat = #{removeFlat}
			</if>
			<if test="model != null and model != ''">
				AND a.model = #{model}
			</if>
			<if test="isAssets != null and isAssets != ''">
				AND a.is_assets = #{isAssets}
			</if>
			<if test="primaryUnit != null and primaryUnit != ''">
				AND a.primary_unit = #{primaryUnit}
			</if>
			<if test="number != null and number != ''">
				AND a.number = #{number}
			</if>
			<if test="removeFlat != null and removeFlat != ''">
				AND a.remove_flat = #{removeFlat}
			</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="findAllList" resultType="WorkCatalogueItem" >
		SELECT 
			<include refid="workCatalogueItemColumns"/>
		FROM work_catalogue_item a
		<include refid="workCatalogueItemJoins"/>
		<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_catalogue_item(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			name,
			number,
			type,
			model,
			primary_unit,
			is_assets,
			remove_flat,
			office_id,
		    company_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{name},
			#{number},
			#{type},
			#{model},
			#{primaryUnit},
			#{isAssets},
			#{removeFlat},
			#{officeId},
			#{companyId}
		)
	</insert>
	
	<update id="update">
		UPDATE work_catalogue_item SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			name = #{name},
			number = #{number},
			type = #{type},
			model = #{model},
			primary_unit = #{primaryUnit},
			is_assets = #{isAssets},
			remove_flat = #{removeFlat},
			office_id = #{officeId},
			company_id = #{companyId}
		WHERE id = #{id}
	</update>

	<update id="removeFlat">
		UPDATE work_catalogue_item SET
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			remove_flat = #{removeFlat}
		WHERE id = #{id}
	</update>
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_catalogue_item
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_catalogue_item SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkCatalogueItem" statementType="STATEMENT">
		select * FROM work_catalogue_item  where ${propertyName} = '${value}'
	</select>

	<select id="findCatalogList" resultType="WorkCatalogueItem" >
		SELECT
		<include refid="workCatalogueItemColumns"/>
		FROM work_catalogue_item a
		<include refid="workCatalogueItemJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			and a.remove_flat='0'
			<if test="name != null and name != ''">
				AND a.name like
				<if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
			</if>
			<if test="type != null and type != ''">
				AND a.type = #{type}
			</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>
</mapper>