package thyyxxk.webserver.dao.his.outpatient.interactive.clinicsatisfied;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import thyyxxk.webserver.entity.outpatient.interactive.clinicsatisfied.request.ModifyStatus;
import thyyxxk.webserver.entity.outpatient.interactive.clinicsatisfied.response.BriefSuggestion;
import thyyxxk.webserver.entity.outpatient.interactive.clinicsatisfied.response.SatisfiedBody;

import java.util.Date;
import java.util.List;

@Mapper
public interface ClinicSatisfiedDao {
    @Select("select * from " +
            "(SELECT ROW_NUMBER() OVER(ORDER BY id) AS idx,* " +
            "from ${table} a ${ew.customSqlSegment}) t " +
            "where t.idx>(#{page}-1)*#{size} and t.idx<=#{page}*#{size}")
    List<SatisfiedBody> selectSatisfiedBodies(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper,
                                              @Param("page") int page, @Param("size") int size,
                                              @Param("table") String table);

    @Select("select count(1) from ${table} ${ew.customSqlSegment}")
    int selectValidCount(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper,
                         @Param("table") String table);

    @Update("update ${table} set deleted=#{deleted},delete_staff=#{staff},delete_time=#{date} where id=#{id}")
    int updateAssessmentStatus(ModifyStatus status);

    @Select("select * from ${table} ${ew.customSqlSegment} order by id")
    List<SatisfiedBody> selectAllSatisfiedBodies(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper,
                                                 @Param("table") String table);

    @Select("select count(1) from ${table} ${ew.customSqlSegment}")
    int selectUnsatisfiedCount(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper,
                               @Param("table") String table);

    @Select("select id,inpatient_no,patient_id,create_datetime,suggestion, " +
            "patName=(select rtrim(d.name) from mz_patient_mi d where d.patient_id=a.patient_id) " +
            "from ${table} a ${ew.customSqlSegment} and suggestion is not null order by id")
    List<BriefSuggestion> selectSuggestions(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper,
                                            @Param("table") String table);
}
