<?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.material.mapper.PurchaseMacodeInfoMapper">
    
    <resultMap type="com.bonus.sgzb.material.domain.PurchaseMacodeInfo" id="PurchaseMacodeInfoResult">
        <result property="taskId"    column="task_id"    />
        <result property="typeId"    column="type_id"    />
        <result property="maCode"    column="ma_code"    />
        <result property="qrCode"    column="qr_code"    />
        <result property="fixCode"    column="fix_code"    />
        <result property="codeType"    column="code_type"    />
        <result property="status"    column="status"    />
        <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>

    <sql id="selectPurchaseMacodeInfoVo">
        select task_id, type_id, ma_code, qr_code, fix_code, code_type, status, create_by, create_time, update_by, update_time, remark, company_id from purchase_macode_info
    </sql>

    <select id="selectPurchaseMacodeInfoList" parameterType="com.bonus.sgzb.material.domain.PurchaseMacodeInfo" resultMap="PurchaseMacodeInfoResult">
        select  pcd.task_id, pcd.type_id, pmi.qr_code, pmi.fix_code, pmi.code_type, pmi.status,
        pmi.remark, pmi.company_id, mt.type_name specificationType, pt.arrival_time arrivalTime,
        mt1.type_name typeName, pcd.check_num checkNum, pcd.purchase_num purchaseNum ,
        IFNULL(pcd.bind_num,0) bindNum, mt.code typeCode,mt.model_code specsCode,pt.purchase_time purchaseTime
        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
        left join purchase_macode_info pmi on pmi.task_id = pcd.task_id and pmi.type_id = pcd.type_id
        left join purchase_check_info pt on pcd.task_id = pt.task_id
        where pcd.task_id = #{taskId}
        and mt.manage_type = '0'
        and pcd.`status`!=3
        and pcd.`status`!=5
        <if test="typeId != null "> and pmi.type_id = #{typeId}</if>
        <if test="keyWord != null and keyWord != ''">and (mt.type_name like concat('%',#{keyWord},'%') or mt1.type_name like concat('%',#{keyWord},'%'))</if>
        <if test="typeName != null  and typeName != ''"> and mt1.type_name = #{typeName}</if>
        <if test="specificationType != null  and specificationType != ''"> and mt.type_name = #{specificationType}</if>
        <if test="codeType != null  and codeType != ''"> and pmi.code_type = #{codeType}</if>
        <if test="status != null  and status != ''"> and pmi.status = #{status}</if>
        <if test="companyId != null "> and pmi.company_id = #{companyId}</if>
    </select>
    
    <select id="selectPurchaseMacodeInfoByTaskId" parameterType="Long" resultMap="PurchaseMacodeInfoResult">
        <include refid="selectPurchaseMacodeInfoVo"/>
        where task_id = #{taskId}
    </select>
        
    <insert id="insertPurchaseMacodeInfo" parameterType="com.bonus.sgzb.material.domain.PurchaseMacodeInfo" useGeneratedKeys="true" keyProperty="taskId">
        insert into purchase_macode_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null">task_id,</if>
            <if test="typeId != null">type_id,</if>
            <if test="maCode != null">ma_code,</if>
            <if test="fixCode != null">fix_code,</if>
            <if test="codeType != null">code_type,</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="maCode != null">#{maCode},</if>
            <if test="fixCode != null">#{fixCode},</if>
            <if test="codeType != null">#{codeType},</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="updatePurchaseMacodeInfo" parameterType="com.bonus.sgzb.material.domain.PurchaseMacodeInfo">
        update purchase_macode_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="typeId != null">type_id = #{typeId},</if>
            <if test="maCode != null">ma_code = #{maCode},</if>
            <if test="fixCode != null">fix_code = #{fixCode},</if>
            <if test="codeType != null">code_type = #{codeType},</if>
            <if test="status != null">status = #{status},</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 id = #{id}
    </update>

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

    <delete id="deletePurchaseMacodeInfoByTaskIds" parameterType="String">
        delete from purchase_macode_info where task_id in 
        <foreach item="taskId" collection="array" open="(" separator="," close=")">
            #{taskId}
        </foreach>
    </delete>

    <select id="getTypeByTypeId" resultType="com.alibaba.fastjson.JSONObject">
        select code specsCode,model_code modelCode,type_name specsName from ma_type
        where type_id = #{typeId}
    </select>

    <select id="getSerialNumber" resultType="int">
        select count(*) from ma_machine where type_id = #{typeId} and DATE_FORMAT(create_time,'%y%m') = DATE_FORMAT(#{nowDate},'%y%m')
    </select>

    <select id="getTypeNameByTypeId" resultType="com.alibaba.fastjson.JSONObject">
        select mt.type_name specsName,mt1.type_name typeName,mt2.type_name itemTypeName from ma_type mt
        left join (select type_id,type_name,parent_id from ma_type
                   where type_id in (select parent_id from ma_type where type_id = #{typeId})) mt1 on mt.parent_id = mt1.type_id
        left join (select type_id,type_name from ma_type
                   where type_id in
                         (select parent_id from ma_type
                          where type_id in (select parent_id from ma_type where type_id = #{typeId}))) mt2 on mt1.parent_id = mt2.type_id
        where mt.type_id = #{typeId}
    </select>

    <delete id="deletePurchaseMacodeInfoById">
        delete from purchase_macode_info where id = #{id}
    </delete>
    <delete id="deletePurchaseMacodeInfoByMaCode">
        delete from purchase_macode_info where ma_code = #{maCode}
    </delete>
    <delete id="deleteMaMachineInfoByMaCode">
        delete from ma_machine where ma_code = #{maCode}
    </delete>

    <delete id="deleteMaMachineInfoByMaId">
        delete from ma_machine where ma_id = #{maId}
    </delete>
    <update id="updatetePurchaseMaCodeInfoByMaCodeAndTaskIdAndTypeId">
        update
            purchase_macode_info
        set status='2'
        where ma_code = #{maCode}
          and task_id = #{taskId}
          and type_id = #{typeId}
    </update>
    <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="selectMachineByAssetsCode" resultType="com.bonus.sgzb.base.api.domain.MaMachine">
        select ma_id, type_id, ma_code, pre_code, ma_status, qr_code, buy_price, ma_vender, out_fac_time, out_fac_code,
               assets_code, check_man, this_check_time, next_check_time, gps_code, rfid_code, erp_code, transfer_code,
               in_out_num, buy_task, own_house ,company_id
        from ma_machine
        where assets_code = #{fixCode}
    </select>

    <insert id="maMachineAdd" parameterType="com.bonus.sgzb.base.api.domain.MaMachine" useGeneratedKeys="true" keyProperty="maId">
        insert into ma_machine (
        <if test="typeId != null and typeId != '' ">type_id,</if>
        <if test="maCode != null and maCode != '' ">ma_code,</if>
        <if test="preCode != null and preCode != '' ">pre_code,</if>
        <if test="maStatus != null and maStatus != '' ">ma_status,</if>
        <if test="qrCode != null and qrCode != ''">qr_code,</if>
        <if test="buyPrice != null and buyPrice != ''">buy_price,</if>
        <if test="maVender != null and maVender != ''">ma_vender,</if>
        <if test="outFacTime != null">out_fac_time,</if>
        <if test="outFacCode != null and outFacCode != ''">out_fac_code,</if>
        <if test="assetsCode != null and assetsCode != ''">assets_code,</if>
        <if test="checkMan != null and checkMan != ''">check_man,</if>
        <if test="thisCheckTime != null">this_check_time,</if>
        <if test="nextCheckTime != null">next_check_time,</if>
        <if test="gpsCode != null and gpsCode != ''">gps_code,</if>
        <if test="rfidCode != null and rfidCode != ''">rfid_code,</if>
        <if test="erpCode != null and erpCode != ''">erp_code,</if>
        <if test="transferCode != null and transferCode != ''">transfer_code,</if>
        <if test="inOutNum != null and inOutNum != ''">in_out_num,</if>
        <if test="buyTask != null and buyTask != ''">buy_task,</if>
        <if test="ownHouse != null and ownHouse != ''">own_house,</if>
        <if test="companyId != null and companyId != ''">company_id,</if>
        <if test="createTime != null">create_time</if>
        )values(
        <if test="typeId != null and typeId != ''">#{typeId},</if>
        <if test="maCode != null and maCode != ''">#{maCode},</if>
        <if test="preCode != null and preCode != ''">#{preCode},</if>
        <if test="maStatus != null and maStatus != ''">#{maStatus},</if>
        <if test="qrCode != null and qrCode != ''">#{qrCode},</if>
        <if test="buyPrice != null and buyPrice != ''">#{buyPrice},</if>
        <if test="maVender != null and maVender != ''">#{maVender},</if>
        <if test="outFacTime != null">#{outFacTime},</if>
        <if test="outFacCode != null and outFacCode != ''">#{outFacCode},</if>
        <if test="assetsCode != null and assetsCode != ''">#{assetsCode},</if>
        <if test="checkMan != null and checkMan != ''">#{checkMan},</if>
        <if test="thisCheckTime != null">#{thisCheckTime},</if>
        <if test="nextCheckTime != null">#{nextCheckTime},</if>
        <if test="gpsCode != null and gpsCode != ''">#{gpsCode},</if>
        <if test="rfidCode != null and rfidCode != ''">#{rfidCode},</if>
        <if test="erpCode != null and erpCode != ''">#{erpCode},</if>
        <if test="transferCode != null and transferCode != ''">#{transferCode},</if>
        <if test="inOutNum != null and inOutNum != ''">#{inOutNum},</if>
        <if test="buyTask != null and buyTask != ''">#{buyTask},</if>
        <if test="ownHouse != null and ownHouse != ''">#{ownHouse},</if>
        <if test="companyId != null and companyId != ''">#{companyId},</if>
        <if test="createTime != null">#{createTime}</if>
        )
    </insert>

    <select id="selectMacodeInfoStatusByTaskId" resultType="java.lang.Integer">
        select count(*) from purchase_check_details where task_id = #{taskId} and status ='1'
    </select>

    <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>
            create_time
        </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>
            now()
        </trim>
    </insert>
    <insert id="insertMaMachineLabel" parameterType="com.bonus.sgzb.base.api.domain.MaMachineLabel" useGeneratedKeys="true" keyProperty="labelId">
        insert into ma_machine_label
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="labelCode != null">label_code,</if>
            <if test="maId != null">ma_id,</if>
            <if test="isBind != null">is_bind,</if>
            <if test="labelType != null">label_type,</if>
            <if test="companyId != null">company_id,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="labelCode != null">#{labelCode},</if>
            <if test="maId != null">#{maId},</if>
            <if test="isBind != null">#{isBind},</if>
            <if test="labelType != null">#{labelType},</if>
            <if test="companyId != null">#{companyId},</if>
            now()
        </trim>
    </insert>
    <insert id="insertMaLabelBind">
        insert into ma_label_bind
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="maId != null">ma_id,</if>
            <if test="labelCode != null">label_code,</if>
            <if test="typeId != null">type_id,</if>
            <if test="binder != null">binder,</if>
            <if test="labelType != null">label_type,</if>
            <if test="companyId != null">company_id,</if>
            <if test="bindTime != null">bind_time,</if>
            <if test="status != null">status,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="maId != null">#{maId},</if>
            <if test="labelCode != null">#{labelCode},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="binder != null">#{binder},</if>
            <if test="labelType != null">#{labelType},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="bindTime != null">#{bindTime},</if>
            <if test="status != null">#{status},</if>
        </trim>
    </insert>
    <insert id="insertInputApplyDetails" parameterType="com.bonus.sgzb.material.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>

    <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="selectMachineByMaCode" resultType="java.lang.Integer">
        select count(*) from ma_machine where ma_code = #{codingRule}
    </select>
    <select id="selectPurchaseMacodeInfo" resultType="com.bonus.sgzb.material.domain.PurchaseMacodeInfo">
        select pcd.production_time productionTime,mt.type_name specificationType, mt1.type_name typeName, 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
        from purchase_macode_info pmi
        left join purchase_check_details pcd 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 pmi.task_id = #{taskId} and pmi.type_id = #{typeId}
            <if test="productionTime != null">pcd.production_time = #{productionTime}</if>

    </select>
    <select id="selectPutinDetails" resultType="com.bonus.sgzb.material.domain.PurchaseMacodeInfo">
        select pcd.production_time as productionTime,
               mt.type_name as specificationType,
               mt1.type_name as typeName,
               mt.manage_type as manageType,
               pmi.ma_code as maCode,
               mm.assets_code as assetsCode,
               pmi.fix_code as fixCode,
               pcd.type_id as typeId,
               pcd.input_time as inputTime,
               pcd.task_id as taskId,
               mt.code as specsCode,
               mt.num as num,
               mt1.code as 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 as qrCode,
               mm.ma_id as 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_type_keeper mtk on pcd.type_id = mtk.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
        <if test="userId != 1">
            and mtk.user_id = #{userId}
        </if>
        <if test="dictName != null and dictName != ''">
            and (mt.type_name like concat('%',#{dictName},'%') or
                mt1.type_name like concat('%',#{dictName},'%'))
        </if>
        order by status, pmi.id
    </select>
    <select id="selectMaCode" resultType="java.lang.Integer">
        select count(ma_id) from ma_machine where ma_code = #{maCode}
    </select>
    <select id="selectLableNumByMonth" resultType="java.lang.Integer">
        select count(*)
        from ma_machine_label
        where DATE_FORMAT(create_time,'%y%m') = DATE_FORMAT(#{nowDate},'%y%m')
    </select>
    <select id="warehousingEntry" resultType="com.bonus.sgzb.material.domain.PurchaseMacodeInfo">
        SELECT
        *
        FROM
        (
        SELECT
        pcd.production_time productionTime,
        mt.type_name specificationType,
        mt1.type_name typeName,
        pmi.ma_code maCode,
        mm.assets_code assetsCode,
        pmi.fix_code fixCode,
        pcd.type_id typeId,
        pcd.task_id taskId,
        pci.remark remark,
        mt.CODE specsCode,
        mt.unit_name unitName,
        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_check_info pci on pci.task_id=pcd.task_id
        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
        <if test="dictName != null and dictName != ''">
            and (mt.type_name like concat('%',#{dictName},'%') or
            mt1.type_name like concat('%',#{dictName},'%'))
        </if>
        ORDER BY
        STATUS
        ) aa
        WHERE
        `status` != 0
        AND `status` !=2
    </select>
    <select id="selectPutinDetailsAddTypeId" resultType="com.bonus.sgzb.material.domain.PurchaseMacodeInfo">
        select pcd.production_time                           productionTime,
        mt.type_name                                  specificationType,
        mt1.type_name                                 typeName,
        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 mt.type_id = #{typeId}
        and pcd.`status`!=3 and pcd.`status`!=7
        <if test="dictName != null and dictName != ''">
            and (mt.type_name like concat('%',#{dictName},'%') or
            mt1.type_name like concat('%',#{dictName},'%'))
        </if>
        order by status
    </select>
    <select id="isOperateAll" resultType="java.lang.Integer">
        SELECT count(*)
        FROM (SELECT CASE

                         WHEN
                             pmi.ma_code IS NOT NULL THEN
                             CASE

                                 WHEN pmi.STATUS = 0 THEN
                                     1
                                 WHEN pmi.STATUS = 1 THEN
                                     4
                                 WHEN pmi.STATUS = 2 THEN
                                     5
                                 ELSE 1
                                 END
                         ELSE pcd.STATUS
                         END AS STATUS
              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
              WHERE pcd.task_id = #{taskId}) aa
        WHERE (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="selectPurchaseMacodeInfoListDetails" resultType="com.bonus.sgzb.material.domain.PurchaseMacodeInfo">
        SELECT
            mt.type_name specificationType,
            mt1.type_name typeName,
            pmi.ma_code maCode,
            pmi.fix_code fixCode,
            mm.assets_code assetsCode
        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
                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 mm.ma_code = pmi.ma_code
                AND pmi.type_id = pcd.type_id
                LEFT JOIN purchase_check_info pt ON pcd.task_id = pt.task_id
        WHERE
            pcd.task_id = #{taskId}
          AND pcd.type_id = #{typeId}
          AND mt.manage_type = '0'
          AND pcd.`status` != 3
	AND pcd.`status` != 5
    </select>
    <select id="selectKeepUser" resultType="java.lang.String">
        SELECT user_id FROM ma_type_keeper WHERE type_id = #{typeId}
    </select>

    <update id="updateTypeByTypeId">
        update ma_type set num = #{num} where type_id = #{typeId}
    </update>

    <update id="updateMaMachine">
        update ma_machine set ma_status = #{maStatus} where ma_id = #{maId}
    </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="updateMaTypeNum">
        UPDATE ma_type
        SET num = ifnull( num, 0 ) + #{partNum} WHERE type_id = #{typeId}
    </update>

    <select id="getCountOfMachineByTypeId" resultType="java.lang.Integer">
        SELECT
            count(mm.ma_id)
        FROM ma_machine mm
                 LEFT JOIN ma_type mt ON mt.type_id = mm.type_id
                 LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        WHERE mm.ma_code is not null and mm.ma_status in (15)
          AND mt.type_id = #{typeId}
    </select>
</mapper>