<?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.fs.company.mapper.CompanyMapper">

    <resultMap type="Company" id="CompanyResult">
        <result property="companyId"    column="company_id"    />
        <result property="companyName"    column="company_name"    />
        <result property="companyMobile"    column="company_mobile"    />
        <result property="companyAddress"    column="company_address"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="status"    column="status"    />
        <result property="startTime"    column="start_time"    />
        <result property="limitTime"    column="limit_time"    />
        <result property="money"    column="money"    />
        <result property="tuiMoney"    column="tui_money"    />
        <result property="companyType"    column="company_type"    />
        <result property="userId"    column="user_id"    />
        <result property="appId"    column="app_id"    />
        <result property="appKey"    column="app_key"    />
        <result property="remark"    column="remark"    />
        <result property="linkName"    column="link_name"    />
        <result property="limitUserCount"    column="limit_user_count"    />
        <result property="isDel"    column="is_del"    />

    </resultMap>

    <sql id="selectCompanyVo">
        select company_id, company_name,manager, company_mobile, company_address, create_time, update_time, status, start_time, limit_time, money ,tui_money, company_type,user_id,app_id,app_key,remark,link_name,limit_user_count,is_del  from company
    </sql>

    <select id="selectCompanyList" parameterType="Company" resultMap="CompanyResult">
        <include refid="selectCompanyVo"/>
        <where>
            <if test="companyName != null  and companyName != ''"> and company_name like concat('%', #{companyName}, '%')</if>
            <if test="companyMobile != null  and companyMobile != ''"> and company_mobile = #{companyMobile}</if>
            <if test="companyAddress != null  and companyAddress != ''"> and company_address = #{companyAddress}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="money != null "> and money = #{money}</if>
            <if test="isDel != null "> and is_del = #{isDel}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectCompanyById" parameterType="Long" resultMap="CompanyResult">
        <include refid="selectCompanyVo"/>
        where company_id = #{companyId}
    </select>

    <insert id="insertCompany" parameterType="Company" useGeneratedKeys="true" keyProperty="companyId">
        insert into company
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyName != null">company_name,</if>
            <if test="companyMobile != null">company_mobile,</if>
            <if test="companyAddress != null">company_address,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="status != null">status,</if>
            <if test="startTime != null">start_time,</if>
            <if test="limitTime != null">limit_time,</if>
            <if test="money != null">money,</if>
            <if test="tuiMoney != null">tui_money,</if>
            <if test="companyType != null">company_type,</if>
            <if test="userId != null">user_id,</if>
            <if test="appId != null">app_id,</if>
            <if test="appKey != null">app_key,</if>
            <if test="remark != null">remark,</if>
            <if test="linkName != null">link_name,</if>
            <if test="limitUserCount != null">limit_user_count,</if>
            <if test="isDel != null">is_del,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="companyName != null">#{companyName},</if>
            <if test="companyMobile != null">#{companyMobile},</if>
            <if test="companyAddress != null">#{companyAddress},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="status != null">#{status},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="limitTime != null">#{limitTime},</if>
            <if test="money != null">#{money},</if>
            <if test="tuiMoney != null">#{tuiMoney},</if>
            <if test="companyType != null">#{companyType},</if>
            <if test="userId != null">#{userId},</if>
            <if test="appId != null">#{appId},</if>
            <if test="appKey != null">#{appKey},</if>
            <if test="remark != null">#{remark},</if>
            <if test="linkName != null">#{linkName},</if>
            <if test="limitUserCount != null">#{limitUserCount},</if>
            <if test="isDel != null">#{isDel},</if>
         </trim>
    </insert>

    <update id="updateCompany" parameterType="Company">
        update company
        <trim prefix="SET" suffixOverrides=",">
            <if test="companyName != null">company_name = #{companyName},</if>
            <if test="companyMobile != null">company_mobile = #{companyMobile},</if>
            <if test="companyAddress != null">company_address = #{companyAddress},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="limitTime != null">limit_time = #{limitTime},</if>
            <if test="money != null">money = #{money},</if>
            <if test="tuiMoney != null">tui_money = #{tuiMoney},</if>
            <if test="companyType != null">company_type = #{companyType},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="appId != null">app_id = #{appId},</if>
            <if test="appKey != null">app_key = #{appKey},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="linkName != null">link_name = #{linkName},</if>
            <if test="limitUserCount != null">limit_user_count = #{limitUserCount},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
        </trim>
        where company_id = #{companyId}
    </update>

    <delete id="deleteCompanyById" parameterType="Long">
        update  company set  is_del=1 where company_id = #{companyId}
    </delete>

    <delete id="deleteCompanyByIds" parameterType="String">
        update  company set  is_del=1 where company_id in
        <foreach item="companyId" collection="array" open="(" separator="," close=")">
            #{companyId}
        </foreach>
    </delete>



    <select id="selectCompanyOptions" resultType="com.fs.company.vo.CompanyOptionRespVO">
        select
        company_id as companyId,
        company_name as companyName
        from company
        where is_del = 0
        <if test="companyName != null and companyName != ''">
            and company_name like concat('%', #{companyName}, '%')
        </if>
        order by company_id desc
        limit 20
    </select>


</mapper>
