<?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 2021 . All rights reserved. -->
<!--  Mybatis数据库持久层（业务层映射文件）：缺陷趋势信息临时表 -->
<!--  version: 1.0  -->
<!--  author:  maven-cctp-plugin    -->
<!--  date:  2021-12-15 14:58:27  -->
<mapper namespace="net.northking.cctp.dw.db.dao.BugTendencyChartTmpDao">


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

  <resultMap id="DetailMap" type="net.northking.cctp.dw.dto.BugTendencyChartTmpDetailDto">
      <!--  -->
    <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="bug_status" jdbcType="VARCHAR" property="bugStatus"/>
        <!-- 数量 -->
    <result column="amount" jdbcType="INTEGER" property="amount"/>
        <!-- 创建时间 -->
    <result column="created_time" jdbcType="VARCHAR" property="createdTime"/>
    </resultMap>

  <select id="pagingQuery" resultMap="DetailMap" parameterType="net.northking.cctp.dw.db.entity.BugTendencyChartTmp">
    select
    id
    ,tenant_id
    ,project_id
    ,bug_status
    ,amount
    ,created_time
    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="bugStatus != null">
        AND bug_status = #{bugStatus,jdbcType=VARCHAR}
      </if>
      <if test="amount != null">
        AND amount = #{amount,jdbcType=INTEGER}
      </if>
      <if test="createdTime != null">
        AND created_time = #{createdTime,jdbcType=VARCHAR}
      </if>
    </trim>
  </select>

  <select id="listQuery" resultType="net.northking.cctp.dw.dto.BugTendencyChartTmpDetailDto">
    SELECT
    tmp.*,
    tmp.solved+tmp.unsolved as allAmount
    from
    (
    SELECT
    SUM(CASE WHEN bug_status IN
    <foreach collection="statusList" item="status" open="(" close=")" separator="," index="i">
      #{status,jdbcType=VARCHAR}
    </foreach>
    THEN amount ELSE 0 END) solved,
    SUM(CASE WHEN bug_status NOT IN
    <foreach collection="statusList" item="status" open="(" close=")" separator="," index="i">
      #{status,jdbcType=VARCHAR}
    </foreach>
    THEN amount ELSE 0 END) unsolved,
    created_time as createTime
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND" >
      <if test="chartDataGeneQueryDto.tenantId != null">
        AND tenant_id = #{chartDataGeneQueryDto.tenantId,jdbcType=VARCHAR}
      </if>
      <if test="chartDataGeneQueryDto.proId != null">
        AND project_id = #{chartDataGeneQueryDto.proId,jdbcType=VARCHAR}
      </if>
      <if test="chartDataGeneQueryDto.startTime != null and chartDataGeneQueryDto.endTime !=null and chartDataGeneQueryDto.startTime != '' and chartDataGeneQueryDto.endTime !=''">
        AND created_time between #{chartDataGeneQueryDto.startTime} and #{chartDataGeneQueryDto.endTime}
      </if>
    </trim>
    GROUP BY created_time
    )tmp
  </select>

  <insert id="insertBatch">
    insert into
    bug_tendency_chart_tmp(id,amount,created_time,tenant_id,project_id,bug_status)
    SELECT
    UUID() id,
    COUNT(bug_status) AS amount,
    date_format(now(),'%Y-%m-%d') as created_time,
    tenant_id,
    pro_id project_id,
    bug_status
    from
    syn_bug_info
    where
    date_format(updated_time,'%Y-%m-%d')&lt;=date_format(now(),'%Y-%m-%d')
    and
    pro_id in
    <foreach collection="dataList" item="synBugInfo" open="(" separator="," close=")">
      #{synBugInfo.proId}
    </foreach>
    and
    tenant_id=#{tenantId}
    GROUP BY bug_status,pro_id
  </insert>

  <delete id="deleteData">
    delete
    from bug_tendency_chart_tmp
    where
    created_time=#{date}
    and tenant_id=#{tenantId}
    and project_id in
    <foreach collection="dataList" item="synBugInfo" open="(" separator="," close=")">
      #{synBugInfo.proId}
    </foreach>
  </delete>
</mapper>