<?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.tc.mapper.AssetTaskMapper">

    <resultMap type="com.ruoyi.tc.entity.AssetTask" id="AssetTaskMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="taskName" column="task_name" jdbcType="VARCHAR"/>
        <result property="taskTime" column="task_time" jdbcType="TIMESTAMP"/>
        <result property="taskDeadline" column="task_deadline" jdbcType="VARCHAR"/>
        <result property="taskFinishTime" column="task_finish_time" jdbcType="TIMESTAMP"/>
        <result property="taskStatus" column="task_status" jdbcType="VARCHAR"/>
        <result property="dwmc" column="dwmc" jdbcType="VARCHAR"/>
        <result property="createId" column="create_id" jdbcType="INTEGER"/>
        <result property="createBy" column="create_by" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateId" column="update_id" jdbcType="INTEGER"/>
        <result property="updateBy" column="update_by" jdbcType="VARCHAR"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="userId" column="user_id" jdbcType="INTEGER"/>
        <result property="deptId" column="dept_id" jdbcType="INTEGER"/>
        <result property="version" column="version" jdbcType="VARCHAR"/>
        <result property="reamark" column="reamark" jdbcType="VARCHAR"/>
        <result property="type" column="type" jdbcType="VARCHAR"/>
    </resultMap>

    <!--查询单个-->
    <select id="queryById" resultType="com.ruoyi.tc.entity.response.AssetTaskResponse">
        select a.id,
               a.task_name,
               a.task_time,
               a.task_deadline,
               a.task_finish_time,
               a.task_status,
               a.dwmc,
               a.create_id,
               a.create_by,
               a.create_time,
               a.update_id,
               a.update_by,
               a.update_time,
               a.user_id,
               a.dept_id,
               a.version,
               a.reamark,
               a.type

        from asset_task a

        where a.id = #{id}
    </select>

    <!--查询指定行数据-->
    <select id="queryAllByLimit" resultMap="AssetTaskMap">
        select
        id,task_name,task_time,task_deadline,task_finish_time,task_status,dwmc,create_id,create_by,create_time,update_id,update_by,update_time,user_id,dept_id,version,reamark
        from asset_task
        <where>
            <if test="id != null">
                and id = #{id}
            </if>

            <if test="taskName != null and taskName != ''">
                and task_name = #{taskName}
            </if>
            <if test="taskTime != null">
                and task_time = #{taskTime}
            </if>
            <if test="taskDeadline != null and taskDeadline != ''">
                and task_deadline = #{taskDeadline}
            </if>
            <if test="taskFinishTime != null">
                and task_finish_time = #{taskFinishTime}
            </if>
            <if test="taskStatus != null and taskStatus != ''">
                and task_status = #{taskStatus}
            </if>
            <if test="dwmc != null">
                and dwmc = #{dwmc}
            </if>
            <if test="createId != null">
                and create_id = #{createId}
            </if>
            <if test="createBy != null and createBy != ''">
                and create_by = #{createBy}
            </if>
            <if test="createTime != null">
                and create_time = #{createTime}
            </if>
            <if test="updateId != null">
                and update_id = #{updateId}
            </if>
            <if test="updateBy != null and updateBy != ''">
                and update_by = #{updateBy}
            </if>
            <if test="updateTime != null">
                and update_time = #{updateTime}
            </if>
            <if test="userId != null">
                and user_id = #{userId}
            </if>
            <if test="deptId != null">
                and dept_id = #{deptId}
            </if>
            <if test="version != null and version != ''">
                and version = #{version}
            </if>
            <if test="reamark != null and reamark != ''">
                and reamark = #{reamark}
            </if>
        </where>
        limit #{pageable.offset}, #{pageable.pageSize}
    </select>

    <!--统计总行数-->
    <select id="count" resultType="java.lang.Long">
        select count(1)
        from asset_task
        <where>
            <if test="id != null">
                and id = #{id}
            </if>

            <if test="taskName != null and taskName != ''">
                and task_name = #{taskName}
            </if>
            <if test="taskTime != null">
                and task_time = #{taskTime}
            </if>
            <if test="taskDeadline != null and taskDeadline != ''">
                and task_deadline = #{taskDeadline}
            </if>
            <if test="taskFinishTime != null">
                and task_finish_time = #{taskFinishTime}
            </if>
            <if test="taskStatus != null and taskStatus != ''">
                and task_status = #{taskStatus}
            </if>
            <if test="dwmc != null">
                and dwmc = #{dwmc}
            </if>
            <if test="createId != null">
                and create_id = #{createId}
            </if>
            <if test="createBy != null and createBy != ''">
                and create_by = #{createBy}
            </if>
            <if test="createTime != null">
                and create_time = #{createTime}
            </if>
            <if test="updateId != null">
                and update_id = #{updateId}
            </if>
            <if test="updateBy != null and updateBy != ''">
                and update_by = #{updateBy}
            </if>
            <if test="updateTime != null">
                and update_time = #{updateTime}
            </if>
            <if test="userId != null">
                and user_id = #{userId}
            </if>
            <if test="deptId != null">
                and dept_id = #{deptId}
            </if>
            <if test="version != null and version != ''">
                and version = #{version}
            </if>
            <if test="reamark != null and reamark != ''">
                and reamark = #{reamark}
            </if>
        </where>
    </select>
    <select id="queryByPage" resultType="com.ruoyi.tc.entity.AssetTask">
        select
        id,task_name,task_time,task_deadline,task_finish_time,task_status,dwmc,create_id,create_by,create_time,update_id,update_by,update_time,user_id,dept_id,version,reamark
        from asset_task
        <where>
            <if test="req.taskName != null and req.taskName != ''">
                and task_name like concat('%',#{req.taskName},'%')
            </if>
            <if test="req.dwmc != null and req.dwmc != ''">
                and dwmc like concat('%',#{req.dwmc},'%')
            </if>
            <if test="req.begainTime != null">
                and task_finish_time &gt;= #{req.begainTime}
            </if>
            <if test="req.endTime != null">
                and task_finish_time &lt;= #{req.endTime}
            </if>
            <if test="req.taskStatus != null and req.taskStatus != ''">
                and task_status = #{req.taskStatus}
            </if>
            <if test="req.id != null ">
                and id = #{req.id}
            </if>

        </where>
        order by task_time desc
    </select>
    <select id="findByDwmcAssetCurrent" resultType="com.ruoyi.tc.entity.po.AssetCurrentCpPo">
        select * from asset_current
        <where>
            del_flag = '0'
            <if test="dwmc!=null and dwmc!='' ">
                and dwmc like concat('%',#{dwmc},'%')
            </if>
        </where>
    </select>
    <select id="zcHc" resultType="com.ruoyi.tc.entity.response.AssestTaskXqresponse">
        select b.xtlx,
        b.xtmc,
        b.dwmc as zcdwmc,
        b.status,
        b.update_time  as updateTime,
        b.id ,
        b.task_id,
        b.btgyy
        from asset_task a
        LEFT JOIN
        asset_current_cp b ON FIND_IN_SET(b.dwmc, a.dwmc)
        <where>
            <if test="req.dwmc!=null and req.dwmc!='' ">
                and b.dwmc like concat('%',#{req.dwmc},'%')
            </if>
            <if test="req.taskId !=null  ">
                and a.id = #{req.taskId}
            </if>
            <if test="req.taskId !=null  ">
                and b.task_id=#{req.taskId}
            </if>
            <if test="req.status!=null  ">
                and b.status =#{req.status}
            </if>
            <if test="req.xtlx!=null and req.xtlx!='' ">
                and b.xtlx = #{req.xtlx}
            </if>
            <if test="req.xtmc!=null and req.xtmc!='' ">
                and b.xtmc  like concat('%',#{req.xtmc},'%')
            </if>
        </where>
        order by FIELD(status,1,4,3,0,5), b.update_time
    </select>
    <select id="findBytaskIdandAssestId" resultType="com.ruoyi.tc.entity.po.AssetCurrentCpPo">
        select *
        from asset_current_cp
        where task_id = #{taskId}
          and id = #{assetId}
    </select>
    <select id="dwHc" resultType="com.ruoyi.tc.entity.response.AssetdwHcResponse">
        select
        h.dwmc,h.task_id,h.latest_create_time,h.task_deadline,h.total,h.checked,h.notChecked,h.dsp,h.shthcs,h.taskStatus,h.checkedRatio
        from(
        SELECT
        s.dwmc,
        s.task_id,
        s.latest_create_time, -- 使用当前时间
        b.task_deadline,
        SUM(s.total) AS total,
        SUM(s.checked) AS checked,
        SUM( s.notChecked ) AS notChecked,
        SUM( s.dsp ) AS dsp,
        COALESCE(SUM(s.returned), 0) AS shthcs,
        ROUND(
        CASE
        WHEN SUM(s.total) > 0 THEN
        SUM(s.checked) * 100.0 / SUM(s.total)
        ELSE 0
        END,
        2
        ) AS checkedRatio,
        CASE
        WHEN ROUND(
        CASE
        WHEN SUM(s.total) > 0 THEN
        SUM(s.checked) * 100.0 / SUM(s.total)
        ELSE 0
        END,
        2
        ) = 100
        AND CURRENT_TIMESTAMP &lt; b.task_deadline THEN 2
        WHEN ROUND(
        CASE
        WHEN SUM(s.total) > 0 THEN
        SUM(s.checked) * 100.0 / SUM(s.total)
        ELSE 0
        END,
        2
        ) = 100
        AND CURRENT_TIMESTAMP >= b.task_deadline THEN 3
        ELSE 1
        END AS taskStatus
        FROM
        (
        SELECT
        dwmc,
        COUNT(*) AS total,
        task_id,
        MAX(create_time) AS latest_create_time,
        SUM(CASE WHEN STATUS = 0 THEN 1 ELSE 0 END) AS notChecked,
        SUM( CASE WHEN STATUS = 1 THEN 1 ELSE 0 END ) AS dsp,
        SUM(CASE WHEN STATUS = 3 OR STATUS = 5 THEN 1 ELSE 0 END) AS checked,
        SUM(count) AS returned
        FROM
        asset_current_cp
        <where>
            <if test="req.dwmc!=null and req.dwmc!='' ">
                and dwmc like concat('%',#{req.dwmc},'%')
            </if>

            <if test="req.taskId!=null and req.taskId!='' ">
                and task_id =#{req.taskId}
            </if>
        </where>
        GROUP BY
        dwmc,
        task_id
        UNION ALL
        SELECT
        ssdw AS dwmc,
        COUNT(*) AS total,
        task_id,
        MAX(create_time) AS latest_create_time,
        SUM(CASE WHEN STATUS = 0 THEN 1 ELSE 0 END) AS notChecked,
        SUM( CASE WHEN STATUS = 1 THEN 1 ELSE 0 END ) AS dsp,
        SUM(CASE WHEN STATUS = 3 OR STATUS = 5 THEN 1 ELSE 0 END) AS checked,
        SUM(count) AS returned
        FROM
        asset_mini_programs_cp
        <where>
            <if test="req.dwmc!=null and req.dwmc!='' ">
                and ssdw like concat('%',#{req.dwmc},'%')
            </if>

            <if test="req.taskId!=null and req.taskId!='' ">
                and task_id =#{req.taskId}
            </if>
        </where>
        GROUP BY
        ssdw,
        task_id
        UNION ALL
        SELECT
        ssdw AS dwmc,
        COUNT(*) AS total,
        task_id,
        MAX(create_time) AS latest_create_time,
        SUM(CASE WHEN STATUS = 0 THEN 1 ELSE 0 END) AS notChecked,
        SUM( CASE WHEN STATUS = 1 THEN 1 ELSE 0 END ) AS dsp,
        SUM(CASE WHEN STATUS = 3 OR STATUS = 5 THEN 1 ELSE 0 END) AS checked,
        SUM(count) AS returned
        FROM
        asset_app_cp
        <where>
            <if test="req.dwmc!=null and req.dwmc!='' ">
                and ssdw like concat('%',#{req.dwmc},'%')
            </if>

            <if test="req.taskId!=null and req.taskId!='' ">
                and task_id =#{req.taskId}
            </if>
        </where>
        GROUP BY
        ssdw ,task_id
        UNION ALL
        SELECT ssdw AS dwmc,
        COUNT(*) AS total,
        task_id,
        MAX(create_time) AS latest_create_time,
        SUM(CASE WHEN STATUS = 0 THEN 1 ELSE 0 END) AS notChecked,
        SUM( CASE WHEN STATUS = 1 THEN 1 ELSE 0 END ) AS dsp,
        SUM(CASE WHEN STATUS = 3 OR STATUS = 5 THEN 1 ELSE 0 END) AS checked,
        SUM(count) AS returned
        FROM
        asset_official_account_cp
        <where>
            <if test="req.dwmc!=null and req.dwmc!='' ">
                and ssdw like concat('%',#{req.dwmc},'%')
            </if>

            <if test="req.taskId!=null and req.taskId!='' ">
                and task_id =#{req.taskId}
            </if>
        </where>
        GROUP BY
        ssdw,
        task_id
        UNION ALL
        SELECT
        ssdw AS dwmc,
        COUNT(*) AS total,
        task_id,
        MAX(create_time) AS latest_create_time,
        SUM(CASE WHEN STATUS = 0 THEN 1 ELSE 0 END) AS notChecked,
        SUM( CASE WHEN STATUS = 1 THEN 1 ELSE 0 END ) AS dsp,
        SUM(CASE WHEN STATUS = 3 OR STATUS = 5 THEN 1 ELSE 0 END) AS checked,
        SUM(count) AS returned
        FROM
        asset_email_cp
        <where>
            <if test="req.dwmc!=null and req.dwmc!='' ">
                and ssdw like concat('%',#{req.dwmc},'%')
            </if>

            <if test="req.taskId!=null and req.taskId!='' ">
                and task_id =#{req.taskId}
            </if>
        </where>
        GROUP BY
        ssdw,
        task_id
        ) AS s left join asset_task b on s.task_id=b.id
        GROUP BY
        s.dwmc ,
        s.task_id)h
        <where>
            <if test="req.taskStatus != null and  req.taskStatus != ''">
                h.taskStatus = #{req.taskStatus}
            </if>
        </where>


    </select>
    <select id="findByTaskId" resultType="com.ruoyi.tc.entity.AssetTask">
        SELECT
        a.STATUS,
        CASE
        WHEN DATE ( a.update_time ) > b.task_deadline THEN
        '6' ELSE '7'
        END AS deadstatus
        FROM
        asset_current_cp a
        LEFT JOIN asset_task b ON a.task_id = b.id
        <where>
            <if test="taskId!=null ">
                and a.task_id =#{taskId}
            </if>
        </where>

    </select>
    <select id="taskList" resultType="com.ruoyi.tc.entity.AssetTask">
        select * from asset_task
        <where>
            <if test="dwmc!=null and dwmc !=''">
                and dwmc =#{dwmc}
            </if>
            and task_status=1
        </where>
    </select>
    <select id="xcxZcHc" resultType="com.ruoyi.tc.entity.response.AssestTaskXqresponse">
        select
        b.xcxmc as xtmc,
        b.ssdw as zcdwmc,
        b.status,
        b.asset_id as id ,
        b.update_time as updateTime,
        b.task_id,
        b.btgyy
        from asset_task a
        LEFT JOIN
        asset_mini_programs_cp b ON FIND_IN_SET(b.ssdw, a.dwmc)
        <where>
            <if test="req.dwmc!=null and req.dwmc!='' ">
                and b.ssdw like concat('%',#{req.dwmc},'%')
            </if>
            <if test="req.taskId !=null  ">
                and a.id = #{req.taskId}
            </if>
            <if test="req.taskId !=null  ">
                and b.task_id=#{req.taskId}
            </if>
            <if test="req.status!=null  ">
                and b.status =#{req.status}
            </if>
            <if test="req.xtmc!=null and req.xtmc!='' ">
                and b.xcxmc like concat('%',#{req.xtmc},'%')
            </if>

        </where>
        order by FIELD(status,1,4,3,0,5), b.update_time
    </select>
    <select id="gzhZcHc" resultType="com.ruoyi.tc.entity.response.AssestTaskXqresponse">
        select
        b.gzhmc as xtmc,
        b.ssdw as zcdwmc,
        b.status,
        b.asset_id as id ,
        b.update_time  as updateTime,
        b.task_id,
        b.btgyy
        from asset_task a
        LEFT JOIN
        asset_official_account_cp b ON FIND_IN_SET(b.ssdw, a.dwmc)
        <where>
            <if test="req.dwmc!=null and req.dwmc!='' ">
                and b.ssdw like concat('%',#{req.dwmc},'%')
            </if>
            <if test="req.taskId !=null  ">
                and a.id = #{req.taskId}
            </if>
            <if test="req.taskId !=null  ">
                and b.task_id=#{req.taskId}
            </if>
            <if test="req.status!=null  ">
                and b.status =#{req.status}
            </if>
            <if test="req.xtmc!=null and req.xtmc!='' ">
                and b.gzhmc like concat('%',#{req.xtmc},'%')
            </if>
        </where>
        order by FIELD(status,1,4,3,0,5), b.update_time
    </select>
    <select id="emailZcHc" resultType="com.ruoyi.tc.entity.response.AssestTaskXqresponse">
        select
        b.dzyxhz as xtmc,
        b.ssdw as zcdwmc,
        b.status,
        b.asset_id  as id,
        b.update_time  as updateTime,
        b.task_id,
        b.btgyy
        from asset_task a
        LEFT JOIN
        asset_email_cp b ON FIND_IN_SET(b.ssdw, a.dwmc)
        <where>
            <if test="req.dwmc!=null and req.dwmc!='' ">
                and b.ssdw like concat('%',#{req.dwmc},'%')
            </if>
            <if test="req.taskId !=null  ">
                and a.id = #{req.taskId}
            </if>
            <if test="req.taskId !=null  ">
                and b.task_id=#{req.taskId}
            </if>
            <if test="req.status!=null  ">
                and b.status =#{req.status}
            </if>
            <if test="req.xtmc!=null and req.xtmc!='' ">
                and b.dzyxhz like concat('%',#{req.xtmc},'%')
            </if>
        </where>
        order by FIELD(status,1,4,3,0,5), b.update_time
    </select>
    <select id="appZcHc" resultType="com.ruoyi.tc.entity.response.AssestTaskXqresponse">
        select
        b.app_name as xtmc,
        b.ssdw as zcdwmc,
        b.status,
        b.asset_id as id,
        b.update_time  as updateTime,
        b.task_id,
        b.btgyy
        from asset_task a
        LEFT JOIN
        asset_app_cp b ON FIND_IN_SET(b.ssdw, a.dwmc)
        <where>
            <if test="req.dwmc!=null and req.dwmc!='' ">
                and b.ssdw like concat('%',#{req.dwmc},'%')
            </if>
            <if test="req.taskId !=null  ">
                and a.id = #{req.taskId}
            </if>
            <if test="req.taskId !=null  ">
                and b.task_id=#{req.taskId}
            </if>
            <if test="req.status!=null  ">
                and b.status =#{req.status}
            </if>
            <if test="req.xtmc!=null and req.xtmc!='' ">
                and b.app_name like concat('%',#{req.xtmc},'%')
            </if>
        </where>
        order by FIELD(status,1,4,3,0,5), b.update_time
    </select>

    <select id="export" resultType="com.ruoyi.tc.entity.response.AssetdwHcResponse"
            parameterType="com.ruoyi.tc.entity.request.AssetdwHcRequest">
        SELECT
        s.dwmc,
        s.task_id,
        CURRENT_TIMESTAMP AS latest_create_time,  -- 使用当前时间
        b.task_deadline,
        SUM(s.total) AS total,
        SUM(s.checked) AS checked,
        SUM( s.notChecked ) AS notChecked,
        SUM( s.dsp ) AS dsp,
        COALESCE(SUM(s.returned), 0) AS shthcs,
        ROUND(
        CASE
        WHEN SUM(s.total) > 0 THEN
        SUM(s.checked) * 100.0 / SUM(s.total)
        ELSE 0
        END,
        2
        ) AS checkedRatio,
        CASE
        WHEN ROUND(
        CASE
        WHEN SUM(s.total) > 0 THEN
        SUM(s.checked) * 100.0 / SUM(s.total)
        ELSE 0
        END,
        2
        ) = 100
        AND CURRENT_TIMESTAMP &lt; b.task_deadline THEN 2
        WHEN ROUND(
        CASE
        WHEN SUM(s.total) > 0 THEN
        SUM(s.checked) * 100.0 / SUM(s.total)
        ELSE 0
        END,
        2
        ) = 100
        AND CURRENT_TIMESTAMP >= b.task_deadline THEN 3
        ELSE 1
        END AS taskStatus
        FROM
        (
        SELECT
        dwmc,
        COUNT(*) AS total,
        task_id,
        MAX(create_time) AS latest_create_time,
        SUM(CASE WHEN STATUS = 0 THEN 1 ELSE 0 END) AS notChecked,
        SUM( CASE WHEN STATUS = 1 THEN 1 ELSE 0 END ) AS dsp,
        SUM(CASE WHEN STATUS = 3 OR STATUS = 5 THEN 1 ELSE 0 END) AS checked,
        SUM(count) AS returned
        FROM
        asset_current_cp
        <where>
            <if test="req.dwmc!=null and req.dwmc!='' ">
                and dwmc like concat('%',#{req.dwmc},'%')
            </if>

            <if test="req.taskId!=null and req.taskId!='' ">
                and task_id =#{req.taskId}
            </if>
        </where>
        GROUP BY
        dwmc,
        task_id
        UNION ALL
        SELECT
        ssdw AS dwmc,
        COUNT(*) AS total,
        task_id,
        MAX(create_time) AS latest_create_time,
        SUM(CASE WHEN STATUS = 0 THEN 1 ELSE 0 END) AS notChecked,
        SUM( CASE WHEN STATUS = 1 THEN 1 ELSE 0 END ) AS dsp,
        SUM(CASE WHEN STATUS = 3 OR STATUS = 5 THEN 1 ELSE 0 END) AS checked,
        SUM(count) AS returned
        FROM
        asset_mini_programs_cp
        <where>
            <if test="req.dwmc!=null and req.dwmc!='' ">
                and ssdw like concat('%',#{req.dwmc},'%')
            </if>

            <if test="req.taskId!=null and req.taskId!='' ">
                and task_id =#{req.taskId}
            </if>
        </where>
        GROUP BY
        ssdw,
        task_id
        UNION ALL
        SELECT
        ssdw AS dwmc,
        COUNT(*) AS total,
        task_id,
        MAX(create_time) AS latest_create_time,
        SUM(CASE WHEN STATUS = 0 THEN 1 ELSE 0 END) AS notChecked,
        SUM( CASE WHEN STATUS = 1 THEN 1 ELSE 0 END ) AS dsp,
        SUM(CASE WHEN STATUS = 3 OR STATUS = 5 THEN 1 ELSE 0 END) AS checked,
        SUM(count) AS returned
        FROM
        asset_app_cp
        <where>
            <if test="req.dwmc!=null and req.dwmc!='' ">
                and ssdw like concat('%',#{req.dwmc},'%')
            </if>

            <if test="req.taskId!=null and req.taskId!='' ">
                and task_id =#{req.taskId}
            </if>
        </where>
        GROUP BY
        ssdw ,task_id
        UNION ALL
        SELECT ssdw AS dwmc,
        COUNT(*) AS total,
        task_id,
        MAX(create_time) AS latest_create_time,
        SUM(CASE WHEN STATUS = 0 THEN 1 ELSE 0 END) AS notChecked,
        SUM( CASE WHEN STATUS = 1 THEN 1 ELSE 0 END ) AS dsp,
        SUM(CASE WHEN STATUS = 3 OR STATUS = 5 THEN 1 ELSE 0 END) AS checked,
        SUM(count) AS returned
        FROM
        asset_official_account_cp
        <where>
            <if test="req.dwmc!=null and req.dwmc!='' ">
                and ssdw like concat('%',#{req.dwmc},'%')
            </if>

            <if test="req.taskId!=null and req.taskId!='' ">
                and task_id =#{req.taskId}
            </if>
        </where>
        GROUP BY
        ssdw,
        task_id
        UNION ALL
        SELECT
        ssdw AS dwmc,
        COUNT(*) AS total,
        task_id,
        MAX(create_time) AS latest_create_time,
        SUM(CASE WHEN STATUS = 0 THEN 1 ELSE 0 END) AS notChecked,
        SUM( CASE WHEN STATUS = 1 THEN 1 ELSE 0 END ) AS dsp,
        SUM(CASE WHEN STATUS = 3 OR STATUS = 5 THEN 1 ELSE 0 END) AS checked,
        SUM(count) AS returned
        FROM
        asset_email_cp
        <where>
            <if test="req.dwmc!=null and req.dwmc!='' ">
                and ssdw like concat('%',#{req.dwmc},'%')
            </if>

            <if test="req.taskId!=null and req.taskId!='' ">
                and task_id =#{req.taskId}
            </if>
        </where>
        GROUP BY
        ssdw,
        task_id
        ) AS s left join asset_task b on s.task_id=b.id
        <where>
            <if test="req.taskStatus != null and  req.taskStatus != ''">
                b.task_status = #{req.taskStatus}
            </if>
        </where>
        GROUP BY
        s.dwmc ,
        s.task_id
    </select>
    <select id="dwHcBl" resultType="com.ruoyi.tc.entity.response.AssetdwHcBlResponse"
            parameterType="com.ruoyi.tc.entity.request.AssetdwHcRequest">
        select count(*) as count ,taskStatus from (
        SELECT
        count(*) AS count,
        g.taskStatus
        FROM
        ( SELECT
        s.dwmc,
        s.task_id,
        CURRENT_TIMESTAMP AS latest_create_time,  -- 使用当前时间
        b.task_deadline,
        SUM(s.total) AS total,
        SUM(s.checked) AS checked,
        COALESCE(SUM(s.returned), 0) AS shthcs,
        ROUND(
        CASE
        WHEN SUM(s.total) > 0 THEN
        SUM(s.checked) * 100.0 / SUM(s.total)
        ELSE 0
        END,
        2
        ) AS checkedRatio,
        CASE
        WHEN ROUND(
        CASE
        WHEN SUM(s.total) > 0 THEN
        SUM(s.checked) * 100.0 / SUM(s.total)
        ELSE 0
        END,
        2
        ) = 100
        AND CURRENT_TIMESTAMP &lt; b.task_deadline THEN 2
        WHEN ROUND(
        CASE
        WHEN SUM(s.total) > 0 THEN
        SUM(s.checked) * 100.0 / SUM(s.total)
        ELSE 0
        END,
        2
        ) = 100
        AND CURRENT_TIMESTAMP >= b.task_deadline THEN 3
        ELSE 1
        END AS taskStatus
        FROM
        (
        SELECT
        dwmc,
        COUNT(*) AS total,
        task_id,
        MAX(create_time) AS latest_create_time,
        SUM(CASE WHEN STATUS = 0 THEN 1 ELSE 0 END) AS notChecked,
        SUM(CASE WHEN STATUS = 3 OR STATUS = 5 THEN 1 ELSE 0 END) AS checked,
        SUM(count) AS returned
        FROM
        asset_current_cp
        <where>
            <if test="req.dwmc!=null and req.dwmc!='' ">
                and dwmc like concat('%',#{req.dwmc},'%')
            </if>

            <if test="req.taskId!=null and req.taskId!='' ">
                and task_id =#{req.taskId}
            </if>
        </where>
        GROUP BY
        dwmc,
        task_id
        UNION ALL
        SELECT
        ssdw AS dwmc,
        COUNT(*) AS total,
        task_id,
        MAX(create_time) AS latest_create_time,
        SUM(CASE WHEN STATUS = 0 THEN 1 ELSE 0 END) AS notChecked,
        SUM(CASE WHEN STATUS = 3 OR STATUS = 5 THEN 1 ELSE 0 END) AS checked,
        SUM(count) AS returned
        FROM
        asset_mini_programs_cp
        <where>
            <if test="req.dwmc!=null and req.dwmc!='' ">
                and ssdw like concat('%',#{req.dwmc},'%')
            </if>

            <if test="req.taskId!=null and req.taskId!='' ">
                and task_id =#{req.taskId}
            </if>
        </where>
        GROUP BY
        ssdw,
        task_id
        UNION ALL
        SELECT
        ssdw AS dwmc,
        COUNT(*) AS total,
        task_id,
        MAX(create_time) AS latest_create_time,
        SUM(CASE WHEN STATUS = 0 THEN 1 ELSE 0 END) AS notChecked,
        SUM(CASE WHEN STATUS = 3 OR STATUS = 5 THEN 1 ELSE 0 END) AS checked,
        SUM(count) AS returned
        FROM
        asset_app_cp
        <where>
            <if test="req.dwmc!=null and req.dwmc!='' ">
                and ssdw like concat('%',#{req.dwmc},'%')
            </if>

            <if test="req.taskId!=null and req.taskId!='' ">
                and task_id =#{req.taskId}
            </if>
        </where>
        GROUP BY
        ssdw ,task_id
        UNION ALL
        SELECT ssdw AS dwmc,
        COUNT(*) AS total,
        task_id,
        MAX(create_time) AS latest_create_time,
        SUM(CASE WHEN STATUS = 0 THEN 1 ELSE 0 END) AS notChecked,
        SUM(CASE WHEN STATUS = 3 OR STATUS = 5 THEN 1 ELSE 0 END) AS checked,
        SUM(count) AS returned
        FROM
        asset_official_account_cp
        <where>
            <if test="req.dwmc!=null and req.dwmc!='' ">
                and ssdw like concat('%',#{req.dwmc},'%')
            </if>

            <if test="req.taskId!=null and req.taskId!='' ">
                and task_id =#{req.taskId}
            </if>
        </where>
        GROUP BY
        ssdw,
        task_id
        UNION ALL
        SELECT
        ssdw AS dwmc,
        COUNT(*) AS total,
        task_id,
        MAX(create_time) AS latest_create_time,
        SUM(CASE WHEN STATUS = 0 THEN 1 ELSE 0 END) AS notChecked,
        SUM(CASE WHEN STATUS = 3 OR STATUS = 5 THEN 1 ELSE 0 END) AS checked,
        SUM(count) AS returned
        FROM
        asset_email_cp
        <where>
            <if test="req.dwmc!=null and req.dwmc!='' ">
                and ssdw like concat('%',#{req.dwmc},'%')
            </if>

            <if test="req.taskId!=null and req.taskId!='' ">
                and task_id =#{req.taskId}
            </if>
        </where>
        GROUP BY
        ssdw,
        task_id
        ) AS s left join asset_task b on s.task_id=b.id
        <where>
            <if test="req.taskStatus != null and  req.taskStatus != ''">
                b.task_status = #{req.taskStatus}
            </if>
        </where>
        GROUP BY
        s.dwmc ,
        s.task_id
        ) g
        GROUP BY
        dwmc,
        taskStatus
        )f group by taskStatus
    </select>
    <select id="findByDwmc" resultType="com.ruoyi.tc.entity.AssetTask" parameterType="java.lang.String">
        select * from  asset_task where dwmc like concat('%', #{dwmc}, '%') and task_status=1
    </select>
    <select id="getByTaskId" resultType="com.ruoyi.tc.entity.AssetTask" parameterType="int">
        select * from  asset_task where  id =#{taskId}
    </select>
    <select id="getJxz" resultType="java.lang.Integer" parameterType="int">
        SELECT SUM(counts) AS total_count
        FROM (
                 SELECT COUNT(*) AS counts FROM `asset_mini_programs_cp` WHERE task_id = #{taskId} AND `status` NOT IN (3, 5)
                 UNION ALL
                 SELECT COUNT(*) AS counts FROM `asset_official_account_cp` WHERE task_id = #{taskId} AND `status` NOT IN (3, 5)
                 UNION ALL
                 SELECT COUNT(*) AS counts FROM `asset_current_cp` WHERE task_id = #{taskId} AND `status` NOT IN (3, 5)
                 UNION ALL
                 SELECT COUNT(*) AS counts FROM `asset_email_cp` WHERE task_id = #{taskId} AND `status` NOT IN (3, 5)
                 UNION ALL
                 SELECT COUNT(*) AS counts FROM `asset_app_cp` WHERE task_id = #{taskId} AND `status` NOT IN (3, 5)
             ) AS combined_counts;
    </select>
    <select id="getifcq" resultType="java.lang.String" parameterType="int">
        SELECT GREATEST(
                       COALESCE((SELECT update_time
                                 FROM `asset_mini_programs_cp`
                                 WHERE task_id = #{taskId}
                                   AND `status` = 3
                                 ORDER BY update_time DESC
                                LIMIT 1), '1970-01-01 00:00:00'),
                       COALESCE((SELECT update_time
                                 FROM `asset_official_account_cp`
                                 WHERE task_id = #{taskId}
                                   AND `status` = 3
                                 ORDER BY update_time DESC
                                LIMIT 1), '1970-01-01 00:00:00'),
                       COALESCE((SELECT update_time
                                 FROM `asset_current_cp`
                                 WHERE task_id = #{taskId}
                                   AND `status` = 3
                                 ORDER BY update_time DESC
                                LIMIT 1), '1970-01-01 00:00:00'),
                       COALESCE((SELECT update_time
                                 FROM `asset_email_cp`
                                 WHERE task_id = #{taskId}
                                   AND `status` = 3
                                 ORDER BY update_time DESC
                                LIMIT 1), '1970-01-01 00:00:00'),
                       COALESCE((SELECT update_time
                                 FROM `asset_app_cp`
                                 WHERE task_id = #{taskId}
                                   AND `status` = 3
                                 ORDER BY update_time DESC
                                LIMIT 1), '1970-01-01 00:00:00')
                   ) AS latest_update_time;
    </select>


    <update id="xcxsh"
            parameterType="com.ruoyi.tc.entity.request.AssetCurrentShRequest">
        update asset_mini_programs_cp
        <set>
            <if test="req.status != null ">
                status = #{req.status},
            </if>
            update_time=now()
        </set>
        where asset_id = #{req.assetId} and task_id = #{req.taskId}
    </update>
    <update id="gzhsh"
            parameterType="com.ruoyi.tc.entity.request.AssetCurrentShRequest">

        update asset_official_account_cp
        <set>
            <if test="req.status != null ">
                status = #{req.status},
            </if>
            update_time=now()
        </set>
        where asset_id = #{req.assetId} and task_id = #{req.taskId}
    </update>

    <update id="emailsh"
            parameterType="com.ruoyi.tc.entity.request.AssetCurrentShRequest">
        update asset_email_cp
        <set>
            <if test="req.status != null ">
                status = #{req.status},
            </if>
            update_time=now()
        </set>
        where asset_id = #{req.assetId} and task_id = #{req.taskId}

    </update>


    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into asset_task(task_name, task_time, task_deadline, task_finish_time, task_status, dwmc,
                               create_id, create_by, create_time, update_id, update_by, update_time, user_id, dept_id,
                               version, reamark,type,total_count,count,deadline_count,ygt)
        values (#{taskName}, #{taskTime}, #{taskDeadline}, #{taskFinishTime}, #{taskStatus}, #{dwmc},
                #{createId}, #{createBy}, #{createTime}, #{updateId}, #{updateBy}, #{updateTime}, #{userId}, #{deptId},
                #{version}, #{reamark},#{type},#{totalCount},#{count},#{deadlineCount},#{ygt})
    </insert>

    <insert id="insertBatch" keyProperty="id" useGeneratedKeys="true">
        insert into
        asset_task(task_name,task_time,task_deadline,task_finish_time,task_status,dwmc,create_id,create_by,create_time,update_id,update_by,update_time,user_id,dept_id,version,reamark)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.taskName},#{entity.taskTime},#{entity.taskDeadline},#{entity.taskFinishTime},#{entity.taskStatus},#{entity.dwmc},#{entity.createId},#{entity.createBy},#{entity.createTime},#{entity.updateId},#{entity.updateBy},#{entity.updateTime},#{entity.userId},#{entity.deptId},#{entity.version},#{entity.reamark})
        </foreach>
    </insert>

    <insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">
        insert into
        asset_task(task_name,task_time,task_deadline,task_finish_time,task_status,dwmc,create_id,create_by,create_time,update_id,update_by,update_time,user_id,dept_id,version,reamark)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.taskName},#{entity.taskTime},#{entity.taskDeadline}#{entity.taskFinishTime}#{entity.taskStatus}#{entity.dwmc}#{entity.createId}#{entity.createBy}#{entity.createTime}#{entity.updateId}#{entity.updateBy}#{entity.updateTime}#{entity.userId}#{entity.deptId}#{entity.version}#{entity.reamark})
        </foreach>
        on duplicate key update
        task_name = values(task_name)task_time = values(task_time)task_deadline =
        values(task_deadline)task_finish_time = values(task_finish_time)task_status = values(task_status)dwmc =
        values(dwmc)create_id = values(create_id)create_by = values(create_by)create_time =
        values(create_time)update_id = values(update_id)update_by = values(update_by)update_time =
        values(update_time)user_id = values(user_id)dept_id = values(dept_id)version = values(version)reamark =
        values(reamark)
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update asset_task
        <set>

            <if test="taskName != null and taskName != ''">
                task_name = #{taskName},
            </if>
            <if test="taskTime != null">
                task_time = #{taskTime},
            </if>
            <if test="taskDeadline != null and taskDeadline != ''">
                task_deadline = #{taskDeadline},
            </if>
            <if test="taskFinishTime != null">
                task_finish_time = #{taskFinishTime},
            </if>
            <if test="taskStatus != null and taskStatus != ''">
                task_status = #{taskStatus},
            </if>
            <if test="dwmc != null">
                dwmc = #{dwmc},
            </if>
            <if test="createId != null">
                create_id = #{createId},
            </if>
            <if test="createBy != null and createBy != ''">
                create_by = #{createBy},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="updateId != null">
                update_id = #{updateId},
            </if>
            <if test="updateBy != null and updateBy != ''">
                update_by = #{updateBy},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="userId != null">
                user_id = #{userId},
            </if>
            <if test="deptId != null">
                dept_id = #{deptId},
            </if>
            <if test="version != null and version != ''">
                version = #{version},
            </if>
            <if test="reamark != null and reamark != ''">
                reamark = #{reamark},
            </if>
            <if test="count != null ">
                count = #{count},
            </if>
            <if test="totalCount != null ">
                total_count = #{totalCount},
            </if>
            <if test="deadlineCount != null">
                deadline_count = #{deadlineCount},
            </if>

            <if test="ygt != null">
                ygt = #{ygt},
            </if>
        </set>
        where id = #{id}
    </update>
    <update id="sh" parameterType="com.ruoyi.tc.entity.request.AssetCurrentShRequest">
        update asset_current_cp
        <set>
            <if test="req.status != null ">
                status = #{req.status},
            </if>
            update_time=now()
        </set>
        where id = #{req.assetId} and task_id = #{req.taskId}
    </update>
    <update id="updateByTaskId">
        update asset_task
        <set>
            <if test="taskStatus != null ">
                task_status=#{taskStatus},
            </if>
            <if test="finishTime != null ">
                task_finish_time=#{finishTime}
            </if>
        </set>

        where id =#{taskId}

    </update>
    <update id="appsh" parameterType="com.ruoyi.tc.entity.request.AssetCurrentShRequest">
        update asset_app_cp
        <set>
            <if test="req.status != null ">
                status = #{req.status},
            </if>
            update_time=now()
        </set>
        where asset_id = #{req.assetId} and task_id = #{req.taskId}

    </update>
    <update id="updateYgtByTaskId" parameterType="java.lang.Integer">
        update  asset_task set ygt=#{count} where id =#{taskId}
    </update>
    <update id="close" parameterType="java.lang.Integer">
        update asset_task
        set task_status=0
        where id = #{taskId}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete
        from asset_task
        where id = #{id}
    </delete>

</mapper>

