<?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.yeejoin.amos.boot.module.hygf.api.mapper.MaintenanceMapper">

    <update id="updateTraining">
    update hygf_maintenance set training_status = '已培训' where foundation_id = (
        select  sequence_nbr from  std_user_biz where amos_user_id = #{userId}
    ) and is_delete = 0
    </update>
    <update id="updateTrainingAgreement">
        update hygf_maintenance set is_sign_an_agreement = 0 where foundation_id = (
            select  sequence_nbr from  std_user_biz where amos_user_id = #{userId}
        ) and is_delete = 0
    </update>


    <select id="selectauthLogininfo" resultType="INT">


    select COUNT(privilege_agency_user.SEQUENCE_NBR ) from  privilege_agency_user where
    privilege_agency_user.MOBILE=#{telephone}
    or privilege_agency_user.USER_NAME=#{telephone}
    or privilege_agency_user.USER_NAME=#{adminLoginName}


</select>
    <select id="selectMaintenance" resultType="INT">
     select COUNT(hygf_maintenance.SEQUENCE_NBR ) from

 hygf_maintenance  LEFT JOIN std_user_biz on hygf_maintenance.foundation_id=std_user_biz.sequence_nbr

 where
    hygf_maintenance.review_status!='驳回'
    and ( std_user_biz.emergency_telephone=#{telephone}
    or std_user_biz.amos_user_name=#{adminLoginName})
</select>


    <select id="Maintenancepage" resultType="com.yeejoin.amos.boot.module.hygf.api.dto.MaintenanceDto">

        select
        ph.*,
        std_user_biz.real_name name,
        std_user_biz.emergency_telephone   telephone,
        std_user_biz.id_type idType,
        std_user_biz.id_number idCard,
        std_user_biz.amos_user_name  adminLoginName,
        std_user_biz.password   adminLoginPwd,
        std_user_biz.amos_user_id    amosUserId,
        CASE WHEN std_user_biz.lock_status  = 'UNLOCK' THEN '否'
        ELSE '是' END AS lockStatus,
        std_user_biz.id_card_front    idCardFront,
        std_user_biz.id_card_opposite    idCardOpposite,
        std_user_biz.amos_org_code   amosDealerCode,
        std_user_biz.amos_id    amosDealerId
        from  hygf_maintenance ph  LEFT JOIN std_user_biz on ph.foundation_id=std_user_biz.sequence_nbr
        LEFT JOIN hygf_unit_info on hygf_unit_info.sequence_nbr = ph.dealer_id
        <where>
            ph.is_delete = 0

            <if test="name != null and name != ''">
                AND std_user_biz.real_name LIKE CONCAT('%', #{name}, '%')
            </if>

            <if test="maintenanceTypeCode != null and maintenanceTypeCode != ''">
                AND ph.maintenance_type_code = #{maintenanceTypeCode}
            </if>
            <if test="maintenanceType != null and maintenanceType != ''">
                AND ph.maintenance_type = #{maintenanceType}
            </if>
            <if test="maintenanceAreaAddress != null and maintenanceAreaAddress != ''">
                AND ph.maintenance_area_address LIKE CONCAT('%', #{maintenanceAreaAddress}, '%')
            </if>
            <if test="lockStatus != null and lockStatus != ''">
                AND std_user_biz.lock_status = #{lockStatus}
            </if>
            <if test="reviewStatus != null and reviewStatus != ''">
                AND ph.review_status = #{reviewStatus}
            </if>
            <if test="regionalCompaniesSeq != null and regionalCompaniesSeq != ''">
                AND ph.regional_companies_seq = #{regionalCompaniesSeq}
            </if>
            <if test="regionalCompaniesCode != null and regionalCompaniesCode != ''">
                AND ph.regional_companies_code = #{regionalCompaniesCode}
            </if>
            <if test="dealerId != null and dealerId != ''">
                AND ph.dealer_id = #{dealerId}
            </if>
            <if test="dealerCode != null and dealerCode != ''">
                AND hygf_unit_info.amos_company_code = #{dealerCode}
            </if>

        </where>
        order by std_user_biz.sequence_nbr desc, ph.rec_date desc
    </select>



    <select id="selectOneById" resultType="com.yeejoin.amos.boot.module.hygf.api.dto.MaintenanceDto">

        select
        ph.*,
        std_user_biz.real_name name,
        std_user_biz.emergency_telephone   telephone,
        std_user_biz.id_type idType,
        std_user_biz.id_number idCard,
        std_user_biz.amos_user_name  adminLoginName,
        std_user_biz.password   adminLoginPwd,
        std_user_biz.amos_user_id    amosUserId,
        std_user_biz.lock_status   lockStatus,
        std_user_biz.id_card_front    idCardFront,
        std_user_biz.id_card_opposite    idCardOpposite,
        std_user_biz.amos_org_code   amosDealerCode,
        std_user_biz.amos_id    amosDealerId
        from  hygf_maintenance ph  LEFT JOIN std_user_biz on ph.foundation_id=std_user_biz.sequence_nbr
        where   ph.is_delete = 0  and  ph.sequence_nbr=#{sequenceNbr}

    </select>


    <select id="getTrainingAgreement" resultType="com.yeejoin.amos.boot.module.hygf.api.dto.MaintenanceDto">

        select
        ph.*,
        std_user_biz.real_name name,
        std_user_biz.emergency_telephone   telephone,
        std_user_biz.id_type idType,
        std_user_biz.id_number idCard,
        std_user_biz.amos_user_name  adminLoginName,
        std_user_biz.password   adminLoginPwd,
        std_user_biz.amos_user_id    amosUserId,
        std_user_biz.lock_status   lockStatus,
        std_user_biz.id_card_front    idCardFront,
        std_user_biz.id_card_opposite    idCardOpposite,
        std_user_biz.amos_org_code   amosDealerCode,
        std_user_biz.amos_id    amosDealerId
        from  hygf_maintenance ph  LEFT JOIN std_user_biz on ph.foundation_id=std_user_biz.sequence_nbr
        where   ph.is_delete = 0  and  std_user_biz.amos_user_id=#{userId}

    </select>


    <select id="queryList" resultType="com.yeejoin.amos.boot.module.hygf.api.dto.MaintenanceDto">
        select
        ph.*,
        std_user_biz.real_name name,
        std_user_biz.emergency_telephone   telephone,
        std_user_biz.id_type idType,
        std_user_biz.id_number idCard,
        std_user_biz.amos_user_name  adminLoginName,
        std_user_biz.password   adminLoginPwd,
        std_user_biz.amos_user_id    amosUserId,
        CASE WHEN std_user_biz.lock_status  = 'UNLOCK' THEN '否'
        ELSE '是' END AS lockStatus,
        std_user_biz.id_card_front    idCardFront,
        std_user_biz.id_card_opposite    idCardOpposite,
        std_user_biz.amos_org_code   amosDealerCode,
        std_user_biz.amos_id    amosDealerId

        from  hygf_maintenance ph  LEFT JOIN std_user_biz on ph.foundation_id=std_user_biz.sequence_nbr

        <where>
            ph.is_delete = 0 and ph.review_status='通过' and  ph.training_status='已培训'  and  ph.is_sign_an_agreement=0
            and std_user_biz.lock_status = 'UNLOCK'

            <if test="maintenanceType != null and maintenanceType != ''">
                AND ph.maintenance_type = #{maintenanceType}
            </if>
            <if test="name != null and name != ''">
                and 	(
                     std_user_biz.real_name LIKE CONCAT('%', #{name}, '%')
                or   ph.maintenance_area like CONCAT('%', #{name}, '%')
                or   ph.maintenance_area_address like CONCAT('%', #{name}, '%')
                or   ph.permanent_address_detail like CONCAT('%', #{name}, '%')
                or   ph.permanent_address_name like CONCAT('%', #{name}, '%'))
            </if>

        </where>
    </select>



</mapper>
