package com.fs.his.mapper;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fs.his.domain.FsPrescribeRecord;
import com.fs.his.dto.FsPrescribeRecordRejectDto;
import com.fs.his.vo.FsPrescribeRecordRejectVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 处方记录Mapper接口
 * 
 * @author fs
 * @date 2025-11-02
 */
public interface FsPrescribeRecordMapper extends BaseMapper<FsPrescribeRecord>{
    /**
     * 查询处方记录
     * 
     * @param id 处方记录主键
     * @return 处方记录
     */
    FsPrescribeRecord selectFsPrescribeRecordById(Long id);

    /**
     * 查询处方记录列表
     * 
     * @return 处方记录集合
     */
    List<FsPrescribeRecord> selectFsPrescribeRecordList(FsPrescribeRecord prescribeRecord);

    /**
     * 新增处方记录
     * 
     * @return 结果
     */
    int insertFsPrescribeRecord(FsPrescribeRecord prescribeRecord);

    /**
     * 修改处方记录
     * 
     * @return 结果
     */
    int updateFsPrescribeRecord(FsPrescribeRecord prescribeRecord);

    /**
     * 删除处方记录
     * 
     * @param id 处方记录主键
     * @return 结果
     */
    int deleteFsPrescribeRecordById(Long id);

    /**
     * 批量删除处方记录
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    int deleteFsPrescribeRecordByIds(Long[] ids);

    @Select(" SELECT count(id)  FROM fs_prescibe_record WHERE prescribe_id = #{prescribeId} AND `status` = 2 ")
    Long selectRefuseCount(@Param("prescribeId") Long prescribeId);

    @Select("SELECT * FROM fs_prescibe_record WHERE prescribe_id = #{prescribeId}  ")
    List<FsPrescribeRecord> selectRecordListByPrescribeId(@Param("prescribeId") Long prescribeId);

    @Select("SELECT \n" +
            "    COUNT(*) AS total_count,\n" +
            "    SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) AS rejected_count,\n" +
            "    CASE \n" +
            "        WHEN COUNT(*) = 0 THEN 0\n" +
            "        ELSE ROUND(\n" +
            "            SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) * 100.0 / COUNT(*), \n" +
            "            2\n" +
            "        )\n" +
            "    END AS percentage\n" +
            "FROM fs_prescibe_record WHERE doctor_id = #{doctorId} and  create_time > '2025-12-22'")
    Map<String,Object> selectDoctorRefusePercentage(@Param("doctorId") Long doctorId);

    //统计拒方数量
    List<FsPrescribeRecordRejectVo> calculateRejectCount(FsPrescribeRecordRejectDto rejectDto);
}
