package cn.hnthyy.thmz.mapper.his.mz;

import cn.hnthyy.thmz.entity.his.mz.MzDrugWin;
import cn.hnthyy.thmz.pageDto.MzDrugWinPageDto;
import cn.hnthyy.thmz.vo.MzDrugWinVo;
import org.apache.ibatis.annotations.*;

import java.util.List;


public interface MzDrugWinMapper {
    /**
     * 加锁
     *
     * @param winNo
     * @return
     */
    @Update("update mz_drug_win set patient_num=patient_num where win_no = #{winNo,jdbcType=CHAR}")
    int updateForBlock(String winNo);


    /**
     * 根据药房类型查询开放中的且排队人数最少的药房窗口
     *
     * @param groupNo
     * @return
     */
    @Select("select top 1 rtrim(win_no) win_no,rtrim(win_name) win_name,rtrim(win_cate) win_cate,rtrim(group_no) group_no,patient_num from " +
            " mz_drug_win where group_no =#{groupNo,jdbcType=CHAR} and win_cate = '0'  order by patient_num asc ")
    MzDrugWin selectMinMzDrugWin(@Param("groupNo") String groupNo);

    /**
     * 更新药房的排队人数
     *
     * @param mzDrugWin
     * @return
     */
    @Update({"<script>",
            "update mz_drug_win ",
            "<trim prefix='set' prefixOverrides=',' suffix=' where win_no = #{winNo,jdbcType=CHAR}' >",
            "<when test='patientNum!=null'>",
            "patient_num =#{patientNum,jdbcType=INTEGER}",
            "</when>",
            "</trim>"
            , "</script>"})
    int updateMzDrugWin(MzDrugWin mzDrugWin);

    /**
     * 按照组号与窗口号查询窗口名称
     *
     * @param groupNo
     * @param winNo
     * @return
     */
    @Select("select rtrim(win_no) win_no,rtrim(win_name) win_name,rtrim(win_cate) win_cate,rtrim(group_no) group_no,patient_num from " +
            " mz_drug_win where group_no =#{groupNo,jdbcType=CHAR} and win_no = #{winNo,jdbcType=CHAR}")
    MzDrugWin selectMzDrugWinByGroupNoAndWinNo(@Param("groupNo") String groupNo, @Param("winNo") String winNo);


    /**
     * 分页查询发药窗口列表
     *
     * @param mzDrugWinPageDto 发药窗口查询条件
     * @return
     */
    @Select({"<script>",
            "SELECT top ${pageSize} win_no,win_name,win_cate,group_no,patient_num" +
                    " FROM (SELECT ROW_NUMBER() OVER (ORDER BY mz_drug_win.group_no desc) " +
                    "AS RowNumber,* from mz_drug_win where 1=1 " +
                    "<when test='mzDrugWin.winNo!=null'>" +
                    " and win_no =#{mzDrugWin.winNo}" +
                    "</when>" +
                    "<when test='mzDrugWin.winName!=null'>" +
                    " and win_name like '%${mzDrugWin.winName}%'" +
                    "</when>" +
                    "<when test='mzDrugWin.winCate!=null'>" +
                    " and win_cate =#{mzDrugWin.winCate}" +
                    "</when>" +
                    "<when test='mzDrugWin.groupNo!=null'>" +
                    " and group_no =#{mzDrugWin.groupNo}" +
                    "</when>" +
                    ") as A WHERE RowNumber >#{pageSize}*#{pageIndex} order by RowNumber ASC",
            "</script>"})
    List<MzDrugWinVo> selectMzDrugWinPage(MzDrugWinPageDto mzDrugWinPageDto);


    /**
     * 统计发药窗口
     *
     * @param mzDrugWin 发药窗口查询条件
     * @return
     */
    @Select({"<script>",
            "SELECT count(1) from mz_drug_win where 1=1 ",
            "<when test='winNo!=null'>",
            " and win_no =#{winNo}",
            "</when>",
            "<when test='winName!=null'>",
            " and win_name like '%${winName}%'",
            "</when>",
            "<when test='winCate!=null'>",
            " and win_cate = #{winCate}",
            "</when>",
            "<when test='groupNo!=null'>",
            " and group_no =#{groupNo}",
            "</when>",
            "</script>"})
    int countMzDrugWin(MzDrugWin mzDrugWin);

    /**
     * 保存药房库字典
     *
     * @param mzDrugWinVo
     * @return
     */
    @Insert("insert into mz_drug_win(win_no,win_name,win_cate,group_no,patient_num)" +
            " values(#{winNo,jdbcType=CHAR},#{winName,jdbcType=CHAR},#{winCate,jdbcType=CHAR},#{groupNo,jdbcType=CHAR},0)")
    int insertMzDrugWin(MzDrugWinVo mzDrugWinVo);

    /**
     * 修改发药窗口字典
     *
     * @param mzDrugWinVo
     * @return
     */
    @Update({"<script>",
            "update mz_drug_win ",
            "<trim prefix='set' prefixOverrides=',' suffix=' where group_no =#{groupNo,jdbcType=CHAR} and win_no = #{winNo,jdbcType=CHAR} ' >",
            "<when test='winName!=null'>",
            ",win_name =#{winName,jdbcType=CHAR}",
            "</when>",
            "<when test='winCate!=null'>",
            ",win_cate =#{winCate,jdbcType=CHAR}",
            "</when>",
            "</trim>",
            "</script>"})
    int updateMzDrugWinVo(MzDrugWinVo mzDrugWinVo);

    /**
     * 根据窗口编码和药房编码删除发药窗口
     *
     * @param groupNo
     * @param winNo
     * @return
     */
    @Delete("delete from mz_drug_win where group_no = #{groupNo} and win_no = #{winNo}")
    int deleteByGroupNoWinNo(@Param("groupNo") String groupNo, @Param("winNo") String winNo);
}
