package com.fs.his.mapper;

import java.util.List;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fs.course.param.FsFirstDiagnosisListUParam;
import com.fs.course.vo.FsFirstDiagnosisListUVO;
import com.fs.his.domain.FsFirstDiagnosis;
import com.fs.his.param.FsDiagnosisListDParam;
import com.fs.his.param.FsFirstDiagnosisParam;
import com.fs.his.vo.FsDiagnosisListDVO;
import com.fs.his.vo.FsFirstDiagnosisVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 初诊单Mapper接口
 * 
 * @author fs
 * @date 2025-09-01
 */
public interface FsFirstDiagnosisMapper extends BaseMapper<FsFirstDiagnosis>{
    /**
     * 查询初诊单
     * 
     * @param id 初诊单主键
     * @return 初诊单
     */
    FsFirstDiagnosis selectFsFirstDiagnosisById(Long id);

    /**
     * 查询初诊单列表
     * 
     * @param fsFirstDiagnosis 初诊单
     * @return 初诊单集合
     */
    List<FsFirstDiagnosis> selectFsFirstDiagnosisList(FsFirstDiagnosis fsFirstDiagnosis);

    /**
     * 新增初诊单
     * 
     * @param fsFirstDiagnosis 初诊单
     * @return 结果
     */
    int insertFsFirstDiagnosis(FsFirstDiagnosis fsFirstDiagnosis);

    /**
     * 修改初诊单
     * 
     * @param fsFirstDiagnosis 初诊单
     * @return 结果
     */
    int updateFsFirstDiagnosis(FsFirstDiagnosis fsFirstDiagnosis);

    /**
     * 删除初诊单
     * 
     * @param id 初诊单主键
     * @return 结果
     */
    int deleteFsFirstDiagnosisById(Long id);

    /**
     * 批量删除初诊单
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    int deleteFsFirstDiagnosisByIds(Long[] ids);

    /**
     * 根据用户id查询初诊单
     * @param userId 用户id
     * @return 初诊单数据
     */
    FsFirstDiagnosis getByUserId(Long userId);

    @Select({"<script> " +
            "SELECT id,doctor_id,patient_name,gender,age,phone,date_time,physical_condition,doctor_status,user_status FROM fs_first_diagnosis " +
            "WHERE 1 = 1" +
            "<if test='maps.type == 0' > and  doctor_status = 0 and doctor_id is null</if>" +
            "<if test='maps.type == 1' > and  doctor_id = #{maps.doctorId}</if>" +
            "<if test='maps.patientName != null'> and patient_name like concat('%',#{maps.patientName},'%')</if>" +
            "ORDER BY create_time desc" +
            "</script>"})
    List<FsDiagnosisListDVO> selectFsDiagnosisListDVO(@Param("maps") FsDiagnosisListDParam param);

    @Select({"<script>" +
            "SELECT fd.*,u.qw_user_name,d.sign_url FROM fs_first_diagnosis fd " +
            " LEFT JOIN qw_user u ON u.id = fd.qw_user_id " +
            " LEFT JOIN fs_doctor d ON d.doctor_id = fd.doctor_id " +
            " WHERE fd.doctor_status = 1 " +
            "<if test='param.doctorStatus != null'> AND fd.doctor_status = #{param.doctorStatus} </if>" +
            "<if test='param.userId != null'> AND fd.user_id = #{param.userId} </if>" +
            " ORDER BY fd.create_time desc " +
            "</script>"})
    List<FsFirstDiagnosisListUVO> selectFsFirstDiagnosisListUVO(@Param("param") FsFirstDiagnosisListUParam param);

    @Select({"<script>" +
            "SELECT fd.*,u.qw_user_name,d.sign_url FROM fs_first_diagnosis fd " +
            "LEFT JOIN qw_user u ON u.id = fd.qw_user_id " +
            " LEFT JOIN fs_doctor d ON d.doctor_id = fd.doctor_id " +
            "WHERE fd.id = #{id}" +
            "</script>"})
    FsFirstDiagnosisListUVO getInfo(Long id);

    @Select({"<script>" +
            "SELECT fd.*,u.nick_name userName,qu.qw_user_name FROM fs_first_diagnosis fd  " +
            " LEFT JOIN fs_user u ON u.user_id = fd.user_id " +
            " LEFT JOIN qw_user qu ON qu.id = fd.qw_user_id " +
            " WHERE 1 = 1 " +
            "<if test='param.doctorStatus != null'> AND fd.doctor_status = #{param.doctorStatus} </if>" +
            "<if test='param.userStatus != null'> AND fd.user_status = #{param.userStatus} </if>" +
            "<if test='param.doctorCertificate != null'> AND fd.doctor_certificate = #{param.doctorCertificate} </if>" +
            "<if test='param.phone != null'> AND fd.phone = like concat('%',#{param.phone},'%') </if>" +
            "<if test='param.patientName != null'> and fd.patient_name like concat('%',#{param.patientName},'%') </if>" +
            "<if test='param.doctorName != null'> and fd.doctor_name like concat('%',#{param.doctorName},'%') </if>" +
            "<if test='param.userName != null'> and u.nick_name  like concat('%',#{param.userName},'%') </if>" +
            "<if test='param.qwUserName != null'> and qu.qw_user_name like concat('%',#{param.qwUserName},'%') </if>" +
            " ORDER BY fd.create_time desc " +
            "</script>"})
    List<FsFirstDiagnosisVO> selectFsFirstDiagnosisVOList(@Param("param") FsFirstDiagnosisParam param);
}
