<?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.workitemcollar.dao.WorkItemCollarCatalogDao">
    
	<sql id="workItemCollarCatalogColumns">
		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.item_collar_id AS "itemCollarId",
		a.item_catalog_id AS "itemCatalogId",
		a.apply_num AS "applyNum",
		a.grant_num AS "grantNum",
		w.id AS "workCatalogueItem.id",
		w.name AS "workCatalogueItem.name",
		w.type AS "workCatalogueItem.type",
		w.number AS "workCatalogueItem.number",
		w.model AS "workCatalogueItem.model",
		w.primary_unit AS "workCatalogueItem.primaryUnit",
		case w.is_assets when '0' then '否'else '是' end AS "workCatalogueItem.isAssets"
	</sql>
	
	<sql id="workItemCollarCatalogJoins">
		left join work_catalogue_item w on w.id=a.item_catalog_id and w.del_flag='0'
	</sql>
	
    
	<select id="get" resultType="WorkItemCollarCatalog" >
		SELECT 
			<include refid="workItemCollarCatalogColumns"/>
		FROM work_item_collar_catalog a
		<include refid="workItemCollarCatalogJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkItemCollarCatalog" >
		SELECT 
			<include refid="workItemCollarCatalogColumns"/>
		FROM work_item_collar_catalog a
		<include refid="workItemCollarCatalogJoins"/>
		<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>
	
	<select id="findAllList" resultType="WorkItemCollarCatalog" >
		SELECT 
			<include refid="workItemCollarCatalogColumns"/>
		FROM work_item_collar_catalog a
		<include refid="workItemCollarCatalogJoins"/>
		<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_collar_catalog(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			item_collar_id,
			item_catalog_id,
			apply_num,
			grant_num
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{itemCollarId},
			#{itemCatalogId},
			#{applyNum},
			#{grantNum}
		)
	</insert>
	
	<update id="update">
		UPDATE work_item_collar_catalog SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			item_collar_id = #{itemCollarId},
			item_catalog_id = #{itemCatalogId},
			apply_num = #{applyNum},
			del_flag=#{delFlag},
			grant_num = #{grantNum}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		UPDATE work_item_collar_catalog SET
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_item_collar_catalog SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkItemCollarCatalog" statementType="STATEMENT">
		select * FROM work_item_collar_catalog  where ${propertyName} = '${value}'
	</select>



	<select id="findByWorkItemCollarId" resultType="WorkItemCollarCatalog" >
		SELECT
		<include refid="workItemCollarCatalogColumns"/>
		FROM work_item_collar_catalog a
		<include refid="workItemCollarCatalogJoins"/>
		<where>
			a.del_flag = '0'
			and a.item_collar_id=#{id}
		</where>
	</select>
</mapper>