<?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.sgzb.material.mapper.AgreementInfoMapper">

    <resultMap type="com.bonus.sgzb.material.domain.AgreementInfo" 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="projectId" column="project_id"/>
        <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="protocol" column="protocol"/>
        <result property="companyId" column="company_id"/>
        <result property="fileName" column="file_name"/>
        <result property="fileUrl" column="file_url"/>
    </resultMap>

    <insert id="add">
        insert into bm_agreement_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="agreementCode != null">agreement_code,</if>
            <if test="signTime != null and signTime != ''">sign_time,</if>
            <if test="unitId != null">unit_id,</if>
            <if test="projectId != null">project_id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="leaseDay != null">lease_day,</if>
            <if test="planStartTime != null">plan_start_time,</if>
            <if test="contractCode != null">contract_code,</if>
            <if test="authPerson != null">auth_person,</if>
            <if test="phone != null">phone,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="protocol != null">protocol,</if>
            update_time,
            <if test="remark != null">remark,</if>
            <if test="companyId != null">company_id,</if>
            <if test="fileName != null">file_name,</if>
            <if test="fileUrl != null">file_url,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="agreementCode != null">#{agreementCode},</if>
            <if test="signTime != null and signTime != ''">#{signTime},</if>
            <if test="unitId != null">#{unitId},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="leaseDay != null">#{leaseDay},</if>
            <if test="planStartTime != null">#{planStartTime},</if>
            <if test="contractCode != null">#{contractCode},</if>
            <if test="authPerson != null">#{authPerson},</if>
            <if test="phone != null">#{phone},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="protocol != null">#{protocol},</if>
            NOW(),
            <if test="remark != null">#{remark},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="fileName != null">#{fileName},</if>
            <if test="fileUrl != null">#{fileUrl},</if>
            NOW()
        </trim>

    </insert>

    <update id="update">
        update bm_agreement_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="signTime != null and signTime != ''">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>
            lease_day = #{leaseDay},
            <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="protocol != null">protocol = #{protocol},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="fileName != null">file_name = #{fileName},</if>
            <if test="fileUrl != null">file_url = #{fileUrl},</if>
        </trim>
        where agreement_id = #{agreementId}
    </update>

    <update id="deleteByIds">
        UPDATE bm_agreement_info SET status = '0' WHERE agreement_id = #{agreementId}
    </update>

    <select id="getAgreementInfoAll" resultType="com.bonus.sgzb.material.domain.AgreementInfo">
        SELECT bai.agreement_id, bai.agreement_code , contract_code,file_url ,file_name,sign_time,
               bui.unit_id,bui.unit_name , bp.lot_id as projectId , bp.lot_name as projectName,
               plan_start_time,lease_day,auth_person,phone,bai.remark,bai.protocol
        FROM bm_agreement_info bai
         LEFT JOIN bm_project_lot bp ON bp.lot_id = bai.project_id
         LEFT JOIN bm_unit_info bui ON bui.unit_id = bai.unit_id
        where bai.status = '1'
        <if test="keyWord != null  and keyWord != ''">
            and (bai.contract_code like concat('%', #{keyWord}, '%') or
            auth_person like concat('%', #{keyWord}, '%') or
            phone like concat('%', #{keyWord}, '%'))
        </if>

        <if test="agreementCode != null  and agreementCode != ''">
            and bai.agreement_code like concat('%', #{agreementCode}, '%')
        </if>
        <if test="unitId != null  and unitId != ''">
            and bui.unit_id = #{unitId}
        </if>
        <if test="projectId != null  and projectId != ''">
            and bp.lot_id = #{projectId}
        </if>
        <if test="dataCondition != null and dataCondition.size()>0">
            AND bai.agreement_id in
            <foreach collection="dataCondition" item="agreementId" index="index" open="(" separator="," close=")">
                #{agreementId}
            </foreach>
        </if>
        ORDER BY bai.agreement_id DESC
    </select>

    <select id="getUnitList" resultType="com.bonus.sgzb.material.domain.AgreementInfo">
        SELECT unit_id , unit_name FROM bm_unit_info
        WHERE del_flag = '0' and status = '0'
    </select>

    <select id="getProjectList" resultType="com.bonus.sgzb.material.domain.AgreementInfo">
        SELECT lot_id as projectId,lot_name as projectName  FROM `bm_project_lot`
        WHERE del_flag = '0' and status = '0'
    </select>

    <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="getAgreementInfoId" resultType="com.bonus.sgzb.material.domain.AgreementInfo">
        SELECT * FROM bm_agreement_info
        WHERE agreement_id = #{agreementId}
    </select>

    <select id="getInfo" resultType="com.bonus.sgzb.material.domain.AgreementInfo">
        SELECT * FROM `bm_agreement_info`
        WHERE status = '1' and unit_id = #{unitId} AND project_id = #{projectId}
    </select>

    <select id="selectByagreementId" resultType="java.lang.Integer">
        SELECT
            COUNT(*)
        FROM
            tm_task_agreement tta
        WHERE
            tta.agreement_id=#{agreementId}
    </select>
    <select id="getAllAgreementId" resultType="com.bonus.sgzb.material.domain.AgreementInfo">
        SELECT
            agreement_id as agreementId
        FROM
            bm_agreement_info
        WHERE
            status = '1'
    </select>

</mapper>