<?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.ruoyi.system.mapper.task.ext.TkCommonTaskExtMapper">
    <select id="selectList" parameterType="java.util.Map" resultMap="com.ruoyi.system.mapper.task.TkCommonTaskMapper.BaseResultMap">
        select
            distinct
            CASE WHEN ta.come_from = 1 THEN '中心发布'
            WHEN ta.come_from = 2 THEN '领导交办'
            WHEN ta.come_from = 3 THEN '12345'
            WHEN ta.come_from = 4 THEN '市长信箱'
            WHEN ta.come_from = 5 THEN '数字城管'
            WHEN ta.come_from = 6 THEN '群众举报'
            WHEN ta.come_from = 8 THEN '信访投诉'
            WHEN ta.come_from = 9 THEN '12319'
            WHEN ta.come_from = 10 THEN '随手拍'
            WHEN ta.come_from = 11 THEN '接警工单'
            WHEN ta.come_from = 12 THEN '乡镇签收'
            WHEN ta.come_from = 13 THEN '来信来访'
            WHEN ta.come_from = 14 THEN '舆情'
            WHEN ta.come_from = 15 THEN '污染防治'
            WHEN ta.come_from = 17 THEN '非诉中心'
            WHEN ta.come_from = 18 THEN '环卫督查'
            ELSE '网格上报' END  comeFrom2,
            ta.id,ta.tk_id,ta.create_time,ta.tk_name,ta.tk_status,ta.release_user_name,ta.release_user_id,ta.release_time,ta.priority_type,
            ta.belong_grid,ta.tk_address,ta.matter_name,ta.standby3,ta.standby4,ta.handle_type,ta.assigned_end_time,ta.verification_end_time,
            ta.pj_person_id,ta.pj_person_name,ta.matter_type, ta.standby2 ,ta.perform_user_id,ta.perform_user_name,ta.come_from,ta.longitude,ta.latitude,ta.gs_type,
            ta.is_youxiao,ta.qpr_ids,ta.task_type,ta.assigned_dept_ids,ta.assigned_dept_names
        from tk_common_task ta
        <if test='roleLeaveFlag != true' >
            LEFT JOIN tk_common_report rep on ta.id=rep.common_tk_id
            LEFT JOIN tk_common_leader lea on ta.id=lea.tk_id
            LEFT JOIN tk_common_qianpi qian on ta.id=qian.tk_id
        </if>
        <if test="(cswdId !=null and cswdId!='') or roleLeaveFlag != true">
            LEFT JOIN tk_common_receive rec on ta.id=rec.common_tk_id
        </if>
        <where>
            <trim prefix="(" suffix=")" prefixOverrides="or">
                <if test="beassignedUserId !=null and beassignedUserId !=''">
                    or (
                        ta.assigned_user_id=#{beassignedUserId}
                        or FIND_IN_SET(#{beassignedUserId},ta.perform_user_id)
                        or ta.verification_user_id=#{beassignedUserId}
                        or ta.pj_person_id=#{beassignedUserId}
                    )
                </if>
                <if test="releaseId !=null and releaseId !=''">
                    or ta.create_by=#{releaseId}
                </if>
                <if test="verificationUserId !=null and verificationUserId != ''">
                    or (ta.standby4 is not  null and ta.assigned_user_id=#{verificationUserId})
                </if>
                <if test="leaderId !=null and leaderId != ''">
                    or (ta.release_user_role_num=1 or ta.release_user_role_num=0)
                </if>
                <if test="zdbId !=null and zdbId !=''">
                    or ta.standby3 like concat('%',#{zdbUserName},'%')
                </if>
                <if test="cswdId !=null and cswdId !=''">
                    or (rec.type=3 and rec.user_id=#{cswdId})
                </if>
            </trim>

            <if test="finishedFlag !=null">
                <if test="finishedFlag">
                    and ta.tk_status= 50
                </if>
                <if test="!finishedFlag">
                    and ta.tk_status!=50
                </if>

            </if>
            <if test='roleLeaveFlag != true' >
                and (
                    lea.user_id=#{loginUserId}
                    or ta.assigned_user_id=#{loginUserId}
                    or ta.release_user_id=#{loginUserId}
                    or ta.create_by=#{loginUserId}
                    or ta.verification_user_id=#{loginUserId}
                    or ta.zp_verification_user_id=#{loginUserId}
                    or FIND_IN_SET(#{loginUserId},ta.perform_user_id)
                    or qian.user_id=#{loginUserId}
                    OR rec.user_id =#{loginUserId}
                    or FIND_IN_SET(#{loginUserId},ta.qpr_ids)
                    or exists(
                        select dept.dept_id from sys_dept dept
                        where ta.assigned_dept_ids = dept.dept_id and FIND_IN_SET(#{loginUserId},dept.task_assigned_user_ids)
                    )
                )
            </if>
            <if test="gsType != null">
                <if test="gsType != 4 ">
                    and ta.gs_type = #{gsType,jdbcType=INTEGER}
                </if>
                <if test="gsType == 4 ">
                    and ta.gs_type is not null
                </if>
            </if>
            <if test="priorityType !=null">
                and ta.priority_type=#{priorityType}
            </if>
            <if test="tkStatuStr !=null and tkStatuStr != ''">
                and
                <foreach collection="tkStatuStr.split(',')" item="item" index="index" open="(" separator="or" close=")">
                    <if test='item =="1"'>
                        ta.tk_status>40
                    </if>
                    <if test='item =="2"'>
                        ta.tk_status=40
                    </if>
                    <if test='item =="3"'>
                        (ta.tk_status>=30 and  ta.tk_status &lt; 40 and ta.tk_status!=36)
                    </if>
                    <if test='item =="4"'>
                        (ta.tk_status>=20 and ta.tk_status &lt; 30 or ta.tk_status=36)
                    </if>
                    <if test='item =="5"'>
                        (ta.tk_status>10 and ta.tk_status &lt; 20 and ta.tk_status!=16)
                    </if>
                    <if test='item =="6"'>
                        (ta.tk_status=10 or ta.tk_status=16)
                    </if>
                    <if test='item =="7"'>
                        ta.tk_status=32
                    </if>
                    <if test='item =="8"'>
                        ta.tk_status=12
                    </if>
                    <if test='item =="9"'>
                        ta.is_youxiao=1
                    </if>
                    <if test='item =="10"'>
                        (ta.tk_status=17 or ta.tk_status=16)
                    </if>
                </foreach>
            </if>

            <if test="releaseStartTime !=null and releaseStartTime != ''">
                and  (ta.release_time between #{releaseStartTime} and  #{releaseEndTime})
            </if>

            <if test="eventTime !=null and eventTime != ''">
                and  date_format(ta.create_time,'%Y-%m-%d') = #{eventTime}
            </if>
            <if test="matterType !=null">
                and ta.matter_type=#{matterType}
            </if>
            <if test="comeFrom !=null">
                <if test='comeFrom == 99'>
                    and (ta.come_from is null )
                </if>
                <if test='comeFrom ==6'>
                    and (ta.come_from=6 or ta.come_from=7 )
                </if>
                <if test='comeFrom !=6  and comeFrom !=99 '>
                    and ta.come_from=#{comeFrom}
                </if>
            </if>

            <if test="notComeFrom != null">
                and (ta.come_from != #{notComeFrom} or ta.come_from is null)
            </if>

            <if test="nameOrId !=null and nameOrId != ''">
                and (ta.tk_id=#{nameOrId} or ta.tk_name like '%${nameOrId}%')
            </if>
            <if test="createUserName !=null and createUserName != ''">
                and  (ta.release_user_name like '%${createUserName}%')
            </if>
            <if test="performUserName !=null and performUserName != ''">
                and  (ta.perform_user_name like '%${performUserName}%')
            </if>

            <if test="wxUserId !=null and wxUserId != ''">
                and (ta.wx_user_id=#{wxUserId})
            </if>
            <if test="appUserId !=null and appUserId != ''">
                and (ta.app_user_id=#{appUserId})
            </if>

            and ta.standby1=0
            and (ta.is_hst=0 or ta.is_hst is null)

        </where>
        ORDER BY ta.create_time desc
    </select>

    <select id="selectSimpleList" resultType="com.ruoyi.system.domain.task.vo.SimpleTaskVo">
        select id, tk_name as tkName, longitude, latitude,
        CASE WHEN come_from = 1 THEN '中心发布'
        WHEN come_from = 2 THEN '领导交办'
        WHEN come_from = 3 THEN '12345'
        WHEN come_from = 4 THEN '市长信箱'
        WHEN come_from = 5 THEN '数字城管'
        WHEN come_from = 6 THEN '群众举报'
        WHEN come_from = 8 THEN '信访投诉'
        WHEN come_from = 9 THEN '12319'
        WHEN come_from = 10 THEN '随手拍'
        WHEN come_from = 11 THEN '接警工单'
        WHEN come_from = 12 THEN '乡镇签收'
        WHEN come_from = 13 THEN '来信来访'
        WHEN come_from = 14 THEN '舆情'
        WHEN come_from = 15 THEN '污染防治'
        WHEN come_from = 17 THEN '非诉中心'
        WHEN come_from = 18 THEN '环卫督查'
        ELSE '网格上报' END comeForm
        from tk_common_task where longitude is not null and latitude is not null
        <if test="unfinished !=null">
            and tk_status!=#{unfinished}
        </if>
        and standby1=0
        and (is_hst=0 or is_hst is null)
        ORDER BY create_time desc
        <if test="currIndex !=null and pageSize != null">
            limit #{currIndex} , #{pageSize}
        </if>
    </select>


    <select id="checkCuiBan" parameterType="java.lang.String" resultType="java.lang.Integer">
        SELECT count(*) FROM `tk_common_task_dynamic` t where t.common_tk_id=#{id} and t.cuiban_reason is not null;
    </select>


    <select id="countAllTypeTask" parameterType="java.util.Map" resultType="java.lang.Integer">
        select count(distinct ta.id)
        from tk_common_task ta
        LEFT JOIN grid_region_user bwu on FIND_IN_SET(bwu.user_id,ta.perform_user_id)
        <where>
            <if test="tkStatus !=null">
                <if test='tkStatus =="1"'>
                    and ta.tk_status>40
                </if>
                <if test='tkStatus =="2"'>
                    and ta.tk_status=40
                </if>
                <if test='tkStatus =="3"'>
                    and (ta.tk_status>=30 and  ta.tk_status &lt; 40 and ta.tk_status!=36)
                </if>
                <if test='tkStatus =="4"'>
                    and (ta.tk_status>=20 and ta.tk_status &lt; 30 or ta.tk_status=36)
                </if>
                <if test='tkStatus =="5"'>
                    and (ta.tk_status>10 and ta.tk_status &lt; 20 and ta.tk_status!=16)
                </if>
                <if test='tkStatus =="6"'>
                    and (ta.tk_status=10 or ta.tk_status=16)
                </if>
                <if test='tkStatus =="7"'>
                    and ta.tk_status=32
                </if>
                <if test='tkStatus =="8"'>
                    and ta.tk_status=12
                </if>
                <if test='tkStatus =="9"'>
                    and ta.is_youxiao=1
                </if>
                <if test='tkStatus =="10"'>
                    and (ta.tk_status=17 or ta.tk_status=16)
                </if>
            </if>
            <if test='wgCode !=null'>
                and wg_code like '${wgCode}%'
            </if>
            <if test="releaseStartTime !=null">
                and  (ta.release_time between #{releaseStartTime} and  #{releaseEndTime})
            </if>
            and ta.standby1=0

        </where>

    </select>



    <select id="selectOnTimeCount" resultType="java.lang.Integer" parameterType="java.util.Map">
        select
        count(*)
        from tk_common_task ta
        where
        standby1=0 and
        tk_status=50 and
        assigned_end_time >=assigned_time and
        (release_time between #{releaseStartTime} and  #{releaseEndTime})
    </select>
    <select id="selectNotOnTimeCount" resultType="java.lang.Integer" parameterType="java.util.Map">
        select
        count(*)
        from tk_common_task ta
        where standby1=0
        and tk_status=50
        and assigned_end_time &lt;  assigned_time
        and (release_time between #{releaseStartTime} and  #{releaseEndTime})
    </select>

    <select id="selectNotOnTimeNotOverCount" parameterType="java.util.Map" resultType="java.lang.Integer">

        select
        count(*)
        from tk_common_task ta
        where  standby1=0 and tk_status &lt; 50
        and (release_time between #{releaseStartTime} and  #{releaseEndTime})
        and ((assigned_end_time &lt;  assigned_time) or(assigned_time is null and assigned_end_time &lt; #{nowTime}));

    </select>

    <select id="zhengchangcl" parameterType="java.util.Map" resultType="java.lang.Integer">

        select
        count(*)
        from tk_common_task ta
        where  standby1=0 and tk_status &lt; 50
        and (release_time between #{releaseStartTime} and  #{releaseEndTime})
        and ((assigned_end_time >=  assigned_time) or(assigned_time is null and assigned_end_time >= #{nowTime}) or ( assigned_end_time is null));

    </select>


    <select id="taskTypeStatistical" parameterType="java.util.Map" resultType="map">

        select  sum(case  WHEN matter_type=1 then 1 else 0 end) as cx,
        sum(case  WHEN matter_type=2 then 1 else 0 end) as qy,
        sum(case  WHEN matter_type=3 then 1 else 0 end) as zc from tk_common_task where standby1=0 and MONTH ( create_time )=#{mm}

    </select>

    <select id="qiyeAbnormalNum" parameterType="java.util.Map" resultType="map">
        select  sum(case  WHEN parent_mattetr_id='qy001' then 1 else 0 end) as hbNum,
        sum(case  WHEN parent_mattetr_id='qy002' then 1 else 0 end) as ajNum,
        sum(case  WHEN (parent_mattetr_id !='qy001' and parent_mattetr_id !='qy002') then 1 else 0 end) as otherNum
        from tk_common_task where standby1=0
        and round_subtask_id is not null and round_subtask_id !=''
        and MONTH ( create_time )=#{mm}

    </select>


    <select id="wgczph"  parameterType="java.util.Map"  resultType="java.util.Map">

        SELECT
        wg_parent_name as parentName,
        count(*) as allNum,
        wg_parent_id as parentId
        FROM
        tk_common_task
        WHERE
        standby1 = 0
        and (release_time between #{releaseStartTime} and  #{releaseEndTime})
        AND wg_parent_name != (select wg_name from grid_region where  wg_parent_id is null and wg_type=1)
        GROUP BY
        wg_parent_name
        ORDER BY allNum desc
        limit 0,50

    </select>

    <select id="wgczphTwoGrid"  parameterType="java.util.Map"  resultType="java.util.Map">

        SELECT
        xxb.wg_name AS parentName,
        count( * ) AS allNum,
        tk.wg_code AS parentId
        FROM
        tk_common_task tk
        LEFT JOIN grid_region xxb on tk.wg_code=xxb.wg_code
        WHERE
        tk.standby1 = 0
        and (tk.release_time between #{releaseStartTime} and  #{releaseEndTime})
        AND xxb.wg_name != (select wg_name from grid_region where  wg_parent_id is null and wg_type=1)
        GROUP BY
        xxb.wg_name
        ORDER BY		allNum desc
        limit 0,50

    </select>

    <select id="nearThreeMonthCount"  parameterType="java.util.Map"  resultType="java.util.Map">

        SELECT
        count(*) as allNum,
        sum(
        CASE
        WHEN tk_status > 40 THEN
        1
        ELSE
        0
        END
        ) as endNum,
        sum(
        CASE
        WHEN ta.priority_type = 0 THEN
        1
        ELSE
        0
        END
        ) as pt,
        sum(
        CASE
        WHEN ta.priority_type = 1 THEN
        1
        ELSE
        0
        END
        ) as zy,
        sum(
        CASE
        WHEN ta.priority_type = 2 THEN
        1
        ELSE
        0
        END
        ) as jj
        FROM
        tk_common_task ta
        WHERE
        (
        create_time BETWEEN #{firstDayOfMonth1}
        AND #{lastDayOfMonth1}
        )
        and
        standby1=0

        <if test="wgParentCode !=null">
            and   wg_parent_id = #{wgParentCode}
        </if>
        <if test="wgCode !=null">
            and   wg_code like '${wgCode}%'
        </if>

    </select>


    <select id="nearSevenDayCountByWgCode"  parameterType="java.util.Map"  resultType="java.util.Map">
        SELECT
            count(*) AS allNum,
            sum( CASE WHEN tk_status > 40 THEN 1 ELSE 0 END ) AS endNum,
            sum( CASE WHEN ta.priority_type = 0 THEN 1 ELSE 0 END ) AS pt,
            sum( CASE WHEN ta.priority_type = 1 THEN 1 ELSE 0 END ) AS zy,
            sum( CASE WHEN ta.priority_type = 2 THEN 1 ELSE 0 END ) AS jj
        FROM
            tk_common_task ta
        WHERE
            create_time LIKE '${time}%' and standby1=0
            <if test="wgParentCode !=null">
                and wg_parent_id = #{wgParentCode}
            </if>
            <if test="wgCode !=null">
                and wg_code like '${wgCode}%'
            </if>
    </select>


    <select id="overBySelfAndSBSJCount"  parameterType="java.util.Map"  resultType="java.util.Map">

        SELECT
        sum(
        CASE
        WHEN handle_type = 0 THEN
        1
        ELSE
        0
        END
        ) as zxcl,
        sum(
        CASE
        WHEN (handle_type = 2 or handle_type = 1) THEN
        1
        ELSE
        0
        END
        ) as sbzx
        FROM
        tk_common_task ta
        WHERE
        (
        release_time BETWEEN #{firstDayOfMonth1}
        AND #{lastDayOfMonth1}
        )
        and
        standby1=0
        and (release_time between #{releaseStartTime} and  #{releaseEndTime})
    </select>

    <select id="countByMonth"  parameterType="java.util.Map"  resultType="java.util.Map">

        SELECT
        count(*) taskCount
        FROM
        tk_common_task ta
        WHERE
        (
        create_time BETWEEN #{firstDayOfMonth1}
        AND #{lastDayOfMonth1}
        )
        and
        standby1=0
    </select>

    <select id="getUserByRoleCodeAndWgCode"  parameterType="java.util.Map"  resultType="java.lang.String">
        select  a.user_id from grid_region_user a
        LEFT JOIN sys_user_role b on a.user_id = b.user_id
        left join sys_role c on b.role_id = c.role_id
        where a.wg_id=#{wgCode} and c.role_key=#{roleCode}
    </select>

    <select id="getLeaderCount"  parameterType="java.util.Map"  resultType="java.lang.Integer">
        select count(*) from tk_common_leader le
        LEFT JOIN tk_common_task ta on le.tk_id=ta.id
        where le.user_id=#{userId}
    </select>

    <select  id="bigWindowWGCount" parameterType="java.util.Map" resultType="java.util.Map">
        select m.wg_code,m.wg_name,m.wg_centre_coordinate ,wgcount,IFNULL(n.wgTaskNum,0) as wgTaskNum,IFNULL(n.overWgTaskNum,0) overWgTaskNum,concat(IFNULL(ROUND(overWgTaskNum/wgTaskNum*100,2),0),'%') as wcl
        from
        (
        select e.wg_code,e.wg_name,e.wg_centre_coordinate,f.wgcount from
        (
        SELECT a.wg_code,a.wg_name,a.wg_centre_coordinate from grid_region a where  a.wg_type='1' and a.wg_parent_id=#{topCode} and a.is_valid='1'
        )e
        LEFT JOIN
        (select wg_parent_id,count(wg_parent_id) as wgcount from grid_region a  where a.wg_type='1' and a.is_valid='1' and a.level='3' GROUP BY a.wg_parent_id
        )f
        on e.wg_code =f.wg_parent_id
        )m
        LEFT JOIN
        (
        select t.wg_parent_name,t.wg_parent_id ,count(*) as wgTaskNum,  IFNULL(w.overWgTaskNum,0) overWgTaskNum
        from tk_common_task t
        LEFT JOIN (
        select ta.wg_parent_name,ta.wg_parent_id ,count(*) as overWgTaskNum
        from tk_common_task ta
        where ta.wg_parent_id!=#{topCode}  and ta.tk_status>=40
        GROUP BY ta.wg_parent_id
        )w on t.wg_parent_id=w.wg_parent_id
        where t.wg_parent_id!=#{topCode}
        GROUP BY t.wg_parent_id

        )n
        on m.wg_code=n.wg_parent_id;

    </select>



    <select id="taskCountByComeFrom"  parameterType="java.util.Map"  resultType="java.util.Map">

        select comeFrom2,sum(comeTaskNum) as comeTaskNum
        FROM
        (
        SELECT
        CASE WHEN ta.come_from = 1 THEN '中心发布'
        WHEN ta.come_from = 2 THEN '领导交办'
        WHEN ta.come_from = 3 THEN '12345'
        WHEN ta.come_from = 4 THEN '市长信箱'
        WHEN ta.come_from = 5 THEN '数字城管'
        WHEN ta.come_from = 6 THEN '群众举报'
        WHEN ta.come_from = 8 THEN '信访投诉'
        WHEN ta.come_from = 9 THEN '12319'
        WHEN ta.come_from = 10 THEN '我发现'
        WHEN ta.come_from = 11 THEN '民意收集'
        WHEN ta.come_from = 12 THEN '隐患整改'
        WHEN ta.come_from = 13 THEN '城运工单'
        WHEN ta.come_from = 14 THEN '舆情'
        WHEN ta.come_from = 15 THEN '污染防治'
        WHEN ta.come_from = 17 THEN '非诉中心'
        WHEN ta.come_from = 18 THEN '环卫督查'
        WHEN ta.come_from is null THEN '网格上报'
        ELSE '其它' END  comeFrom2,
        count(*) comeTaskNum
        from tk_common_task ta
        where
        ta.standby1=0
        <if test=" date !=null ">
            and ta.release_time like #{date}
        </if>
        <if test=" startTime !=null ">
            and DATE_FORMAT(ta.release_time ,'%Y-%m-%d') >= DATE_FORMAT(#{startTime},'%Y-%m-%d')
        </if>
        <if test=" endTime !=null ">
            and DATE_FORMAT(#{endTime},'%Y-%m-%d') >= DATE_FORMAT(ta.release_time ,'%Y-%m-%d')
        </if>

        <if test=" type !=null and type == 1">
            and ta.tk_status>=12
        </if>
        <if test=" wgCode !=null ">
            and ta.wg_code like '${wgCode}%'
        </if>

        GROUP BY ta.come_from
        ) b
        GROUP BY b.comeFrom2
        ORDER BY comeTaskNum desc

    </select>

    <select id="taskCountByBaseId"  parameterType="java.util.Map"  resultType="java.util.Map">

        select comeFrom2,sum(comeTaskNum) as comeTaskNum
        FROM
        (
        SELECT
        CASE WHEN (ta.come_from = '1' )   THEN '中心'
        WHEN ta.come_from = '2' THEN '领导交办'
        WHEN ta.come_from = '3' THEN '12345'
        WHEN ta.come_from = '4' THEN '市长信箱'
        WHEN ta.come_from = '5' THEN '数字城管'
        ELSE '综合治理' END  comeFrom2,
        count(*) comeTaskNum
        from tk_common_task ta
        where ta.come_from is not null
        and ta.standby1=0

        GROUP BY ta.come_from
        ) b
        GROUP BY b.comeFrom2
        ORDER BY comeTaskNum desc

    </select>





    <select id="selectMatterRank"  parameterType="java.util.Map"  resultType="java.util.Map">

        SELECT
        parent_mattetr_name,
        count(*) num
        FROM
        tk_common_task
        where
        standby1=0
        and parent_mattetr_name is not null
        and (release_time between #{releaseStartTime} and  #{releaseEndTime})
        GROUP BY
        parent_mattetr_name
        ORDER BY
        num DESC

    </select>


    <select id="selectMatterRankThreeType"  parameterType="java.util.Map"  resultType="java.util.Map">

        SELECT
        matter_name,
        count(*) num
        FROM
        tk_common_task
        where
        standby1=0
        and matter_name is not null
        and (release_time between #{releaseStartTime} and  #{releaseEndTime})
        GROUP BY
        matter_name
        ORDER BY
        num DESC

    </select>

    <select id="manyiRank"  parameterType="java.util.Map"  resultType="java.util.Map">

        SELECT
        wg_parent_name,
        sum(
        CASE
        WHEN tk_score = 5 THEN
        1
        ELSE
        0
        END
        ) AS num,
        count(*) allNum,
        CAST(
        (
        sum(
        CASE
        WHEN tk_score = 5 THEN
        1
        ELSE
        0
        END
        )
        ) / (count(*)) * 100 AS DECIMAL (10, 2)
        ) AS bf
        FROM
        tk_common_task t
        GROUP BY
        wg_parent_name
        ORDER BY
        bf DESC

    </select>


    <select id="personManYiTJ"  parameterType="java.util.Map"  resultType="java.util.Map">

        SELECT
        perform_user_name,
        sum(
        CASE
        WHEN tk_score = 5 THEN
        1
        ELSE
        0
        END
        ) AS num,
        count(*) allNum,
        CAST(
        (
        sum(
        CASE
        WHEN tk_score = 5 THEN
        1
        ELSE
        0
        END
        )
        ) / (count(*)) * 100 AS DECIMAL (10, 2)
        ) AS pfbfb,
        CAST(
        (CAST(
        (
        sum(
        CASE
        WHEN tk_score = 5 THEN
        1
        ELSE
        0
        END
        )
        ) / (count(*)) * 100 AS DECIMAL (10, 2)
        ))/20 AS DECIMAL (10, 2)
        ) as xjbfb

        FROM
        tk_common_task t
        WHERE
        perform_user_id=#{performUserId}
        ORDER BY
        pfbfb DESC

    </select>



    <select id="getTkScore"  parameterType="java.util.Map"  resultType="java.util.Map">

        SELECT
        SUM(
        CASE tk_score
        WHEN 5 THEN
        1
        ELSE
        0
        END
        ) '5',
        SUM(
        CASE tk_score
        WHEN 4 THEN
        1
        ELSE
        0
        END
        ) '4',
        SUM(
        CASE tk_score
        WHEN 3 THEN
        1
        ELSE
        0
        END
        ) '3',
        SUM(
        CASE tk_score
        WHEN 2 THEN
        1
        ELSE
        0
        END
        ) '2',
        SUM(
        CASE tk_score
        WHEN 1 THEN
        1
        ELSE
        0
        END
        ) '1'
        FROM
        tk_common_task
        WHERE
        standby1=0
        and (release_time between #{releaseStartTime} and  #{releaseEndTime})
        and tk_score IS NOT NULL
    </select>



    <select id="taskListByWgCode" parameterType="java.util.Map" resultMap="com.ruoyi.system.mapper.task.TkCommonTaskMapper.BaseResultMap">
        select
        *
        from tk_common_task ta
        <where>
            ta.wg_code in
            <foreach collection="gridIds" item="code" index="index" open="(" close=")" separator=",">
                #{code}
            </foreach>

            and ta.release_time=#{selectDate}

        </where>

        GROUP BY ta.wg_code


    </select>

    <select id="thirdWgTaskList" parameterType="java.util.Map" resultMap="com.ruoyi.system.mapper.task.TkCommonTaskMapper.BaseResultMap">
        select
        *
        from tk_common_task ta
        <where>

            ta.wg_code in
            <foreach collection="gridIds" item="code" index="index" open="(" close=")" separator=",">
                #{code}
            </foreach>

            <if test="ymFlag == 1">
                and DATE_FORMAT(ta.release_time ,'%Y-%m')=DATE_FORMAT(#{startDate,jdbcType=DATE},'%Y-%m')
            </if>
            <if test="ymFlag == 2">
                and DATE_FORMAT(ta.release_time ,'%Y-%m-%d')=DATE_FORMAT(#{startDate,jdbcType=DATE},'%Y-%m-%d')
            </if>
            and ta.standby1 = 0
        </where>
    </select>

    <select id="countThirdWgTaskList" parameterType="java.util.Map" resultType="java.lang.Long">
        select
        count(1)
        from tk_common_task ta
        <where>

            ta.wg_code in
            <foreach collection="gridIds" item="code" index="index" open="(" close=")" separator=",">
                #{code}
            </foreach>

            <if test="ymFlag == 1">
                and DATE_FORMAT(ta.release_time ,'%Y-%m')=DATE_FORMAT(#{startDate,jdbcType=DATE},'%Y-%m')
            </if>
            <if test="ymFlag == 2">
                and DATE_FORMAT(ta.release_time ,'%Y-%m-%d')=DATE_FORMAT(#{startDate,jdbcType=DATE},'%Y-%m-%d')
            </if>
            and ta.standby1 = 0
        </where>

    </select>

    <select id="countComeFromThirdTask" parameterType="java.util.Map" resultType="java.lang.Long">
        select
        count(1)
        from tk_common_task ta
        <where>
            DATE_FORMAT(ta.release_time ,'%Y-%m-%d')=DATE_FORMAT(#{startDate,jdbcType=DATE},'%Y-%m-%d')
            <if test="comeFrom != null and comeFrom != ''">
                and ta.come_from = #{comeFrom}
            </if>
        </where>

    </select>

    <select id="ComeFromThirdTaskStat" parameterType="java.util.Map" resultMap="com.ruoyi.system.mapper.task.TkCommonTaskMapper.BaseResultMap">

        SELECT
        COUNT(1) num
        ,guishu_code guishuCode
        FROM
        tk_common_task
        <where>
            come_from=#{comeFrom}
            <if test=" startDate !=null ">
                and DATE_FORMAT(release_time ,'%Y-%m')=DATE_FORMAT(#{startDate,jdbcType=DATE},'%Y-%m')
            </if>
        </where>
        GROUP BY guishu_code

    </select>

    <select id="matterTask" parameterType="java.util.Map" resultMap="com.ruoyi.system.mapper.task.TkCommonTaskMapper.BaseResultMap">

        SELECT
        COUNT(1) num
        ,parent_mattetr_name matter_name
        FROM
        tk_common_task
        <where>
            1=1
            <!--      (come_from IS NULL or come_from in(1,2,10,11,12))-->
            <!--      and handle_type=2-->
            <if test=" startDate !=null ">
                and DATE_FORMAT(release_time ,'%Y-%m')=DATE_FORMAT(#{startDate,jdbcType=DATE},'%Y-%m')
            </if>
            <if test=" startTime !=null ">
                and DATE_FORMAT(release_time ,'%Y-%m-%d') >= DATE_FORMAT(#{startTime},'%Y-%m-%d')
            </if>
            <if test=" endTime !=null ">
                and DATE_FORMAT(#{endTime},'%Y-%m-%d') >= DATE_FORMAT(release_time ,'%Y-%m-%d')
            </if>
            <if test="wgCode !=null">
                and   wg_code like '${wgCode}%'
            </if>
            and parent_mattetr_name is not null
            and standby1=0
        </where>
        GROUP BY parent_mattetr_name

    </select>


    <select id="taskCountByWgcode" parameterType="java.util.Map" resultType="java.util.Map">

        SELECT
        wgxx.wg_code,
        wgxx.wg_name cunName,
        tk.banjieNum,tk.daibanNum

        FROM
        grid_region wgxx
        left JOIN (
        SELECT
        sum(case when tk.tk_status>40 then 1 else 0 end ) banjieNum,
        sum(case when tk.tk_status=12 then 1 else 0 end ) daibanNum,
        tk.*
        FROM
        tk_common_task tk
        WHERE 1=1
        AND tk.standby1 = 0
        GROUP BY tk.wg_parent_id
        ) tk on wgxx.wg_code=tk.wg_parent_id
        WHERE
            wgxx.`level` = 2;

    </select>


    <select id="taskCountBySecWgcode" parameterType="java.util.Map" resultType="java.util.Map">

        SELECT
        wgxx.wg_code,
        wgxx.wg_name cunName,
        tk.banjieNum,tk.daibanNum

        FROM
        grid_region wgxx
        left JOIN (
        SELECT
        sum(case when tk.tk_status>40 then 1 else 0 end ) banjieNum,
        sum(case when tk.tk_status &lt; 40 then 1 else 0 end ) daibanNum,
        tk.wg_code
        FROM
        tk_common_task tk,
        grid_region xxb
        WHERE 1=1
        and xxb.wg_code=tk.wg_code
        and xxb.`level`=2
        AND tk.standby1 = 0
        GROUP BY tk.wg_code
        ) tk on wgxx.wg_code=tk.wg_code
        WHERE
        wgxx.`level` = 2
        <if test=" wgCode !=null ">
            AND wgxx.wg_code = #{wgCode}
        </if>

    </select>

    <select id="taskCountByThirdWgcode" parameterType="java.util.Map" resultType="java.util.Map">

        SELECT
        wgxx.wg_code,
        wgxx.wg_name cunName,
        tk.banjieNum,tk.daibanNum

        FROM
        grid_region wgxx
        left JOIN (
        SELECT
        sum(case when tk.tk_status>40 then 1 else 0 end ) banjieNum,
        sum(case when tk.tk_status &lt; 40 then 1 else 0 end ) daibanNum,
        tk.wg_parent_id
        FROM
        tk_common_task tk,
        grid_region xxb
        WHERE 1=1
        and xxb.wg_code=tk.wg_parent_id
        and xxb.`level`=2
        AND tk.standby1 = 0
        GROUP BY tk.wg_parent_id
        ) tk on wgxx.wg_code=tk.wg_parent_id
        WHERE
        wgxx.`level` = 2
        <if test=" wgCode !=null ">
            AND wgxx.wg_code = #{wgCode}
        </if>


    </select>

    <select id="groupByMatterType" parameterType="java.util.Map" resultType="java.util.Map">

        SELECT
        count( * ) num,
        tk.parent_mattetr_name,
        tk.parent_mattetr_id
        FROM
        tk_common_task tk
        GROUP BY
        tk.parent_mattetr_name
        ORDER BY
        num DESC LIMIT 6;

    </select>

    <select id="groupByPriority" parameterType="java.util.Map" resultType="java.util.Map">

        SELECT
        count( * ) num,
        tk.parent_mattetr_name matterType,
        tk.parent_mattetr_id,
        CASE

        WHEN tk.priority_type = 0 THEN
        '普通'
        WHEN tk.priority_type = 1 THEN
        '重要'
        WHEN tk.priority_type = 2 THEN
        '紧急'
        END type
        FROM
        tk_common_task tk
        WHERE
        tk.parent_mattetr_id = #{parentMatterId}
        GROUP BY
        tk.priority_type

    </select>


    <select id="groupMatter" parameterType="java.util.Map" resultType="java.util.Map">

        SELECT
        tk.matter_name tkName,
        count( * ) tkNum
        FROM
        tk_common_task tk
        WHERE 1=1
        <if test="wgParentCode !=null">
            and   wg_code like #{wgParentCode}
        </if>
        <if test="wgCode !=null">
            and   wg_code = #{wgCode}
        </if>
        GROUP BY
        tk.matter_name

    </select>

    <select id="overCount"  parameterType="java.util.Map"  resultType="java.util.Map">

        SELECT
        sum(1) as wgCount
        FROM
        tk_common_task ta
        WHERE
        (
        create_time BETWEEN #{firstDayOfMonth1}
        AND #{lastDayOfMonth1}
        )
        and
        standby1=0
        and (release_time between #{releaseStartTime} and  #{releaseEndTime})
        and wg_code in
        <foreach collection="wgIds" item="code" index="index" open="(" close=")" separator=",">
            #{code}
        </foreach>
    </select>

    <select id="selectMatterRankByWg"  parameterType="java.util.Map"  resultType="java.util.Map">

        SELECT
        parent_mattetr_name,
        count(*) num
        FROM
        tk_common_task
        where
        standby1=0
        and parent_mattetr_name is not null
        and (release_time between #{releaseStartTime} and  #{releaseEndTime})
        and wg_code in
        <foreach collection="wgIds" item="code" index="index" open="(" close=")" separator=",">
            #{code}
        </foreach>
        GROUP BY
        parent_mattetr_name
        ORDER BY
        num DESC

    </select>


    <select id="getTaskByUserId" parameterType="java.util.Map" resultType="java.util.Map">

        select sxb.fgld,tk.*
        from tk_common_task tk
        left join grid_item_daily sxb on sxb.id=tk.matter_id
        WHERE 1=1
        and tk.standby1=0
        and tk.tk_status between 12 and 40
        <if test="matterType !=null">
            and   tk.matter_type = #{matterType}
        </if>
        <if test='roleCode=="twogrid" or roleCode=="threegird"'>
            and tk.perform_user_id=#{userId}
        </if>
        ORDER BY  tk.create_time desc
        <if test="currIndex !=null">
            limit #{currIndex} , #{pageSize}
        </if>

    </select>

    <select id ="enviromentManStatistic" parameterType="java.util.Map"
            resultType="java.util.Map">
        select
        t.release_user_name,
        DATE_FORMAT(t.release_time ,'%m') as month,
        count(t.release_user_name) as num
        from tk_common_task  t
        where t.come_from=#{comeFrom,jdbcType=VARCHAR}
        and DATE_FORMAT(t.release_time ,'%Y-%m')=DATE_FORMAT(#{startDate,jdbcType=DATE},'%Y-%m')
        group by t.release_user_name
        order by t.release_user_name
    </select>

    <select id ="enviromentMatterStatistic" parameterType="java.util.Map"
            resultType="java.util.Map">
        select
        t.matter_name,
        count(t.matter_name)  as num
        from tk_common_task  t
        where t.matter_id is not null
        and t.matter_name is not null
        and t.come_from=#{comeFrom,jdbcType=VARCHAR}
        and DATE_FORMAT(t.release_time ,'%Y-%m')=DATE_FORMAT(#{startDate,jdbcType=DATE},'%Y-%m')
        group by t.matter_id
        order by t.matter_name
    </select>


    <select id="countTaskBySq" parameterType="java.util.Map" resultType="java.util.Map">

        SELECT count(*) AS allNum,
        wg_parent_id AS parentId,
        wg_code,
        belong_grid
        FROM
        tk_common_task
        WHERE
        standby1 = 0
        and wg_code != #{rootCode} and wg_code LIKE '${wgCode}%'
        AND DATE_FORMAT(release_time ,'%Y-%m')=DATE_FORMAT(#{startDate,jdbcType=DATE},'%Y-%m')
        GROUP BY belong_grid
        ORDER BY allNum DESC

    </select>
    <select id="selectTaskList" resultType="java.util.Map">
        SELECT
        id,
        tk_name,
        release_user_name,
        DATE_FORMAT(release_time,'%Y-%m-%d') as release_time,
        tk_status,
        (
        CASE
        WHEN tk_status > 40 THEN
        100
        WHEN tk_status = 40 THEN
        80
        WHEN tk_status >= 30 THEN
        60
        WHEN tk_status >= 20 THEN
        40
        WHEN tk_status >= 10 THEN
        20
        ELSE
        0
        END
        ) AS percent
        FROM
        tk_common_task
        WHERE
        standby1 = '0'
        AND (
        tk_name LIKE '%${userName}%'
        OR release_user_name LIKE '%${userName}%'
        OR perform_user_name LIKE '%${userName}%'
        OR assigned_user_name LIKE '%${userName}%'
        OR zp_verification_user_name LIKE '%${userName}%'
        OR verification_user_name LIKE '%${userName}%'
        )
    </select>

    <select id="countByReleaseTimeAndUserId" resultType="java.lang.Integer">
        select count(1) from tk_common_task
        <where>
            standby1 = '0'
            <if test="userId != null and userId != ''">
                and (
                    assigned_user_id = #{userId} or
                    perform_user_id = #{userId} or
                    verification_user_id = #{userId} or
                    pj_person_id = #{userId} or
                    create_by = #{userId} or
                    verification_user_id = #{userId}
                )
            </if>
            <if test="releaseTimeStart != null">
                and release_time >= #{releaseTimeStart}
            </if>
            <if test="releaseTimeEnd != null">
                and release_time &lt;= #{releaseTimeEnd}
            </if>
            <if test="tkStatus != null">
                and tk_status = #{tkStatus}
            </if>
            <if test="comeFrom != null">
                and come_from = #{comeFrom}
            </if>
        </where>
    </select>
</mapper>