<?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.mmc.iuav.user.dao.BackUserAccountDao">
    <resultMap id="backUserAccountResultMap" type="com.mmc.iuav.user.entity.BackUserAccountDO">
        <id property="id" column="id"/>
        <result property="accountNo" column="account_no"/>
        <result property="phoneNum" column="phone_num"/>
        <result property="userName" column="user_name"/>
        <result property="userSex" column="user_sex"/>
        <result property="email" column="email"/>
        <result property="accountStatus" column="account_status"/>
        <result property="provinceCode" column="province_code"/>
        <result property="cityCode" column="city_code"/>
        <result property="districtCode" column="district_code"/>
        <result property="address" column="address"/>
        <result property="remark" column="remark"/>
        <result property="disable" column="disable"/>
        <result property="createTime" column="createTime"/>
        <result property="updateTime" column="update_time"/>
        <association property="roleInfoDO" javaType="com.mmc.iuav.user.entity.RoleInfoDO">
            <id property="id" column="ri_id"/>
            <result property="roleNo" column="role_no"/>
            <result property="roleName" column="role_name"/>
        </association>
    </resultMap>

    <insert id="insertBackUserAccount" parameterType="com.mmc.iuav.user.entity.BackUserAccountDO" keyProperty="id"
            useGeneratedKeys="true">
        insert into back_user_account(account_no, pass_word, phone_num, user_name, user_sex, email, province_code,
                                      city_code, district_code, address, remark, create_time)
        values (#{accountNo}, #{password}, #{phoneNum}, #{userName}, #{userSex}, #{email}, #{provinceCode}, #{cityCode},
                #{districtCode}, #{address}, #{remark}, NOW())
    </insert>

    <update id="updateBackUserAccount">
        update back_user_account
        <set>
            <if test="accountNo != null">
                account_no = #{accountNo},
            </if>
            <if test="phoneNum != null">
                phone_num = #{phoneNum},
            </if>
            <if test="userName != null">
                user_name = #{userName},
            </if>
            <if test="userSex != null">
                user_sex = #{userSex},
            </if>
            <if test="email != null">
                email = #{email},
            </if>
            <if test="accountStatus != null">
                account_status = #{accountStatus},
            </if>
            <if test="provinceCode != null">
                province_code = #{provinceCode},
            </if>
            <if test="cityCode != null">
                city_code = #{cityCode},
            </if>
            <if test="districtCode != null">
                district_code = #{districtCode},
            </if>
            <if test="remark != null">
                remark = #{remark},
            </if>
            <if test="address != null">
                address = #{address}
            </if>
        </set>
        <where>
            id = #{id}
        </where>
    </update>

    <update id="disableBackUserAccountDO">
        update back_user_account
        set disable = 1, account_no = #{accountNo}
        where disable = 0
          and id = #{id}
    </update>

    <update id="updatePassword">
        update back_user_account
        set pass_word = #{passWord}
        where id = #{id}
    </update>

    <select id="getBackUserAccountDO" resultType="com.mmc.iuav.user.entity.BackUserAccountDO">
        select id,
               account_no,
               phone_num,
               user_name,
               user_sex,
               email,
               account_status,
               remark,
               province_code,
               city_code,
               district_code,
               address,
               disable,
               create_time,
               update_time
        from back_user_account
        where id = #{id}
    </select>

    <select id="countListBackUserAccount" resultType="java.lang.Integer"
            parameterType="com.mmc.iuav.user.model.qo.BUserAccountQO">
        select count(*) from back_user_account bua
        inner join company_back_user cbu on cbu.back_user_account_id = bua.id
        left join user_role_rel urr on urr.back_user_account_id = bua.id
        where bua.disable = 0
        <if test=" keyword != null and keyword != '' ">
            and ( bua.account_no like CONCAT("%",#{keyword},"%") or
            bua.user_name like CONCAT("%",#{keyword},"%") )
        </if>
        <if test="provinceCode != null">
            and bua.province_code = #{provinceCode}
        </if>
        <if test="cityCode != null">
            and bua.city_code = #{cityCode}
        </if>
        <if test="districtCode != null">
            and bua.district_code = #{districtCode}
        </if>
        <if test="accountStatus != null">
            and bua.account_status = #{accountStatus}
        </if>
        <if test="roleId != null">
            and urr.role_id = #{roleId}
        </if>
        <if test="companyInfoId != null">
            and cbu.company_info_id = #{companyInfoId}
        </if>
    </select>

    <select id="listBackUserAccount" resultMap="backUserAccountResultMap"
            parameterType="com.mmc.iuav.user.model.qo.BUserAccountQO">
        SELECT
        bua.id,
        bua.account_no,
        bua.phone_num,
        bua.user_name,
        bua.user_sex,
        bua.email,
        bua.account_status,
        bua.province_code,
        bua.city_code,
        bua.district_code,
        bua.address,
        bua.remark,
        bua.create_time,
        bua.update_time,
        ri.id as ri_id,
        ri.role_name,
        ri.role_no
        FROM
        back_user_account bua
        inner join company_back_user cbu on cbu.back_user_account_id = bua.id
        inner join company_info ci on ci.id = cbu.company_info_id
        left join user_role_rel urr on urr.back_user_account_id = bua.id
        left join role_info ri on ri.id = urr.role_id
        WHERE
        bua.disable = 0
        <if test=" keyword != null and keyword != '' ">
            and ( bua.account_no like CONCAT("%",#{keyword},"%") or
            bua.user_name like CONCAT("%",#{keyword},"%") )
        </if>
        <if test="provinceCode != null">
            and bua.province_code = #{provinceCode}
        </if>
        <if test="cityCode != null">
            and bua.city_code = #{cityCode}
        </if>
        <if test="districtCode != null">
            and bua.district_code = #{districtCode}
        </if>
        <if test="accountStatus != null">
            and bua.account_status = #{accountStatus}
        </if>
        <if test="roleId != null">
            and urr.role_id = #{roleId}
        </if>
        <if test="companyInfoId != null">
            and cbu.company_info_id = #{companyInfoId}
        </if>
        order by bua.id desc
        limit #{pageNo}, #{pageSize}
    </select>

    <select id="feignListBackUserAccount" resultMap="backUserAccountResultMap"
            parameterType="com.mmc.iuav.user.model.qo.BUserAccountQO">
        select ua.id, ua.account_no, ua.phone_num, ua.user_name, ua.user_sex, ua.email, ua.account_status, ua.remark,
        ua.disable, ua.create_time, ua.update_time
        from back_user_account ua
        where ua.disable = 0
        <if test=" userIds != null ">
            <foreach collection="userIds" item="id" open="and ua.id in (" close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="keyword != null">
            and (
            user_name like CONCAT("%",#{keyword},"%") or
            phone_num like CONCAT("%",#{keyword},"%") or
            id like CONCAT("%",#{keyword},"%"))
        </if>
        <if test="provinceCode != null">
            and ua.province_code = #{provinceCode}
        </if>
        <if test="cityCode != null">
            and ua.city_code = #{cityCode}
        </if>
        <if test="districtCode != null">
            and ua.district_code = #{districtCode}
        </if>
    </select>

    <select id="getAuthUserPwd" resultType="com.mmc.iuav.user.entity.BackUserAccountDO">
        select id,
               account_no,
               pass_word
        from back_user_account
        where disable = 0
          and id =
              #{id}
    </select>

    <select id="getUserLoginInfo" resultType="com.mmc.iuav.user.entity.BackUserAccountDO">
        select bua.id,
               bua.account_no,
               bua.phone_num,
               bua.pass_word,
               bua.account_status,
               bua.user_name
        from back_user_account bua
        where bua.disable = 0
          and BINARY bua.account_no = #{accountNo}
    </select>

    <select id="countUserAccountNo" resultType="java.lang.Integer">
        select count(*) from back_user_account
        where account_no = #{accountNo} and disable = 0
        <if test="excludeId != null">
            and id != #{excludeId}
        </if>
    </select>

</mapper>
