<?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.purchase.mapper.PurchaseCheckDetailsMapper">
    <resultMap type="com.bonus.material.purchase.domain.PurchaseCheckDetails" id="PurchaseCheckDetailsResult">
        <result property="id"    column="id"    />
        <result property="taskId"    column="task_id"    />
        <result property="typeId"    column="type_id"    />
        <result property="typeName"    column="type_name"    />
        <result property="unitName"    column="unit_name"    />
        <result property="unitValue"    column="unit_value"    />
        <result property="maTypeName"    column="ma_type_name"    />
        <result property="purchasePrice"    column="purchase_price"    />
        <result property="purchaseTaxPrice"    column="purchase_tax_price"    />
        <result property="purchaseNum"    column="purchase_num"    />
        <result property="checkNum"    column="check_num"    />
        <result property="bindNum"    column="bind_num"    />
        <result property="checkResult"    column="check_result"    />
        <result property="supplierId"    column="supplier_id"    />
        <result property="status"    column="status"    />
        <result property="createBy"    column="create_by"    />
        <result property="productionTime"    column="production_time"    />
        <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="checkUrlName"    column="check_url_name"    />
        <result property="checkUrl"    column="check_url"    />
        <result property="inputNum"    column="input_num"    />
        <result property="inputStatus"    column="input_status"    />
        <result property="inputTime"    column="input_time"    />
        <result property="fileName"    column="file_name"    />
        <result property="fileUrl"    column="file_url"    />
        <result property="companyId"    column="company_id"    />
        <result property="fixCode"    column="fix_code"    />
        <result property="manageType"    column="manage_type"    />
    </resultMap>

    <sql id="selectPurchaseCheckDetailsVo">
        select id, task_id, type_id, purchase_price, purchase_tax_price, purchase_num, check_num, bind_num, check_result,
               supplier_id, status, create_by, production_time, create_time, update_by, update_time, remark, check_url_name,
               check_url, input_num, input_status, input_time, file_name, file_url, company_id, fix_code
        from purchase_check_details
    </sql>

    <sql id="selectPurchaseCheckDetailsJoinVo">
        select pcd.id, pcd.task_id, pcd.type_id, pcd.purchase_price, pcd.purchase_tax_price, pcd.purchase_num, pcd.check_num, pcd.bind_num, pcd.check_result,
               pcd.supplier_id, pcd.status, pcd.create_by, pcd.production_time, pcd.create_time, pcd.update_by, pcd.update_time,
               pcd.remark, pcd.check_url_name, pcd.check_url, pcd.input_num, pcd.input_status, pcd.input_time, pcd.file_name,
               pcd.file_url, pcd.company_id, pcd.fix_code, mt.type_name, mt.unit_name, mt.unit_value,mtp.type_name as ma_type_name, mt.manage_type as manage_type,
               pcd.warn_documents as warnDocuments, pcd.reason as reason, mt.rent_price as rentPrice, su.sign_url as signUrl, su.sign_type as signType
        from purchase_check_details pcd
        left join ma_type mt on pcd.type_id = mt.type_id
        left join ma_type mtp on mt.parent_id = mtp.type_id
        left join sys_user su on pcd.check_user = su.user_id
    </sql>

    <select id="selectPurchaseCheckDetailsList" parameterType="com.bonus.material.purchase.domain.PurchaseCheckDetails" resultMap="PurchaseCheckDetailsResult">
        <include refid="selectPurchaseCheckDetailsVo"/>
        <where>
            <if test="taskId != null "> and task_id = #{taskId}</if>
            <if test="typeId != null "> and type_id = #{typeId}</if>
            <if test="purchasePrice != null "> and purchase_price = #{purchasePrice}</if>
            <if test="purchaseTaxPrice != null "> and purchase_tax_price = #{purchaseTaxPrice}</if>
            <if test="purchaseNum != null "> and purchase_num = #{purchaseNum}</if>
            <if test="checkNum != null "> and check_num = #{checkNum}</if>
            <if test="bindNum != null "> and bind_num = #{bindNum}</if>
            <if test="checkResult != null  and checkResult != ''"> and check_result = #{checkResult}</if>
            <if test="supplierId != null "> and supplier_id = #{supplierId}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="productionTime != null "> and production_time = #{productionTime}</if>
            <if test="checkUrlName != null  and checkUrlName != ''"> and check_url_name like concat('%', #{checkUrlName}, '%')</if>
            <if test="checkUrl != null  and checkUrl != ''"> and check_url = #{checkUrl}</if>
            <if test="inputNum != null "> and input_num = #{inputNum}</if>
            <if test="inputStatus != null  and inputStatus != ''"> and input_status = #{inputStatus}</if>
            <if test="inputTime != null "> and input_time = #{inputTime}</if>
            <if test="fileName != null  and fileName != ''"> and file_name like concat('%', #{fileName}, '%')</if>
            <if test="fileUrl != null  and fileUrl != ''"> and file_url = #{fileUrl}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="fixCode != null "> and fix_code = #{fixCode}</if>
        </where>
    </select>

    <select id="selectPurchaseCheckDetailsListFromInfo" parameterType="com.bonus.material.purchase.domain.PurchaseCheckInfo" resultMap="PurchaseCheckDetailsResult">
        <include refid="selectPurchaseCheckDetailsJoinVo"/>
        where task_id = #{taskId}
    </select>

    <select id="selectPurchaseCheckDetailsById" parameterType="Long" resultMap="PurchaseCheckDetailsResult">
        <include refid="selectPurchaseCheckDetailsVo"/>
        where id = #{id}
    </select>

    <select id="selectPurchaseCheckDetailsListByTaskId" resultMap="PurchaseCheckDetailsResult">
        <include refid="selectPurchaseCheckDetailsJoinVo"/>
        <if test="userId != null">
            right join ma_type_keeper mtk ON mtk.type_id = mt.type_id AND mtk.user_id = #{userId}
        </if>
        where 1=1
<!--            <if test="status != null">-->
<!--                and pcd.status = #{status}-->
<!--            </if>-->
            <if test="taskId != null">
                and pcd.task_id = #{taskId}
            </if>
    </select>

    <select id="selectPurchaseCheckDetailsListByQueryDto" parameterType="com.bonus.material.purchase.domain.dto.PurchaseQueryDto" resultMap="PurchaseCheckDetailsResult">
        <include refid="selectPurchaseCheckDetailsJoinVo"/>
        <if test="userId != null">
            right join ma_type_keeper mtk ON mtk.type_id = mt.type_id AND mtk.user_id = #{userId}
        </if>
        where pcd.task_id = #{taskId}
        <if test="typeId != null">
            and pcd.type_id = #{typeId}
        </if>
        <if test="supplierId != null">
            and pcd.supplier_id = #{supplierId}
        </if>
        <if test="productionTime != null">
            and pcd.production_time = #{productionTime}
        </if>
        <if test="keyWord != null and keyWord != ''">
            and (mt.type_name like concat('%', #{keyWord}, '%')
            or mt.unit_name like concat('%', #{keyWord}, '%')
            or mtp.type_name like concat('%', #{keyWord}, '%'))
        </if>
    </select>

    <insert id="insertPurchaseCheckDetails" parameterType="com.bonus.material.purchase.domain.PurchaseCheckDetails" useGeneratedKeys="true" keyProperty="id">
        insert into purchase_check_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null">task_id,</if>
            <if test="typeId != null">type_id,</if>
            <if test="purchasePrice != null">purchase_price,</if>
            <if test="purchaseTaxPrice != null">purchase_tax_price,</if>
            <if test="purchaseNum != null">purchase_num,</if>
            <if test="checkNum != null">check_num,</if>
            <if test="bindNum != null">bind_num,</if>
            <if test="checkResult != null">check_result,</if>
            <if test="supplierId != null">supplier_id,</if>
            <if test="status != null">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="productionTime != null">production_time,</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="checkUrlName != null">check_url_name,</if>
            <if test="checkUrl != null">check_url,</if>
            <if test="inputNum != null">input_num,</if>
            <if test="inputStatus != null">input_status,</if>
            <if test="inputTime != null">input_time,</if>
            <if test="fileName != null">file_name,</if>
            <if test="fileUrl != null">file_url,</if>
            <if test="companyId != null">company_id,</if>
            <if test="fixCode != null">fix_code,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskId != null">#{taskId},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="purchasePrice != null">#{purchasePrice},</if>
            <if test="purchaseTaxPrice != null">#{purchaseTaxPrice},</if>
            <if test="purchaseNum != null">#{purchaseNum},</if>
            <if test="checkNum != null">#{checkNum},</if>
            <if test="bindNum != null">#{bindNum},</if>
            <if test="checkResult != null">#{checkResult},</if>
            <if test="supplierId != null">#{supplierId},</if>
            <if test="status != null">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="productionTime != null">#{productionTime},</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="checkUrlName != null">#{checkUrlName},</if>
            <if test="checkUrl != null">#{checkUrl},</if>
            <if test="inputNum != null">#{inputNum},</if>
            <if test="inputStatus != null">#{inputStatus},</if>
            <if test="inputTime != null">#{inputTime},</if>
            <if test="fileName != null">#{fileName},</if>
            <if test="fileUrl != null">#{fileUrl},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="fixCode != null">#{fixCode},</if>
         </trim>
    </insert>

    <update id="updatePurchaseCheckDetails" parameterType="com.bonus.material.purchase.domain.PurchaseCheckDetails">
        update purchase_check_details
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="typeId != null">type_id = #{typeId},</if>
            <if test="purchasePrice != null">purchase_price = #{purchasePrice},</if>
            <if test="purchaseTaxPrice != null">purchase_tax_price = #{purchaseTaxPrice},</if>
            <if test="purchaseNum != null">purchase_num = #{purchaseNum},</if>
            <if test="checkNum != null">check_num = #{checkNum},</if>
            <if test="bindNum != null">bind_num = #{bindNum},</if>
            <if test="checkResult != null">check_result = #{checkResult},</if>
            <if test="supplierId != null">supplier_id = #{supplierId},</if>
            <if test="status != null">`status` = #{status},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="productionTime != null">production_time = #{productionTime},</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="checkUrlName != null">check_url_name = #{checkUrlName},</if>
            <if test="checkUrl != null">check_url = #{checkUrl},</if>
            <if test="inputNum != null">input_num = #{inputNum},</if>
            <if test="inputStatus != null">input_status = #{inputStatus},</if>
            <if test="inputTime != null">input_time = #{inputTime},</if>
            <if test="fileName != null">file_name = #{fileName},</if>
            <if test="fileUrl != null">file_url = #{fileUrl},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="fixCode != null">fix_code = #{fixCode},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePurchaseCheckDetailsById" parameterType="Long">
        delete from purchase_check_details where id = #{id}
    </delete>

    <!-- 限制条件 严格匹配字典表及枚举类中定义的状态码 -->
    <delete id="deletePurchaseCheckDetailsByParentIds" parameterType="Long">
        delete from purchase_check_details where task_id in (
            select task_id
            from purchase_check_info
            where
                id in
                <foreach item="id" collection="array" open="(" separator="," close=")">
                    #{id}
                </foreach>
        ) and status = 1 or status = 12
    </delete>

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

<!--    <select id="selectMaTypeNameByTaskId" 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-->
<!--                1=1-->
<!--                <if test="status != null">-->
<!--                    and pcd.status = #{status}-->
<!--                </if>-->
<!--                <if test="taskId != null">-->
<!--                    and pcd.task_id = #{taskId}-->
<!--                </if>-->
<!--            ) t-->
<!--        GROUP BY task_id-->
<!--    </select>-->

    <select id="selectMaTypeNameByTaskAndStatusList" 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
        1=1
        <if test="statusList != null and statusList.size() > 0">
            and pcd.status in
            <foreach item="item" collection="statusList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="taskId != null">
            and pcd.task_id = #{taskId}
        </if>
        ) t
        GROUP BY task_id
    </select>

    <insert id="insertPurchaseCheckDetailsList">
        insert into purchase_check_details(task_id,type_id,purchase_price,purchase_tax_price,
            purchase_num,check_num,bind_num,check_result,supplier_id,
            status,production_time,create_by,create_time,update_by,
            update_time,remark,check_url_name,check_url,input_num,
            input_status,input_time,file_name,file_url,company_id,fix_code)
        values
        <foreach collection="list" item="item" index="index" separator=",">
                (#{item.taskId},#{item.typeId},#{item.purchasePrice},#{item.purchaseTaxPrice},
             #{item.purchaseNum},#{item.checkNum},#{item.bindNum},#{item.checkResult},#{item.supplierId},
             #{item.status},#{item.productionTime},#{item.createBy},NOW(),#{item.updateBy},
             #{item.updateTime},#{item.remark},#{item.checkUrlName},#{item.checkUrl},#{item.inputNum},
             #{item.inputStatus},#{item.inputTime},#{item.fileName},#{item.fileUrl},#{item.companyId},#{item.fixCode})
        </foreach>
    </insert>

    <!-- 这里的status限制是待通知或验收被驳回后对应的字典值 -->
    <update id="batchUpdateDetailsTaskStatus">
        update
            purchase_check_details
        set
            `status` = #{newTaskStatus}, check_result = #{checkResult}
        where
             task_id = #{taskId}
        and
            `status` = 1 or `status` = 12
    </update>

<!--    <update id="updatePurchaseDetails">-->
<!--        update-->
<!--        purchase_check_details-->
<!--        set-->
<!--        `status` = #{status}, check_result = #{checkResult}, check_num = purchase_num-->
<!--        where-->
<!--        1=1-->
<!--        <if test="taskId != null">-->
<!--            and task_id = #{taskId}-->
<!--        </if>-->
<!--        <if test="typeId != null">-->
<!--            and type_id = #{typeId}-->
<!--        </if>-->
<!--    </update>-->

    <update id="updatePurchaseDetails4Check">
        update
            purchase_check_details
        <set>
            `status` = #{status},check_result = #{checkResult},check_num = purchase_num,check_time = NOW(),
            <if test="warnDocuments != null and warnDocuments != ''">
                warn_documents = #{warnDocuments},
            </if>
            <if test="reason != null and reason != ''">
                reason = #{reason},
            </if>
            <if test="checkUser != null">
                check_user = #{checkUser}
            </if>
        </set>
        <where>
            <if test="taskId != null">
                and task_id = #{taskId}
            </if>
            <if test="typeId != null">
                and type_id = #{typeId}
            </if>
        </where>
    </update>

    <select id="getCountOfNoneThisStatus" resultType="Integer">
        select count(1)
        from
            purchase_check_details
        where
            `status` != #{status}
            and (del_flag = '0' or del_flag is null)
            and task_id = #{taskId}
    </select>
</mapper>
