<?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.sgzb.app.mapper.PurchaseInputMapper">

  <resultMap type="com.bonus.sgzb.app.domain.PurchaseCheckInfo" id="PurchaseCheckInfoResult">
    <result property="id"    column="id"    />
    <result property="taskId"    column="task_id"    />
    <result property="purchaseTime"    column="purchase_time"    />
    <result property="arrivalTime"    column="arrival_time"    />
    <result property="purchaser"    column="purchaser"    />
    <result property="createBy"    column="create_by"    />
    <result property="createTime"    column="create_time"    />
    <result property="updateBy"    column="update_by"    />
    <result property="updateTime"    column="update_time"    />
    <result property="remark"    column="remark"    />
    <result property="companyId"    column="company_id"    />
  </resultMap>

    <insert id="insertInputApplyDetails" parameterType="com.bonus.sgzb.app.domain.InputApplyDetails" useGeneratedKeys="true" keyProperty="id">
        insert into input_apply_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null">task_id,</if>
            <if test="maId != null">ma_id,</if>
            <if test="typeId != null">type_id,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="inputNum != null">input_num,</if>
            <if test="inputType != null">input_type,</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="maId != null">#{maId},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="inputNum != null">#{inputNum},</if>
            <if test="inputType != null">#{inputType},</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="insertMaInputRecord" parameterType="com.bonus.sgzb.base.api.domain.MaInputRecord" useGeneratedKeys="true" keyProperty="id">
        insert into ma_input_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null">task_id,</if>
            <if test="typeId != null">type_id,</if>
            <if test="maId != null">ma_id,</if>
            <if test="inputType != null">input_type,</if>
            <if test="inputNum != null">input_num,</if>
            <if test="status != null">status,</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="typeId != null">#{typeId},</if>
            <if test="maId != null">#{maId},</if>
            <if test="inputType != null">#{inputType},</if>
            <if test="inputNum != null">#{inputNum},</if>
            <if test="status != null">#{status},</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>

    <update id="updateMaMachine">
        update ma_machine set ma_status = #{maStatus} where ma_id = #{maId}
    </update>
    <update id="updateTypeByTypeId">
        update ma_type set num = #{num} where type_id = #{typeId}
    </update>
    <update id="updateByTaskIdTypeId">
        update purchase_check_details set check_num = #{inputNum},status = #{status},input_num=#{inputNum},input_status='1',input_time=NOW() where task_id = #{taskId} and type_id = #{typeId}
    </update>
    <update id="updateMacodeByType">
        update purchase_macode_info
        set status = #{status}
        where task_id = #{taskId}
          and type_id = #{typeId}
          and ma_code = #{maCode}
    </update>

    <update id="updateTmTask">
        update tm_task
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskType != null">task_type = #{taskType},</if>
            <if test="taskStatus != null">task_status = #{taskStatus},</if>
            <if test="code != null">code = #{code},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
        </trim>
        where task_id = #{taskId}
    </update>
    <update id="updatetePurchaseMaCodeInfoByMaCodeAndTaskIdAndTypeId">
        update
            purchase_macode_info
        set status='2'
        where ma_code = #{maCode}
          and task_id = #{taskId}
          and type_id = #{typeId}
    </update>
    <update id="updatePurchaseCheckDetailsByTaskId">
        update purchase_check_details set bind_num = bind_num - 1 , status='5' where task_id = #{taskId} and type_id = #{typeId}
    </update>

    <delete id="deleteMaMachineInfoByMaId">
        delete from ma_machine where ma_id = #{maId}
    </delete>
    <delete id="deleteMaMachineLabelByMaId">
        delete from ma_machine_label where ma_id = #{maId}
    </delete>
    <delete id="deleteMaLabelBindByMaId">
        delete from ma_label_bind where ma_id = #{maId}
    </delete>

    <select id="selectPutInListList" resultType="com.bonus.sgzb.app.domain.PurchaseCheckInfo">
        select pci.id,pci.task_id, pci.purchase_time, pci.arrival_time, pci.purchaser, pci.create_by, pci.create_time,
        pci.update_by,
        pci.update_time, pci.remark, pci.company_id ,dict.name purchasingStatus,tk.code,tk.task_status
        taskStatus,su.nick_name purchaserName,
        tk.create_by
        from purchase_check_info pci
        left join tm_task tk on pci.task_id = tk.task_id
        left join (select id,name from sys_dic where p_id = 50) dict on tk.task_status = dict.id
        left join sys_user su on pci.purchaser = su.user_id
        where task_type = 23 and tk.task_status in (26,28,107)
        <if test="keyWord != null  and keyWord != ''">and tk.code like concat('%',#{keyWord},'%')</if>
        <if test="purchaseTime != null  and purchaseTime != ''">and pci.purchase_time = #{purchaseTime}</if>
        <if test="arrivalTime != null  and arrivalTime != ''">and pci.arrival_time = #{arrivalTime}</if>
        <if test="purchaser != null ">and pci.purchaser = #{purchaser}</if>
        <if test="companyId != null ">and pci.company_id = #{companyId}</if>
        order by pci.id desc
  </select>

  <select id="selectTypeNameByTaskId" resultType="java.lang.String">
    select GROUP_CONCAT(type_name) typeName from
      (select distinct pcd.task_id, mt1.type_name
       from purchase_check_details pcd
              left join ma_type mt on pcd.type_id = mt.type_id
              left join ma_type mt1 on mt.parent_id = mt1.type_id
       where pcd.task_id = #{taskId}
      <if test="type == 2">and pcd.`status`!=3</if>
      <if test="type == 3">and pcd.`status`!=3 and pcd.`status` !=7</if>
      ) t
    GROUP BY task_id
  </select>
  <select id="selectPutinDetails" resultType="com.bonus.sgzb.app.domain.PurchaseMacodeInfo">
      select pcd.production_time                                            productionTime,
             mt.type_name                                                   specificationType,
             mt1.type_name                                                  typeName,
             mt.manage_type as manageType,
             pmi.ma_code                                                    maCode,
             mm.assets_code                                                 assetsCode,
             pmi.fix_code                                                   fixCode,
             pcd.type_id                                                    typeId,
             pcd.task_id                                                    taskId,
             mt.code                                                        specsCode,
             mt1.code                                                       typeCode,
             CASE
                 WHEN pmi.ma_code is null THEN
                     CASE
                         WHEN pcd.status = 4 THEN 1
                         WHEN pcd.status = 5 THEN 2
                         ELSE 0
                         END
                 ELSE pmi.status
                 END AS                                    status,
             mm.qr_code                                                     qrCode,
             mm.ma_id                                                       maId,
             if(pmi.ma_code is not null, 1, pcd.check_num)                  checkNum
      from purchase_check_details pcd
               left join purchase_macode_info pmi on pmi.task_id = pcd.task_id and pmi.type_id = pcd.type_id
               left join ma_machine mm on pmi.ma_code = mm.ma_code
               left join ma_type mt on pcd.type_id = mt.type_id
               left join ma_type mt1 on mt.parent_id = mt1.type_id
      where pcd.task_id = #{taskId}
        and pcd.`status`!=3 and pcd.`status`!=7
      order by status
  </select>
    <select id="selectTypeByTypeId" resultType="com.bonus.sgzb.base.api.domain.MaType">
        select type_id typeId, type_name, parent_id, status, num, unit_id, manage_type, lease_price, buy_price, pay_price,
               level, rated_load, test_load, holding_time, warn_num, del_flag, create_by,
               create_time, remark, company_id
        from ma_type where type_id = #{typeId}
    </select>
    <select id="selectInputNumByTypeId" resultType="java.math.BigDecimal">
        select IFNULL(input_num,0) from purchase_check_details where task_id = #{taskId} and type_id = #{typeId}
    </select>
    <select id="selectMacodeInfoStatusByTaskId" resultType="java.lang.Integer">
        select count(*) from purchase_macode_info where task_id = #{taskId} and status not in ('1','2')
    </select>
    <select id="isOperateAll" resultType="java.lang.Integer">
        SELECT
            count(*)
        FROM
            purchase_check_details
        WHERE
            task_id =  #{taskId} and (status = 1 or status=6)
    </select>
    <select id="selectPurchaseCheckDetailsStatus" resultType="java.lang.Integer">
        SELECT
            count(*)
        FROM
            purchase_check_details
        WHERE
            task_id = #{taskId} and status =4
    </select>
    <select id="selectExamineType" resultType="java.lang.Integer">
        SELECT
            COUNT(*)
        FROM
            bm_flow_relation bfr
                LEFT JOIN bm_flow bf on bfr.flow_id=bf.id
        WHERE
            bf.task_type='23'
          AND bfr.company_id = #{companyId}
          and bfr.`status`='1'
          and bf.`status`='1'
    </select>
    <select id="selectPutInListExamine" resultType="com.bonus.sgzb.app.domain.PurchaseCheckInfo">
        select pci.id,pci.task_id, pci.purchase_time, pci.arrival_time, pci.purchaser, pci.create_by, pci.create_time,
        pci.update_by,
        pci.update_time, pci.remark, pci.company_id ,dict.name purchasingStatus,tk.code,tk.task_status
        taskStatus,su.nick_name purchaserName,
        tk.create_by
        from purchase_check_info pci
        left join tm_task tk on pci.task_id = tk.task_id
        left join (select id,name from sys_dic where p_id = 50) dict on tk.task_status = dict.id
        left join sys_user su on pci.purchaser = su.user_id
        where task_type = 23 and tk.task_status in (105,28,107)
        <if test="keyWord != null  and keyWord != ''">and tk.code like concat('%',#{keyWord},'%')</if>
        <if test="purchaseTime != null  and purchaseTime != ''">and pci.purchase_time = #{purchaseTime}</if>
        <if test="arrivalTime != null  and arrivalTime != ''">and pci.arrival_time = #{arrivalTime}</if>
        <if test="purchaser != null ">and pci.purchaser = #{purchaser}</if>
        <if test="companyId != null ">and pci.company_id = #{companyId}</if>
        order by pci.id desc
    </select>
</mapper>