<?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.fjhx.victoriatourist.mapper.quality.QualityInfoMapper">
    <select id="getPage" resultType="com.fjhx.victoriatourist.entity.quality.vo.QualityInfoVo">
        select
            qi.id,
            qi.type,
            qi.`code`,
            qi.bussiness_id,
            qi.bussiness_code,
            qi.warehouse_id,
            qi.purchase_id,
            qi.purchase_code,
            qi.`status`,
            qi.result_type,
            qi.create_user,
            qi.create_time,
            qi.update_user,
            qi.update_time
        from quality_info qi
            ${ew.customSqlSegment}
    </select>
    <select id="getPage1" resultType="com.fjhx.victoriatourist.entity.quality.vo.QualityInfoVo">
        SELECT
            asr.id,
            p.`code` purchaseCode,
            p.supply_id,
            dg.id deliverGoodsId,
            json_unquote( p.victoriatourist_json -> '$.contractCode' ) contractCode,
            lis.logistics_company_code,
            lis.`code` AS logisticsCode,
            p.create_user purchaseUserId,
            p.create_time purchaseTime,
            asr.quality_status
        FROM
            bytesailing_wms.arrival_stock_records asr
                JOIN bytesailing_wms.arrival_stock_records_details asrd ON asrd.arrival_stock_records_id = asr.id AND asrd.del_flag = 0
                LEFT JOIN bytesailing_item.product_info pi ON asrd.product_info_id = pi.id and pi.del_flag = 0
                JOIN bytesailing_wms.stock_wait sw ON asr.stock_wait_id = sw.id
                JOIN bytesailing_purchase.purchase p ON sw.business_id = p.id
                JOIN deliver_goods dg ON dg.id = json_unquote(sw.victoriatourist_json -> '$.deliverGoodsId')
                LEFT JOIN bytesailing_victoriatourist.logistics_infos lis ON lis.business_id = dg.id
            ${ew.customSqlSegment}
    </select>
    <select id="getQualityRecordByPurchaseId" resultType="com.fjhx.victoriatourist.entity.quality.vo.QualityInfoVo">
        SELECT
            qi.id,
            qi.create_time,
            qi.create_user,
            IF( sum( qd.disqualification_quantity )> 0, 1, 0 ) isReject
        FROM
            quality_details qd
                JOIN quality_info qi ON qd.quality_info_id = qi.id
                JOIN bytesailing_wms.arrival_stock_records asr ON qi.bussiness_id = asr.id
                AND asr.del_flag = 0
                JOIN bytesailing_wms.stock_wait sw ON asr.stock_wait_id = sw.id
                AND sw.del_flag = 0
        WHERE
            sw.business_id = #{purchaseId}
        GROUP BY
            qi.id
    </select>

</mapper>
