<?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.ltgk.crew.mapper.DsBoatCrewMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.ltgk.crew.domain.entity.DsBoatCrew">
        <result property="id" column="id"/>
        <result property="waterCode" column="water_code"/>
        <result property="crewName" column="crew_name"/>
        <result property="crewNamePy" column="crew_name_py"/>
        <result property="idCardNo" column="id_card_no"/>
        <result property="idCardPic" column="id_card_pic"/>
        <result property="sex" column="sex"/>
        <result property="phone" column="phone"/>
        <result property="birthday" column="birthday"/>
        <result property="nation" column="nation"/>
        <result property="age" column="age"/>
        <result property="education" column="education"/>
        <result property="residenceProvince" column="residence_province"/>
        <result property="residenceCity" column="residence_city"/>
        <result property="residenceDistrict" column="residence_district"/>
        <result property="residence" column="residence"/>
        <result property="address" column="address"/>
        <result property="certificateType" column="certificate_type"/>
        <result property="certificateTypeName" column="certificate_type_name"/>
        <result property="categoryCode" column="category_code"/>
        <result property="categoryName" column="category_name"/>
        <result property="certificateKindCode" column="certificate_kind_code"/>
        <result property="certificateKindName" column="certificate_kind_name"/>
        <result property="certificateLevel" column="certificate_level"/>
        <result property="certificateLevelName" column="certificate_level_name"/>
        <result property="certificatePost" column="certificate_post"/>
        <result property="certificatePostName" column="certificate_post_name"/>
        <result property="applyBoat" column="apply_boat"/>
        <result property="certificateCode" column="certificate_code"/>
        <result property="boatDutyId" column="boat_duty_id"/>
        <result property="boatDutyName" column="boat_duty_name"/>
        <result property="certificateAuthority" column="certificate_authority"/>
        <result property="certificateAuthorityName" column="certificate_authority_name"/>
        <result property="certificatePeople" column="certificate_people"/>
        <result property="certificateIssuedDate" column="certificate_issued_date"/>
        <result property="certificateExpiredDate" column="certificate_expired_date"/>
        <result property="certificateStatus" column="certificate_status"/>
        <result property="certificatePic" column="certificate_pic"/>
        <result property="archivesCode" column="archives_code"/>
        <result property="manyPositionsFlag" column="many_positions_flag"/>
        <result property="urgentMan" column="urgent_man"/>
        <result property="urgentPhone" column="urgent_phone"/>
        <result property="boatId" column="boat_id"/>
        <result property="boatName" column="boat_name"/>
        <result property="onceBoat" column="once_boat"/>
        <result property="areaCode" column="area_code"/>
        <result property="areaName" column="area_name"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createAt" column="create_at"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateAt" column="update_at"/>
        <result property="remarks" column="remarks"/>
    </resultMap>

    <select id="selectCrewPage" resultType="com.ltgk.crew.domain.vo.DsBoatBindCrewInfoVO">
        select
        crew.id,crew.water_code,crew.crew_name,crew.crew_name_py,crew.id_card_no,crew.id_card_pic,crew.sex,crew.phone,crew.birthday,crew.nation,
        crew.age,crew.education,crew.residence_province,crew.residence_city,crew.residence_district,crew.residence,crew.address,crew.certificate_type,
        crew.certificate_type_name,crew.category_code,crew.category_name,crew.certificate_kind_code,crew.certificate_kind_name,crew.certificate_level,
        crew.certificate_level_name,crew.certificate_post,crew.certificate_post_name,crew.apply_boat,crew.certificate_code,crew.boat_duty_id,crew.boat_duty_name,
        crew.certificate_authority,crew.certificate_authority_name,crew.certificate_people,crew.certificate_issued_date,crew.certificate_expired_date,crew.certificate_status,
        crew.certificate_pic,crew.archives_code,crew.many_positions_flag,crew.urgent_man,crew.urgent_phone,crew.boat_id,crew.boat_name,crew.once_boat,crew.area_code,
        crew.area_name,crew.del_flag,crew.create_by,crew.create_at,crew.update_by,crew.update_at,crew.remarks,
        GROUP_CONCAT(distinct bind.boat_name1) AS boatName1,GROUP_CONCAT(distinct bind.boat_name2) AS boatName2
        from ds_boat_crew crew
        left join ds_boat_bind_crew bind on crew.id=bind.crew_id and bind.del_flag=0
        LEFT JOIN ds_boat_data boat ON boat.boat_name=bind.boat_name1 OR boat.boat_name=bind.boat_name2 AND
        boat.del_flag=0
        <where>
            crew.del_flag=0
            <if test="params.ownerPhone != null and params.ownerPhone != ''">
                AND bind.owner_phone like concat('%',#{params.ownerPhone},'%')
            </if>
            <if test="params.crewPhone != null and params.crewPhone != ''">
                AND bind.crew_phone like concat('%',#{params.crewPhone},'%')
            </if>
            <if test="params.streets != null and params.streets != ''">
                AND boat.street_id in
                <foreach collection="params.streets.split(',')" item="id" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="params.villages != null and params.villages != ''">
                AND boat.village in
                <foreach collection="params.villages.split(',')" item="id" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="params.boatName != null and params.boatName != ''">
                AND crew.boat_name like concat('%',#{params.boatName},'%')
            </if>
            <if test="params.crewName != null and params.crewName != ''">
                AND crew.crew_name like concat('%',#{params.crewName},'%')
            </if>
            <if test="params.boatDutyName != null and params.boatDutyName != ''">
                AND crew.boat_duty_name like concat('%',#{params.boatDutyName},'%')
            </if>
            <if test="params.idCardNo != null and params.idCardNo != ''">
                AND crew.id_card_no like concat('%',#{params.idCardNo},'%')
            </if>
            <if test="params.phone != null and params.phone != ''">
                AND crew.phone like concat('%',#{params.phone},'%')
            </if>
            <if test="params.beginTime != null and params.beginTime != ''">
                <![CDATA[ AND crew.update_at >= #{params.beginTime}]]>
            </if>
            <if test="params.endTime != null and params.endTime != ''">
                <![CDATA[ AND crew.update_at <= #{params.endTime}]]>
            </if>
        </where>
        group by crew.id_card_no
        order by crew.update_at desc
    </select>

    <select id="selectCount" resultType="java.lang.Long">
        select count(id)
        from ds_boat_crew
        where del_flag = 0
    </select>
</mapper>
