<?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.workcontent.dao.WorkContentTypeDao">
    
	<sql id="workContentTypeColumns">
		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",
		ct.del_flag AS "delFlag",
		a.type_id AS "typeId",
		a.type_name AS "typeName",
		a.parent_id AS "parentId",
		a.parent_ids AS "parentIds"
	</sql>
	
	<sql id="workContentTypeJoins">
        LEFT JOIN work_content_type_company ct ON ct.type_id = a.type_id
	</sql>
	
    
	<select id="get" resultType="WorkContentType" >
		SELECT
			a.*
		FROM work_content_type a
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkContentType" >
		SELECT DISTINCT
			<include refid="workContentTypeColumns"/>
		FROM work_content_type a
		<include refid="workContentTypeJoins"/>
		<where>
		     a.del_flag = '0'
			<if test="typeId !=null and typeId != ''">
                AND a.type_id = #{typeId}
            </if>
            <if test="typeName != null and typeName != ''">
                AND (a.type_name LIKE concat('%',#{typeName},'%') OR a.parent_id ='0')
            </if>
            <if test="company != null and company.id != null and company.id != ''">
                AND ct.com_id = #{company.id}
            </if>
            <if test="parentId !=null and parentId !=''">
                AND a.parent_id = #{parentId} AND ct.del_flag = '0'
            </if>
		</where>
		<choose>
			<otherwise>
				ORDER BY a.type_id
			</otherwise>
		</choose>
	</select>
	<select id="findProjectList" resultType="WorkContentType" >
		SELECT DISTINCT
		<include refid="workContentTypeColumns"/>
		FROM work_content_type a
		LEFT JOIN work_content_type_company ct ON ct.type_id = a.type_id
		LEFT JOIN `project_content_data`  p ON p.type = a.type_id
		<where>
			p.project_id=#{projectId}
			AND ct.com_id = #{company.id}
			AND a.parent_id = #{parentId}
			AND ct.del_flag = '0'
		</where>
		<choose>
			<otherwise>
				ORDER BY a.type_id
			</otherwise>
		</choose>
	</select>
	<select id="findAllList" resultType="WorkContentType" >
		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.type_id AS "typeId",
        a.type_name AS "typeName",
        a.parent_id AS "parentId",
        a.parent_ids AS "parentIds"
		FROM work_content_type a
	</select>
	
	<insert id="insert">
		INSERT INTO work_content_type(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			type_id,
			type_name,
			parent_id,
			parent_ids
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{typeId},
			#{typeName},
			#{parentId},
			#{parentIds}
		)
	</insert>
	
	<update id="update">
		UPDATE work_content_type SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			type_id = #{typeId},
			type_name = #{typeName},
			parent_id = #{parentId},
			parent_ids = #{parentIds}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_content_type
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_content_type SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>

    <!--逻辑删除-->
    <update id="changeStatus">
        UPDATE work_content_type_company SET
        del_flag = #{delFlag}
        WHERE type_id = #{typeId} AND com_id = #{company.id}
    </update>
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkContentType" statementType="STATEMENT">
		select * FROM work_content_type  where ${propertyName} = '${value}'
	</select>

    <select id="countContentType" resultType="int">
        SELECT count(*) from work_content_type_company WHERE com_id = #{id}
    </select>

    <insert id="batchInsertComType">
        INSERT INTO work_content_type_company (type_id,com_id,del_flag)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.typeId},#{item.company.id},'0')
        </foreach>
    </insert>
</mapper>