<?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.MtCaseDesignChartDao">


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

    <resultMap id="DetailMap" type="net.northking.cctp.dw.dto.MtCaseDesignChartDetailDto">
        <!--  -->
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <!--  -->
        <result column="case_type" jdbcType="VARCHAR" property="caseType"/>
        <!--  -->
        <result column="amount" jdbcType="INTEGER" property="amount"/>
        <!-- 租户id -->
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <!--  -->
        <result column="create_by" jdbcType="VARCHAR" property="createBy"/>
        <!-- 创建时间 -->
        <result column="created_time" jdbcType="VARCHAR" property="createdTime"/>
        <!--  -->
        <result column="project_id" jdbcType="VARCHAR" property="projectId"/>
    </resultMap>

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

    <select id="listQuery" resultType="net.northking.cctp.dw.dto.MtCaseDesignChartDetailDto">
        SELECT
        create_by createBy,
        case_type caseType,
        SUM(amount) amount
        from
        <include refid="Table_Name"/>
        <trim prefix="WHERE" prefixOverrides="AND">
            <if test="tenantId != null">
                AND tenant_id = #{tenantId,jdbcType=VARCHAR}
            </if>
            <if test="proId != null">
                AND project_id = #{proId,jdbcType=VARCHAR}
            </if>
            <if test="startTime != null and endTime !=null and startTime != '' and endTime !=''">
                AND created_time between #{startTime} and #{endTime}
            </if>
        </trim>
        GROUP BY create_by,case_type
    </select>

    <select id="getTenantIdList" resultType="java.lang.String">
        SELECT
        distinct tenant_id
        FROM dw_case_design_info
    </select>

    <select id="syncDesignNotice" resultType="net.northking.cctp.dw.dto.MtCaseDesignChartAddDto">
        SELECT
        UUID() id,
        mt.tenant_id tenantId,
        date_format(CURDATE(),'%Y-%m-%d') AS createdTime,
        mt.project_id projectId,
        mt.created_by userName,
        case mt.case_type
        <foreach collection="dictItems" item="item">
            when #{item.itemCode} then #{item.itemCode}
        </foreach>
        end as caseType,
        count(mt.case_type) amount
        FROM
        dw_case_design_info mt
        WHERE
        date_format(mt.created_time,'%Y-%m-%d') =date_format(curdate(),'%Y-%m-%d')
        AND
        tenant_id=#{tenantId}
        and
        case_status='1'
        GROUP BY mt.project_id,mt.created_by,mt.case_type
    </select>

    <select id="syncDesignNoticeByProjectId" resultType="net.northking.cctp.dw.dto.MtCaseDesignChartAddDto">
        SELECT
        UUID() id,
        mt.tenant_id tenantId,
        date_format(CURDATE(),'%Y-%m-%d') AS createdTime,
        mt.project_id projectId,
        mt.created_by userName,
        mt.created_by createBy,
        case mt.case_type
        <foreach collection="dictItems" item="item">
            when #{item.itemCode} then #{item.itemCode}
        </foreach>
        end as caseType,
        count(mt.case_type) amount
        FROM
        dw_case_design_info mt
        WHERE
        date_format(mt.created_time,'%Y-%m-%d') =date_format(curdate(),'%Y-%m-%d')
        AND
        project_id=#{projectId}
        and
        case_status='1'
        GROUP BY mt.created_by,mt.case_type
    </select>

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

    <update id="syncDesignNoticeByProjectIds" parameterType="net.northking.cctp.dw.dto.DwCaseDesignInfoUpdateDto">
        INSERT INTO mt_case_design_chart (id, tenant_id, created_time, project_id, create_by, case_type, amount)
        SELECT
        UUID() id,
        mt.tenant_id tenantId,
        date_format(CURDATE(),'%Y-%m-%d') AS createdTime,
        mt.project_id projectId,
        mt.created_by createBy,
        case mt.case_type
        <foreach collection="dictItems" item="item">
            when #{item.itemCode} then #{item.itemCode}
        </foreach>
        end as caseType,
        count(mt.case_type) amount
        FROM
        dw_case_design_info mt
        WHERE
        date_format(mt.created_time,'%Y-%m-%d') =date_format(curdate(),'%Y-%m-%d')
        AND
        project_id IN
        <foreach collection="projectIds" item="projectId" open="(" separator="," close=")">
            #{projectId, jdbcType=VARCHAR}
        </foreach>
        AND
        tenant_id = #{tenantId,jdbcType=VARCHAR}
        and
        case_status='1'
        GROUP BY mt.project_id,mt.created_by,mt.case_type
    </update>


</mapper>