<?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.xunmei.core.resumption.mapper.AppPlanMapper">
    <sql id="tableName">
        core_resumption_plan
    </sql>

    <sql id="baseColumn">
        id,plan_name,plan_type,plan_cycle,plan_exec,exec_org_type,check_org_type,check_type,exec_type,plan_status,description,create_time,modified_name,update_time,modified_by
    </sql>

    <resultMap id="appPlans" type="com.xunmei.core.resumption.domain.AppPlan">
        <id column="id" property="id"/>
        <result column="plan_name" property="planName"/>
        <result column="plan_type" property="planType"/>
        <result column="plan_cycle" property="planCycle"/>
        <result column="plan_exec" property="planExec"/>
        <result column="exec_org_type" property="execOrgType"/>
        <result column="check_org_type" property="checkOrgType"/>
        <result column="check_type" property="checkType"/>
        <result column="exec_type" property="execType"/>
        <result column="plan_status" property="planStatus"/>
        <result column="plan_status" property="planStatus"/>
        <result column="description" property="description"/>
        <result column="create_time" property="createTime"/>
        <result column="modified_name" property="modifiedName"/>
        <result column="update_time" property="updateTime"/>
        <result column="modified_by" property="modifiedBy"/>
        <result column="roleNames" property="roleNames"/>
        <result column="orgNames" property="orgNames"/>
    </resultMap>


    <select id="selectPage" parameterType="com.xunmei.core.resumption.vo.appPlan.AppPlanVo" resultMap="appPlans">
        SELECT
        a.*,
        d.roleNames,
        b.orgId,
        b.orgNames
        FROM
        core_resumption_plan a
        LEFT JOIN (
        SELECT
        m.plan_id,
        GROUP_CONCAT( n.`name` ) orgNames,
        GROUP_CONCAT( n.id ) orgId
        FROM
        core_resumption_plan_to_exec_org m
        LEFT JOIN sys_org n ON m.org_id = n.id
        GROUP BY
        m.plan_id
        ) b ON a.id = b.plan_id
        LEFT JOIN (
        SELECT
        r.plan_id,
        group_concat( t.`role_name` ) roleNames
        FROM
        core_resumption_plan_to_role r
        LEFT JOIN sys_role t ON r.role_id = t.id
        GROUP BY
        r.plan_id
        ) d ON a.id = d.plan_id
        <where>
            1=1
            <if test="info.planName != '' and info.planName != null">
                and a.plan_name LIKE CONCAT('%', #{info.planName}, '%' )
            </if>
            <if test="info.planStatus != null">
                and a.plan_status = #{info.planStatus}
            </if>
            <if test="info.planCycle != null">
                and a.plan_cycle = #{info.planCycle}
            </if>
            <if test="info.planExec != null">
                and a.plan_exec = #{info.planExec}
            </if>
            <if test="info.orgType != null">
                and a.exec_org_type = #{info.orgType}
            </if>
            <if test="info.roleList != null and info.roleList.size > 0">
                and a.id in (select plan_id from core_resumption_plan_to_role where role_id in
                <foreach collection="info.roleList" item="roleId" index="index" open="(" separator="," close=")">
                    #{roleId}
                </foreach>
                )
            </if>
            <if test="info.orgList != null and info.orgList.size > 0">
                and a.id in (select plan_id from core_resumption_plan_to_exec_org where org_id in
                <foreach collection="info.orgList" item="orgId" index="index" open="(" separator="," close=")">
                    #{orgId}
                </foreach>
                )
            </if>
        </where>
    </select>
    <select id="selectItemPage" parameterType="com.xunmei.core.resumption.vo.appPlan.RuleItemVo" resultType="com.xunmei.core.resumption.vo.appPlan.RuleItemVo">
        select
        a.id,
        a.item_id,
        b.item_num,
        b.item_name,
        b.item_desc,
        a.safe_type,
        c.`name` area_name,
        c.id as areaId,
        a.point_name,
        a.point_num,
        a.time_type,
        d.id as ruleId,
        d.rule_name as ruleName,
        a.business_type as businessType
        from sys_rule_point a
        inner JOIN sys_rule_item b on a.item_id = b.id
        inner JOIN t_area c on a.area_id = c.id
        inner JOIN sys_rule d on b.rule_id = d.id
        where 1=1
        <if test="info.ruleId != null">
            and a.rule_id = #{info.ruleId}
        </if>
        <if test="info.itemName != '' and info.itemName != null">
            and b.item_name like CONCAT('%', #{info.itemName}, '%' )
        </if>
        and a.deleted = 0
        order by b.item_num,a.point_num
    </select>


    <select id="selectByPlanId" resultType="com.xunmei.core.resumption.vo.appPlan.RuleItemVo">
        SELECT a.item_id,
               b.name as item_name,
               b.desc as item_desc,
--                b.item_num,
--                a.safe_type,
               c.`name`           area_name,
               a.name as point_name,
--                a.point_num,
               h.point_scan,
               h.required,
               d.name  as ruleName,
               a.business_type as businessType
        FROM (SELECT item_id, point_scan, required FROM core_resumption_plan_to_item WHERE plan_id = #{planId}) h
                 LEFT JOIN core_resumption_rule_point a ON h.item_id = a.item_id
                 LEFT JOIN core_resumption_rule_item b ON a.item_id = b.id
                 LEFT JOIN sys_area c ON a.area_id = c.id
                 LEFT JOIN core_resumption_rule d on b.rule_id = d.id
--         order by b.item_num, a.point_num
    </select>

    <select id="findExecOrgByPlan" parameterType="Long" resultType="Long">
        select org_id
        from core_resumption_plan_to_exec_org a
        INNER JOIN sys_org o on a.org_id=o.id and o.deleted=0 and o.is_lock=0
        where a.plan_id = #{plan_id}
    </select>
    <select id="findRoleByPlan" parameterType="Long" resultType="Long">
        select role_id
        from core_resumption_plan_to_role a
        where a.plan_id = #{plan_id}
    </select>

    <select id="selectPlanList" resultType="com.xunmei.core.resumption.vo.appPlan.ObjectDataClass">
        select id as id, plan_name as name
        from core_resumption_plan
        where plan_status = 0 and exec_type = 0
        <if test="orgType != null">
            and exec_org_type = #{orgType}
        </if>
        <if test="execTime != null">
            and plan_exec = #{execTime}
        </if>
        <if test="planCycle != null">
            and plan_cycle = #{planCycle}
        </if>
        <!--
        2022-8-30需求改动,之前是查询出来的履职计划是加上了执行机构为具体机构的计划,
        8.30以后不再需要此类计划

        (select id, plan_name as name
        from core_resumption_plan
        where plan_status = 0 and exec_type = 0
        <if test="orgType != null">
            and exec_org_type = #{orgType}
        </if>
        <if test="execTime != null">
            and plan_exec = #{execTime}
        </if>

        )
        UNION ALL
        (SELECT p.id,
        p.plan_name as name
        FROM core_resumption_plan_to_exec_org tapo
        INNER JOIN t_org o ON tapo.org_id = o.id
        INNER JOIN core_resumption_plan p ON tapo.plan_id = p.id

        where p.plan_status = 0

        <if test="orgType != null">
            and o.type = #{orgType}
        </if>
        <if test="execTime != null">
            and plan_exec = #{execTime}
        </if>
        )-->
    </select>

    <select id="selectPlanByName" resultMap="appPlans">
        select *
        from core_resumption_plan
        where plan_name like CONCAT('%', #{planName}, '%')
        limit 1
    </select>
    <select id="selectPlanNameById" resultType="java.lang.String">
        select plan_name from core_resumption_plan where id = #{planId}
    </select>
    <select id="selectPlanNames" resultType="com.xunmei.common.core.vo.IdNameVo">
        select id, plan_name as name from sys_plan where id in
            <foreach collection="ids" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
    </select>
</mapper>