<?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.charge.core.dao.ThingyearSettingMapper">

    <!--  实体栏位  -->
    <sql id="thingyearSettingColumns">
         a.id as "id" ,
         a.tenant_id as "tenantId" ,
         a.company_org_id as "companyOrgId" ,
         a.dept_org_id as "deptOrgId" ,
         a.thing_type as "thingType" ,
         a.thing_year_type as "thingYearType" ,
         a.thing_count as "thingCount" ,
         a.remark as "remark" ,
         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="findThingyearSettingById" resultType="com.zcxk.charge.core.entity.ThingyearSettingEntity">
        SELECT
        <include refid="thingyearSettingColumns"/>
        FROM sms_thingyear_setting a
        WHERE a.id = #{id} and a.status=1
    </select>

    <!--  根据获取实体List   -->
    <select id="findList" resultType="com.zcxk.charge.core.entity.ThingyearSettingEntity">
        SELECT
        <include refid="thingyearSettingColumns"/>
        FROM sms_thingyear_setting a

        <where>
            a.status=1
            <if test="thingyearSetting.tenantId != null  and thingyearSetting.tenantId != ''">and a.tenant_id = #{thingyearSetting.tenantId}</if>
            <if test="thingyearSetting.thingType != null ">and a.thing_type = #{thingyearSetting.thingType}</if>
            <if test="thingyearSetting.userType!=null and thingyearSetting.userType!=-999 and thingyearSetting.userType!=-9999 and  thingyearSetting.programItems != null and thingyearSetting.programItems.size() > 0">
                <if test="thingyearSetting.permissonType == 5 or thingyearSetting.permissonType == 2">
                    and ( a.DEPT_ORG_ID in
                    <foreach collection="thingyearSetting.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    or
                    a.COMPANY_ORG_ID in
                    <foreach collection="v.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    )
                </if>
                <if test="thingyearSetting.permissonType == 4 or thingyearSetting.permissonType == 3">
                    and a.DEPT_ORG_ID in
                    <foreach collection="thingyearSetting.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                </if>
                <if test="thingyearSetting.permissonType == 1">
                    and a.COMPANY_ORG_ID in
                    <foreach collection="thingyearSetting.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>

    <!--  根据获取实体 page   -->
    <select id="findPage" resultType="com.zcxk.charge.core.entity.ThingyearSettingEntity">
        SELECT
        <include refid="thingyearSettingColumns"/>
        FROM sms_thingyear_setting a
        <where>
            a.status=1
            <if test="thingyearSetting.tenantId != null  and thingyearSetting.tenantId != ''">and a.tenant_id =
                #{thingyearSetting.tenantId}
            </if>
            <if test="thingyearSetting.thingType != null ">and a.thing_type = #{thingyearSetting.thingType}</if>
        </where>
    </select>
</mapper>