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

import cn.hnthyy.thmz.entity.his.zd.ZdUnitCode;
import cn.hnthyy.thmz.pageDto.ZdUnitCodePageDto;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

public interface ZdUnitCodeMapper {
    /**
     * 分页查询科室字典列表
     *
     * @param zdUnitCodePageDto 科室查询条件
     * @return
     */
    @Select({"<script>",
            "SELECT top ${pageSize} code,name,class_code,py_code,d_code wbCode,mz_flag,del_flag,n_code ynCode,yj_flag,mzrc_flag,parent_code,gh_charge_flag,office_pos,",
            "supply_flag,xnh_dept_code,xnh_dept_name,ghjz_flag,mz_class,sort_code,op_id,op_date FROM (SELECT ROW_NUMBER() OVER (ORDER BY zd_unit_code_page.${orderByCase} asc) ",
            "AS RowNumber,* from dbo.zd_unit_code zd_unit_code_page where 1=1 ",
            "<when test='zdUnitCode.delFlag!=null'>",
            " and del_flag =#{zdUnitCode.delFlag,jdbcType=VARCHAR}",
            "</when>",
            "<when test='zdUnitCode.code!=null'>",
            " and code =#{zdUnitCode.code,jdbcType=VARCHAR}",
            "</when>",
            "<when test='zdUnitCode.name!=null'>",
            " and name like #{zdUnitCode.name,jdbcType=VARCHAR}",
            "</when>",
            "<when test='zdUnitCode.classCode!=null'>",
            " and class_code =#{zdUnitCode.classCode,jdbcType=VARCHAR}",
            "</when>",
            "<when test='zdUnitCode.pyCode!=null'>",
            " and py_code =#{zdUnitCode.pyCode,jdbcType=VARCHAR}",
            "</when>",
            "<when test='zdUnitCode.wbCode!=null'>",
            " and d_code =#{zdUnitCode.wbCode,jdbcType=VARCHAR}",
            "</when>",
            "<when test='zdUnitCode.mzFlag!=null'>",
            " and mz_flag =#{zdUnitCode.mzFlag,jdbcType=CHAR}",
            "</when>",
            "<when test='zdUnitCode.ynCode!=null'>",
            " and n_code =#{zdUnitCode.ynCode,jdbcType=VARCHAR}",
            "</when>",
            "<when test='zdUnitCode.yjFlag!=null'>",
            " and yj_flag =#{zdUnitCode.yjFlag,jdbcType=CHAR}",
            "</when>",
            "<when test='zdUnitCode.ghChargeFlag!=null'>",
            " and gh_charge_flag =#{zdUnitCode.ghChargeFlag,jdbcType=CHAR}",
            "</when>",
            "<when test='zdUnitCode.ghjzFlag!=null'>",
            " and ghjz_flag =#{zdUnitCode.ghjzFlag,jdbcType=CHAR}",
            "</when>",
            ") as A WHERE RowNumber >#{pageSize}*#{pageIndex} order by RowNumber asc",
            "</script>"})
    List<ZdUnitCode> selectZdUnitCodeWithPage(ZdUnitCodePageDto zdUnitCodePageDto);


    /**
     * 统计科室字典总数
     *
     * @param zdUnitCode 科室查询条件
     * @return
     */
    @Select({"<script>",
            "SELECT count(1) from dbo.zd_unit_code where 1=1 ",
            "<when test='delFlag!=null'>",
            " and del_flag =#{delFlag,jdbcType=VARCHAR}",
            "</when>",
            "<when test='code!=null'>",
            " and code =#{code,jdbcType=VARCHAR}",
            "</when>",
            "<when test='name!=null'>",
            " and name like #{name,jdbcType=VARCHAR}",
            "</when>",
            "<when test='classCode!=null'>",
            " and class_code =#{classCode,jdbcType=VARCHAR}",
            "</when>",
            "<when test='pyCode!=null'>",
            " and py_code =#{pyCode,jdbcType=VARCHAR}",
            "</when>",
            "<when test='wbCode!=null'>",
            " and d_code =#{wbCode,jdbcType=VARCHAR}",
            "</when>",
            "<when test='mzFlag!=null'>",
            " and mz_flag =#{mzFlag,jdbcType=CHAR}",
            "</when>",
            "<when test='ynCode!=null'>",
            " and n_code =#{ynCode,jdbcType=VARCHAR}",
            "</when>",
            "<when test='yjFlag!=null'>",
            " and yj_flag =#{yjFlag,jdbcType=CHAR}",
            "</when>",
            "<when test='ghChargeFlag!=null'>",
            " and gh_charge_flag =#{ghChargeFlag,jdbcType=CHAR}",
            "</when>",
            "<when test='ghjzFlag!=null'>",
            " and ghjz_flag =#{ghjzFlag,jdbcType=CHAR}",
            "</when>",
            "</script>"})
    int countZdUnitCode(ZdUnitCode zdUnitCode);


    /**
     * 检查修改或者新增数据是否重复 部门编码 部门名称 新农合编码 新农合名称
     *
     * @param zdUnitCode 科室查询条件
     * @return
     */
    @Select({"<script>",
            "SELECT code,name,xnh_dept_code,xnh_dept_name from dbo.zd_unit_code ",
            "<choose>",
            "<when test='originalCode!=null'>",
            "<![CDATA[ where code <> #{originalCode,jdbcType=VARCHAR} and (1=0 ]]>",
            "</when>",
            "<otherwise>",
            " where (1=0 ",
            "</otherwise>",
            "</choose>",
            "<when test='code!=null'>",
            " or code =#{code,jdbcType=VARCHAR}",
            "</when>",
            "<when test='name!=null'>",
            " or name =#{name,jdbcType=VARCHAR}",
            "</when>",
            "<when test='xnhDeptCode!=null'>",
            " or xnh_dept_code =#{xnhDeptCode,jdbcType=VARCHAR}",
            "</when>",
            "<when test='xnhDeptName!=null'>",
            " or xnh_dept_name =#{xnhDeptName,jdbcType=VARCHAR}",
            "</when>",
            " ) </script>"})
    List<ZdUnitCode> checkZdUnitCode(ZdUnitCode zdUnitCode);

    /**
     * 根据编码模糊查询类似编码最大的记录
     * @param code
     * @return
     */
    @Select(" select top 1* from zd_unit_code where code  like #{code}  order BY code desc ")
    ZdUnitCode selectLastZdUnitCodeByCode(@Param("code") String code);

    /**
     * 新增科室字典
     *
     * @param zdUnitCode
     * @return
     */
    @Insert("INSERT INTO dbo.zd_unit_code(code,name,class_code,py_code,d_code,mz_flag,del_flag,n_code,yj_flag,mzrc_flag,parent_code," +
            "gh_charge_flag,office_pos,supply_flag,xnh_dept_code,xnh_dept_name,ghjz_flag,mz_class,sort_code,op_id,op_date )VALUES(#{code,jdbcType=VARCHAR},#{name,jdbcType=VARCHAR}" +
            ",#{classCode,jdbcType=VARCHAR},#{pyCode,jdbcType=VARCHAR},#{wbCode,jdbcType=VARCHAR},#{mzFlag,jdbcType=CHAR},#{delFlag,jdbcType=CHAR},#{ynCode,jdbcType=VARCHAR},#{yjFlag,jdbcType=CHAR}" +
            ",#{mzrcFlag,jdbcType=CHAR},#{parentCode,jdbcType=CHAR},#{ghChargeFlag,jdbcType=CHAR},#{officePos,jdbcType=VARCHAR},#{supplyFlag,jdbcType=CHAR},#{xnhDeptCode,jdbcType=VARCHAR}" +
            ",#{xnhDeptName,jdbcType=VARCHAR},#{ghjzFlag,jdbcType=CHAR},#{mzClass,jdbcType=VARCHAR},#{sortCode,jdbcType=VARCHAR},#{opId,jdbcType=VARCHAR},#{opDate,jdbcType=TIMESTAMP}) ")
    int insertZdUnitCode(ZdUnitCode zdUnitCode);

    /**
     * 修改科室字典
     *
     * @param zdUnitCode
     * @return
     */
    @Update({"<script>",
            "update dbo.zd_unit_code",
            "<trim prefix='set' prefixOverrides=',' suffix=' where code = #{originalCode} '>",
            "<when test='code!=null'>",
            "code =#{code,jdbcType=VARCHAR}",
            "</when>",
            "<when test='name!=null'>",
            ",name =#{name,jdbcType=VARCHAR}",
            "</when>",
            "<when test='classCode!=null'>",
            ",class_code =#{classCode,jdbcType=VARCHAR}",
            "</when>",
            "<when test='pyCode!=null'>",
            ",py_code =#{pyCode,jdbcType=VARCHAR}",
            "</when>",
            "<when test='wbCode!=null'>",
            ",d_code =#{wbCode,jdbcType=VARCHAR}",
            "</when>",
            "<when test='mzFlag!=null'>",
            ",mz_flag =#{mzFlag,jdbcType=CHAR}",
            "</when>",
            "<when test='delFlag!=null'>",
            ",del_flag =#{delFlag,jdbcType=CHAR}",
            "</when>",
            "<when test='ynCode!=null'>",
            ",n_code =#{ynCode,jdbcType=VARCHAR}",
            "</when>",
            "<when test='yjFlag!=null'>",
            ",yj_flag =#{yjFlag,jdbcType=CHAR}",
            "</when>",
            "<when test='mzrcFlag!=null'>",
            ",mzrc_flag =#{mzrcFlag,jdbcType=CHAR}",
            "</when>",
            "<when test='parentCode!=null'>",
            ",parent_code =#{parentCode,jdbcType=VARCHAR}",
            "</when>",
            "<when test='ghChargeFlag!=null'>",
            ",gh_charge_flag =#{ghChargeFlag,jdbcType=CHAR}",
            "</when>",
            "<when test='officePos!=null'>",
            ",office_pos =#{officePos,jdbcType=VARCHAR}",
            "</when>",
            "<when test='supplyFlag!=null'>",
            ",supply_flag =#{supplyFlag,jdbcType=CHAR}",
            "</when>",
            "<when test='xnhDeptCode!=null'>",
            ",xnh_dept_code =#{xnhDeptCode,jdbcType=VARCHAR}",
            "</when>",
            "<when test='xnhDeptName!=null'>",
            ",xnh_dept_name =#{xnhDeptName,jdbcType=VARCHAR}",
            "</when>",
            "<when test='ghjzFlag!=null'>",
            ",ghjz_flag =#{ghjzFlag,jdbcType=CHAR}",
            "</when>",
            "<when test='mzClass!=null'>",
            ",mz_class =#{mzClass,jdbcType=VARCHAR}",
            "</when>",
            "<when test='sortCode!=null'>",
            ",sort_code =#{sortCode,jdbcType=VARCHAR}",
            "</when>",
            "<when test='opId!=null'>",
            ",op_id =#{opId,jdbcType=VARCHAR}",
            "</when>",
            "<when test='opDate!=null'>",
            ",op_date =#{opDate,jdbcType=TIMESTAMP}",
            "</when>",
            "</trim>",
            "</script>"})
    int updateZdUnitCode(ZdUnitCode zdUnitCode);

    /**
     * 根据科室编码查询科室信息
     *
     * @param code 科室编码
     * @return
     */
    @Select("select code,name,class_code,py_code,d_code wbCode,mz_flag,del_flag,n_code ynCode,yj_flag,mzrc_flag,parent_code,gh_charge_flag,office_pos,supply_flag,xnh_dept_code," +
            "xnh_dept_name,ghjz_flag,mz_class,sort_code,op_id,op_date from dbo.zd_unit_code where code=#{code,jdbcType=VARCHAR}")
    ZdUnitCode selectByCode(@Param("code") String code);

    /**
     * 查询所有的科室信息
     * @return
     */
    @Select("select code,name,class_code,py_code,d_code wbCode,mz_flag,del_flag,n_code ynCode,yj_flag,mzrc_flag,parent_code,gh_charge_flag,office_pos,supply_flag,xnh_dept_code," +
            "xnh_dept_name,ghjz_flag,mz_class,sort_code,op_id,op_date from dbo.zd_unit_code")
    List<ZdUnitCode> selectAll();




    /**
     * 查询科室字典列表
     *
     * @param zdUnitCodePageDto 科室查询条件
     * @return
     */
    @Select({"<script>",
            "SELECT code,name,class_code,py_code,d_code wbCode,mz_flag,del_flag,n_code ynCode,yj_flag,mzrc_flag,parent_code,gh_charge_flag,office_pos,",
            "supply_flag,xnh_dept_code,xnh_dept_name,ghjz_flag,mz_class,sort_code,op_id,op_date FROM  dbo.zd_unit_code where 1=1 ",
            "<when test='zdUnitCode.delFlag!=null'>",
            " and del_flag =#{zdUnitCode.delFlag,jdbcType=VARCHAR}",
            "</when>",
            "<when test='zdUnitCode.code!=null'>",
            " and code =#{zdUnitCode.code,jdbcType=VARCHAR}",
            "</when>",
            "<when test='zdUnitCode.name!=null'>",
            " and name like #{zdUnitCode.name,jdbcType=VARCHAR}",
            "</when>",
            "<when test='zdUnitCode.classCode!=null'>",
            " and class_code =#{zdUnitCode.classCode,jdbcType=VARCHAR}",
            "</when>",
            "<when test='zdUnitCode.pyCode!=null'>",
            " and py_code =#{zdUnitCode.pyCode,jdbcType=VARCHAR}",
            "</when>",
            "<when test='zdUnitCode.wbCode!=null'>",
            " and d_code =#{zdUnitCode.wbCode,jdbcType=VARCHAR}",
            "</when>",
            "<when test='zdUnitCode.mzFlag!=null'>",
            " and mz_flag =#{zdUnitCode.mzFlag,jdbcType=CHAR}",
            "</when>",
            "<when test='zdUnitCode.ynCode!=null'>",
            " and n_code =#{zdUnitCode.ynCode,jdbcType=VARCHAR}",
            "</when>",
            "<when test='zdUnitCode.yjFlag!=null'>",
            " and yj_flag =#{zdUnitCode.yjFlag,jdbcType=CHAR}",
            "</when>",
            "<when test='zdUnitCode.ghChargeFlag!=null'>",
            " and gh_charge_flag =#{zdUnitCode.ghChargeFlag,jdbcType=CHAR}",
            "</when>",
            "<when test='zdUnitCode.ghjzFlag!=null'>",
            " and ghjz_flag =#{zdUnitCode.ghjzFlag,jdbcType=CHAR}",
            "</when>",
            "<when test='zdUnitCode.mzClass!=null'>",
            " and mz_class =#{zdUnitCode.mzClass,jdbcType=CHAR}",
            "</when>",
            " order by ${orderByCase} asc",
            "</script>"})
    List<ZdUnitCode> selectZdUnitCode(ZdUnitCodePageDto zdUnitCodePageDto);


    /**
     * 查询所有门诊科室信息
     * @return
     */
    @Select("select code,name,class_code,py_code,d_code wbCode,mz_flag,del_flag,n_code ynCode,yj_flag,mzrc_flag,parent_code,gh_charge_flag,office_pos,supply_flag,xnh_dept_code," +
            "xnh_dept_name,ghjz_flag,mz_class,sort_code,op_id,op_date from dbo.zd_unit_code where del_flag=0 and mz_flag =1")
    List<ZdUnitCode> selectAllMzZdUnitCode();

    /**
     * 根据住院病人编号查询对应的科室 和医生
     * @param inpatientNo
     * @return
     */
    @Select("select ISNULL(zk_ward,small_dept) zkWard,refer_physician,consult_physician,dept_director from zy_actpatient where inpatient_no =#{inpatientNo}")
    Map<String,String> selectZkWardByInpatientNo(@Param("inpatientNo") String inpatientNo);

    /**
     * 根据科室编码集合查询科室集合
     * @param codes
     * @return
     */
    @Select({"<script>",
                "select code,name,class_code,py_code,d_code wbCode,mz_flag,del_flag,n_code ynCode,yj_flag,mzrc_flag,parent_code,gh_charge_flag,office_pos,supply_flag,xnh_dept_code,xnh_dept_name,ghjz_flag,mz_class,sort_code,op_id,op_date from dbo.zd_unit_code ",
            "<when test='codes!=null'>" +
                    " where code in" +
                    "<foreach item='item' index='index' collection='codes' open='(' separator=',' close=')'>" +
                    "#{item}" +
                    "</foreach>" +
                    "</when>",
            "</script>"})
    List<ZdUnitCode> selectZdUnitCodeByCodes(@Param("codes") List<String> codes);



    /**
     * 查询所有的病区
     * @return
     */
    @Select("select code,name,class_code,py_code,d_code wbCode,mz_flag,del_flag,n_code ynCode,yj_flag,mzrc_flag,parent_code,gh_charge_flag,office_pos,supply_flag,xnh_dept_code,xnh_dept_name,ghjz_flag,mz_class,sort_code,op_id,op_date from zd_unit_code where code like '8%'")
    List<ZdUnitCode> selectAllInpatientWard();

    /**
     * 查询小科室
     * @param code
     * @return
     */
    @Select("select rtrim(small_dept) small_dept from zd_dept_all where dept =#{code} ")
    List<String> selectSmallDeptByDept(@Param("code") String code);

    /**
     * 根据病区id查询病床
     * @param code
     * @return
     */
    @Select("select bed_no from zy_bed_mi where ward_code =#{code}  and bed_status = 1")
    List<String> selectBedNumByDept(@Param("code") String code);


}
