<?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.jeeplus.test.reimbursementsys.mapper.ReimbursementUserMapper">
    <sql id="reimbursementUserColumns">
        a.id AS "id",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.user_id AS "userId",
        a.year AS "year",
        a.reimbursement_quota AS "reimbursementQuota",
        a.reimbursement_all_day AS "reimbursementAllDay",
        a.reimbursement_quota_day AS "reimbursementQuotaDay",
        su.id as "user.id",
        su.name as "user.name"
    </sql>

    <sql id="reimbursementUserJoinColumns">
        left join sys_user su on su.id = a.user_id
    </sql>

    <sql id="userColumns">
    	a.id,
    	a.company_id AS "companyDTO.id",
    	a.office_id AS "officeDTO.id",
    	a.login_name AS "loginName",
		a.is_admin AS "isAdmin",
    	a.password,
    	a.no,
		a.name,
		a.email,
		a.phone,
		a.mobile,
		a.login_ip AS "loginIp",
		a.login_date AS "loginDate",
		a.remarks,
		a.login_flag,
		a.photo,
		a.qr_code,
		a.sign,
		a.create_by AS "createBy.id",
		a.create_date,
		a.update_by AS "updateBy.id",
		a.update_date,
    	c.name AS "companyDTO.name",
    	c.parent_id AS "companyDTO.parent.id",
    	c.parent_ids AS "companyDTO.parentIds",
    	o.name AS "officeDTO.name",
    	o.parent_id AS "officeDTO.parent.id",
    	o.parent_ids AS "officeDTO.parentIds"
    </sql>

    <sql id="userJoins">
		LEFT JOIN sys_office c ON c.id = a.company_id
		LEFT JOIN sys_office o ON o.id = a.office_id
    </sql>

    <select id="findList" resultType="com.jeeplus.test.reimbursementsys.domain.dto.ReimbursementUserDTO">
        SELECT
            <include refid="reimbursementUserColumns"/>
            ,
        zrbi.reimbursement_address as "addressName"
            ,
        ifnull(sum(zrbi.reimbursement_day),0) as "alreadyReimbursementDay",
        ifnull(sum(zrbi.reimbursement_amount),0) as "alreadyReimbursementAmount",
        ifnull((a.reimbursement_quota - ifnull(sum(zrbi.reimbursement_amount),0)),0) as "remainReimbursementAmount",
        ifnull(( CONVERT ( a.reimbursement_quota / a.reimbursement_quota_day, DECIMAL ( 15, 2 )) - ifnull(sum( zrbi.reimbursement_day ),0)), 0 ) AS "surplusReimbursementDay"
        FROM zs_reimbursement_user_info a
        <include refid="reimbursementUserJoinColumns"/>
        LEFT JOIN zs_reimbursement_business_info zrbi ON zrbi.user_id = a.user_id and zrbi.year = a.year
        <where>
            a.del_flag = 0
            <if test="reimbursementUser.year != null and reimbursementUser.year != ''">
                and a.year like concat('%',#{reimbursementUser.year},'%')
            </if>
            <if test="reimbursementUser.userId != null and reimbursementUser.userId != ''">
                and a.user_id = #{reimbursementUser.userId}
            </if>
        </where>
        group by a.user_id,a.year
        order by a.update_date desc,a.year desc, a.user_id asc
    </select>

    <select id="getYearList" resultType="com.jeeplus.test.reimbursementsys.domain.dto.ReimbursementUserDTO">
        SELECT
            <include refid="reimbursementUserColumns"/>
        FROM zs_reimbursement_user_info a
        <include refid="reimbursementUserJoinColumns"/>
        <where>
            a.del_flag = 0
            <if test="reimbursementUser.userId != null and reimbursementUser.userId != ''">
                and a.user_id = #{reimbursementUser.userId}
            </if>
        </where>
        order by a.year desc
    </select>

    <insert id="save">
        insert into zs_reimbursement_user_info (
            id,
            create_by,
            create_date,
            update_by,
            update_date,
            remarks,
            del_flag,
            user_id,
            year,
            reimbursement_quota,
            reimbursement_all_day,
            reimbursement_quota_day
        )
        values
        (
            #{id},
            #{createBy},
            #{createDate},
            #{updateBy},
            #{updateDate},
            #{remarks},
            #{delFlag},
            #{userId},
            #{year},
            #{reimbursementQuota},
            #{reimbursementAllDay},
            #{reimbursementQuotaDay}
        )
    </insert>

    <insert id="saveList">
        insert into zs_reimbursement_user_info (
        id,
        create_by,
        create_date,
        update_by,
        update_date,
        remarks,
        del_flag,
        user_id,
        year,
        reimbursement_quota,
        reimbursement_all_day,
        reimbursement_quota_day
        )
        values
        <foreach collection="reimbursementUserList" item="item" separator=",">
            ( #{item.id},
            #{item.createBy},
            #{item.createDate},
            #{item.updateBy},
            #{item.updateDate},
            #{item.remarks},
            #{item.delFlag},
            #{item.userId},
            #{item.year},
            #{item.reimbursementQuota},
            #{item.reimbursementAllDay},
            #{item.reimbursementQuotaDay}
            )
        </foreach>
    </insert>



    <update id="update">
        update zs_reimbursement_user_info
        set
            update_by = #{updateBy}
            ,update_date = #{updateDate}
            <if test="year != null and year != ''">
                ,year = #{year}
            </if>
            <if test="reimbursementQuota != null and reimbursementQuota != ''">
                ,reimbursement_quota = #{reimbursementQuota}
            </if>
            <if test="reimbursementAllDay != null and reimbursementAllDay != ''">
                ,reimbursement_all_day = #{reimbursementAllDay}
            </if>
            <if test="reimbursementQuotaDay != null and reimbursementQuotaDay != ''">
                ,reimbursement_quota_day = #{reimbursementQuotaDay}
            </if>
        <where>
            id = #{id}
        </where>
    </update>

    <select id="queryById" resultType="com.jeeplus.test.reimbursementsys.domain.dto.ReimbursementUserDTO">
        SELECT
        <include refid="reimbursementUserColumns"/>
            FROM zs_reimbursement_user_info a
        <include refid="reimbursementUserJoinColumns"/>
        <where>
            a.del_flag = 0
            and a.id = #{id}
        </where>
    </select>

    <select id="queryByUserIdAndYear" resultType="com.jeeplus.test.reimbursementsys.domain.dto.ReimbursementUserDTO">
        SELECT
        <include refid="reimbursementUserColumns"/>
            FROM zs_reimbursement_user_info a
        <include refid="reimbursementUserJoinColumns"/>
        <where>
            a.del_flag = 0
            and a.user_id = #{userId}
            and a.year = #{year}
        </where>
        order by id limit 1
    </select>

    <select id="wuHanQueryByUserIdAndYear" resultType="com.jeeplus.test.reimbursementsys.domain.dto.ReimbursementUserDTO">
        SELECT
        <include refid="reimbursementUserColumns"/>
        FROM zs_reimbursement_user_info a
        <include refid="reimbursementUserJoinColumns"/>
        <where>
            a.del_flag = 0
            and a.user_id = #{userId}
            and a.year = #{year}
        </where>
        order by id limit 1
    </select>

    <delete id="delete">
        delete from zs_reimbursement_user_info
        <where>
            <if test="idList != null and idList.size>0">
                and id in
                <foreach collection="idList" item="id" separator="," open="(" close=")">
                    #{id}
                </foreach>
            </if>
        </where>
    </delete>

    <select id="getUserByNameAndOfficeName" resultType="com.jeeplus.sys.service.dto.UserDTO">
        SELECT
        <include refid="userColumns"/>
        FROM sys_user a
        <include refid="userJoins"/>
        <where>
            a.del_flag = 0
            and a.name = #{userName}
            and o.name = #{officeName}
        </where>

    </select>
</mapper>
