<?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.reimbursementAssess.mapper.AssessReimbursementBusinessTwoMapper">
    <sql id="zsReimbursementBusinessAssessInfoColumns">
        a.id as 'id',
        a.create_by as 'createBy',
        a.create_date as 'createDate',
        a.update_by as 'updateBy',
        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',
        a.year as 'year',
        a.reimbursement_day as 'reimbursementDay',
        a.reimbursement_amount as 'reimbursementAmount',
        a.reimbursement_type as 'reimbursementType'
    </sql>
    <sql id="joinZsReimbursementBusinessAssessInfoColumns">
        a.id as 'id',
        a.create_by as 'createBy',
        a.create_date as 'createDate',
        a.update_by as 'updateBy',
        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',
        a.year as 'year',
        a.reimbursement_day as 'reimbursementDay',
        a.reimbursement_amount as 'reimbursementAmount',
        a.reimbursement_type as 'reimbursementType',
        a.reimbursement_fsalary as 'reimbursementFsalary',
        (case when a.reimbursement_type = '1' then '正常报销' when a.reimbursement_type = '2' then '补差' else '' end) as reimbursementTypeStr,
        (case when a.reimbursement_fsalary = '1' then '报销不实发' when a.reimbursement_fsalary = '2' then '报销并实发' else '' end) as reimbursementFsalaryStr,
        su.name as 'userName'
    </sql>
    <sql id="reimbursementUserJoinColumns">
        left join sys_user su on su.id = a.user_id
    </sql>
    <insert id="insertAssessReimbursementBusiness">
        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_amount,
            reimbursement_type,
            reimbursement_fsalary,
            office_type
        )
        values
        (
                    #{id},
            #{createBy},
            #{createDate},
            #{updateBy},
            #{updateDate},
            #{remarks},
            #{delFlag},
            #{businessCodeId},
            #{userId},
            #{year},
            <if test="reimbursementDay != null and reimbursementDay != ''">
                #{reimbursementDay},
            </if>
            #{reimbursementAmount},
            #{reimbursementType},
            #{reimbursementFsalary},
            #{officeType}
        )
    </insert>

    <select id="findListPage"
            resultType="com.jeeplus.test.reimbursementAssess.domain.dto.AssessReimbursementBusinessTwoDTO">
        select
        <include refid="joinZsReimbursementBusinessAssessInfoColumns"/>
        ,(SELECT NAME FROM sys_office WHERE id = ( (case when (SELECT substring_index( substring_index( parent_ids, ',', 3 ), ',', - 1 ) FROM sys_office WHERE id = su.office_id ) = '' then (select id FROM sys_office WHERE id = su.office_id )  else (SELECT substring_index( substring_index( parent_ids, ',', 3 ), ',', - 1 ) FROM sys_office WHERE id = su.office_id ) end) )) AS 'userOfficeName'
        from zs_reimbursement_business_info a
        <include refid="reimbursementUserJoinColumns"/>
        left join sys_office so on so.id = su.office_id
        <where>
            a.del_flag = 0 and (a.business_code_id='' or a.business_code_id=null)
            <if test="assessReimbursementBusiness.year != null and assessReimbursementBusiness.year != ''">
                and a.year like concat('%',#{assessReimbursementBusiness.year},'%')
            </if>
            <if test="assessReimbursementBusiness.userId != null and assessReimbursementBusiness.userId != ''">
                and a.user_id = #{assessReimbursementBusiness.userId}
            </if>
            and a.office_type = #{assessReimbursementBusiness.officeType}
        </where>
        order by a.update_date desc,a.year desc, a.user_id asc
    </select>
    <select id="findSelectList"
            resultType="com.jeeplus.test.reimbursementAssess.domain.dto.AssessReimbursementDTO">

        select id,business_code as businessCode from zs_reimbursement_assess_info where business_code is not null and business_code != '' group by business_code
    </select>
    <select id="findSelectYearList" resultType="java.lang.String">
        SELECT year FROM `zs_reimbursement_assess_info` GROUP BY `year` order by `year` desc
    </select>
    <select id="findSelectCodeListByYear"
            resultType="com.jeeplus.test.reimbursementAssess.domain.dto.AssessReimbursementDTO">
        select id,business_code as businessCode
        from zs_reimbursement_assess_info
        where year = #{year} and del_flag = 0
        and business_code is not null and business_code != '' and reimbursement_ratio is not null and reimbursement_ratio !=''
        group by business_code
    </select>
    <select id="findDataById"
            resultType="com.jeeplus.test.reimbursementAssess.domain.dto.AssessReimbursementBusinessTwoDTO">
        select
        <include refid="zsReimbursementBusinessAssessInfoColumns"/>
        from zs_reimbursement_business_info a
        where id=#{id}
    </select>


    <delete id="delPhysicsById">
        delete from zs_reimbursement_business_info
        where id = #{id}
    </delete>

    <delete id="deleteByList">
        delete from zs_reimbursement_business_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>

    <update id="modifyBindCode">
        update zs_reimbursement_business_info
        set
                business_code_id=#{businessCode}
        where id=#{ids}
    </update>


</mapper>