<?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.mapper.check.CheckInfoMapper">

    <select id="getPage" resultType="java.util.Map">
        select cd.id,
               cd.check_id,
               m.technology_type,
               m.code  materialCode,
               m.name  materialName,
               sh.name houseName,
               cd.stock_count,
               cd.stock_quantity,
               cd.stock_house_id,
               cd.material_id
        from check_detail cd
                 left join check_info ci on ci.id = cd.check_id
                 left join material m on cd.material_id = m.id
                 left join stock_house sh on cd.stock_house_id = sh.id
            ${ew.customSqlSegment}
    </select>

    <select id="monthlyStatistics" resultType="java.util.LinkedHashMap">
        select ifnull(sum(if(ci.type = 1, 1, 0)), 0)          automaticCheckCount,
               ifnull(sum(if(ci.type = 2, 1, 0)), 0)          userCheckCount,
               ifnull(sum(distinct ci.oper_user_id), 0)       checkUserCount,
               ifnull(sum(if(ct.tag_status = 0, 1, 0)), 0)    normalTagCount,
               ifnull(sum(if(ct.tag_status &gt; 0, 1, 0)), 0) errorTagCount,
               (select count(distinct cd2.material_id)
                from check_info ci2
                         inner join check_detail cd2 ON ci2.id = cd2.check_id
                where cd2.check_result = 1)                   normalMaterialCount,
               (select count(distinct cd2.material_id)
                from check_info ci2
                         inner join check_detail cd2 ON ci2.id = cd2.check_id
                where cd2.check_result = 0)                   errorMaterialCount,
               ifnull(sum(wt.price * wt.quantity), 0)         money
        from check_info ci
                 left join check_detail cd on ci.id = cd.check_id
                 left join check_tag ct on cd.id = ct.check_detail_id
                 left join water_tag wt on ct.water_tag_id = wt.id
            ${ew.customSqlSegment}
    </select>

    <select id="houseCount" resultType="java.util.Map">
        select sh.id               houseId,
               sh.name             houseName,
               ifnull(ci.count, 0) count
        from stock_house sh
                 left join (
            select ci.stock_house_id,
                   count(0) count
            from check_info ci
                ${ew.customSqlSegment}
        ) ci on sh.id = ci.stock_house_id
    </select>

    <select id="webPage" resultType="java.util.LinkedHashMap">
        select ci.id,
               ci.create_time,
               ci.begin_time,
               ci.type,
               sh.name                               houseName,
               if(ci.check_result = 1, 3, ci.status) status,
               ci.people_check_result,
               ci.end_time,
               ci.oper_user_id
        from check_info ci
                 left join stock_house sh on sh.id = ci.stock_house_id
            ${ew.customSqlSegment}
    </select>

    <select id="errorTagTechnologyTypeCount" resultType="java.util.Map">
        select m.technology_type,
               count(0) count
        from check_info ci
                 inner join check_detail cd on ci.id = cd.check_id
                 inner join check_tag ct on cd.id = ct.check_detail_id
                 inner join water_tag wt on ct.water_tag_id = wt.id
                 inner join material m on wt.material_id = m.id
            ${ew.customSqlSegment}
    </select>

    <select id="errorTagPurposeCount" resultType="java.util.Map">
        select m.purpose,
               count(0) count
        from check_info ci
                 inner join check_detail cd on ci.id = cd.check_id
                 inner join check_tag ct on cd.id = ct.check_detail_id
                 inner join water_tag wt on ct.water_tag_id = wt.id
                 inner join material m on wt.material_id = m.id
            ${ew.customSqlSegment}
    </select>

    <select id="errorTagHandleTypeCount" resultType="java.util.Map">
        select ct.handle_type,
               count(0) count
        from check_info ci
                 inner join check_detail cd on ci.id = cd.check_id
                 inner join check_tag ct on cd.id = ct.check_detail_id
                 inner join water_tag wt on ct.water_tag_id = wt.id
                 inner join material m on wt.material_id = m.id
            ${ew.customSqlSegment}
    </select>
    <select id="errorTagPage" resultType="java.util.LinkedHashMap">
        select sh.name                                                     houseName,
               m.code                                                      materialCode,
               m.name                                                      materialName,
               m.purpose,
               wt.rfid_code,
               wt.quantity,
               ct.tag_status,
               ct.handle_type,
               if(ct.handle_type = 11, wt.quantity * wt.price,
                  if(ct.handle_type = 21, - wt.quantity * wt.price, null)) money,
               ci.create_time,
               ci.oper_user_id
        from check_info ci
                 inner join stock_house sh on ci.stock_house_id = sh.id
                 inner join check_detail cd on ci.id = cd.check_id
                 inner join check_tag ct on cd.id = ct.check_detail_id
                 inner join water_tag wt on ct.water_tag_id = wt.id
                 inner join material m on wt.material_id = m.id
            ${ew.customSqlSegment}
    </select>

    <select id="errorMaterialPurpose" resultType="java.lang.String">
        select distinct m.purpose
        from check_info ci
                 inner join check_detail cd on ci.id = cd.check_id
                 inner join material m on cd.material_id = m.id
            ${ew.customSqlSegment}
    </select>

    <select id="errorMaterialPage" resultType="java.util.LinkedHashMap">
        select t.id   materialId,
               t.technology_type,
               t.purpose,
               t.code materialCode,
               t.name materialName,
               cd2.stock_count,
               cd2.scan_count,
               cd2.check_count,
               cd2.hand_count,
               ci2.create_time,
               ci2.oper_user_id,
               cd2.id
        from (
                 select m.id,
                        m.technology_type,
                        m.purpose,
                        m.code,
                        m.name,
                        max(cd.id) maxDetailId
                 from check_info ci
                          inner join check_detail cd on ci.id = cd.check_id
                          inner join material m on cd.material_id = m.id
                     ${ew.customSqlSegment}
             ) t
                 left join check_detail cd2 on cd2.id = t.maxDetailId
                 left join check_info ci2 on ci2.old_id = cd2.check_id
        where cd2.check_result = 0
    </select>

    <select id="errorMaterialTagPage" resultType="java.util.Map">
        select sh.name                                                    houseName,
               m.code                                                     materialCode,
               m.name                                                     materialName,
               m.purpose,
               ct.rfid_code,
               ct.quantity,
               ct.tag_status,
               ct.handle_type,
               if(ct.handle_type = 11, ct.quantity * m.price,
                  if(ct.handle_type = 21, - ct.quantity * m.price, null)) money,
               ci.create_time,
               ci.oper_user_id
        from check_detail cd
                 left join check_info ci on cd.check_id = ci.id
                 left join check_tag ct on cd.id = ct.check_detail_id
                 left join material m on cd.material_id = m.id
                 left join stock_house sh on cd.stock_house_id = sh.id
            ${ew.customSqlSegment}
    </select>

    <select id="materialDetailPage" resultType="java.util.Map">
        select m.id   materialId,
               m.technology_type,
               m.purpose,
               m.code materialCode,
               m.name materialName,
               cd.stock_count,
               cd.scan_count,
               cd.check_count,
               cd.hand_count,
               cd.check_result,
               ci.create_time,
               ci.oper_user_id,
               cd.id
        from check_info ci
                 left join check_detail cd on cd.check_id = ci.id
                 left join material m on cd.material_id = m.id
            ${ew.customSqlSegment}
    </select>
    <select id="checkResultStatistics" resultType="java.util.Map">
        select ifnull(sum(if(cd.check_result = 1, 1, 0)), 0) normalCount,
               ifnull(sum(if(cd.check_result = 0, 1, 0)), 0) errorCount
        from check_info ci
                 left join check_detail cd on cd.check_id = ci.id
                 left join material m on cd.material_id = m.id
            ${ew.customSqlSegment}
    </select>

</mapper>
