<?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.zcxk.evaluation.dao.EvaluationItemMapper">


    <resultMap type="com.zcxk.evaluation.entity.EvaluationItemEntity" id="EvaluationItemResult">
        <result property="id" column="id"/>
        <result property="tenantId" column="tenant_id"/>
        <result property="companyOrgId" column="company_org_id"/>
        <result property="deptOrgId" column="dept_org_id"/>
        <result property="type" column="type"/>
        <result property="name" column="name"/>
        <result property="value" column="value"/>
        <result property="valueCondition" column="value_condition"/>
        <result property="cycle" column="cycle"/>
        <result property="remarks" column="remarks"/>
        <result property="status" column="status"/>
        <result property="dateCreate" column="date_create"/>
        <result property="createBy" column="create_by"/>
        <result property="dateUpdate" column="date_update"/>
        <result property="updateBy" column="update_by"/>
    </resultMap>

    <resultMap  id="evaluationItemVo" type="com.zcxk.evaluation.vo.EvaluationItemVo">
        <result property="itemId" column="itemId"/>
        <result property="name" column="NAME"/>
        <result property="type" column="TYPE"/>
        <result property="value" column="VALUE"/>
        <result property="valueCondition" column="VALUE_CONDITION"/>
        <result property="cycle" column="CYCLE"/>
        <result property="remarks" column="REMARKS"/>
        <collection property="evaluationItemValueVoList" resultMap="evaluationItemValueVo"></collection>
    </resultMap>

    <resultMap  id="evaluationItemValueVo" type="com.zcxk.evaluation.vo.EvaluationItemValueVo">
        <result property="itemType" column="itemType"/>
        <result property="itemValue" column="itemValue"/>
        <result property="valueOne" column="VALUE_ONE"/>
        <result property="valueTwo" column="VALUE_TWO"/>
    </resultMap>

    <!--  实体栏位  -->
    <sql id="evaluationItemColumns">
         a.id as "id" ,
         a.tenant_id as "tenantId" ,
         a.company_org_id as "companyOrgId" ,
         a.dept_org_id as "deptOrgId" ,
         a.type as "type" ,
         a.name as "name" ,
         a.value as "value" ,
         a.value_condition as "valueCondition" ,
         a.cycle as "cycle" ,
         a.remarks as "remarks" ,
         a.status as "status" ,
         a.date_create as "dateCreate" ,
         a.create_by as "createBy" ,
         a.date_update as "dateUpdate" ,
         a.update_by as "updateBy" 
     </sql>


    <!--  根据主键获取实体   -->
    <select id="findEvaluationItemById" resultType="com.zcxk.evaluation.entity.EvaluationItemEntity">
        SELECT
        <include refid="evaluationItemColumns"/>
        FROM sc_evaluation_item a

        WHERE a.id = #{id} and a.status=1
    </select>

    <!--  根据获取实体List   -->
    <select id="findList" resultType="com.zcxk.evaluation.entity.EvaluationItemEntity">
        SELECT
        <include refid="evaluationItemColumns"/>
        FROM sc_evaluation_item a

        <where>
            <if test="companyOrgId!=null and deptOrgId!=null">
                and a.DEPT_ORG_ID = #{deptOrgId} and a.COMPANY_ORG_ID = #{companyOrgId}
            </if>

            <if test="companyOrgId!=null and deptOrgId == null">
                and a.DEPT_ORG_ID is null and a.COMPANY_ORG_ID = #{companyOrgId}
            </if>
            <if test="name != null  and name != ''">
                and a.name LIKE concat('%',#{name},'%')
            </if>
            <if test="tenantId != null ">and a.TENANT_ID = #{tenantId}</if>
            and a.status=1
        </where>
    </select>

    <!--  根据获取实体 page   -->
    <select id="findPage" resultType="com.zcxk.evaluation.entity.EvaluationItemEntity">
        SELECT
        <include refid="evaluationItemColumns"/>
        FROM sc_evaluation_item a

        <where>
            <if test="evaluationItem.companyOrgId!=null and evaluationItem.deptOrgId!=null">
                and a.DEPT_ORG_ID = #{evaluationItem.deptOrgId} and a.COMPANY_ORG_ID = #{evaluationItem.companyOrgId}
            </if>

            <if test="evaluationItem.companyOrgId!=null and evaluationItem.deptOrgId == null">
               and a.COMPANY_ORG_ID = #{evaluationItem.companyOrgId} and a.DEPT_ORG_ID is null
            </if>
            <if test="evaluationItem.name != null  and evaluationItem.name != ''">
                and a.name LIKE concat('%',#{evaluationItem.name},'%')
            </if>
            <if test="evaluationItem.tenantId != null ">and a.TENANT_ID = #{evaluationItem.tenantId}</if>
            and a.status=1
            <if test="evaluationItem.userType!=null and evaluationItem.userType!=-999 and evaluationItem.userType!=-9999 and  evaluationItem.programItems != null and evaluationItem.programItems.size() > 0">
                <if test="evaluationItem.permissonType == 5 or evaluationItem.permissonType == 2">
                    and ( a.DEPT_ORG_ID in
                    <foreach collection="evaluationItem.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    or
                    a.COMPANY_ORG_ID in
                    <foreach collection="evaluationItem.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    )
                </if>
                <if test="evaluationItem.permissonType == 4 or evaluationItem.permissonType == 3">
                    and a.DEPT_ORG_ID in
                    <foreach collection="evaluationItem.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                </if>
                <if test="evaluationItem.permissonType == 1">
                    and a.COMPANY_ORG_ID in
                    <foreach collection="evaluationItem.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
                </if>
            </if>
        </where>
    </select>

    <select id="findListItem" resultMap="evaluationItemVo">
        SELECT
        a.id as "itemId", a.`NAME`, a.`TYPE`,a.`VALUE`,a.VALUE_CONDITION,a.`CYCLE`,a.REMARKS,
        v.`TYPE` as "itemType", v.`VALUE` as "itemValue", v.VALUE_ONE, v.VALUE_TWO
        FROM sc_evaluation_item a
        inner join sc_evaluation_item_value v on a.id = v.EVALUATION_ITEM_ID
        <where>
            a.COMPANY_ORG_ID = #{companyOrgId}
            <if test="deptOrgId == null">
                and a.DEPT_ORG_ID is null
            </if>
            <if test="deptOrgId != null">
                and a.DEPT_ORG_ID = #{deptOrgId}
            </if>
            and a.TENANT_ID = #{tenantId}
            and a.status=1
            and v.status=1
        </where>
    </select>

</mapper>