<?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.WorkerAreaMapper">

    <resultMap id="WorkerAreaResult"            type="com.ghy.worker.domain.WorkerArea">
        <result property="workerAreaId"         column="worker_area_id"/>
        <result property="workerId"             column="worker_id"/>
        <result property="provinceId"           column="province_id"/>
        <result property="cityId"               column="city_id"/>
        <result property="districtId"           column="district_id"/>
        <result property="streetId"             column="street_id"/>
        <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="mergerName"           column="merger_name"/>
    </resultMap>

    <sql id="selectWorkerArea">
        SELECT wa.worker_area_id, wa.worker_id, wa.province_id, wa.city_id, wa.district_id, wa.street_id, wa.create_by, wa.create_time,
        wa.update_by, wa.update_time, wa.remark, sa.area_name, sa.merger_name
        FROM worker_area wa
        LEFT JOIN sys_area sa on wa.street_id = sa.area_id
    </sql>

    <insert id="insert" parameterType="com.ghy.worker.domain.WorkerArea" useGeneratedKeys="true" keyProperty="workerAreaId">
        INSERT INTO worker_area(
        <if test="workerId != null and workerId > 0">worker_id,</if>
        <if test="provinceId != null and provinceId > 0">province_id,</if>
        <if test="cityId != null and cityId > 0">city_id,</if>
        <if test="districtId != null and districtId > 0">district_id,</if>
        <if test="streetId != null and streetId > 0">street_id,</if>
        <if test="createBy != null and createBy != ''">create_by,</if>
        <if test="remark != null and remark != ''">remark,</if>
        create_time
        )VALUES(
        <if test="workerId != null and workerId > 0">#{workerId},</if>
        <if test="provinceId != null and provinceId > 0">#{provinceId},</if>
        <if test="cityId != null and cityId > 0">#{cityId},</if>
        <if test="districtId != null and districtId > 0">#{districtId},</if>
        <if test="streetId != null and streetId > 0">#{streetId},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        <if test="remark != null and remark != ''">#{remark},</if>
        sysdate()
        )
    </insert>

    <insert id="batchInsert" parameterType="list" >
        INSERT INTO worker_area ( worker_id, province_id, city_id, district_id, street_id, create_by, create_time)
        VALUES
        <foreach collection="workerAreas" separator=","  item="workerArea">
            (#{workerArea.workerId}, #{workerArea.provinceId}, #{workerArea.cityId}, #{workerArea.districtId},
            #{workerArea.streetId}, #{workerArea.workerId}, sysdate())
        </foreach>
    </insert>

    <select id="getByWorker" parameterType="Long" resultMap="WorkerAreaResult">
        <include refid="selectWorkerArea"></include>
        WHERE wa.worker_id = #{workerId}
    </select>

    <delete id="deleteByWorker" parameterType="Long">
        DELETE FROM worker_area WHERE worker_id = #{workerId}
    </delete>

    <delete id="delete" parameterType="Long">
        DELETE FROM worker_area WHERE worker_area_id IN
        <foreach collection="array" item="areaId" open="(" separator="," close=")">
            #{areaId}
        </foreach>
    </delete>

</mapper>
