<?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="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>
        <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.dept_id, sd.dept_name, sdc.banner_url, 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
        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>

    <select id="getWorkerList" parameterType="com.ghy.worker.domain.Worker" resultMap="WorkerResult">
        <include refid="selectWorker" />
        <where>
            <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="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>
        </where>
    </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="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="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="status != null"> status = #{status},</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="leaderTeamRate != null and leaderTeamRate != ''"> leader_team_rate = #{leaderTeamRate},</if>
            <if test="leaderTeamMoney != null and leaderTeamMoney != ''"> leader_team_money = #{leaderTeamMoney},</if>
        </set>
        where worker_id = #{workerId}
    </update>

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