<?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.bonus.material.part.mapper.PartArrivedMapper">

    <update id="updateTaskStatus">
        update
            tm_task
        set
            task_status = #{status}
        where
            task_id = #{taskId}
    </update>

    <select id="getTypeTree" resultType="com.bonus.material.part.domain.PartTypeDto">
        select DISTINCT m.pa_id as partId, m.pa_name as partName, m.parent_id as parentId, m.unit_id as unitId, m.unit_name as unitName, m.storage_num as storageNum,
                        m.rent_price as rentPrice, m.buy_price as buyPrice, m.level,m.del_flag as delFlag,m.remark
        from ma_part_type m
        where
            m.del_flag = '0'
    </select>

    <select id="getMonthMaxOrderByDate" resultType="java.lang.Integer">
        select COALESCE(max(month_order), 0) from tm_task
        where
        month(create_time) = #{month} and year(create_time) = #{year}
        <if test="taskType != null and taskType !=''">
            and task_type = #{taskType}
        </if>
    </select>

    <insert id="insertTmTask" parameterType="com.bonus.material.task.domain.TmTask" useGeneratedKeys="true" keyProperty="taskId">
        insert into tm_task
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="preTaskId != null">pre_task_id,</if>
            <if test="taskType != null">task_type,</if>
            <if test="taskStatus != null">task_status,</if>
            <if test="code != null">`code`,</if>
            <if test="monthOrder != null">month_order,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="companyId != null">company_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="preTaskId != null">#{preTaskId},</if>
            <if test="taskType != null">#{taskType},</if>
            <if test="taskStatus != null">#{taskStatus},</if>
            <if test="code != null">#{code},</if>
            <if test="monthOrder != null">#{monthOrder},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="companyId != null">#{companyId},</if>
        </trim>
    </insert>

    <insert id="insertPartTypeCheckInfo" parameterType="com.bonus.material.part.domain.PartTypeCheckInfo" useGeneratedKeys="true" keyProperty="id">
        insert into purchase_part_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null">task_id,</if>
            <if test="purchaseTime != null">purchase_time,</if>
            <if test="arrivalTime != null">arrival_time,</if>
            <if test="purchaser != null">purchaser,</if>
            <if test="supplierId != null">supplier_id,</if>
            <if test="taxRate != null">tax_rate,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="companyId != null">company_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskId != null">#{taskId},</if>
            <if test="purchaseTime != null">#{purchaseTime},</if>
            <if test="arrivalTime != null">#{arrivalTime},</if>
            <if test="purchaser != null">#{purchaser},</if>
            <if test="supplierId != null">#{supplierId},</if>
            <if test="taxRate != null">#{taxRate},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="companyId != null">#{companyId},</if>
        </trim>
    </insert>


    <insert id="insertPartTypeCheckDetailsList">
        insert into purchase_part_details(task_id,part_id,purchase_price,purchase_tax_price,
        purchase_num,check_num,check_result,supplier_id,
        status,production_time,create_by,create_time,update_by,
        update_time,remark,input_num,
        input_time,company_id,del_flag)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.taskId},#{item.partId},#{item.purchasePrice},#{item.purchaseTaxPrice},
            #{item.purchaseNum},#{item.checkNum},#{item.checkResult},#{item.supplierId},
            #{item.status},#{item.productionTime},#{item.createBy},NOW(),#{item.updateBy},
            #{item.updateTime},#{item.remark},#{item.inputNum},
            #{item.inputTime},#{item.companyId},0)
        </foreach>
    </insert>

    <select id="selectPartTypeCheckInfoJoinList" resultType="com.bonus.material.part.domain.PartTypeCheckInfo">
        select
            ppi.id, ppi.task_id as taskId, ppi.purchase_time as purchaseTime, ppi.arrival_time as arrivalTime, ppi.purchaser, ppi.supplier_id as supplierId, ppi.tax_rate as taxRate,
            ppi.create_by as createBy, ppi.create_time as createTime, ppi.update_by as updateBy, ppi.update_time as updateTime, ppi.remark, ppi.company_id as companyId,
            t.task_status as taskStatus,t.code,msi.supplier
        from
            purchase_part_info ppi
        left join tm_task t on t.task_id = ppi.task_id
        left join sys_user su ON ppi.create_by = su.user_id
        left join ma_supplier_info msi on ppi.supplier_id = msi.supplier_id
        <where>
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                <![CDATA[and DATE_FORMAT( ppi.arrival_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime} ]]>
            </if>
        and t.task_status!=12
        </where>
        order by ppi.create_time desc
    </select>

    <select id="selectPartTypeCheckDetailsList" resultType="com.bonus.material.part.domain.PartTypeCheckDetails">
        select part_id                  as partId,
               mpt.pa_name              as maTypeName,
               purchase_price           as purchasePrice,
               purchase_tax_price       as purchaseTaxPrice,
               purchase_num             as purchaseNum,
               ppd.status               as status,
               IFNULL(ppd.input_num, 0) as inputNum
        from purchase_part_details ppd
                 left join ma_part_type mpt on ppd.part_id = mpt.pa_id and mpt.del_flag = 0
        where ppd.task_id = #{taskId}
          and ppd.del_flag = 0
    </select>

    <select id="selectPartTypeCheckDetailsListByQueryDto" resultType="com.bonus.material.part.domain.PartTypeCheckDetails">
        select ppd.id as id,part_id as partId,mpt.pa_name as partName,ppd.task_id as taskId,purchase_price as purchasePrice,purchase_tax_price as purchaseTaxPrice,purchase_num as purchaseNum,
               mpt.unit_name as unitName,mpt.rent_price as rentPrice,ppd.production_time as productionTime,mpt2.pa_name as maTypeName,ppd.status,
               mpt3.pa_name as maName,msi.supplier as supplierName, ppd.check_result as checkResult,
               IFNULL(ppd.input_num, 0) as inputNum
        from purchase_part_details ppd
                 left join ma_part_type mpt on ppd.part_id = mpt.pa_id and mpt.del_flag = 0
                 left join purchase_part_info ppi on ppi.id = 2
                 left join ma_supplier_info msi on ppi.supplier_id = msi.supplier_id
                 left join ma_part_type mpt2 on mpt.parent_id = mpt2.pa_id and mpt2.del_flag = 0
                 left join ma_part_type mpt3 on mpt2.parent_id = mpt3.pa_id and mpt3.del_flag = 0
        where ppd.task_id = #{taskId} and ppd.del_flag = 0
        <if test="partId != null">
            and ppd.part_id = #{partId}
        </if>
        <if test="productionTime != null">
            and  date_format(ppd.production_time,'%y%m%d') = date_format(#{productionTime},'%y%m%d')
        </if>
        <if test="keyWord != null and keyWord!=''">
            and (
            mpt.pa_name like concat('%', #{keyWord}, '%') or
            mpt3.pa_name like concat('%', #{keyWord}, '%') or
            mpt2.pa_name like concat('%', #{keyWord}, '%')
            )
        </if>
    </select>
    <select id="selectPartTypeCheckInfoById" resultType="com.bonus.material.part.domain.PartTypeCheckInfo">
        select
            id, task_id as taskId, purchase_time purchaseTime, arrival_time as arrivalTime, purchaser, supplier_id supplierId,
            tax_rate as taxRate, create_by as createBy,create_time as createTime, update_by as updateBy, update_time as updateTime, remark, company_id as companyId
        from
            purchase_part_info
        where id = #{id}
    </select>

    <!-- 限制条件 严格匹配字典表及枚举类中定义的状态码 -->
    <delete id="deletePartTypeCheckDetailsByParentIds" parameterType="Long">
        delete from purchase_part_details where task_id = #{taskId}  and status = 1
    </delete>


    <delete id="deleteByParentIds" parameterType="Long">
        delete from purchase_part_details where task_id = #{taskId}
    </delete>

    <delete id="deleteTmTask" parameterType="Long">
        delete from tm_task where task_id = #{taskId}
    </delete>

    <delete id="deletePartTypCheckInfoById" parameterType="Long">
        delete from purchase_part_info where id = #{id}
    </delete>

    <select id="getPartFilelist" resultType="com.bonus.material.part.domain.BmPartFileInfo">
        select
        distinct task_type as taskType,task_id as taskId,model_id as modelId, file_type as fileType
        from bm_file_info bfi
        where task_type = #{taskType} and  task_id = #{taskId} and model_id = #{modelId}
    </select>

    <select id="getPartFileDetail" resultType="com.bonus.material.part.domain.BmPartFileDetail">
        select
        id,task_type as taskType,task_id as taskId,model_id as modelId, file_type as fileType,name,url
        from bm_file_info bfi
        where task_type = #{taskType} and  task_id = #{taskId} and model_id = #{modelId} and file_type = #{fileType}
    </select>

    <delete id="deleteBmFileList" parameterType="Long">
        delete from bm_file_info bfi where task_id = #{taskId} and task_type = 13
    </delete>

    <select id="getCheckInfoForm" resultType="com.bonus.material.part.domain.PartTypeCheckInfo">
        select
            ppi.id, ppi.task_id as taskId, ppi.purchase_time as purchasePrice, ppi.arrival_time as arrivalTime, ppi.purchaser, ppi.supplier_id as supplierId, ppi.tax_rate as taxRate,
            ppi.create_by as createBy, ppi.create_time as createTime, ppi.update_by as updateBy, ppi.update_time as updateTime, ppi.remark, ppi.company_id as companyId,
            t.task_status as taskStatus,t.code,msi.supplier
        from
            purchase_part_info ppi
                left join tm_task t on t.task_id = ppi.task_id
                left join sys_user su ON ppi.create_by = su.user_id
                left join ma_supplier_info msi on ppi.supplier_id = msi.supplier_id
        where ppi.id = #{id}
    </select>
</mapper>