<?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.docus.server.collection.mapper.PowerDeptMapper">
    <insert id="addDept">
        INSERT INTO `docus_system`.`power_dept`(`dept_id`,
                                                `dept_code`,
                                                `dept_name`,
                                                `create_date`,
                                                `creater`,
                                                `update_date`,
                                                `updater`)
        VALUES (#{dept.deptId},#{dept.deptCode},#{dept.deptName},now(),#{dept.authorName},now(),#{dept.authorName})
    </insert>
    <insert id="addWardDept">
        INSERT INTO `docus_system`.`power_ward`(`ward_id`,
                                                `ward_code`,
                                                `ward_name`,
                                                `create_date`,
                                                `creater`,
                                                `update_date`,
                                                `updater`)
        VALUES (#{dept.deptId},#{dept.deptCode},#{dept.deptName},now(),#{dept.authorName},now(),#{dept.authorName})
    </insert>
    <insert id="addMzDept">
        INSERT INTO `docus_system`.`power_area`(`area_id`,
                                                `area_code`,
                                                `area_name`)
        VALUES (#{dept.deptId},#{dept.deptCode},#{dept.deptName})
    </insert>
    <update id="updateDept">
        update `docus_system`.`power_dept` set
        `dept_name`=#{dept.deptName},
        `updater`=#{dept.authorName},
        `update_date`=now()
        where `dept_code`=#{dept.deptCode}
    </update>
    <update id="updateWardDept">
        update
            `docus_system`.`power_ward`
        set
            `ward_name`=#{dept.deptName},
            `updater`=#{dept.authorName},
            `update_date`=now()
        where
              `ward_code`=#{dept.deptCode}
    </update>
    <update id="updateMzDept">
        update
            `docus_system`.`power_area`
        set
            `area_name`=#{dept.deptName}
        where
              `area_code`=#{dept.deptCode}
    </update>
    <delete id="delDeptByDeptCode">
        delete from `docus_system`.`power_dept` where `dept_code` = #{deptCode}
    </delete>
    <delete id="delWardDeptByDeptCode">
        delete from `docus_system`.`power_ward` where `ward_code` = #{deptCode}
    </delete>
    <delete id="delMzDeptByDeptCode">
        delete from `docus_system`.`power_area` where `area_code` = #{deptCode}
    </delete>

    <select id="getDeptByDeptCode" resultType="com.docus.server.collection.entity.PowerDept">
        SELECT `dept_id`,`dept_code`,`dept_name` FROM `docus_system`.`power_dept`
        WHERE `dept_code` = #{deptCode}   LIMIT 1
    </select>
    <select id="getWardDeptByDeptCode" resultType="com.docus.server.collection.entity.PowerDept">
        SELECT `ward_id` as deptId,`ward_code` as deptCode,`ward_name` as deptName FROM `docus_system`.`power_ward`
        WHERE `ward_code` = #{deptCode}   LIMIT 1
    </select>
    <select id="getMzDeptByDeptCode" resultType="com.docus.server.collection.entity.PowerDept">
        SELECT `area_id` as deptId,`area_code` as deptCode,`area_name` as deptName FROM `docus_system`.`power_area`
        WHERE `area_code` = #{deptCode}   LIMIT 1
    </select>
</mapper>
