<?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.ruoyi.mall.mapper.MallAppCompanyUserMapper">

    <resultMap type="MallAppCompanyUser" id="MallAppCompanyUserResult">
        <result property="companyUserId"    column="company_user_id"    />
        <result property="companyId"    column="company_id"    />
        <result property="sysUserId"    column="sys_user_id"    />
        <result property="roleId"    column="role_id"    />
        <result property="companyUserType"    column="company_user_type"    />
        <result property="companyUserPortrait"    column="company_user_portrait"    />
        <result property="companyUserName"    column="company_user_name"    />
        <result property="companyUserPhone"    column="company_user_phone"    />
        <result property="companyUserAccount"    column="company_user_account"    />
        <result property="companyUserPassword"    column="company_user_password"    />
        <result property="provinceId"    column="province_id"    />
        <result property="provinceName"    column="province_name"    />
        <result property="cityId"    column="city_id"    />
        <result property="cityName"    column="city_name"    />
        <result property="areaId"    column="area_id"    />
        <result property="areaName"    column="area_name"    />
        <result property="address"    column="address"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
        <result property="companyUserStatus"    column="company_user_status"    />
        <result property="createAt"    column="create_at"    />
        <result property="updateAt"    column="update_at"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectMallAppCompanyUserVo">
        select company_user_id, company_id, sys_user_id, role_id, company_user_type, company_user_portrait, company_user_name, company_user_phone, company_user_account, company_user_password, province_id, province_name, city_id, city_name, area_id, area_name, address, longitude, latitude, company_user_status, create_at, update_at, del_flag from mall_app_company_user
    </sql>

    <select id="selectMallAppCompanyUserList" parameterType="MallAppCompanyUser" resultMap="MallAppCompanyUserResult">
        select macu.company_user_id, macu.company_id, macu.sys_user_id, macu.role_id, macu.company_user_type, su.avatar company_user_portrait, macu.company_user_name, macu.company_user_phone, macu.company_user_account, macu.company_user_password,
               macu.province_id, macu.province_name, macu.city_id, macu.city_name, macu.area_id, macu.area_name, macu.address, macu.longitude, macu.latitude,
               macu.company_user_status, macu.create_at, macu.update_at, macu.del_flag
        from mall_app_company_user macu
        left join sys_user su on macu.sys_user_id = su.user_id
        <where>
            <if test="companyId != null "> and macu.company_id = #{companyId}</if>
            <if test="sysUserId != null "> and macu.sys_user_id = #{sysUserId}</if>
            <if test="roleId != null "> and macu.role_id = #{roleId}</if>
            <if test="companyUserType != null "> and macu.company_user_type = #{companyUserType}</if>
            <if test="companyUserName != null  and companyUserName != ''"> and macu.company_user_name like concat('%', #{companyUserName}, '%')</if>
            <if test="companyUserPhone != null  and companyUserPhone != ''"> and macu.company_user_phone like concat('%', #{companyUserPhone}, '%')</if>
            <if test="companyUserAccount != null  and companyUserAccount != ''"> and macu.company_user_account = #{companyUserAccount}</if>
            <if test="companyUserPassword != null  and companyUserPassword != ''"> and macu.company_user_password = #{companyUserPassword}</if>
            <if test="provinceId != null "> and macu.province_id = #{provinceId}</if>
            <if test="provinceName != null  and provinceName != ''"> and macu.province_name like concat('%', #{provinceName}, '%')</if>
            <if test="cityId != null "> and macu.city_id = #{cityId}</if>
            <if test="cityName != null  and cityName != ''"> and macu.city_name like concat('%', #{cityName}, '%')</if>
            <if test="areaId != null "> and macu.area_id = #{areaId}</if>
            <if test="areaName != null  and areaName != ''"> and macu.area_name like concat('%', #{areaName}, '%')</if>
            <if test="address != null  and address != ''"> and macu.address = #{address}</if>
            <if test="longitude != null  and longitude != ''"> and macu.longitude = #{longitude}</if>
            <if test="latitude != null  and latitude != ''"> and macu.latitude = #{latitude}</if>
            <if test="companyUserStatus != null "> and macu.company_user_status = #{companyUserStatus}</if>
            <if test="createAt != null  and createAt != ''"> and macu.create_at = #{createAt}</if>
            <if test="updateAt != null  and updateAt != ''"> and macu.update_at = #{updateAt}</if>
            <if test="delFlag != null "> and macu.del_flag = #{delFlag}</if>
        </where>
        order by macu.create_at desc
    </select>

    <select id="selectMallAppCompanyUserByCompanyUserId" parameterType="Long" resultMap="MallAppCompanyUserResult">
        <include refid="selectMallAppCompanyUserVo"/>
        where company_user_id = #{companyUserId}
    </select>

    <insert id="insertMallAppCompanyUser" parameterType="MallAppCompanyUser" useGeneratedKeys="true" keyProperty="companyUserId">
        insert into mall_app_company_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyId != null">company_id,</if>
            <if test="sysUserId != null">sys_user_id,</if>
            <if test="roleId != null">role_id,</if>
            <if test="companyUserType != null">company_user_type,</if>
            <if test="companyUserPortrait != null">company_user_portrait,</if>
            <if test="companyUserName != null">company_user_name,</if>
            <if test="companyUserPhone != null">company_user_phone,</if>
            <if test="companyUserAccount != null">company_user_account,</if>
            <if test="companyUserPassword != null">company_user_password,</if>
            <if test="provinceId != null">province_id,</if>
            <if test="provinceName != null">province_name,</if>
            <if test="cityId != null">city_id,</if>
            <if test="cityName != null">city_name,</if>
            <if test="areaId != null">area_id,</if>
            <if test="areaName != null">area_name,</if>
            <if test="address != null">address,</if>
            <if test="longitude != null">longitude,</if>
            <if test="latitude != null">latitude,</if>
            <if test="companyUserStatus != null">company_user_status,</if>
            <if test="createAt != null">create_at,</if>
            <if test="updateAt != null">update_at,</if>
            <if test="delFlag != null">del_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="companyId != null">#{companyId},</if>
            <if test="sysUserId != null">#{sysUserId},</if>
            <if test="roleId != null">#{roleId},</if>
            <if test="companyUserType != null">#{companyUserType},</if>
            <if test="companyUserPortrait != null">#{companyUserPortrait},</if>
            <if test="companyUserName != null">#{companyUserName},</if>
            <if test="companyUserPhone != null">#{companyUserPhone},</if>
            <if test="companyUserAccount != null">#{companyUserAccount},</if>
            <if test="companyUserPassword != null">#{companyUserPassword},</if>
            <if test="provinceId != null">#{provinceId},</if>
            <if test="provinceName != null">#{provinceName},</if>
            <if test="cityId != null">#{cityId},</if>
            <if test="cityName != null">#{cityName},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="areaName != null">#{areaName},</if>
            <if test="address != null">#{address},</if>
            <if test="longitude != null">#{longitude},</if>
            <if test="latitude != null">#{latitude},</if>
            <if test="companyUserStatus != null">#{companyUserStatus},</if>
            <if test="createAt != null">#{createAt},</if>
            <if test="updateAt != null">#{updateAt},</if>
            <if test="delFlag != null">#{delFlag},</if>
         </trim>
    </insert>

    <update id="updateMallAppCompanyUser" parameterType="MallAppCompanyUser">
        update mall_app_company_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="sysUserId != null">sys_user_id = #{sysUserId},</if>
            <if test="roleId != null">role_id = #{roleId},</if>
            <if test="companyUserType != null">company_user_type = #{companyUserType},</if>
            <if test="companyUserPortrait != null">company_user_portrait = #{companyUserPortrait},</if>
            <if test="companyUserName != null">company_user_name = #{companyUserName},</if>
            <if test="companyUserPhone != null">company_user_phone = #{companyUserPhone},</if>
            <if test="companyUserAccount != null">company_user_account = #{companyUserAccount},</if>
            <if test="companyUserPassword != null">company_user_password = #{companyUserPassword},</if>
            <if test="provinceId != null">province_id = #{provinceId},</if>
            <if test="provinceName != null">province_name = #{provinceName},</if>
            <if test="cityId != null">city_id = #{cityId},</if>
            <if test="cityName != null">city_name = #{cityName},</if>
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="areaName != null">area_name = #{areaName},</if>
            <if test="address != null">address = #{address},</if>
            <if test="longitude != null">longitude = #{longitude},</if>
            <if test="latitude != null">latitude = #{latitude},</if>
            <if test="companyUserStatus != null">company_user_status = #{companyUserStatus},</if>
            <if test="createAt != null">create_at = #{createAt},</if>
            <if test="updateAt != null">update_at = #{updateAt},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where company_user_id = #{companyUserId}
    </update>

    <delete id="deleteMallAppCompanyUserByCompanyUserId" parameterType="Long">
        delete from mall_app_company_user where company_user_id = #{companyUserId}
    </delete>

    <delete id="deleteMallAppCompanyUserByCompanyUserIds" parameterType="String">
        delete from mall_app_company_user where company_user_id in
        <foreach item="companyUserId" collection="array" open="(" separator="," close=")">
            #{companyUserId}
        </foreach>
    </delete>

    <select id="selectMallAppCompanyUserByCompanyUserName" parameterType="String" resultMap="MallAppCompanyUserResult">
        <include refid="selectMallAppCompanyUserVo"/>
        where company_user_name = #{companyUserName}
    </select>

    <select id="selectMallAppCompanyUserByCompanyUserAccount" parameterType="String" resultMap="MallAppCompanyUserResult">
        <include refid="selectMallAppCompanyUserVo"/>
        where company_user_account = #{companyUserAccount}
    </select>

    <select id="selectMallAppCompanyUserBySysUserId" parameterType="Long" resultMap="MallAppCompanyUserResult">
        select ms.id company_user_id, ms.company_id, ms.sys_user_id, ms.role_id, ms.company_user_type,
               su.avatar company_user_portrait,
               ms.name company_user_name, ms.phone company_user_phone, ms.phone company_user_account, ms.real_password company_user_password,
               ms.status company_user_status, ms.create_at, ms.update_at, ms.del_flag
        from mall_salesman ms
                 left join sys_user su on ms.sys_user_id = su.user_id
        where ms.sys_user_id = #{sysUserId}
    </select>

    <select id="selectMallSalesmanList" parameterType="MallAppCompanyUser" resultMap="MallAppCompanyUserResult">
        select ms.id company_user_id, ms.company_id, ms.sys_user_id, ms.role_id, ms.company_user_type,
               su.avatar company_user_portrait,
               ms.name company_user_name, ms.phone company_user_phone, ms.phone company_user_account, ms.real_password company_user_password,
        ms.status company_user_status, ms.create_at, ms.update_at, ms.del_flag
        from mall_salesman ms
        left join sys_user su on ms.sys_user_id = su.user_id
        <where>
            <if test="companyId != null "> and ms.company_id = #{companyId}</if>
            <if test="sysUserId != null "> and ms.sys_user_id = #{sysUserId}</if>
            <if test="roleId != null "> and ms.role_id = #{roleId}</if>
            <if test="companyUserType != null "> and ms.company_user_type = #{companyUserType}</if>
            <if test="companyUserName != null  and companyUserName != ''"> and ms.name like concat('%', #{companyUserName}, '%')</if>
            <if test="companyUserPhone != null  and companyUserPhone != ''"> and ms.phone like concat('%', #{companyUserPhone}, '%')</if>
            <if test="companyUserAccount != null  and companyUserAccount != ''"> and ms.phone = #{companyUserAccount}</if>
            <if test="companyUserPassword != null  and companyUserPassword != ''"> and ms.real_password = #{companyUserPassword}</if>
            <if test="companyUserStatus != null "> and ms.status = #{companyUserStatus}</if>
            <if test="createAt != null  and createAt != ''"> and ms.create_at = #{createAt}</if>
            <if test="updateAt != null  and updateAt != ''"> and ms.update_at = #{updateAt}</if>
            and ms.del_flag = 0
        </where>
        order by ms.create_at desc
    </select>
</mapper>
