<?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.BmProjectMapper">
    <resultMap type="com.bonus.material.basic.domain.BmProject" id="BmProjectResult">
        <result property="proId"    column="pro_id"    />
        <result property="proName"    column="pro_name"    />
        <result property="externalId"    column="external_id"    />
        <result property="externalInfo"    column="external_info"    />
        <result property="proTypeId"    column="pro_type_id"    />
        <result property="lon"    column="lon"    />
        <result property="lat"    column="lat"    />
        <result property="proManager"    column="pro_manager"    />
        <result property="telphone"    column="telphone"    />
        <result property="planStartDate"    column="plan_start_date"    />
        <result property="planEndDate"    column="plan_end_date"    />
        <result property="actualStartDate"    column="actual_start_date"    />
        <result property="actualEndDate"    column="actual_end_date"    />
        <result property="createBy"    column="create_by"    />
        <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="delFlag"    column="del_flag"    />
        <result property="proCode"    column="pro_code"    />
        <result property="impUnit"    column="imp_unit"    />
        <result property="proNature"    column="pro_nature"    />
        <result property="proStatus"    column="pro_status"    />
        <result property="contractPart"    column="contract_part"    />
        <result property="proCenter"    column="pro_center"    />
    </resultMap>

    <sql id="selectBmProjectVo">
        select bp.pro_id,
               bp.pro_name,
               bp.external_id,
               bp.external_info,
               bp.pro_type_id,
               bp.lon,
               bp.lat,
               bp.pro_manager,
               bp.telphone,
               bp.plan_start_date,
               bp.plan_end_date,
               bp.actual_start_date,
               bp.actual_end_date,
               bp.create_by,
               bp.create_time,
               bp.update_by,
               bp.update_time,
               bp.remark,
               bp.del_flag,
               bp.pro_code,
               bp.imp_unit,
               bp.pro_nature,
               bp.pro_status,
               bp.contract_part,
               bp.pro_center
        from bm_project bp
    </sql>

    <select id="selectBmProjectList" parameterType="com.bonus.material.basic.domain.BmProject" resultMap="BmProjectResult">
        <include refid="selectBmProjectVo"/>
        <where>
            bp.del_flag = 0
            <if test="proName != null  and proName != ''"> and pro_name like concat('%', #{proName}, '%')</if>
            <if test="externalId != null  and externalId != ''"> and external_id = #{externalId}</if>
            <if test="externalInfo != null  and externalInfo != ''"> and external_info = #{externalInfo}</if>
            <if test="proTypeId != null  and proTypeId != ''"> and pro_type_id = #{proTypeId}</if>
            <if test="lon != null  and lon != ''"> and lon = #{lon}</if>
            <if test="lat != null  and lat != ''"> and lat = #{lat}</if>
            <if test="proManager != null  and proManager != ''"> and pro_manager = #{proManager}</if>
            <if test="telphone != null  and telphone != ''"> and telphone = #{telphone}</if>
            <if test="planStartDate != null "> and plan_start_date = #{planStartDate}</if>
            <if test="planEndDate != null "> and plan_end_date = #{planEndDate}</if>
            <if test="actualStartDate != null "> and actual_start_date = #{actualStartDate}</if>
            <if test="actualEndDate != null "> and actual_end_date = #{actualEndDate}</if>
        </where>
        order by bp.create_time desc
    </select>
    
    <select id="selectBmProjectByProId" parameterType="Long" resultMap="BmProjectResult">
        <include refid="selectBmProjectVo"/>
        where del_flag = 0 and pro_id = #{proId}
    </select>

    <select id="selectBmProjectByProName" resultType="com.bonus.material.basic.domain.BmProject">
        select bp.pro_id as proId,
               bp.pro_name as proName,
               bp.pro_code as proCode
        from bm_project bp
        where bp.del_flag = '0'
        <if test="proName != null and proName != ''">
            <if test="proCode != null and proCode != ''">
                and (bp.pro_name = #{proName} or bp.pro_code = #{proCode})
            </if>
            <if test="proCode == null or proCode == ''">
                and bp.pro_name = #{proName}
            </if>
        </if>
        <if test="proName == null or proName == ''">
            <if test="proCode != null and proCode != ''">
                and bp.pro_code = #{proCode}
            </if>
        </if>
    </select>

    <insert id="insertBmProject" parameterType="com.bonus.material.basic.domain.BmProject" useGeneratedKeys="true" keyProperty="proId">
        insert into bm_project
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="proId != null">pro_id,</if>
            <if test="proName != null and proName != ''">pro_name,</if>
            <if test="externalId != null">external_id,</if>
            <if test="externalInfo != null">external_info,</if>
            <if test="proTypeId != null">pro_type_id,</if>
            <if test="lon != null">lon,</if>
            <if test="lat != null">lat,</if>
            <if test="proManager != null">pro_manager,</if>
            <if test="telphone != null">telphone,</if>
            <if test="planStartDate != null">plan_start_date,</if>
            <if test="planEndDate != null">plan_end_date,</if>
            <if test="actualStartDate != null">actual_start_date,</if>
            <if test="actualEndDate != null">actual_end_date,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            del_flag,
            <if test="proCode != null">pro_code,</if>
            <if test="impUnit != null">imp_unit,</if>
            <if test="proNature != null">pro_nature,</if>
            <if test="proStatus != null">pro_status,</if>
            <if test="contractPart != null">contract_part,</if>
            <if test="proCenter != null">pro_center</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="proId != null">#{proId},</if>
            <if test="proName != null and proName != ''">#{proName},</if>
            <if test="externalId != null">#{externalId},</if>
            <if test="externalInfo != null">#{externalInfo},</if>
            <if test="proTypeId != null">#{proTypeId},</if>
            <if test="lon != null">#{lon},</if>
            <if test="lat != null">#{lat},</if>
            <if test="proManager != null">#{proManager},</if>
            <if test="telphone != null">#{telphone},</if>
            <if test="planStartDate != null">#{planStartDate},</if>
            <if test="planEndDate != null">#{planEndDate},</if>
            <if test="actualStartDate != null">#{actualStartDate},</if>
            <if test="actualEndDate != null">#{actualEndDate},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            0,
            <if test="proCode != null">#{proCode},</if>
            <if test="impUnit != null">#{impUnit},</if>
            <if test="proNature != null">#{proNature},</if>
            <if test="proStatus != null">#{proStatus},</if>
            <if test="contractPart != null">#{contractPart},</if>
            <if test="proCenter != null">#{proCenter}</if>
         </trim>
    </insert>

    <update id="updateBmProject" parameterType="com.bonus.material.basic.domain.BmProject">
        update bm_project
        <trim prefix="SET" suffixOverrides=",">
            <if test="proName != null and proName != ''">pro_name = #{proName},</if>
            <if test="externalId != null">external_id = #{externalId},</if>
            <if test="externalInfo != null">external_info = #{externalInfo},</if>
            <if test="proTypeId != null">pro_type_id = #{proTypeId},</if>
            <if test="lon != null">lon = #{lon},</if>
            <if test="lat != null">lat = #{lat},</if>
            <if test="proManager != null">pro_manager = #{proManager},</if>
            <if test="telphone != null">telphone = #{telphone},</if>
            <if test="planStartDate != null">plan_start_date = #{planStartDate},</if>
            <if test="planEndDate != null">plan_end_date = #{planEndDate},</if>
            <if test="actualStartDate != null">actual_start_date = #{actualStartDate},</if>
            <if test="actualEndDate != null">actual_end_date = #{actualEndDate},</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="proCode != null">pro_code = #{proCode},</if>
            <if test="impUnit != null">imp_unit = #{impUnit},</if>
            <if test="proNature != null">pro_nature = #{proNature},</if>
            <if test="proStatus != null">pro_status = #{proStatus},</if>
            <if test="contractPart != null">contract_part = #{contractPart},</if>
            <if test="proCenter != null">pro_center = #{proCenter}</if>
        </trim>
        where pro_id = #{proId}
    </update>

    <delete id="deleteBmProjectByProId" parameterType="Long">
        update bm_project set del_flag = 2 where pro_id = #{proId}
    </delete>

</mapper>