<?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.ghy.worker.mapper.WorkerMapper">

    <resultMap id="WorkerResult"                        type="com.ghy.worker.domain.Worker">
        <result property="workerId"                     column="worker_id"/>
        <result property="name"                         column="name"/>
        <result property="account"                      column="account"/>
        <result property="phone"                        column="phone"/>
        <result property="openId"                       column="open_id"/>
        <result property="unionId"                       column="union_id"/>
        <result property="wxOpenId"                       column="wx_open_id"/>
        <result property="alipayAccount"                column="alipay_account"/>
        <result property="alipayName"                   column="alipay_name"/>
        <result property="password"                     column="password"/>
        <result property="status"                       column="status"/>
        <result property="type"                         column="type"/>
        <result property="deptName"                     column="dept_name"/>
        <result property="deptId"                       column="dept_id"/>
        <result property="workerLogoUrl"                column="worker_logo_url"/>
        <result property="leaderTeamRate"               column="leader_team_rate"/>
        <result property="leaderTeamMoney"              column="leader_team_money"/>
        <result property="createBy"                     column="create_by"/>
        <result property="createTime"                   column="create_time"   />
        <result property="updateBy"                     column="update_by"/>
        <result property="updateTime"                   column="update_time"/>
        <result property="remark"                       column="remark"/>
        <result property="bannerUrl"                    column="banner_url"/>
        <result property="storeStatus"                  column="store_status"/>
    </resultMap>

    <sql id="selectWorker">
        SELECT
            w.worker_id, w.name, w.account, w.phone, w.password, w.open_id, w.wx_open_id, w.dept_id, w.status, w.worker_logo_url,
            w.leader_team_rate, w.leader_team_money, w.create_by, w.create_time, w.update_by, w.update_time,
            w.remark, w.type, w.store_status, w.alipay_account, w.alipay_name,
            sd.dept_name, sdc.banner_url
        FROM worker w
        LEFT JOIN sys_dept sd ON w.dept_id = sd.dept_id
        LEFT JOIN sys_dept_config sdc ON w.dept_id = sdc.dept_id
    </sql>

    <sql id="selectBasic">
        SELECT
            w.worker_id, w.name, w.account, w.phone, w.password, w.open_id, w.wx_open_id, w.dept_id, w.status, w.worker_logo_url,
            w.leader_team_rate, w.leader_team_money, w.create_by, w.create_time, w.update_by, w.update_time,
            w.remark, w.type, w.store_status, w.alipay_account, w.alipay_name
        FROM worker w
    </sql>

    <select id="getWorkerList" parameterType="com.ghy.worker.domain.Worker" resultMap="WorkerResult">
        SELECT
        w.worker_id, w.name, w.account, w.phone, w.password, w.open_id, w.wx_open_id, w.dept_id, w.status, w.worker_logo_url,
        w.leader_team_rate, w.leader_team_money, w.create_by, w.create_time, w.update_by, w.update_time,
        w.remark, w.type, w.store_status, w.alipay_account, w.alipay_name,concat(wc.surname, wc.name) as realName,
        sd.dept_name, sdc.banner_url
        FROM worker w
        LEFT JOIN sys_dept sd ON w.dept_id = sd.dept_id
        LEFT JOIN sys_dept_config sdc ON w.dept_id = sdc.dept_id
        LEFT JOIN worker_certification wc on w.worker_id = wc.worker_id
             where w.worker_id in (
            SELECT w.worker_id FROM worker w
            LEFT JOIN worker_area wa ON wa.worker_id = w.worker_id
            LEFT JOIN worker_goods_category wgc ON wgc.worker_id = w.worker_id
            LEFT JOIN worker_certification wc on w.worker_id = wc.worker_id
            <where>
                <if test="deptId != null and deptId != ''">
                    AND dept_id = #{deptId}
                </if>
                <if test="openId != null and openId != ''">
                    AND open_id = #{openId}
                </if>
                <if test="workerIds != null and workerIds != ''">
                    AND w.worker_id IN
                    <foreach item="item" collection="workerIds" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
                <if test="keyWords != null and keyWords != ''">
                    AND (w.name LIKE '%${keyWords}%'
                    OR w.phone LIKE '%${keyWords}%'
                    OR concat(wc.surname,'', wc.name) like '%${keyWords}%')
                </if>
                <if test="name != null and name != ''">
                    AND w.name LIKE '%${name}%'
                </if>
                <if test="phone != null and phone != ''">
                    AND w.phone = #{phone}
                </if>
                <if test="password != null and password != ''">
                    AND w.password = #{password}
                </if>
                <if test="districtId != null">
                    AND wa.district_id = #{districtId}
                </if>
                <if test="districtIds != null and districtIds.size > 0">
                    AND wa.district_id in
                    <foreach collection="districtIds" item="districtId" open="(" separator="," close=")">
                        #{districtId}
                    </foreach>
                </if>
                <if test="exceptDistrictIds != null and exceptDistrictIds.size > 0">
                    AND wa.district_id not in
                    <foreach collection="exceptDistrictIds" item="exceptDistrictId" open="(" separator="," close=")">
                        #{exceptDistrictId}
                    </foreach>
                </if>
                <if test="exceptParentAreaId != null">
                    AND sa.parent_code != #{exceptParentAreaId}
                </if>
                <if test="goodsCategoryId != null">
                    AND wgc.goods_category_id = #{goodsCategoryId}
                </if>
                <if test="goodsCategoryIds != null">
                    AND wgc.goods_category_id in
                    <foreach collection="goodsCategoryIds" item="goodsCategoryId" open="(" separator="," close=")">
                        #{goodsCategoryId}
                    </foreach>
                </if>
            </where>
        )
    </select>

    <select id="getWorkerByOpenId" parameterType="String" resultMap="WorkerResult">
        <include refid="selectBasic"></include>
        where open_id = #{openId}
    </select>

    <select id="getWorkByPhoneAndPwd" parameterType="com.ghy.worker.domain.Worker" resultMap="WorkerResult">
        <include refid="selectBasic" />
        where phone = #{phone} and password = #{password}
    </select>

    <select id="selectById" parameterType="Long" resultMap="WorkerResult">
        <include refid="selectWorker" /> WHERE worker_id = #{workerId}
    </select>

    <insert id="insertWorker" parameterType="com.ghy.worker.domain.Worker" useGeneratedKeys="true" keyProperty="workerId">
        insert into worker(
        <if test="name != null and name != ''">name,</if>
        <if test="account != null and account != ''">account,</if>
        <if test="phone != null and phone != ''">phone,</if>
        <if test="password != null and password != ''">password,</if>
        <if test="openId != null and openId != ''">open_id,</if>
        <if test="alipayAccount != null and alipayAccount != ''">alipay_account,</if>
        <if test="alipayName != null and alipayName != ''">alipay_name,</if>
        <if test="deptId != null and deptId != ''">dept_id,</if>
        <if test="status != null and status != ''">status,</if>
        <if test="workerLogoUrl != null and workerLogoUrl != ''">worker_logo_url,</if>
        <if test="remark != null and remark != ''">remark,</if>
        <if test="createBy != null and createBy != ''">create_by,</if>
        create_time
        )values(
        <if test="name != null and name != ''">#{name},</if>
        <if test="account != null and account != ''">#{account},</if>
        <if test="phone != null and phone != ''">#{phone},</if>
        <if test="password != null and password != ''">#{password},</if>
        <if test="openId != null and openId != ''">#{openId},</if>
        <if test="deptId != null and deptId != ''">#{deptId},</if>
        <if test="alipayAccount != null and alipayAccount != ''">#{alipayAccount},</if>
        <if test="alipayName != null and alipayName != ''">#{alipayName},</if>
        <if test="status != null and status != ''">#{status},</if>
        <if test="workerLogoUrl != null and workerLogoUrl != ''">#{workerLogoUrl},</if>
        <if test="remark != null and remark != ''">#{remark},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        sysdate()
        )
    </insert>

    <update id="updateWorker" >
        update worker
        <set>
            <if test="type != null"> type = #{type},</if>
            <if test="remark != null and remark != ''">remark = #{remark},</if>
            <if test="status != null"> status = #{status},</if>
            <if test="wxOpenId != null"> wx_open_id = #{wxOpenId},</if>
            <if test="storeStatus != null"> store_status = #{storeStatus},</if>
            <if test="account != null and account != ''"> account = #{account},</if>
            <if test="phone != null and phone != ''"> phone = #{phone},</if>
            <if test="password != null and password != ''"> password = #{password},</if>
            <if test="name != null and name != ''"> name = #{name},</if>
            <if test="workerLogoUrl != null and workerLogoUrl != ''">worker_logo_url = #{workerLogoUrl},</if>
            <if test="leaderTeamRate != null and leaderTeamRate != ''"> leader_team_rate = #{leaderTeamRate},</if>
            <if test="leaderTeamMoney != null and leaderTeamMoney != ''"> leader_team_money = #{leaderTeamMoney},</if>
            <if test="alipayAccount != null and alipayAccount != ''">alipay_account = #{alipayAccount},</if>
            <if test="alipayName != null and alipayName != ''">alipay_name = #{alipayName},</if>
        </set>
        where worker_id = #{workerId}
    </update>

    <select id="selectByIds" resultMap="WorkerResult">
        <include refid="selectWorker" />
        <where>
            and w.worker_id in
            <foreach item="item" index="workIds" collection="workIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </where>
    </select>
</mapper>
