<?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.bonus.material.basic.mapper.BmAgreementInfoMapper">
    <resultMap type="com.bonus.material.basic.domain.BmAgreementInfo" id="BmAgreementInfoResult">
        <result property="agreementId"    column="agreement_id"    />
        <result property="agreementCode"    column="agreement_code"    />
        <result property="signTime"    column="sign_time"    />
        <result property="unitId"    column="unit_id"    />
        <result property="unitName"    column="unit_name"    />
        <result property="createBy"    column="create_by"    />
        <result property="leaseDay"    column="lease_day"    />
        <result property="planStartTime"    column="plan_start_time"    />
        <result property="contractCode"    column="contract_code"    />
        <result property="authPerson"    column="auth_person"    />
        <result property="phone"    column="phone"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="companyId"    column="company_id"    />
        <result property="status"    column="status"    />
        <result property="protocol"    column="protocol"    />
        <result property="isSlt"    column="is_slt"    />
        <result property="taskId"    column="task_id"    />
    </resultMap>

    <select id="selectBmAgreementInfoList" parameterType="com.bonus.material.basic.domain.BmAgreementInfo" resultMap="BmAgreementInfoResult">
        SELECT bai.agreement_id, bai.agreement_code , contract_code,sign_time,
        bu.unit_id,bu.unit_name , bp.pro_id as projectId , bp.pro_name as projectName,
        plan_start_time,lease_day,auth_person,phone,bai.remark,bai.protocol
        FROM bm_agreement_info bai
        LEFT JOIN bm_project bp ON bp.pro_id = bai.project_id
        LEFT JOIN bm_unit bu ON bu.unit_id = bai.unit_id
        where bai.status = '1'
        <if test="keyWord != null  and keyWord != ''">
            and (bai.contract_code like concat('%', #{keyWord}, '%') or
            bai.agreement_code like concat('%', #{keyWord}, '%') or
            auth_person like concat('%', #{keyWord}, '%') or
            phone like concat('%', #{keyWord}, '%'))
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            <![CDATA[ AND DATE_FORMAT( bai.plan_start_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime} ]]>
        </if>
        <if test="status != null and status != ''">
            AND bai.`status` = #{status}
        </if>
        <if test="agreementCode != null  and agreementCode != ''">
            and bai.agreement_code like concat('%', #{agreementCode}, '%')
        </if>
        <if test="unitId != null  and unitId != ''">
            and bu.unit_id = #{unitId}
        </if>
        <if test="projectId != null  and projectId != ''">
            and bp.pro_id = #{projectId}
        </if>
        ORDER BY bai.agreement_id DESC
    </select>
    
    <select id="selectBmAgreementInfoByAgreementId" parameterType="Long" resultMap="BmAgreementInfoResult">
        SELECT bai.agreement_id, bai.agreement_code , contract_code,sign_time,
               bu.unit_id,bu.unit_name , bp.pro_id as projectId , bp.pro_name as projectName,
               plan_start_time,lease_day,auth_person,phone,bai.remark,bai.protocol,tta.task_id
        FROM bm_agreement_info bai
             LEFT JOIN bm_project bp ON bp.pro_id = bai.project_id
             LEFT JOIN bm_unit bu ON bu.unit_id = bai.unit_id
             LEFT JOIN (select MIN(task_id) as task_id,agreement_id from tm_task_agreement
                        where agreement_id = #{agreementId}) tta on bai.agreement_id = tta.agreement_id
        where bai.status = '1' and bai.agreement_id = #{agreementId}
    </select>
        
    <insert id="insertBmAgreementInfo" parameterType="com.bonus.material.basic.domain.BmAgreementInfo" useGeneratedKeys="true" keyProperty="agreementId">
        insert into bm_agreement_info (agreement_code, sign_time, unit_id,
                                       project_id, create_by, lease_day,
                                       plan_start_time, contract_code, auth_person,
                                       phone, create_time, update_by,
                                       update_time, remark, company_id, protocol
        )
        values (#{agreementCode,jdbcType=VARCHAR}, #{signTime,jdbcType=VARCHAR}, #{unitId,jdbcType=INTEGER},
                #{projectId,jdbcType=INTEGER}, #{createBy,jdbcType=VARCHAR}, #{leaseDay,jdbcType=INTEGER},
                #{planStartTime,jdbcType=TIMESTAMP}, #{contractCode,jdbcType=VARCHAR}, #{authPerson,jdbcType=VARCHAR},
                #{phone,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{updateBy,jdbcType=VARCHAR},
                #{updateTime,jdbcType=TIMESTAMP}, #{remark,jdbcType=VARCHAR}, #{companyId,jdbcType=INTEGER}, #{protocol,jdbcType=VARCHAR}
               )
    </insert>

    <update id="updateBmAgreementInfo" parameterType="com.bonus.material.basic.domain.BmAgreementInfo">
        update bm_agreement_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="agreementCode != null">agreement_code = #{agreementCode},</if>
            <if test="signTime != null">sign_time = #{signTime},</if>
            <if test="unitId != null">unit_id = #{unitId},</if>
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="leaseDay != null">lease_day = #{leaseDay},</if>
            <if test="planStartTime != null">plan_start_time = #{planStartTime},</if>
            <if test="contractCode != null">contract_code = #{contractCode},</if>
            <if test="authPerson != null">auth_person = #{authPerson},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="status != null">status = #{status},</if>
            <if test="protocol != null">protocol = #{protocol},</if>
            <if test="isSlt != null">is_slt = #{isSlt},</if>
        </trim>
        where agreement_id = #{agreementId}
    </update>

    <delete id="deleteBmAgreementInfoByAgreementId" parameterType="Long">
        delete from bm_agreement_info where agreement_id = #{agreementId}
    </delete>

    <delete id="deleteBmAgreementInfoByAgreementIds" parameterType="String">
        delete from bm_agreement_info where agreement_id in 
        <foreach item="agreementId" collection="array" open="(" separator="," close=")">
            #{agreementId}
        </foreach>
    </delete>

    <select id="selectNumByMonth" resultType="java.lang.Integer">
        select count(*) from bm_agreement_info where DATE_FORMAT(create_time,'%y%m') = DATE_FORMAT(#{date},'%y%m')
    </select>

    <select id="getProtocol" resultType="java.lang.String">
        select protocol
        from bm_agreement_info
        where agreement_id = #{agreementId}
    </select>
</mapper>