<?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.xhpc.user.mapper.XhpcOperatorMapper">

    <resultMap type="com.xhpc.user.domain.XhpcOperator" id="XhpcOperatorResult">
        <result column="operator_id" property="operatorId"/>
        <result column="name" property="name"/>
        <result column="contact_name" property="contactName"/>
        <result column="contact_phone" property="contactPhone"/>
        <result column="phone" property="phone"/>
        <result column="attribute" property="attribute"/>
        <result column="duty_paragraph" property="dutyParagraph"/>
        <result column="open_bank" property="openBank"/>
        <result column="card_number" property="cardNumber"/>
        <result column="area_code" property="areaCode"/>
        <result column="address" property="address"/>
        <result column="detailed_address" property="detailedAddress"/>
        <result column="longitude" property="longitude"/>
        <result column="latitude" property="latitude"/>
        <result column="email" property="email"/>
        <result column="commission_type" property="commissionType"/>
        <result column="platform_commission_rate" property="platformCommissionRate"/>
        <result column="maintenance_commission_rate" property="maintenanceCommissionRate"/>
        <result column="business_license_id" property="businessLicenseId"/>
        <result column="withdrawal_time" property="withdrawalTime"/>
        <result column="soc" property="soc"/>
        <result column="status" property="status"/>
        <result column="del_flag" property="delFlag"/>
        <result column="create_time" property="createTime"/>
        <result column="create_by" property="createBy"/>
        <result column="update_time" property="updateTime"/>
        <result column="update_by" property="updateBy"/>
        <result column="remark" property="remark"/>
        <result column="operator_id_evcs" property="operatorIdEvcs"/>
    </resultMap>

    <sql id="Base_Column_List">
                xo.operator_id operatorId, xo.name, xo.contact_name contactName,  xo.contact_phone contactPhone,
                xo.phone, xo.attribute,xo.open_bank openBank,xo.card_number cardNumber, xo.area_code areaCode, xo.address,
                xo.detailed_address detailedAddress,   xo.duty_paragraph dutyParagraph,
                xo.longitude, xo.latitude, xo.email,  xo.commission_type commissionType,
                xo.platform_commission_rate platformCommissionRate,
                xo.maintenance_commission_rate maintenanceCommissionRate,
                xo.business_license_id businessLicenseId,  xo.withdrawal_time withdrawalTime, xo.soc,
                xo.status, xo.del_flag delFlag, xo.create_time createTime,
                xo.create_by createBy, xo.update_time updateTime,  xo.update_by updateBy, xo.remark,
    </sql>

    <insert id="insert" parameterType="com.xhpc.user.domain.XhpcOperator" useGeneratedKeys="true"
            keyProperty="operatorId">
        insert into xhpc_operator
        <trim prefix="(" suffix=")" suffixOverrides=",">
        <if test="null != name and '' != name">
            name,
        </if>
        <if test="null != contactName and '' != contactName">
            contact_name,
        </if>
        <if test="null != contactPhone and '' != contactPhone">
            contact_phone,
        </if>
        <if test="null != phone and '' != phone">
            phone,
        </if>
        <if test="null != attribute and '' != attribute">
            attribute,
        </if>
            <if test="null != dutyParagraph and '' != dutyParagraph">
                duty_paragraph,
            </if>
            <if test="null != detailedAddress and '' != detailedAddress">
                detailed_address,
            </if>
            <if test="null != openBank and '' != openBank">
                open_bank,
            </if>
            <if test="null != cardNumber and '' != cardNumber">
                card_number,
            </if>
        <if test="null != areaCode and '' != areaCode">
            area_code,
        </if>
        <if test="null != address and '' != address">
            address,
        </if>
        <if test="null != longitude and '' != longitude">
            longitude,
        </if>
        <if test="null != latitude and '' != latitude">
            latitude,
        </if>
        <if test="null != email and '' != email">
            email,
        </if>
        <if test="null != commissionType and '' != commissionType">
            commission_type,
        </if>
        <if test="null != platformCommissionRate and '' != platformCommissionRate">
            platform_commission_rate,
        </if>
        <if test="null != maintenanceCommissionRate and '' != maintenanceCommissionRate">
            maintenance_commission_rate,
        </if>
        <if test="null != businessLicenseId and '' != businessLicenseId">
            business_license_id,
        </if>
        <if test="null != withdrawalTime and '' != withdrawalTime">
            withdrawal_time,
        </if>
        <if test="null != soc and '' != soc">
            soc,
        </if>
        <if test="null != status and '' != status">
            status,
        </if>
        <if test="null != delFlag and '' != delFlag">
            del_flag,
        </if>
        <if test="null != createTime ">
            create_time,
        </if>
        <if test="null != createBy and '' != createBy">
            create_by,
        </if>
        <if test="null != updateTime ">
            update_time,
        </if>
        <if test="null != updateBy and '' != updateBy">
            update_by,
        </if>
        <if test="null != remark and '' != remark">
            remark,
        </if>
        <if test="null != operatorIdEvcs and '' != operatorIdEvcs">
            operator_id_evcs
        </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
        <if test="null != name and '' != name">
            #{name},
        </if>
        <if test="null != contactName and '' != contactName">
            #{contactName},
        </if>
        <if test="null != contactPhone and '' != contactPhone">
            #{contactPhone},
        </if>
        <if test="null != phone and '' != phone">
            #{phone},
        </if>
        <if test="null != attribute and '' != attribute">
            #{attribute},
        </if>
            <if test="null != dutyParagraph and '' != dutyParagraph">
                #{dutyParagraph},
            </if>
            <if test="null != detailedAddress and '' != detailedAddress">
                #{detailedAddress},
            </if>
            <if test="null != openBank and '' != openBank">
                #{openBank},
            </if>
            <if test="null != cardNumber and '' != cardNumber">
                #{cardNumber},
            </if>
        <if test="null != areaCode and '' != areaCode">
            #{areaCode},
        </if>
        <if test="null != address and '' != address">
            #{address},
        </if>
        <if test="null != longitude and '' != longitude">
            #{longitude},
        </if>
        <if test="null != latitude and '' != latitude">
            #{latitude},
        </if>
        <if test="null != email and '' != email">
            #{email},
        </if>
        <if test="null != commissionType and '' != commissionType">
            #{commissionType},
        </if>
        <if test="null != platformCommissionRate and '' != platformCommissionRate">
            #{platformCommissionRate},
        </if>
        <if test="null != maintenanceCommissionRate and '' != maintenanceCommissionRate">
            #{maintenanceCommissionRate},
        </if>
        <if test="null != businessLicenseId and '' != businessLicenseId">
            #{businessLicenseId},
        </if>
        <if test="null != withdrawalTime and '' != withdrawalTime">
            #{withdrawalTime},
        </if>
        <if test="null != soc and '' != soc">
            #{soc},
        </if>
        <if test="null != status and '' != status">
            #{status},
        </if>
        <if test="null != delFlag and '' != delFlag">
            #{delFlag},
        </if>
        <if test="null != createTime ">
            #{createTime},
        </if>
        <if test="null != createBy and '' != createBy">
            #{createBy},
        </if>
        <if test="null != updateTime ">
            #{updateTime},
        </if>
        <if test="null != updateBy and '' != updateBy">
            #{updateBy},
        </if>
        <if test="null != remark and '' != remark">
            #{remark},
        </if>
        <if test="null != operatorIdEvcs and '' != operatorIdEvcs">
            #{operatorIdEvcs}
        </if>
        </trim>
    </insert>

    <update id="update" parameterType="com.xhpc.user.domain.XhpcOperator">
        UPDATE xhpc_operator
        <set>
            <if test="null != operatorId and '' != operatorId">operator_id = #{operatorId},</if>
            <if test="null != name and '' != name">name = #{name},</if>
            <if test="null != contactName and '' != contactName">contact_name = #{contactName},</if>
            <if test="null != contactPhone and '' != contactPhone">contact_phone = #{contactPhone},</if>
            <if test="null != phone and '' != phone">phone = #{phone},</if>
            <if test="null != attribute ">attribute = #{attribute},</if>
            <if test="null != openBank and '' != openBank">open_bank = #{openBank},</if>
            <if test="null != cardNumber">card_number = #{cardNumber},</if>
            <if test="null != areaCode and '' != areaCode">area_code = #{areaCode},</if>
            <if test="null != address and '' != address">address = #{address},</if>
            <if test="null != longitude and '' != longitude">longitude = #{longitude},</if>
            <if test="null != latitude and '' != latitude">latitude = #{latitude},</if>
            <if test="null != email and '' != email">email = #{email},</if>
            <if test="null != commissionType ">commission_type = #{commissionType},</if>
            <if test="null != platformCommissionRate">platform_commission_rate =
                #{platformCommissionRate},
            </if>
            <if test="null != maintenanceCommissionRate">maintenance_commission_rate
                = #{maintenanceCommissionRate},
            </if>
            <if test="null != businessLicenseId and '' != businessLicenseId">business_license_id =
                #{businessLicenseId},
            </if>
            <if test="null != withdrawalTime and '' != withdrawalTime">withdrawal_time = #{withdrawalTime},</if>
            <if test="null != soc and '' != soc">soc = #{soc},</if>
            <if test="null != status">status = #{status},</if>
            <if test="null != delFlag and '' != delFlag">del_flag = #{delFlag},</if>
            <if test="null != createTime ">create_time = #{createTime},</if>
            <if test="null != createBy and '' != createBy">create_by = #{createBy},</if>
            <if test="null != updateTime">update_time = #{updateTime},</if>
            <if test="null != updateBy and '' != updateBy">update_by = #{updateBy},</if>
            <if test="null != remark and '' != remark">remark = #{remark},</if>
            <if test="null != operatorIdEvcs and '' != operatorIdEvcs">operator_id_evcs = #{operatorIdEvcs}</if>
        </set>
        WHERE operator_id = #{operatorId}
    </update>

    <delete id="deleteByIds" parameterType="java.lang.String">
        delete from xhpc_operator where operator_id in
        <foreach collection="array" item="ids" open="(" separator="," close=")">
            #{ids}
        </foreach>
    </delete>

    <select id="checkAccountUnique" parameterType="java.lang.String" resultMap="XhpcOperatorResult">
		select * from xhpc_operator where phone = #{phone} limit 1
	</select>

    <select id="info" parameterType="java.lang.Long" resultType="java.util.Map">
        select
        <include refid="Base_Column_List"/>
        xo.operator_id_evcs operatorIdEvcs,
        xdb.dict_value attributenName,`xa`.merger_name mergerName,
        GROUP_CONCAT(DISTINCT xi.`url` ORDER BY xi.create_time DESC separator ',') businessLicenseUrl
        from xhpc_operator `xo`
        LEFT JOIN xhpc_dict_biz xdb on xdb.`code` = 'operator_attribute' and xdb.dict_key = xo.attribute
        LEFT JOIN xhpc_area `xa` on `xa`.`code` = xo.area_code
        LEFT JOIN xhpc_img xi on FIND_IN_SET(xi.img_id,xo.business_license_id )
        where xo.del_flag = 0 and xo.operator_id = #{operatorId}
    </select>

    <select id="selectOperatorList" parameterType="java.lang.Long" resultType="java.util.Map">
        select xo.operator_id operatorId, xo.name, xo.contact_name contactName, xo.duty_paragraph dutyParagraph,
        xo.contact_phone contactPhone, xo.phone, xo.attribute,`xo`.create_time createTime,`xo`.status,
        CASE WHEN xo.`status` = 0 THEN '正常' else '禁用' end statusName,su.user_id userId,
		xdb.dict_value attributenName
		from xhpc_operator xo
        LEFT JOIN sys_user su on su.operator_id = xo.operator_id
		LEFT JOIN xhpc_dict_biz xdb on xdb.`code` = 'operator_attribute' and xdb.dict_key = xo.attribute
        where xo.del_flag = 0
        <if test="name != null and name != ''">
            and xo.name like concat(concat('%', #{name}), '%')
        </if>
        <if test="contactName != null and contactName != ''">
            and xo.contact_name like concat(concat('%', #{contactName}), '%')
        </if>
        <if test="contactPhone != null and contactPhone != ''">
            and xo.contact_phone like concat(concat('%', #{contactPhone}), '%')
        </if>
        <if test="createTimeStart != null and createTimeStart != ''"><!-- 开始时间检索 -->
            AND xo.create_time &gt;= #{createTimeStart}
        </if>
        <if test="createTimeEnd != null and createTimeEnd != ''"><!-- 结束时间检索 -->
            AND xo.create_time &lt;= #{createTimeEnd}
        </if>
        group by xo.operator_id
        ORDER BY xo.update_time DESC
    </select>

    <delete id="deleteUserRoleByUserId" parameterType="java.lang.Long">
		delete from sys_user_role where user_id=#{userId}
	</delete>

    <delete id="deleteUserPostByUserId" parameterType="java.lang.Long">
		delete from sys_user_post where user_id=#{userId}
	</delete>

    <update id="deleteUserById" parameterType="com.xhpc.system.api.domain.SysUser">
		update sys_user set del_flag = #{delFlag} where user_id = #{userId}
 	</update>

    <select id="getUserByOperatorId" parameterType="java.lang.Long" resultType="com.xhpc.system.api.domain.SysUser">
           select user_id          userId,
               dept_id          deptid,
               user_name        userName,
               nick_name        nickName,
               user_type        userType,
               email,
               phonenumber,
               operator_id      operatorId,
               internet_user_id internetUserId,
               sex,
               avatar,
               password,
               data_power_type  dataPowerType,
               status,
               del_flag         delFlag,
               login_ip         loginIp,
               login_date       loginDate,
               create_by        createBy,
               create_time      createTime,
               update_by        updateBy,
               update_time      updateTime,
               remark
        from sys_user
        where del_flag = 0
          and operator_id = #{operatorId}
    </select>

    <select id="getOperatorId" resultType="java.util.Map">
        select  xo.operator_id operatorId, xo.name, xo.contact_name contactName,
		 xo.contact_phone contactPhone,  xo.phone, xo.attribute, xo.duty_paragraph dutyParagraph,
		xdb.dict_value attributenName
        from xhpc_operator `xo`
        LEFT JOIN xhpc_dict_biz xdb on xdb.`code` = 'operator_attribute' and xdb.dict_key = xo.attribute
        where xo.del_flag = 0
        ORDER BY xo.create_time DESC
    </select>


    <delete id="deleteRoleByName" parameterType="java.lang.String">
        delete from sys_role
        where role_name = #{roleName}
    </delete>
</mapper>
