<?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.huimv.admin.mapper.BillPersonnelAdmissionMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.huimv.admin.entity.BillPersonnelAdmission">
        <id column="id" property="id"/>
        <result column="admission_type" property="admissionType"/>
        <result column="vistit_type" property="vistitType"/>
        <result column="dest_name" property="destName"/>
        <result column="dest_id" property="destId"/>
        <result column="admission_user_name" property="admissionUserName"/>
        <result column="admission_user_id" property="admissionUserId"/>
        <result column="vistit_date" property="vistitDate"/>
        <result column="sub_date" property="subDate"/>
        <result column="phone" property="phone"/>
        <result column="work_name" property="workName"/>
        <result column="papers_type" property="papersType"/>
        <result column="papers_code" property="papersCode"/>
        <result column="img_url" property="imgUrl"/>
        <result column="vistit_reson" property="vistitReson"/>
        <result column="farm_id" property="farmId"/>
        <result column="bill_status" property="billStatus"/>
        <result column="pass_date" property="passDate"/>
        <result column="pass_user_name" property="passUserName"/>
        <result column="pass_user_id" property="passUserId"/>
        <result column="process_id" property="processId"/>
    </resultMap>
    <select id="listAll" resultType="com.huimv.admin.entity.vo.FactoryDirectorVo">
        SELECT count(*) count,id,admission_user_name AS 'userName', bill_status, isolate_location, isolate_start_date AS check_date, isolate_start_date AS 'startTime', isolate_end_date AS 'endTime', NULL AS test_location, NULL AS vistit_type, NULL AS dest_name, NULL AS vistit_date,3 AS 'type',sub_date
FROM bill_isolate WHERE farm_id = #{farmId} AND vistit_type = #{type}

UNION ALL

SELECT count(*) count,id,admission_user_name AS 'userName', bill_status, test_location, check_date, NULL, NULL, NULL, vistit_type, NULL, NULL,2 AS 'type',sub_date
FROM bill_clean WHERE farm_id = #{farmId} AND vistit_type = #{type}

UNION ALL

SELECT count(*) count, id,admission_user_name AS 'userName', bill_status, test_location, check_date, NULL, NULL, NULL, vistit_type, NULL, NULL,1 AS 'type',sub_date
FROM bill_pcr WHERE farm_id = #{farmId} AND vistit_type = #{type}

UNION ALL

SELECT count(*) count, id,admission_user_name AS 'userName', bill_status, NULL, NULL, NULL, NULL, NULL, vistit_type, dest_name, vistit_date,0 AS 'type',sub_date
FROM bill_personnel_admission WHERE farm_id = #{farmId} AND vistit_type = #{type}
ORDER BY sub_date DESC
    </select>


    <select id="listCarAll" resultType="com.huimv.admin.entity.vo.FactoryDirectorVo">
SELECT id,admission_user_name AS 'userName',car_num, bill_status, isolate_location, isolate_start_date AS check_date, isolate_start_date AS 'startTime', isolate_end_date AS 'endTime', NULL AS test_location, NULL AS vistit_type, NULL AS dest_name, NULL AS vistit_date,3 AS 'type',sub_date
FROM bill_isolate WHERE farm_id = #{farmId} AND vistit_type = #{type}

UNION ALL

SELECT id,admission_user_name AS 'userName',car_num, bill_status, test_location, check_date, NULL, NULL, NULL, vistit_type, NULL, NULL,4 AS 'type',sub_date
FROM bill_dry WHERE farm_id = #{farmId} AND vistit_type = #{type} AND img_status = 1

UNION ALL

SELECT id,admission_user_name AS 'userName',car_num, bill_status, test_location, check_date, NULL, NULL, NULL, vistit_type, NULL, NULL,2 AS 'type',sub_date
FROM bill_clean WHERE farm_id = #{farmId} AND vistit_type = #{type} AND img_status = 1

UNION ALL

SELECT id,admission_user_name AS 'userName',car_num, bill_status, test_location, check_date, NULL, NULL, NULL, vistit_type, NULL, NULL,1 AS 'type',sub_date
FROM bill_pcr WHERE farm_id = #{farmId} AND vistit_type = #{type}

UNION ALL

SELECT id,admission_user_name AS 'userName',car_num, bill_status, NULL, NULL, NULL, NULL, NULL, vistit_type, dest_name, vistit_date,0 AS 'type',sub_date
FROM bill_personnel_admission WHERE farm_id = #{farmId} AND vistit_type = #{type}
ORDER BY sub_date DESC
    </select>

    <select id="countAdmission" resultType="com.huimv.admin.entity.vo.ListAdmissionVo">
        select count(*) count, vistit_type type from bill_personnel_admission
        ${ew.customSqlSegment}
        group by vistit_type
    </select>
    <select id="admissionTail" resultType="com.huimv.admin.entity.vo.BillAdmissionVo">
SELECT
    DATE(sub_date) as dateTime,
    SUM(CASE WHEN bill_status = 0 THEN 1 ELSE 0 END) AS commitCount,
    SUM(CASE WHEN bill_status = 1 THEN 1 ELSE 0 END) AS passCount,
    SUM(CASE WHEN bill_status = 2 THEN 1 ELSE 0 END) AS refuseCount
FROM
    `bill_personnel_admission`

    ${ew.customSqlSegment}
GROUP BY
    DATE(sub_date)
ORDER BY
    DATE(sub_date)
    </select>

</mapper>
