<?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-16 11:43:35  -->
<mapper namespace="net.northking.cctp.dw.db.dao.MidProjectInfoDao">

    <update id="updateById" parameterType="net.northking.cctp.dw.db.entity.MidProjectInfo">
        update
        <include refid="Table_Name"/>
        <set>
            <if test="startTime != null">
                start_time = #{startTime, jdbcType=TIMESTAMP},
            </if>
            <if test="productionTime != null">
                production_time = #{productionTime, jdbcType=TIMESTAMP},
            </if>
            <if test="deleted != null">
                is_deleted=#{deleted,jdbcType=TINYINT},
            </if>
            <if test="tenantId != null">
                tenant_id=#{tenantId,jdbcType=VARCHAR}
            </if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>

    <update id="deleteByProjectIds" parameterType="java.lang.String">
        update
        <include refid="Table_Name"/>
        set is_deleted = 1
        where project_id in
            <foreach collection="projectIds" open="(" close=")" item="projectId" separator="," index="i">
                #{projectId}
            </foreach>

    </update>

    <select id="findByTenantId" resultType="java.lang.String">
        select
        distinct
        project_id
        from
        <include refid="Table_Name"/>
        <trim prefix="WHERE" prefixOverrides="AND">
            <if test="tenantId != null and tenantId != ''">
                AND tenant_id=#{tenantId,jdbcType=VARCHAR}
            </if>
            and 1=1
        </trim>
    </select>
    <select id="findByProjectIds" resultMap="BaseResultMap">
        select
        project_id
        ,start_time
        ,production_time
        ,tenant_id
        from mid_project_info where project_id in
        <foreach collection="midProjectInfos" item="midProjectInfo" open="(" close=")" separator="," >
            #{midProjectInfo.projectId}
        </foreach>
    </select>

    <select id="getFinishedProjectAmountByTenantId" resultType="net.northking.cctp.dw.dto.ProjectDataVo">
        select
        count(tmp.projectId)-sum(case when tmp.percent_actual=100.0 then 1 else 0 end) testing,
        sum(case when tmp.percent_actual=100.0 then 1 else 0 end) finished,
        sum(case when date_format(NOW(),'%Y-%m-%d')&lt;date_format(tmp.production_time,'%Y-%m-%d') and tmp.percent_actual&lt;100.0 then 1 else 0 end) delay,
        sum(case when tmp.percent_actual &lt;tmp.percent_plan then 1 else 0 end) exception
        from
        (
        select
        mc.percent_actual,
        mc.percent_plan,
        mc.project_id projectId,
        md.production_time
        from mt_test_chart mc join mid_project_info md on mc.project_id=md.project_id
        where
        mc.tenant_id=#{tenantId}
        and
        date_format(mc.record_date,'%Y-%m-%d')=date_format(NOW(),'%Y-%m-%d')
        and
        mc.test_type=4
        and mc.project_id in
        <foreach collection="projectIds" item="projectId" open="(" separator="," close=")">
            #{projectId}
        </foreach>
        )tmp
    </select>


    <select id="getProjectStatuss" resultType="net.northking.cctp.dw.dto.ProjectDataVo">
        select
        tmp.projectId,
        case
        when tmp.percent_actual=100.0 then '已完成'
        when date_format(NOW(),'%Y-%m-%d')&lt;date_format(tmp.production_time,'%Y-%m-%d') and tmp.percent_actual&lt;100.0 then '延期'
        when tmp.percent_actual &lt;tmp.percent_plan then '异常'
        when tmp.percent_actual &lt;100.0 then '进行中'
        else '未知状态'
        end as projectStatus
        from
        (
        select
        mc.percent_actual,
        mc.percent_plan,
        mc.project_id projectId,
        md.production_time
        from mt_test_chart mc join mid_project_info md on mc.project_id=md.project_id
        where
        date_format(mc.record_date,'%Y-%m-%d')=date_format(NOW(),'%Y-%m-%d')
        and
        mc.test_type=4
        and mc.project_id in
        <foreach collection="projectIds" item="projectId" open="(" separator="," close=")">
            #{projectId}
        </foreach>
        )tmp
    </select>
</mapper>