<?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">
<!--  Copyright (c) 京北方信息技术股份有限公司 Corporation 2023 . All rights reserved. -->
<!--  Mybatis数据库持久层（业务层映射文件）：未执行状态数据统计 -->
<!--  version: 1.0  -->
<!--  author:  maven-cctp-plugin    -->
<!--  date:  2023-04-20 16:02:50  -->
<mapper namespace="net.northking.cctp.dw.db.dao.MtCaseNonExecDao">


  <!-- 请在本文件内添加自定义SQL语句 -->

  <resultMap id="DetailMap" type="net.northking.cctp.dw.dto.MtCaseNonExecDetailDto">
      <!-- 主键 -->
    <id column="id" jdbcType="VARCHAR" property="id"/>
                    <!-- 租户id -->
    <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <!-- 系统id -->
    <result column="project_id" jdbcType="VARCHAR" property="projectId"/>
        <!-- 创建时间 -->
    <result column="created_time" jdbcType="VARCHAR" property="createdTime"/>
        <!-- 未执行状态数量 -->
    <result column="amount" jdbcType="INTEGER" property="amount"/>
    </resultMap>

  <select id="pagingQuery" resultMap="DetailMap" parameterType="net.northking.cctp.dw.db.entity.MtCaseNonExec">
    select
    id
    ,tenant_id
    ,project_id
    ,created_time
    ,amount
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND" >
      <if test="id != null">
        AND id = #{id,jdbcType=VARCHAR}
      </if>
      <if test="tenantId != null">
        AND tenant_id = #{tenantId,jdbcType=VARCHAR}
      </if>
      <if test="projectId != null">
        AND project_id = #{projectId,jdbcType=VARCHAR}
      </if>
      <if test="createdTime != null">
        AND created_time = #{createdTime,jdbcType=VARCHAR}
      </if>
      <if test="amount != null">
        AND amount = #{amount,jdbcType=INTEGER}
      </if>
    </trim>
  </select>

  <delete id="deleteByExampleByProjectIds" parameterType="net.northking.cctp.dw.db.entity.MtCaseNonExec">
    DELETE FROM cctp_dw.mt_case_non_exec WHERE project_id IN
    <foreach collection="projectIds" item="projectId" open="(" separator="," close=")">
      #{projectId, jdbcType=VARCHAR}
    </foreach>
    AND created_time=#{createdTime}
  </delete>

  <update id="syncExecTendencyNotice">
    INSERT INTO cctp_dw.mt_case_non_exec ( id, tenant_id, project_id,created_time, amount ) SELECT
    temp.id,
    temp.tenantId,
    temp.projectId,
    temp.createdTime,
    temp.amount
    FROM
    (
    SELECT
    UUID() id,
    date_format( now(), '%Y-%m-%d' ) AS createdTime,
    tenant_id tenantId,
    project_id projectId,
    cast((SUM( CASE WHEN exec_status = 0  THEN 1 ELSE 0 END )+0) as signed) amount
    FROM
    cctp_dw.dw_manual_exec_job
    WHERE
    date_format(created_time, '%Y-%m-%d' )&lt;= date_format( now(), '%Y-%m-%d' )
    and project_id in
    <foreach collection="projectIds" item="projectId" open="(" separator="," close=")">
      #{projectId, jdbcType=VARCHAR}
    </foreach>
    and tenant_id = #{tenantId}
    GROUP BY project_id
    ) temp
  </update>
</mapper>