<?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.reimbursementProject.mapper.ProjectReimbursementBusinessMapper">
    <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="reimbursementBusinessColumns">
        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.business_code_id as "businessCodeId",
		a.user_id AS "userId",
        su.name as "userName",
        a.year AS "year",
        a.reimbursement_day as "reimbursementDay",
        a.reimbursement_type AS "reimbursementType",
        a.reimbursement_fsalary AS "reimbursementFsalary",
        a.reimbursement_amount as "reimbursementAmount"
    </sql>

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

    <select id="findList" resultType="com.jeeplus.test.reimbursementProject.domain.dto.ProjectReimbursementUserDTO">
        SELECT
            <include refid="reimbursementUserColumns"/>
        FROM zs_reimbursement_user_project_info a
        <include refid="reimbursementUserJoinColumns"/>
        <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>
        order by a.update_date desc,a.year desc, a.user_id asc
    </select>

    <select id="getYearList" resultType="com.jeeplus.test.reimbursementProject.domain.dto.ProjectReimbursementUserDTO">
        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>

    <select id="getReimbursementMoneyByYear" resultType="com.jeeplus.test.reimbursementProject.domain.dto.ProjectReimbursementUserDTO">
        SELECT
            a.year as year,
            ifnull(sum(a.reimbursement_amount),0) as alreadyReimbursementAmount,
            ifnull(sum(a.reimbursement_day),0) as alreadyReimbursementDay
        FROM zs_reimbursement_business_info a
        <where>
            a.del_flag = 0
            and a.user_id = #{userId}
            and a.year in (${year})
            and a.reimbursement_type = 1
        </where>
        group by a.year
        order by a.year desc
    </select>

    <select id="reimbursementUserByUserIdAndYear" resultType="com.jeeplus.test.reimbursementProject.domain.dto.ProjectReimbursementUserDTO">
        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>
    </select>

    <insert id="save">
        insert into zs_reimbursement_business_info (
            id,
            create_by,
            create_date,
            update_by,
            update_date,
            remarks,
            del_flag,
            business_code_id,
            user_id,
            year,
            <if test="reimbursementDay != null and reimbursementDay != ''">
                reimbursement_day,
            </if>
            reimbursement_type,
            reimbursement_fsalary,
            reimbursement_amount,
            office_type
        )
        values
        (
            #{id},
            #{createBy},
            #{createDate},
            #{updateBy},
            #{updateDate},
            #{remarks},
            #{delFlag},
            #{businessCodeId},
            #{userId},
            #{year},
            <if test="reimbursementDay != null and reimbursementDay != ''">
                #{reimbursementDay},
            </if>
            #{reimbursementType},
            #{reimbursementFsalary},
            #{reimbursementAmount},
            #{officeType}
        )
    </insert>

    <update id="update">
        update zs_reimbursement_user_project_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="reimbursementQuotaDay != null and reimbursementQuotaDay != ''">
                ,reimbursement_quota_day = #{reimbursementQuotaDay}
            </if>
        <where>
            id = #{id}
        </where>
    </update>

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

    <select id="queryByUserIdAndYear" resultType="com.jeeplus.test.reimbursementProject.domain.dto.ProjectReimbursementUserDTO">
        SELECT
        <include refid="reimbursementUserColumns"/>
            FROM zs_reimbursement_user_project_info a
        <include refid="reimbursementUserJoinColumns"/>
        <where>
            a.del_flag = 0
            and a.user_id = #{userId}
            and a.year = #{year}
        </where>
    </select>

    <delete id="delete">
        delete from zs_reimbursement_user_project_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="getAlreadyReimbursementQuotaByUserIdAndYear" resultType="java.lang.String">
        select ifnull(sum(a.reimbursement_amount),0)
        from zs_reimbursement_business_info a
        where a.del_flag = 0
        and a.user_id= #{userId}
        and a.year = #{year}
        and a.reimbursement_type = 1
    </select>

    <select id="getAlreadyReimbursementQuotaByBusinessCodeId" resultType="java.lang.String">
        select ifnull(sum(a.reimbursement_amount),0)
        from zs_reimbursement_business_info a
        where a.del_flag = 0
        and a.business_code_id= #{businessCodeId}
        and office_type = #{officeType}
    </select>

    <select id="getGatheringStatusById" resultType="java.lang.String">
        select a.gathering_status
        from zs_reimbursement_project_info a
        where a.del_flag = 0
        and a.id= #{businessCodeId}
    </select>



    <select id="findListByBusinessCodeId" resultType="com.jeeplus.test.reimbursementProject.domain.dto.ProjectReimbursementBusinessDTO">
        SELECT
        <include refid="reimbursementBusinessColumns"/>
        FROM zs_reimbursement_business_project_info a
        <include refid="reimbursementUserJoinColumns"/>
        <where>
            a.del_flag = 0
            and a.business_code_id = #{businessCodeId}
        </where>
        order by a.update_date desc,a.year desc, a.user_id asc
    </select>


    <select id="findListByUserIdAndYear" resultType="com.jeeplus.test.reimbursementProject.domain.dto.ProjectReimbursementBusinessDTO">
        SELECT
        <include refid="reimbursementBusinessColumns"/>
        ,zri.business_code as "businessCode"
        FROM zs_reimbursement_business_project_info a
        <include refid="reimbursementUserJoinColumns"/>
        left join zs_reimbursement_project_info zri on zri.id = a.business_code_id
        <where>
            a.del_flag = 0
            and a.user_id = #{userId}
            and a.year = #{year}

            <if test="beginReimbursementQuota != null and beginReimbursementQuota != ''">
                and a.reimbursement_amount >= #{beginReimbursementQuota}
            </if>
            <if test="endReimbursementQuota != null and endReimbursementQuota != ''">
                and a.reimbursement_amount &lt;= #{endReimbursementQuota}
            </if>
            <if test="reimbursementFsalary != null and reimbursementFsalary != ''">
                and a.reimbursement_fsalary = #{reimbursementFsalary}
            </if>
            <if test="beginDate != null">
                and a.create_date >= #{beginDate}
            </if>
            <if test="endDate != null">
                and a.create_date &lt;= #{endDate}
            </if>
        </where>
        order by a.update_date desc,a.year desc, a.user_id asc
    </select>

    <update id="updateReimbursementInfo">
        update zs_reimbursement_project_info
        set reimbursement_status = #{reimbursementStatus}
        where id = #{id}
    </update>


    <select id="getReimbursementFsalaryByUserIdAndYear" resultType="com.jeeplus.test.reimbursementsys.domain.dto.ReimbursementAddressDTO">
        SELECT
        a.reimbursement_fsalary AS "value",
        (case when a.reimbursement_fsalary = '1' then '报销不实发' when a.reimbursement_fsalary = '2' then '报销并实发' else 0 end) as "label"
        FROM zs_reimbursement_business_project_info a
        <include refid="reimbursementUserJoinColumns"/>
        left join zs_reimbursement_project_info zri on zri.id = a.business_code_id
        <where>
            a.del_flag = 0
            and a.user_id = #{userId}
            and a.year = #{year}
        </where>
        group by a.reimbursement_fsalary
        order by a.reimbursement_fsalary desc
    </select>
</mapper>
