<?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.web.modules.diagnosis.mapper.BizMedicalResourceMapper">

    <resultMap type="com.ruoyi.web.modules.diagnosis.domain.BizMedicalResource" id="BizMedicalResourceResult">
        <id     property="id"                    column="id"/>
        <result property="resourceType"          column="resource_type"/>
        <result property="resourceName"          column="resource_name"/>
        <result property="photoFileUrl"          column="photo_file_url"/>
        <result property="photoFilePath"         column="photo_file_path"/>
        <result property="introduction"          column="introduction"/>
        <result property="contactPhone"          column="contact_phone"/>
        <result property="affiliatedUnit"        column="affiliated_unit"/>
        <result property="detailAddress"         column="detail_address"/>
        <result property="consultModes"          column="consult_modes"/>
        <result property="serviceArea"           column="service_area"/>
        <result property="feeStandard"           column="fee_standard"/>
        <result property="serviceStartTime"      column="service_start_time"/>
        <result property="serviceEndTime"        column="service_end_time"/>
        <result property="serviceWeekdays"       column="service_weekdays"/>
        <result property="maxDailyAppointments"  column="max_daily_appointments"/>
        <result property="establishDate"         column="establish_date"/>
        <result property="teamSize"              column="team_size"/>
        <result property="teamMembers"           column="team_members"/>
        <result property="personInCharge"        column="person_in_charge"/>
        <result property="orgLevel"              column="org_level"/>
        <result property="equipmentModel"        column="equipment_model"/>
        <result property="longitude"             column="longitude"/>
        <result property="latitude"              column="latitude"/>
        <result property="publishStatus"         column="publish_status"/>
        <result property="publishTime"           column="publish_time"/>
        <result property="offlineTime"           column="offline_time"/>
        <result property="accountAssigned"       column="account_assigned"/>
        <result property="sysUserId"             column="sys_user_id"/>
        <result property="assignedLoginName"     column="assigned_login_name"/>
        <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="selectVo">
        select id, resource_type, resource_name, photo_file_url, photo_file_path, introduction,
               contact_phone, affiliated_unit, detail_address, consult_modes, service_area,
               fee_standard, service_start_time, service_end_time, service_weekdays,
               max_daily_appointments, establish_date, team_size, team_members, person_in_charge,
               org_level, equipment_model, longitude, latitude,
               publish_status, publish_time, offline_time,
               account_assigned, sys_user_id, assigned_login_name, del_flag,
               create_by, create_time, update_by, update_time, remark
        from biz_medical_resource
    </sql>

    <select id="selectBizMedicalResourceById" parameterType="long" resultMap="BizMedicalResourceResult">
        <include refid="selectVo"/>
        where id = #{id} and del_flag = '0'
    </select>

    <select id="selectBizMedicalResourceByIdAny" parameterType="long" resultMap="BizMedicalResourceResult">
        <include refid="selectVo"/>
        where id = #{id}
    </select>

    <select id="selectVetResourceBySysUserId" resultMap="BizMedicalResourceResult">
        <include refid="selectVo"/>
        where sys_user_id = #{sysUserId}
          and resource_type = '004001'
          and account_assigned = 1
          and del_flag = '0'
        limit 1
    </select>

    <select id="selectOrgResourceBySysUserId" resultMap="BizMedicalResourceResult">
        <include refid="selectVo"/>
        where sys_user_id = #{sysUserId}
          and resource_type = '004003'
          and account_assigned = 1
          and del_flag = '0'
        limit 1
    </select>

    <select id="countBySysUserId" resultType="int">
        select count(1)
        from biz_medical_resource
        where sys_user_id = #{sysUserId}
          and del_flag = '0'
    </select>

    <select id="selectBizMedicalResourceList" parameterType="com.ruoyi.web.modules.diagnosis.domain.BizMedicalResource" resultMap="BizMedicalResourceResult">
        <include refid="selectVo"/>
        <where>
            del_flag = '0'
            <if test="resourceType != null">
                and resource_type = #{resourceType}
            </if>
            <if test="resourceName != null and resourceName != ''">
                and resource_name like concat('%', #{resourceName}, '%')
            </if>
            <if test="queryPublishStatus != null and queryPublishStatus == 0">
                and publish_status = 0 and offline_time is null
            </if>
            <if test="queryPublishStatus != null and queryPublishStatus == 1">
                and publish_status = 1
            </if>
            <if test="queryPublishStatus != null and queryPublishStatus == 2">
                and publish_status = 0 and offline_time is not null
            </if>
        </where>
        order by create_time desc, id desc
    </select>

    <insert id="insertBizMedicalResource" parameterType="com.ruoyi.web.modules.diagnosis.domain.BizMedicalResource" useGeneratedKeys="true" keyProperty="id">
        insert into biz_medical_resource (
            resource_type, resource_name, photo_file_url, photo_file_path, introduction,
            contact_phone, affiliated_unit, detail_address, consult_modes, service_area,
            fee_standard, service_start_time, service_end_time, service_weekdays,
            max_daily_appointments, establish_date, team_size, team_members, person_in_charge,
            org_level, equipment_model, longitude, latitude,
            publish_status, publish_time, offline_time,
            account_assigned, sys_user_id, assigned_login_name, del_flag,
            create_by, create_time, remark
        ) values (
            #{resourceType}, #{resourceName}, #{photoFileUrl}, #{photoFilePath}, #{introduction},
            #{contactPhone}, #{affiliatedUnit}, #{detailAddress}, #{consultModes}, #{serviceArea},
            #{feeStandard}, #{serviceStartTime}, #{serviceEndTime}, #{serviceWeekdays},
            #{maxDailyAppointments}, #{establishDate}, #{teamSize}, #{teamMembers}, #{personInCharge},
            #{orgLevel}, #{equipmentModel}, #{longitude}, #{latitude},
            #{publishStatus}, #{publishTime}, #{offlineTime},
            #{accountAssigned}, #{sysUserId}, #{assignedLoginName}, #{delFlag},
            #{createBy}, sysdate(), #{remark}
        )
    </insert>

    <update id="updateBizMedicalResource" parameterType="com.ruoyi.web.modules.diagnosis.domain.BizMedicalResource">
        update biz_medical_resource set
            resource_name = #{resourceName},
            photo_file_url = #{photoFileUrl},
            photo_file_path = #{photoFilePath},
            introduction = #{introduction},
            contact_phone = #{contactPhone},
            affiliated_unit = #{affiliatedUnit},
            detail_address = #{detailAddress},
            consult_modes = #{consultModes},
            service_area = #{serviceArea},
            fee_standard = #{feeStandard},
            service_start_time = #{serviceStartTime},
            service_end_time = #{serviceEndTime},
            service_weekdays = #{serviceWeekdays},
            max_daily_appointments = #{maxDailyAppointments},
            establish_date = #{establishDate},
            team_size = #{teamSize},
            team_members = #{teamMembers},
            person_in_charge = #{personInCharge},
            org_level = #{orgLevel},
            equipment_model = #{equipmentModel},
            longitude = #{longitude},
            latitude = #{latitude},
            publish_status = #{publishStatus},
            publish_time = #{publishTime},
            offline_time = #{offlineTime},
            account_assigned = #{accountAssigned},
            sys_user_id = #{sysUserId},
            assigned_login_name = #{assignedLoginName},
            update_by = #{updateBy},
            update_time = sysdate(),
            remark = #{remark}
        where id = #{id} and del_flag = '0'
    </update>

    <update id="logicDeleteBizMedicalResourceByIds">
        update biz_medical_resource set del_flag = '2', update_by = #{updateBy}, update_time = sysdate()
        where del_flag = '0' and publish_status = 0 and id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
</mapper>
