package cn.hnthyy.thmz.mapper.thmz;

import cn.hnthyy.thmz.entity.thmz.ScheduleOfMedical;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface ScheduleOfMedicalMapper {
    /**
     * 新增科室排班档期
     *
     * @param scheduleOfMedical
     * @return
     */
    @Insert("INSERT INTO t_schedule_of_medical(dep_no,jcxm, room_num,record_date,begin_time,end_time,status,create_id, create_date,update_id,update_date) VALUES " +
            "(#{depNo,jdbcType=VARCHAR}, #{jcxm,jdbcType=VARCHAR},#{roomNum,jdbcType=INTEGER},#{recordDate,jdbcType=VARCHAR},#{beginTime,jdbcType=VARCHAR},#{endTime,jdbcType=VARCHAR}," +
            "#{status,jdbcType=INTEGER}, #{createId,jdbcType=BIGINT}, #{createDate,jdbcType=TIMESTAMP},#{updateId,jdbcType=BIGINT}, #{updateDate,jdbcType=TIMESTAMP})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insertScheduleOfMedical(ScheduleOfMedical scheduleOfMedical);




    /**
     * 批量新增科室排班档期
     *
     * @param scheduleOfMedicalList
     * @return
     */
    @Insert({
            "<script>",
            "INSERT INTO  t_schedule_of_medical(dep_no,jcxm, room_num,record_date,begin_time,end_time,status,create_id, create_date,update_id,update_date) VALUES ",
            "<foreach collection='scheduleOfMedicalList' item='item' index='index' separator=','>",
            "(#{item.depNo,jdbcType=VARCHAR}, #{item.jcxm,jdbcType=VARCHAR}, #{item.roomNum,jdbcType=INTEGER},#{item.recordDate,jdbcType=VARCHAR},#{item.beginTime,jdbcType=VARCHAR},#{item.endTime,jdbcType=VARCHAR}," +
                    "#{item.status,jdbcType=INTEGER}, #{item.createId,jdbcType=BIGINT}, #{item.createDate,jdbcType=TIMESTAMP},#{item.updateId,jdbcType=BIGINT}, #{item.updateDate,jdbcType=TIMESTAMP})",
            "</foreach>",
            "</script>"
    })
    int batchInsertScheduleOfMedical(@Param(value = "scheduleOfMedicalList") List<ScheduleOfMedical> scheduleOfMedicalList);

    /**
     * 根据通用参数查询排班
     * @param scheduleOfMedical
     * @return
     */
    @Select({"<script>",
            "select id,dep_no,jcxm, room_num,record_date,begin_time,end_time,status,create_id, create_date,update_id,update_date from t_schedule_of_medical where 1=1 " ,
            "<when test='depNo!=null'>",
            " and dep_no=#{depNo,jdbcType=VARCHAR}",
            "</when>",
            "<when test='roomNum!=null'>",
            "  and room_num=#{roomNum,jdbcType=INTEGER}",
            "</when>",
            "<when test='recordDate!=null'>",
            "  and record_date=#{recordDate,jdbcType=VARCHAR}",
            "</when>",
            "<when test='recordDates!=null'>" +
                    " and record_date in" +
                    "<foreach item='item' index='index' collection='recordDates' open='(' separator=',' close=')'>" +
                    "#{item}" +
                    "</foreach>" +
                    "</when>"+
            "</script>"})
    List<ScheduleOfMedical> selectByCommonParams(ScheduleOfMedical scheduleOfMedical);


    /**
     * 分页查询排班档期
     *
     *
     * @param scheduleOfMedical
     * @return
     */
    @Select({"<script>" +
            "select a.* " +
            " from t_schedule_of_medical a join  ("+
            "select id from t_schedule_of_medical where 1=1"+
            "<when test='depNo!=null'>",
            " and dep_no=#{depNo,jdbcType=VARCHAR}",
            "</when>",
            "<when test='roomNum!=null'>",
            "  and room_num=#{roomNum,jdbcType=INTEGER}",
            "</when>",
            "<when test='recordDate!=null'>",
            "  and record_date=#{recordDate,jdbcType=VARCHAR}",
            "</when>",
            "<when test='recordDates!=null'>" +
                    " and record_date in" +
                    "<foreach item='item' index='index' collection='recordDates' open='(' separator=',' close=')'>" +
                    "#{item}" +
                    "</foreach>" +
             "</when>"+
            " order by id desc limit #{offset},#{pageSize} )b on a.id=b.id"
            + "</script>"})
    List<ScheduleOfMedical> selectScheduleOfMedicalWithPage(ScheduleOfMedical scheduleOfMedical);


    /**
     * 查询排班档期总数
     *
     *
     * @param scheduleOfMedical
     * @return
     */
    @Select({"<script>" +
            "select count(id) from t_schedule_of_medical where 1=1"+
            "<when test='depNo!=null'>",
            " and dep_no=#{depNo,jdbcType=VARCHAR}",
            "</when>",
            "<when test='roomNum!=null'>",
            "  and room_num=#{roomNum,jdbcType=INTEGER}",
            "</when>",
            "<when test='recordDate!=null'>",
            "  and record_date=#{recordDate,jdbcType=VARCHAR}",
            "</when>",
            "<when test='recordDates!=null'>" +
                    " and record_date in" +
                    "<foreach item='item' index='index' collection='recordDates' open='(' separator=',' close=')'>" +
                    "#{item}" +
                    "</foreach>" +
                    "</when>"
                    + "</script>"})
    Integer selectCountScheduleOfMedical(ScheduleOfMedical scheduleOfMedical);


    /**
     * 更新排班档期
     * @param scheduleOfMedical
     * @return
     */
    @Update({"<script>",
            "update t_schedule_of_medical ",
            "<trim prefix='set' prefixOverrides=',' suffix=' where id =#{id,jdbcType=BIGINT} ' >",
            "<when test='depNo!=null'>",
            ",dep_no =#{depNo,jdbcType=VARCHAR}",
            "</when>",
            "<when test='jcxm!=null'>",
            ",jcxm =#{jcxm,jdbcType=VARCHAR}",
            "</when>",
            "<when test='roomNum!=null'>",
            ",room_num=#{roomNum,jdbcType=INTEGER}",
            "</when>",
            "<when test='recordDate!=null'>",
            ",record_date=#{recordDate,jdbcType=VARCHAR}",
            "</when>",
            "<when test='beginTime!=null'>",
            ",begin_time=#{beginTime,jdbcType=VARCHAR}",
            "</when>",
            "<when test='status!=null'>",
            ",status=#{status,jdbcType=INTEGER}",
            "</when>",
            "<when test='endTime!=null'>",
            ",end_time=#{endTime,jdbcType=VARCHAR}",
            "</when>",
            "<when test='updateId!=null'>",
            ",update_id=#{updateId,jdbcType=BIGINT}",
            "</when>",
            "<when test='updateDate!=null'>",
            ",update_date=#{updateDate,jdbcType=TIMESTAMP}",
            "</when>",
            "</trim>",
            "</script>"})
    int updateScheduleOfMedical(ScheduleOfMedical scheduleOfMedical);

    /**
     * 删除档期
     * @param id
     * @return
     */
    @Delete("delete from t_schedule_of_medical where id=#{id,jdbcType=BIGINT}")
    int deleteScheduleOfMedical(@Param("id") Long id);



    /**
     * 按照id查询档期
     * @param id
     * @return
     */
    @Select("select * from t_schedule_of_medical where id=#{id,jdbcType=BIGINT}")
    ScheduleOfMedical selectScheduleOfMedicalById(@Param("id") Long id);

    /**
     * 按照日期和人员查询所有科室
     * @param scheduleOfMedical
     * @return
     */
    @Select({"<script>",
            "select dep_no from t_schedule_of_medical where 1=1 ",
            "<when test='recordDates!=null'>" +
                    " and record_date in" +
                    "<foreach item='item' index='index' collection='recordDates' open='(' separator=',' close=')'>" +
                    "#{item}" +
                    "</foreach>" +
                    "</when>",
            "<when test='depNo!=null'>",
            " and dep_no=#{depNo,jdbcType=VARCHAR}",
            "</when>",
            " GROUP BY dep_no "
            ,"</script>"})
    List<String> selectDeptNos(ScheduleOfMedical scheduleOfMedical);
}
