<?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="com.cf.imes.module.manage.dal.mysql.process.ProcessStepMapper">



    <update id="updateStatusById">

        UPDATE order_process_step SET status = true,finish_time = now(),salary_amount = #{salary} WHERE id = #{id}

    </update>


    <update id="updateStatus"
            parameterType="java.lang.Object">


        update  order_process_step
        set status = true,
            finish_time = now(),
            manager = #{nickname},
            salary_amount = #{salary}
        where id = #{id};

    </update>


    <update id="updateStatusByName" parameterType="java.lang.Object">

        update  order_process_step
        set  manager = #{nickname}
        where id = #{id};


    </update>


    <resultMap id="OrderMap" type="com.cf.imes.module.manage.controller.admin.process.vo.ProcessRespVO">

        <result property="orderId" column="orderId"/>
<!--        <result property="processStepItemId" column="stepItemId"/>-->
        <result property="customOrderNo" column="customOrderNo"/>
        <result property="customer" column="customer"/>
        <result property="address" column="address"/>
        <result property="processStatus" column="processStatus"/>
        <result property="stepName" column="processName"/>
        <result property="name" column="name"/>
        <result property="manager" column="manager"/>
        <result property="processStepId" column="processStepId"/>
        <result property="processID" column="processID"/>
        <result property="processType" column="processType"/>

    </resultMap>

    <select id="selectOrder" resultMap="OrderMap" parameterType="map">


        select distinct
        ops.order_id as orderId,
        o.custom_order_no as customOrderNo,
        o.customer as customer,
        o.address as address,
        opsi.status as processStatus,
        opsi.name as name,
        ops.name as processName,
        ops.manager as manager,
        ops.id as processStepId,
        ops.processinfo_id as processID,
        ops.type as processType
        from order_process_step ops
        join `orders` o on ops.order_id = o.id and ops.organ_id = o.organ_id and o.deleted = 0
        join order_process_step_item opsi on ops.id = opsi.process_step_id and  ops.organ_id = opsi.organ_id

        <where>

            ops.organ_id  =  #{organId}


            <if test="pageReqVO.processID != null">
                and ops.processinfo_id = #{pageReqVO.processID}
            </if>
            <if test="pageReqVO.processStatus != null">
                and opsi.status = #{pageReqVO.processStatus}
            </if>
            <if test="pageReqVO.orderId != null">
                and ops.order_id  = #{pageReqVO.orderId}
            </if>
            <if test="pageReqVO.customOrderNo != null">
                and o.custom_order_no like concat('%', #{pageReqVO.customOrderNo},'%')
            </if>
            <if test="pageReqVO.customer != null">
                and o.customer like concat('%',#{pageReqVO.customer},'%')
            </if>
            <if test="pageReqVO.address != null">
                and o.address like concat('%',#{pageReqVO.address},'%')
            </if>

        </where>

        order by orderId desc
    </select>




    <resultMap id="priceResultMap" type="com.cf.imes.module.manage.controller.admin.query.vo.salary.PriceRateReqVO">
        <result property="name" column="name"/>
        <result property="priceRate" column="piece_rate"/>
        <result property="num" column="total_piece_num"/>
        <result property="priceType" column="piece_type"/>
        <result property="amount" column="total_piece_rate"/>
        <result property="manager" column="manager"/>
    </resultMap>

    <select id="selectSalaryList"
            resultMap="priceResultMap"
            parameterType="map">

        select ops.name as name ,
               ops.piece_rate as piece_rate ,
               ops.piece_type as piece_type,
               ops.manager as manager,
               SUM(CASE
                       WHEN ops.piece_type = 1 THEN ops.piece_rate
                       ELSE ROUND((op.size / ops.piece_rate), 2) * ops.piece_rate
                   END) AS total_piece_rate,

               SUM(CASE
                       WHEN ops.piece_type = 1 THEN 1
                       ELSE  ROUND((op.size / ops.piece_rate), 2)
                   END) AS total_piece_num

        from order_process_step ops
            join order_process op on ops.order_process_id = op.id and ops.organ_id = op.organ_id

        <where>

            <if test="pageReqVO.creator != null">
                and ops.manager = #{pageReqVO.creator}
            </if>
            <if test="pageReqVO.name != null">
                and ops.name = #{pageReqVO.name}
            </if>
            <if test="pageReqVO.startTime != null and  pageReqVO.endTime != null">
                and ops.update_time between #{pageReqVO.startTime} and #{pageReqVO.endTime}
            </if>
            <if test="pageReqVO.noShowZero == true">
                and ops.piece_rate &lt;&gt; 0
            </if>
            and ops.status = true
            and ops.organ_id =#{organId}
            and ops.manager != ''
        </where>

        group by name,piece_rate,piece_type,creator;

    </select>


    <resultMap id="ProductionGroupMap" type="com.cf.imes.module.manage.controller.admin.query.vo.productionStatus.ProductionGroupVO">

        <result property="groupName" column="groupName"/>
        <result property="name" column="name"/>
        <result property="status" column="status"/>
        <result property="manager" column="manager"/>
        <result property="processDate" column="processDate"/>

    </resultMap>

    <select id="selectProducitonList"
            resultMap="ProductionGroupMap"
            parameterType="java.lang.Long">


        select distinct
            ops.name as name,
            ops.status as status,
            ops.manager as manager,
            ops.finish_time as processDate,
            op.name as groupName,
            ops.sort

        from order_process_step ops
                 join order_process op on ops.order_process_id = op.id and ops.organ_id = op.organ_id
        where ops.order_id = #{orderNo} and ops.organ_id = #{organId}
        order by ops.sort ;


    </select>


    <select id="calculateSalary" resultType="java.math.BigDecimal">



        select distinct case when ops.piece_type = 1 then ops.piece_rate * count(distinct opsi.name)
                             else ROUND(ops.piece_rate * ROUND((op.size/ #{area}),3),3) end  as rate
        from order_process_step ops
                 join order_process_step_item opsi on ops.id = opsi.process_step_id and ops.organ_id = opsi.organ_id
                 join order_process op on ops.order_process_id = op.id and ops.organ_id = op.organ_id
        where ops.id = #{processStepId}
        group by ops.piece_type,piece_rate,op.size;

    </select>


    <!--    select ops.name as name,-->
<!--    ops.status as status,-->
<!--    ops.manager as manager,-->
<!--    (CASE-->
<!--    WHEN ops.status =true THEN ops.update_time-->
<!--    ELSE ops.update_time = null END) as updateTime,-->
<!--    op.plan_no as planNo,-->
<!--    opsi.group_name as groupName-->
<!--    from order_process_step ops-->
<!--    join order_process_step_item opsi on ops.id = opsi.process_step_id-->
<!--    join order_plan  op on ops.order_id = op.order_nos-->
<!--    where ops.order_id = #{orderNo}-->
<!--    ORDER BY-->
<!--    CASE-->
<!--    WHEN updateTime IS NOT NULL THEN 0  &#45;&#45; 符合条件的情况，将其排在前面-->
<!--    ELSE 1  &#45;&#45; 不符合条件的情况，将其排在后面-->
<!--    END,-->
<!--    &#45;&#45; 如果 updateTime 不为空，按照 updateTime 进行排序-->
<!--    CASE-->
<!--    WHEN updateTime IS NOT NULL THEN updateTime-->
<!--    ELSE ops.update_time-->
<!--    END;-->

</mapper>