<?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.ruoyi.care.mapper.CareCompaniesMapper">
    
    <resultMap type="CareCompanies" id="CareCompaniesResult">
        <result property="companyId"    column="company_id"    />
        <result property="name"    column="name"    />
        <result property="address"    column="address"    />
        <result property="legalPerson"    column="legal_person"    />
        <result property="phoneNumber"    column="phone_number"    />
        <result property="email"    column="email"    />
        <result property="dispatchPersonNumber"    column="dispatch_person_number"    />
        <result property="userId"    column="user_id"    />
        <result property="loginName"    column="login_name"    />
        <result property="status"    column="status"    />
        <result property="delFlag"    column="del_flag"    />
        <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"    />
    </resultMap>

    <sql id="selectCareCompaniesVo">
        select company_id, name, address, legal_person, phone_number, email, dispatch_person_number, user_id, status, del_flag, create_by, create_time, update_by, update_time, remark from care_companies
    </sql>

    <select id="selectCareCompaniesList" parameterType="CareCompanies" resultMap="CareCompaniesResult">
        SELECT
        c.company_id,
        c.NAME,
        c.address,
        c.legal_person,
        c.phone_number,
        c.email,
        c.dispatch_person_number,
        c.user_id,
        c.STATUS,
        c.del_flag,
        c.create_by,
        c.create_time,
        c.update_by,
        c.update_time,
        c.remark,
        u.user_name login_name
        FROM
        care_companies c
        LEFT JOIN sys_user u ON u.user_id=c.user_id
        <where>
            <if test="1==1"> and c.del_flag = '0'</if>
            <if test="name != null  and name != ''"> and c.name like concat('%', #{name}, '%')</if>
            <if test="address != null  and address != ''"> and c.address = #{address}</if>
            <if test="legalPerson != null  and legalPerson != ''"> and c.legal_person = #{legalPerson}</if>
            <if test="phoneNumber != null  and phoneNumber != ''"> and c.phone_number = #{phoneNumber}</if>
            <if test="email != null  and email != ''"> and c.email = #{email}</if>
            <if test="dispatchPersonNumber != null "> and c.dispatch_person_number = #{dispatchPersonNumber}</if>
            <if test="userId != null "> and c.user_id = #{userId}</if>
            <if test="status != null  and status != ''"> and c.status = #{status}</if>
        </where>
    </select>
    
    <select id="selectCareCompaniesByCompanyId" parameterType="Long" resultMap="CareCompaniesResult">
        SELECT
            c.company_id,
            c.NAME,
            c.address,
            c.legal_person,
            c.phone_number,
            c.email,
            c.dispatch_person_number,
            c.user_id,
            c.STATUS,
            c.del_flag,
            c.create_by,
            c.create_time,
            c.update_by,
            c.update_time,
            c.remark,
            u.user_name login_name
        FROM
            care_companies c
                LEFT JOIN sys_user u ON u.user_id=c.user_id
        where c.company_id = #{companyId} and c.del_flag = '0'
    </select>

    <insert id="insertCareCompanies" parameterType="CareCompanies" useGeneratedKeys="true" keyProperty="companyId">
        insert into care_companies
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="address != null">address,</if>
            <if test="legalPerson != null">legal_person,</if>
            <if test="phoneNumber != null">phone_number,</if>
            <if test="email != null">email,</if>
            <if test="dispatchPersonNumber != null">dispatch_person_number,</if>
            <if test="userId != null">user_id,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="delFlag != null">del_flag,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="address != null">#{address},</if>
            <if test="legalPerson != null">#{legalPerson},</if>
            <if test="phoneNumber != null">#{phoneNumber},</if>
            <if test="email != null">#{email},</if>
            <if test="dispatchPersonNumber != null">#{dispatchPersonNumber},</if>
            <if test="userId != null">#{userId},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="delFlag != null">#{delFlag},</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>
         </trim>
    </insert>

    <update id="updateCareCompanies" parameterType="CareCompanies">
        update care_companies
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="address != null">address = #{address},</if>
            <if test="legalPerson != null">legal_person = #{legalPerson},</if>
            <if test="phoneNumber != null">phone_number = #{phoneNumber},</if>
            <if test="email != null">email = #{email},</if>
            <if test="dispatchPersonNumber != null">dispatch_person_number = #{dispatchPersonNumber},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</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>
        </trim>
        where company_id = #{companyId}
    </update>

    <delete id="deleteCareCompaniesByCompanyId" parameterType="Long">
        update care_companies set del_flag = '2' where company_id = #{companyId}
    </delete>

    <delete id="deleteCareCompaniesByCompanyIds" parameterType="String">
        update care_companies set del_flag = '2' where company_id in
        <foreach item="companyId" collection="array" open="(" separator="," close=")">
            #{companyId}
        </foreach>
    </delete>
</mapper>