<?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="org.springblade.modules.pl.productProcess.mapper.ProductProcessMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="productProcessResultMap" type="org.springblade.modules.pl.productProcess.entity.ProductProcessEntity">
        <result column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="product_id" property="productId"/>
        <result column="process_id" property="processId"/>
        <result column="process_name" property="processName"/>
        <result column="process_code" property="processCode"/>
        <result column="is_retrospect" property="isRetrospect"/>
        <result column="is_standards" property="isStandards"/>
        <result column="create_user" property="createUser"/>
        <result column="create_dept" property="createDept"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
    </resultMap>


    <select id="selectProductProcessPage" resultType="org.springblade.modules.pl.productProcess.excel.ProductProcessExcel">
        SELECT
            t.processName,
            t.productModel,
            t.unfinishedCount,
            t.completedCount,
            t.successCount,
            t.errorCount ,
            CONCAT(FORMAT(t.successCount/t.count*100,2),"%") yield
        FROM
            (
                SELECT
                    a.process_id processId,
                    a.process_name processName,
                    b.product_model productModel,
                    sum( CASE WHEN a.STATUS = 1 THEN 1 ELSE 0 END ) AS count,
                    sum( CASE WHEN a.STATUS = 0 THEN 1 ELSE 0 END ) AS unfinishedCount,
                    sum( CASE WHEN a.STATUS = 1 THEN 1 ELSE 0 END ) AS completedCount,
                    sum( CASE WHEN (a.is_standards = 0 and a.STATUS = 1) THEN 1 ELSE 0 END ) AS errorCount,
                    sum( CASE WHEN (a.is_standards != 0 and a.STATUS = 1) THEN 1 ELSE 0 END ) AS successCount
                FROM
                    pl_product_process a
                LEFT JOIN pl_product b ON a.product_id = b.id
                WHERE
                    1 = 1
                    <if test="param.updateTimeArr!=null">
                        and date_format(a.update_time, '%Y-%m-%d') between #{param.updateTimeStart} and #{param.updateTimeEnd}
                    </if>
                    <if test="param.processName!=null and param.processName!=''">
                        and a.process_name like concat(concat('%', #{param.processName}),'%')
                    </if>
                    <if test="param.productModel!=null and param.productModel!=''">
                        and a.product_model like concat(concat('%', #{param.productModel}),'%')
                    </if>
                    <if test="param.factoryId!=null and param.factoryId!=''">
                        and b.factory_id = #{param.factoryId}
                    </if>
                    <if test="param.processId!=null and param.processId!=''">
                        and a.process_id = #{param.processId}
                    </if>
                    <if test="param.productModelId!=null and param.productModelId!=''">
                        and b.product_model_id = #{param.productModelId}
                    </if>
                GROUP BY
                    a.process_id,
                    a.process_name,
                    b.product_model
            ) t
        ORDER BY
            count DESC
    </select>


    <select id="exportProductProcess" resultType="org.springblade.modules.pl.productProcess.excel.ProductProcessExcel">
        SELECT
            t.processName,
            t.productModel,
            t.unfinishedCount,
            t.completedCount,
            t.successCount,
            t.errorCount ,
            CONCAT(FORMAT(t.successCount/t.count*100,2),"%") yield
        FROM
            (
                SELECT
                    a.process_id processId,
                    a.process_name processName,
                    b.product_model productModel,
                    sum( CASE WHEN a.STATUS = 1 THEN 1 ELSE 0 END ) AS count,
                    sum( CASE WHEN a.STATUS = 0 THEN 1 ELSE 0 END ) AS unfinishedCount,
                    sum( CASE WHEN a.STATUS = 1 THEN 1 ELSE 0 END ) AS completedCount,
                    sum( CASE WHEN (a.is_standards = 0 and a.STATUS = 1) THEN 1 ELSE 0 END ) AS errorCount,
                    sum( CASE WHEN (a.is_standards != 0 and a.STATUS = 1) THEN 1 ELSE 0 END ) AS successCount
                FROM
                    pl_product_process a
                    LEFT JOIN pl_product b ON a.product_id = b.id
                WHERE
                    1 = 1
                    <if test="param.updateTimeArr!=null">
                        and date_format(a.update_time, '%Y-%m-%d') between #{payOrder.updateTimeStart} and #{payOrder.updateTimeEnd}
                    </if>
                    <if test="param.processName!=null and param.processName!=''">
                        and a.process_name like concat(concat('%', #{param.processName}),'%')
                    </if>
                    <if test="param.productModel!=null and param.productModel!=''">
                        and b.product_model like concat(concat('%', #{param.productModel}),'%')
                    </if>
                    <if test="param.productModelId!=null and param.productModelId!=''">
                        and b.product_model_id = #{param.productModelId}
                    </if>
                    <if test="param.factoryId!=null and param.factoryId!=''">
                        and b.factory_id = #{param.factoryId}
                    </if>
                    <if test="param.processId!=null and param.processId!=''">
                        and a.process_id = #{param.processId}
                    </if>
                GROUP BY
                    a.process_id,
                    a.process_name,
                    b.product_model
            ) t
        ORDER BY
            count DESC
    </select>

</mapper>
