package com.fs.his.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fs.his.domain.FsUserInformationCollection;
import com.fs.his.dto.FsUserInformationCollectionDTO;
import com.fs.his.param.FsUserInformationCollectionListDParam;
import com.fs.his.param.UserInformationDoctorType2Param;
import com.fs.his.vo.FsUserInformationCollectionListDVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 用户信息采集Mapper接口
 *
 * @author fs
 * @date 2025-10-14
 */
public interface FsUserInformationCollectionMapper extends BaseMapper<FsUserInformationCollection>{
    /**
     * 查询用户信息采集
     *
     * @param id 用户信息采集主键
     * @return 用户信息采集
     */
    FsUserInformationCollection selectFsUserInformationCollectionById(Long id);

    FsUserInformationCollection selectFsUserInformationCollectionByUserId(Long userId);

    /**
     * 查询用户信息采集列表
     *
     * @param fsUserInformationCollection 用户信息采集
     * @return 用户信息采集集合
     */
    List<FsUserInformationCollection> selectFsUserInformationCollectionList(FsUserInformationCollection fsUserInformationCollection);

    /**
     * 新增用户信息采集
     *
     * @param fsUserInformationCollection 用户信息采集
     * @return 结果
     */
    int insertFsUserInformationCollection(FsUserInformationCollection fsUserInformationCollection);

    /**
     * 修改用户信息采集
     *
     * @param fsUserInformationCollection 用户信息采集
     * @return 结果
     */
    int updateFsUserInformationCollection(FsUserInformationCollection fsUserInformationCollection);

    @Select("UPDATE fs_user_information_collection SET package_order_code = NULL WHERE id = #{id}")
    void collectionOderCodeNULL(@Param("id") Long id);
    /**
     * 删除用户信息采集
     *
     * @param id 用户信息采集主键
     * @return 结果
     */
    int deleteFsUserInformationCollectionById(Long id);

    /**
     * 批量删除用户信息采集
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    int deleteFsUserInformationCollectionByIds(Long[] ids);

    FsUserInformationCollection selectByUserId(Long userId);
    FsUserInformationCollection selectByOrderCode(String orderCode);

    @Select({"<script> " +
            " SELECT uic.*,u.nickname userName FROM fs_user_information_collection uic LEFT JOIN fs_user u ON uic.user_id = u.user_id " +
            "WHERE doctor_id = #{maps.doctorId} and is_package = 0  and user_confirm = 1 " +
            "<if test='maps.doctorConfirm != null' > and uic.doctor_confirm = #{maps.doctorConfirm}  </if>" +
            "<if test='maps.userConfirm != null' > and uic.user_confirm = #{maps.userConfirm}  </if>" +
            "<if test='maps.userName != null ' > and u.nickname like concat('%',#{maps.userName},'%')</if>" +
            "ORDER BY create_time desc" +
            "</script>"})
    List<FsUserInformationCollectionListDVO> selectFsUserInformationCollectionListDVO(@Param("maps") FsUserInformationCollectionListDParam param);

    /**
     * 查询需要自动退款订单(用户支付超过24小时，未第二次确认的订单号)
     */
    @Select("SELECT id,package_order_code FROM fs_user_information_collection uic LEFT JOIN fs_package_order po ON uic.package_order_code = po.order_sn  WHERE uic.doctor_confirm_time <= DATE_SUB(NOW(), INTERVAL 24 HOUR) and  uic.package_order_code IS NOT NULL   AND  uic.user_confirm2 = 0 AND po.is_pay = 1")
    List<FsUserInformationCollection> selectAutoRefundOrderCode();

    /**
     * 查询需要自动确认订单(医生确认超过2小时，未第二次确认的信息采集)
     */
    @Select("SELECT * FROM fs_user_information_collection WHERE is_package = 0 AND user_confirm = 1 AND doctor_confirm = 1 AND user_confirm2 = 0 AND doctor_confirm_time <= DATE_SUB(NOW(), INTERVAL 2 HOUR)")
    List<FsUserInformationCollection> selectAutoConfirm();

    FsUserInformationCollectionDTO selectFsUserInformationCollectionDTOById(Long id);

    List<FsUserInformationCollection>selectFsUserInformationCollectionByDoctorType2(@Param("maps") UserInformationDoctorType2Param userInformationDoctorType2Param);
    List<FsUserInformationCollection>selectFsUserInformationCollectionByDoctorType1(@Param("maps") UserInformationDoctorType2Param userInformationDoctorType2Param);
    FsUserInformationCollection selectFsUserInformationCollectionByOrderCode(String orderCode);

    List<FsUserInformationCollection> selectListByIsPayAndConfirmStatus();
}
