<?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.zlpt.company.mapper.ServiceGreementInfoMapper">

    <insert id="insertConfig" keyColumn="service_id" keyProperty="serviceId" parameterType="com.bonus.zlpt.company.domain.dto.ServiceGreementDto" useGeneratedKeys="true">
        insert into service_greement_info (
        <if test="serviceName != null and serviceName != '' ">service_name,</if>
        <if test="userId != null and userId != '' ">user_id,</if>
        status,
        create_time
        )values(
        <if test="serviceName != null and serviceName != ''">#{serviceName},</if>
        <if test="userId != null and userId != ''">#{userId},</if>
        '0',
        sysdate()
        )
    </insert>

    <insert id="insertAttachment">
        insert into sys_file_info (
        <if test="modelId != null and modelId != '' ">model_id,</if>
        <if test="fileUrl != null and fileUrl != '' ">file_url,</if>
        create_time
        )values(
        <if test="modelId != null and modelId != '' ">#{modelId},</if>
        <if test="fileUrl != null and fileUrl != '' ">#{fileUrl},</if>
        sysdate()
         )
    </insert>

    <update id="update">
        update service_greement_info
        <set>
            <if test="serviceName != null and serviceName != ''">service_name = #{serviceName},</if>
            <if test="userId != null and userId != ''">userId = #{userId},</if>
            <if test="status != null and status != ''">status = #{status},</if>
        </set>
            where service_id = #{serviceId}
    </update>

    <update id="updateAttachment">
        update sys_file_info
        <set>
            <if test="fileUrl != null and fileUrl != ''">file_url = #{fileUrl},</if>
        </set>
            where model_id = #{modelId}
    </update>

    <delete id="delete">
        delete from service_greement_info
        where service_id = #{id}
    </delete>

    <delete id="deleteById">
        delete from sys_file_info
        where model_id = #{id}
    </delete>

    <select id="selectListById" resultType="com.bonus.zlpt.company.domain.vo.ServiceGreementVo">
        select sg.service_id AS serviceId, sg.service_name AS serviceName, sg.status AS status, sf.file_url AS fileUrl, sg.create_time AS createTime
        from service_greement_info sg
                 left join sys_file_info sf on sg.service_id = sf.model_id
        where sg.service_id = #{id}
    </select>

    <select id="selectGreementInfoList" resultType="com.bonus.zlpt.company.domain.vo.ServiceGreementVo">
        select sg.service_id AS serviceId, sg.service_name AS serviceName, sg.status AS status, sf.file_url AS fileUrl, sg.create_time AS createTime
        from service_greement_info sg
        left join sys_file_info sf on sg.service_id = sf.model_id
        <where>
            <if test="status != null and status != ''">
                AND sg.status = #{status}
            </if>
        </where>
    </select>
</mapper>