<?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="cn.ezhizao.project.business.accountAdjustment.mapper.BizFinancialAccountAdjustmentMapper">
    <!-- 基础的返回map 多表复杂的 自行组合 -->
    <resultMap id="BaseResultMap" type="cn.ezhizao.project.business.accountAdjustment.domain.BizFinancialAccountAdjustment">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="id" jdbcType="BIGINT" property="id"/>
        <result column="company_id" jdbcType="BIGINT" property="companyId"/>
        <result column="year" jdbcType="CHAR" property="year"/>
        <result column="month" jdbcType="CHAR" property="month"/>
        <result column="actually_finished_date" jdbcType="DATE" property="actuallyFinishedDate"/>
        <result column="applier_id" jdbcType="BIGINT" property="applierId"/>
        <result column="accountant_id" jdbcType="BIGINT" property="accountantId"/>
        <result column="status" jdbcType="TINYINT" property="status"/>
        <result column="adjustment_result" jdbcType="CHAR" property="adjustmentResult"/>
        <result column="remark" jdbcType="CHAR" property="remark"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="creator_id" jdbcType="BIGINT" property="creatorId"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="updater_id" jdbcType="BIGINT" property="updaterId"/>
        <result column="deleted" jdbcType="TINYINT" property="deleted"/>
        <result column="version" jdbcType="INTEGER" property="version"/>
        <result column="tenant_id" jdbcType="BIGINT" property="tenantId"/>
<!--        <association property="companyName" javaType="java.lang.String" select="getCompanyName" column="company_id"/>-->
        <association property="applieName" javaType="java.lang.String" select="getApplieName" column="applier_id"/>
        <association property="accountantName" javaType="java.lang.String" select="getAccountantName"
                     column="accountant_id"/>
    </resultMap>

    <select id="getCompanyName" resultType="java.lang.String">
        SELECT name FROM biz_company
        WHERE deleted = 0 AND id = #{company_id};
    </select>

    <select id="getApplieName" resultType="java.lang.String">
        SELECT nick_name from sys_user
        WHERE del_flag = 0 AND user_id = #{applier_id};
    </select>

    <select id="getAccountantName" resultType="java.lang.String">
        SELECT nick_name from sys_user
        WHERE del_flag = 0 AND user_id = #{accountant_id};
    </select>

    <!--带条件的List查询-->
    <select id="getList" resultMap="BaseResultMap">
        <include refid="getListSql"/>
        order by id desc
    </select>

    <!--带条件的List查询 数据总条数-->
    <select id="getTotal" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM (<include refid="getListSql"/>) AS C
    </select>

    <sql id="getListSql">
        SELECT faa.*, c.name as company_name FROM biz_financial_account_adjustment faa left join biz_company c on c.id = faa.company_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            faa.deleted = 0 AND
            <if test="id != null  ">
                faa.id = #{id} AND
            </if>
            <if test="companyId != null  ">
                faa.company_id = #{companyId} AND
            </if>
            <if test="companyName != null  ">
                c.name like concat('%',#{companyName},'%') AND
            </if>
            <if test="year != null  and year != '' ">
                faa.year = #{year} AND
            </if>
            <if test="month != null  and month != '' ">
                faa.month = #{month} AND
            </if>
            <if test="actuallyFinishedDate != null  ">
                faa.actually_finished_date = #{actuallyFinishedDate} AND
            </if>
            <if test="applierId != null  ">
                faa.applier_id = #{applierId} AND
            </if>
            <if test="accountantId != null  ">
                faa.accountant_id = #{accountantId} AND
            </if>
            <if test="status != null  ">
                faa.status = #{status} AND
            </if>
            <if test="adjustmentResult != null  and adjustmentResult != '' ">
                faa.adjustment_result = #{adjustmentResult} AND
            </if>
            <if test="remark != null  and remark != '' ">
                faa.remark = #{remark} AND
            </if>
            <if test="createTime != null  ">
                faa.create_time = #{createTime} AND
            </if>
            <if test="creatorId != null  ">
                faa.creator_id = #{creatorId} AND
            </if>
            <if test="updateTime != null  ">
                faa.update_time = #{updateTime} AND
            </if>
            <if test="updaterId != null  ">
                faa.updater_id = #{updaterId} AND
            </if>
            <if test="deleted != null  ">
                faa.deleted = #{deleted} AND
            </if>
            <if test="version != null  ">
                faa.version = #{version} AND
            </if>
            <if test="tenantId != null  ">
                faa.tenant_id = #{tenantId} AND
            </if>
        </trim>
    </sql>

    <!-- 工业大学科研管理系统 导出数据用-->
    <select id="getOldData" resultMap="BaseResultMap">
        SELECT * FROM biz_financial_account_adjustment
    </select>

    <delete id="clear" parameterType="java.lang.Long">
        DELETE FROM biz_financial_account_adjustment WHERE id IS NULL;
    </delete>
    <!-- 工业大学科研管理系统 导出数据用-->
</mapper>
