<?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.boman.web.core.mapper.AccountingDataMapper">

    <resultMap type="com.boman.web.core.domain.vo.AccountingDataVo" id="AccountingDataResult">
        <result property="id" column="id"/>
        <result property="jcsj" column="jcsj"/>
        <result property="lxdh" column="lxdh"/>
        <result property="address" column="address"/>
        <result property="batchNumber" column="batch_number"/>
        <result property="focusPerson" column="focus_person"/>
        <result property="jcjg" column="jcjg"/>
        <result property="fromCity" column="from_city"/>
        <result property="jcdd" column="jcdd"/>
        <result property="cjdd" column="cjdd"/>
        <result property="toCity" column="to_city"/>
        <result property="source" column="source"/>
        <result property="zjhm" column="zjhm"/>
        <result property="cjsj" column="cjsj"/>
        <result property="certificateNoType" column="certificate_no_type"/>
        <result property="xm" column="xm"/>
        <result property="transit" column="transit"/>
        <result property="collectorName" column="collector_name"/>
        <result property="cjssxq" column="cjssxq"/>
        <result property="mainKey" column="main_key"/>
        <result property="modifyDate" column="modify_date"/>
        <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="isDel" column="is_del"/>
        <result property="hjh" column="hjh"/>


        <!--以下是返回是需要使用-->
        <result property="phoneNum" column="phone_num"/>
        <result property="province" column="province"/>
        <result property="city" column="city"/>
        <result property="region" column="region"/>
        <result property="villageTowns" column="village_towns"/>
        <result property="village" column="village"/>
        <result property="nowIn" column="now_in"/>
        <result property="nowInSelect" column="nowInSelect"/>
        <result property="domicileSelect" column="domicileSelect"/>
        <result property="isNucleicAcid" column="isNucleicAcid"/>


        <result property="provinceXjd" column="provinceXjd"/>
        <result property="cityXjd" column="cityXjd"/>
        <result property="regionXjd" column="regionXjd"/>
        <result property="villageTownsXjd" column="villageTownsXjd"/>
        <result property="villageXjd" column="villageXjd"/>
        <result property="nowXjd" column="nowXjd"/>
    </resultMap>

    <sql id="selectAccountingDataVo">
        select d.id,
               d.jcsj,
               d.lxdh,
               d.address,
               d.batch_number,
               d.focus_person,
               d.jcjg,
               d.from_city,
               d.jcdd,
               d.cjdd,
               d.to_city,
               d.source,
               d.zjhm,
               d.cjsj,
               d.certificate_no_type,
               d.xm,
               d.transit,
               d.collector_name,
               d.cjssxq,
               d.main_key,
               d.modify_date,
               d.create_by,
               d.create_time,
               d.update_by,
               d.update_time
        from accounting_data d
    </sql>

    <select id="selectAccountingDataHjList" parameterType="com.boman.web.core.domain.vo.AccountingDataVo"
            resultMap="AccountingDataResult">
        SELECT
        r.id,r.user_name as xm,r.id_card as
        zjhm,r.province,r.city,r.region,r.village_towns,r.village,r.now_in,r.phone_num,d.lxdh,d.jcsj,d.jcdd,d.cjdd,d.cjsj
        FROM czrk r left join `accounting_data` d on r.id_card = d.zjhm
        <where>
            r.is_del = 'N'
            and r.status = '1'
            <if test="zjhm != null  and zjhm != ''">and r.id_card = #{zjhm}</if>
            <if test="xm != null  and xm != ''">and r.user_name like concat(#{xm}, '%')</if>
            <if test="params.startTime != null  and params.startTime != '' and params.startTime != 'null'">and
                DATE_FORMAT(d.cjsj,'%Y-%m-%d %H:%i:%s') &gt;= DATE_FORMAT(#{params.startTime},'%Y-%m-%d %H:%i:%s')
            </if>
            <if test="params.endTime != null  and params.endTime != ''  and params.endTime != 'null'">and
                DATE_FORMAT(d.cjsj,'%Y-%m-%d %H:%i:%s') &lt;= DATE_FORMAT(#{params.endTime},'%Y-%m-%d %H:%i:%s')
            </if>
            <if test="isNucleicAcid != null  and isNucleicAcid != ''">and r.is_nucleicAcid = #{isNucleicAcid}</if>
            <if test="provinceId != null  and provinceId != ''">and r.province_id = #{provinceId}</if>
            <if test="cityId != null  and cityId != ''">and r.city_id = #{cityId}</if>
            <if test="regionId != null  and regionId != ''">and r.region_id = #{regionId}</if>
            <if test="villageTownsId != null  and villageTownsId != ''">and r.village_towns_id = #{villageTownsId}</if>
            <if test="villageId != null  and villageId != ''">and r.village_id = #{villageId}</if>
        </where>
        order by d.cjsj DESC,r.create_time DESC
    </select>

    <select id="selectAccountingDataJzdzList" parameterType="com.boman.web.core.domain.vo.AccountingDataVo"
            resultMap="AccountingDataResult">
        select r.user_name as xm, r.phone_num, r.id_card as zjhm,
        z.province as provinceXjd , z.city as cityXjd,z.region as regionXjd , z.town as villageTownsXjd, z.village as
        villageXjd,z.now_in,d.lxdh,d.jcsj,d.jcdd,d.cjdd,d.cjsj
        from czrk r left join czrk_jzdz z on r.id =z.czrk_id
        left join `accounting_data` d on r.id_card = d.zjhm
        <where>
            r.is_del = 'N'
            and r.status = '1'
            <if test="zjhm != null  and zjhm != ''">and r.id_card = #{zjhm}</if>
            <if test="xm != null  and xm != ''">and r.user_name like concat(#{xm}, '%')</if>
            <if test="params.startTime != null  and params.startTime != '' and params.startTime != 'null'">and
                DATE_FORMAT(d.cjsj,'%Y-%m-%d %H:%i:%s') &gt;= DATE_FORMAT(#{params.startTime},'%Y-%m-%d %H:%i:%s')
            </if>
            <if test="params.endTime != null  and params.endTime != '' and params.endTime != 'null'">and
                DATE_FORMAT(d.cjsj,'%Y-%m-%d %H:%i:%s') &lt;= DATE_FORMAT(#{params.endTime},'%Y-%m-%d %H:%i:%s')
            </if>
            <if test="isNucleicAcid != null  and isNucleicAcid != ''">and r.is_nucleicAcid = #{isNucleicAcid}</if>
            <if test="provinceIdXjd != null and provinceIdXjd != ''">and z.province_id = #{provinceIdXjd}</if>
            <if test="cityIdXjd != null and cityIdXjd != ''">and z.city_id = #{cityIdXjd}</if>
            <if test="regionIdXjd != null and regionIdXjd != ''">and z.region_id = #{regionIdXjd}</if>
            <if test="villageTownsIdXjd != null and villageTownsIdXjd != ''">and z.town_id = #{villageTownsIdXjd}</if>
            <if test="villageIdXjd != null and villageIdXjd != ''">and z.village_id = #{villageIdXjd}</if>
        </where>
        order by d.cjsj DESC,r.create_time DESC
    </select>


    <insert id="insertAccountingData" useGeneratedKeys="true" keyProperty="id">
        insert into accounting_data
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="jcsj != null">jcsj,</if>
            <if test="lxdh != null">lxdh,</if>
            <if test="address != null">address,</if>
            <if test="batchNumber != null">batch_number,</if>
            <if test="focusPerson != null">focus_person,</if>
            <if test="jcjg != null">jcjg,</if>
            <if test="fromCity != null">from_city,</if>
            <if test="jcdd != null">jcdd,</if>
            <if test="cjdd != null">cjdd,</if>
            <if test="toCity != null">to_city,</if>
            <if test="source != null">source,</if>
            <if test="zjhm != null">zjhm,</if>
            <if test="cjsj != null">cjsj,</if>
            <if test="certificateNoType != null">certificate_no_type,</if>
            <if test="xm != null">xm,</if>
            <if test="collectorName != null">collector_name,</if>
            <if test="cjssxq != null">cjssxq,</if>
            <if test="createBy != null">create_by,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="jcsj != null">#{jcsj},</if>
            <if test="lxdh != null">#{lxdh},</if>
            <if test="address != null">#{address},</if>
            <if test="batchNumber != null">#{batchNumber},</if>
            <if test="focusPerson != null">#{focusPerson},</if>
            <if test="jcjg != null">#{jcjg},</if>
            <if test="fromCity != null">#{fromCity},</if>
            <if test="jcdd != null">#{jcdd},</if>
            <if test="cjdd != null">#{cjdd},</if>
            <if test="toCity != null">#{toCity},</if>
            <if test="source != null">#{source},</if>
            <if test="zjhm != null">#{zjhm},</if>
            <if test="cjsj != null">#{cjsj},</if>
            <if test="certificateNoType != null">#{certificateNoType},</if>
            <if test="xm != null">#{xm},</if>
            <if test="collectorName != null">#{collectorName},</if>
            <if test="cjssxq != null">#{cjssxq},</if>
            <if test="createBy != null">#{createBy},</if>
            sysdate()
        </trim>
    </insert>

    <select id="selectById" parameterType="String" resultType="int">
        select count(1)
        from accounting_data
        where id = #{id}
    </select>

    <select id="getExportHj" resultMap="AccountingDataResult">
        SELECT
        r.id,r.user_name as xm,r.id_card as zjhm,r.code as hjh,r.is_nucleicAcid as isNucleicAcid,
        r.province,r.city,r.region,r.village_towns,r.village,r.now_in,r.phone_num,
        d.lxdh,d.jcsj,d.jcdd,d.cjdd,d.cjsj,
        z.province as provinceXjd , z.city as cityXjd,z.region as regionXjd , z.town as villageTownsXjd, z.village as
        villageXjd,z.now_in
        FROM czrk r
        left join czrk_jzdz z on r.id =z.czrk_id
        left join `accounting_data` d on r.id_card = d.zjhm
        <where>
            r.is_del = 'N'
            and r.status = '1'
            <if test="zjhm != null  and zjhm != ''">and r.id_card = #{zjhm}</if>
            <if test="xm != null  and xm != ''">and r.user_name like concat(#{xm}, '%')</if>
            <if test="params.startTime != null  and params.startTime != '' and params.startTime != 'null'">and
                DATE_FORMAT(d.cjsj,'%Y-%m-%d %H:%i:%s') &gt;= DATE_FORMAT(#{params.startTime},'%Y-%m-%d %H:%i:%s')
            </if>
            <if test="params.endTime != null  and params.endTime != ''  and params.endTime != 'null'">and
                DATE_FORMAT(d.cjsj,'%Y-%m-%d %H:%i:%s') &lt;= DATE_FORMAT(#{params.endTime},'%Y-%m-%d %H:%i:%s')
            </if>
            <if test="isNucleicAcid != null  and isNucleicAcid != ''">and r.is_nucleicAcid = #{isNucleicAcid}</if>
            <if test="provinceId != null  and provinceId != ''">and r.province_id = #{provinceId}</if>
            <if test="cityId != null  and cityId != ''">and r.city_id = #{cityId}</if>
            <if test="regionId != null  and regionId != ''">and r.region_id = #{regionId}</if>
            <if test="villageTownsId != null  and villageTownsId != ''">and r.village_towns_id = #{villageTownsId}</if>
            <if test="villageId != null  and villageId != ''">and r.village_id = #{villageId}</if>
        </where>
        order by d.cjsj DESC,r.create_time DESC
    </select>


    <select id="exportCz" resultMap="AccountingDataResult">
        select
        r.id,r.user_name as xm,r.id_card as zjhm,r.code as hjh,r.is_nucleicAcid as isNucleicAcid,
        r.province,r.city,r.region,r.village_towns,r.village,r.now_in,r.phone_num,
        d.lxdh,d.jcsj,d.jcdd,d.cjdd,d.cjsj,
        z.province as provinceXjd , z.city as cityXjd,z.region as regionXjd , z.town as villageTownsXjd, z.village as
        villageXjd,z.now_in
        from czrk r
        left join czrk_jzdz z on r.id =z.czrk_id
        left join `accounting_data` d on r.id_card = d.zjhm
        <where>
            r.is_del = 'N'
            and r.status = '1'
            <if test="zjhm != null  and zjhm != ''">and r.id_card = #{zjhm}</if>
            <if test="xm != null  and xm != ''">and r.user_name like concat(#{xm}, '%')</if>
            <if test="params.startTime != null  and params.startTime != '' and params.startTime != 'null'">and
                DATE_FORMAT(d.cjsj,'%Y-%m-%d %H:%i:%s') &gt;= DATE_FORMAT(#{params.startTime},'%Y-%m-%d %H:%i:%s')
            </if>
            <if test="params.endTime != null  and params.endTime != '' and params.endTime != 'null'">and
                DATE_FORMAT(d.cjsj,'%Y-%m-%d %H:%i:%s') &lt;= DATE_FORMAT(#{params.endTime},'%Y-%m-%d %H:%i:%s')
            </if>
            <if test="isNucleicAcid != null  and isNucleicAcid != ''">and r.is_nucleicAcid = #{isNucleicAcid}</if>
            <if test="provinceIdXjd != null and provinceIdXjd != ''">and z.province_id = #{provinceIdXjd}</if>
            <if test="cityIdXjd != null and cityIdXjd != ''">and z.city_id = #{cityIdXjd}</if>
            <if test="regionIdXjd != null and regionIdXjd != ''">and z.region_id = #{regionIdXjd}</if>
            <if test="villageTownsIdXjd != null and villageTownsIdXjd != ''">and z.town_id = #{villageTownsIdXjd}</if>
            <if test="villageIdXjd != null and villageIdXjd != ''">and z.village_id = #{villageIdXjd}</if>
        </where>
        order by d.cjsj DESC,r.create_time DESC
    </select>

    <select id="getAccountingDataOne" resultMap="AccountingDataResult">
        select id, cjsj, lxdh, create_date, modify_date
        FROM accounting_data
        order by cjsj desc limit 1;
    </select>
    <select id="getAllData" resultMap="AccountingDataResult">
        select id, cjsj, lxdh, create_date, zjhm, xm
        FROM accounting_data
    </select>
    <select id="thirdNucleicAcid" resultMap="AccountingDataResult">
        select a.oid                 as main_key,
               a.`name`              as xm,
               a.id_card             as zjhm,
               a.tel                 as lxdh,
               a.collect_time        as cjsj,
               c.dis_name_str        as cjssxq,
               b.`name`              as cjdd,
               a.verify_finish_time  as jcsj,
               a.verify_result       as jcjg,
               a.create_date         as create_date,
               a.modify_date         as modify_date,
               a.source              as source,
               a.batch_number        as batch_number,
               a.collector_name      as collector_name,
               a.verify_point_name   as jcdd,
               a.certificate_no_type as certificate_no_type,
               a.focus_person        as focus_person,
               a.from_city           as from_city,
               a.to_city             as to_city,
               a.address             as address,
               a.transit             as transit
        from t_sampling_514 a
                 left join t_collect_verify_point b on a.collect_oid = b.oid
                 left join t_sys_district c on b.district_oid = c.oid
        where use_flag = 'Y'
          and type = 'h'
          and (c.dis_name_str like '%潜山%'
            or left (a.id_card,6) in ('340824','340882'))
          and (`a`.`collect_time` > #{startTime}
            AND #{endTime} >= `a`.`collect_time`);
    </select>
</mapper>