<?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="cn.iocoder.yudao.module.system.dal.mysql.taskinfo.TaskInfoMapper">

    <!--
        一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见:https://www.iocoder.cn/MyBatis/x-plugins/
     -->

    <select id="selectMyPage" resultType="cn.iocoder.yudao.module.system.dal.dataobject.taskinfo.TaskInfoDO">
        SELECT
            t.*,
            count(DISTINCT ei.id) as taskExecNum,
            SUM(CASE WHEN il.status = 2 THEN 1 ELSE 0 END) AS taskFinishNum
        FROM task_info t
             left join enterprise_inspections ei on t.id = ei.task_id
             left join inspections_log il on ei.id = il.inspections_id
             left join enterprises e on e.id = ei.enterprise_id
             left join tag_library tl on tl.id = t.task_type
        WHERE
            t.deleted = 0  and ei.deleted = 0 and ei.status = 2
        <if test="params.userId != null and params.userId != ''">
            AND ei.user_id = #{params.userId}
        </if>
        <if test="params.priority != null and params.priority != ''">
            AND t.priority = #{params.priority}
        </if>
        <if test="params.status != null and params.status != ''">
            AND t.status = #{params.status}
        </if>
        <if test="params.deptId != null and params.deptId != ''">
            AND e.department_id = #{params.deptId}
        </if>
        <if test="params.endTime != null and params.endTime != ''">
            AND t.end_date between #{params.startTime} and #{params.endTime}
        </if>
        <if test="params.tagList != null and params. tagList != ''">
            AND t.task_type =  #{params.tagList}
        </if>
            GROUP BY t.id ORDER BY t.create_time DESC
    </select>


    <select id="selectPageByEnterpriseId" resultType="cn.iocoder.yudao.module.system.dal.dataobject.taskinfo.TaskInfoDO">
        SELECT
            t.*
        FROM task_info t
            left join enterprise_inspections ei on t.id = ei.task_id
        WHERE
            t.deleted = 0  and ei.deleted = 0 and ei.enterprise_id = #{reqVO.enterpriseId}
        GROUP BY t.id
        ORDER BY t.create_time DESC
    </select>

    <select id="selectHomeGroupCount" resultType="cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeGroupCountVO">
        SELECT
            su.`real_name` as name,
            SUM(CASE WHEN il.status = 2 THEN 1 ELSE 0 END) as finishCount,
            COUNT(DISTINCT ei.id) as execCount,
            round( SUM(CASE WHEN il.status = 2 THEN 1 ELSE 0 END)  /COUNT(DISTINCT ei.id) * 100,1) as completionRate
        FROM
            enterprise_inspections ei
        LEFT JOIN
            enterprises e ON e.id = ei.enterprise_id
        LEFT JOIN
            inspections_log il ON ei.id = il.inspections_id
        LEFT JOIN
            system_users su on su.id = ei.user_id
        WHERE
            ei.deleted = 0 AND e.deleted = 0 and ei.status = 2 and e.region = #{region}
        GROUP BY
            ei.user_id
        order by
            finishCount
    </select>
    <select id="selectEnterpriseNum" resultType="cn.iocoder.yudao.module.system.controller.admin.home.vo.EnterpriseNumVO">
        select
            DATEDIFF(eq.expiry_date, NOW()) AS count,
            e.enterprises_name as name
        from
            enterprise_qualification eq
            LEFT JOIN
            enterprises e ON eq.enterprise_id = e.id
        WHERE
            e.deleted = 0 and eq.deleted = 0 AND eq.expiry_date &lt; DATE_ADD(NOW(), INTERVAL 16 DAY)  and e.region = #{region}
        <if test="deptId != null and deptId != ''">
            and e.department_id = #{deptId}
        </if>
        <if test="userId != null and userId != ''">
            and eq.user_id = #{userId}
        </if>
        <if test="time != null and time != ''">
            and eq.expiry_date between
            <foreach collection="time" item="i" open="" separator="and" close="">
                #{i}
            </foreach>
        </if>
        ORDER BY count ASC ;
    </select>

    <select id="selectEnterpriseStatus" resultType="cn.iocoder.yudao.module.system.controller.admin.home.vo.EnterpriseNumVO">
        SELECT
             e.enterprises_name as name,
            sum(CASE WHEN il.status=3 THEN 1 ELSE 0 END) as count
        FROM
            enterprise_inspections ei
            LEFT JOIN inspections_log il on il.inspections_id = ei.id
            LEFT JOIN enterprises e ON e.id = ei.enterprise_id
        <where>
            ei.deleted = 0 and il.status = 3 and e.region = #{region}
            <if test="deptId != null and deptId != ''">
                and e.department_id = #{deptId}
            </if>
            <if test="time != null and time != ''">
                and ei.create_time between
                <foreach collection="time" item="i" open="" separator="and" close="">
                    #{i}
                </foreach>
            </if>
        </where>
        GROUP BY e.enterprises_name order by count desc;
    </select>

    <select id="selectHomeExecFinish" resultType="cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeExecFinishVO">
        SELECT
            count( DISTINCT ei.id) as execCount,
            SUM(CASE WHEN il.status = 2 THEN 1 ELSE 0 END) AS finishCount
        FROM task_info t
                 left join enterprise_inspections ei on t.id = ei.task_id
                 left join inspections_log il on ei.id = il.inspections_id
                 left join  enterprises e on e.id = ei.enterprise_id
                 left join task_tag tt on t.id = tt.task_id
                 left join tag_library tl on tl.id = tt.tag_id
        WHERE
            t.deleted = 0  and ei.deleted = 0 and ei.status = 2 and e.region = #{region}
            <if test="deptId != null and deptId != ''">
                and e.department_id = #{deptId}
            </if>
            <if test="time != null and time != ''">
                and t.end_date between
                <foreach collection="time" item="i" open="" separator="and" close="">
                    #{i}
                </foreach>
            </if>
        GROUP By t.id
    </select>

</mapper>