package cn.hnthyy.thmz.mapper.his;

import cn.hnthyy.thmz.entity.his.YpZdGroupName;
import cn.hnthyy.thmz.pageDto.YpZdGroupNamePageDto;
import cn.hnthyy.thmz.vo.YpZdGroupNameVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface YpZdGroupNameMapper {

    /**
     * 根据编码查询药房名称
     *
     * @param groupNo
     * @return
     */
    @Select("select group_no,dept_code,py_code,d_code wb_code,group_name,mz_flag,yf_flag from yp_zd_group_name where group_no=#{groupNo}")
    YpZdGroupName selectByGroupNo(@Param("groupNo") String groupNo);

    /**
     * 分页查询药房库字典列表
     *
     * @param ypZdGroupNamePageDto 药房库查询条件
     * @return
     */
    @Select({"<script>",
            "SELECT top ${pageSize} group_no,dept_code,py_code,d_code wb_code,group_name,mz_flag,yf_flag" +
                    " FROM (SELECT ROW_NUMBER() OVER (ORDER BY yp_zd_group_name.group_no desc) " +
                    "AS RowNumber,* from yp_zd_group_name where 1=1 " +
                    "<when test='ypZdGroupName.groupNo!=null'>" +
                    " and group_no =#{ypZdGroupName.groupNo}" +
                    "</when>" +
                    "<when test='ypZdGroupName.groupName!=null'>" +
                    " and group_name like '%${ypZdGroupName.groupName}%'" +
                    "</when>" +
                    "<when test='ypZdGroupName.deptCode!=null'>" +
                    " and dept_code =#{ypZdGroupName.deptCode}" +
                    "</when>" +
                    "<when test='ypZdGroupName.pyCode!=null'>" +
                    " and py_code =#{ypZdGroupName.pyCode}" +
                    "</when>" +
                    "<when test='ypZdGroupName.wbCode!=null'>" +
                    " and d_code =#{ypZdGroupName.wbCode}" +
                    "</when>" +
                    "<when test='ypZdGroupName.mzFlag!=null'>" +
                    " and mz_flag =#{ypZdGroupName.mzFlag}" +
                    "</when>" +
                    "<when test='ypZdGroupName.yfFlag!=null'>" +
                    " and yf_flag =#{ypZdGroupName.yfFlag}" +
                    "</when>" +
                    ") as A WHERE RowNumber >#{pageSize}*#{pageIndex} order by RowNumber ASC",
            "</script>"})
    List<YpZdGroupNameVo> selectYpZdGroupNamePage(YpZdGroupNamePageDto ypZdGroupNamePageDto);

    /**
     * 查询药房库字典列表
     *
     * @return
     */
    @Select({"<script>",
            "SELECT group_no,dept_code,py_code,d_code wb_code,group_name,mz_flag,yf_flag" +
                    " FROM  yp_zd_group_name ",
            " order by group_no ASC",
            "</script>"})
    List<YpZdGroupName> selectYpZdGroupName();


    /**
     * 统计药房库字典
     *
     * @param ypZdGroupName 药房库查询条件
     * @return
     */
    @Select({"<script>",
            "SELECT count(1) from yp_zd_group_name where 1=1 " +
                    "<when test='groupNo!=null'>" +
                    " and group_no =#{groupNo}" +
                    "</when>" +
                    "<when test='groupName!=null'>" +
                    " and group_name like #{groupName}" +
                    "</when>" +
                    "<when test='deptCode!=null'>" +
                    " and dept_code =#{deptCode}" +
                    "</when>" +
                    "<when test='pyCode!=null'>" +
                    " and py_code =#{pyCode}" +
                    "</when>" +
                    "<when test='wbCode!=null'>" +
                    " and d_code =#{wbCode}" +
                    "</when>" +
                    "<when test='mzFlag!=null'>" +
                    " and mz_flag =#{mzFlag}" +
                    "</when>" +
                    "<when test='yfFlag!=null'>" +
                    " and yf_flag =#{yfFlag}" +
                    "</when>",
            "</script>"})
    int countYpZdGroupName(YpZdGroupName ypZdGroupName);

    /**
     * 保存药房库字典
     *
     * @param ypZdGroupNameVo
     * @return
     */
    @Insert("insert into yp_zd_group_name(group_no,dept_code,py_code,d_code,group_name,mz_flag,yf_flag)" +
            " values(#{groupNo,jdbcType=VARCHAR},#{deptCode,jdbcType=VARCHAR},#{pyCode,jdbcType=VARCHAR}," +
            "#{wbCode,jdbcType=VARCHAR},#{groupName,jdbcType=VARCHAR},#{mzFlag,jdbcType=CHAR},#{yfFlag,jdbcType=CHAR})")
    int insertYpZdGroupName(YpZdGroupNameVo ypZdGroupNameVo);

    /**
     * 修改药库房字典
     *
     * @param ypZdGroupNameVo
     * @return
     */
    @Update({"<script>",
            "update yp_zd_group_name ",
            "<trim prefix='set' prefixOverrides=',' suffix=' where group_no =#{groupNo,jdbcType=VARCHAR} ' >",
            "<when test='groupNo!=null'>",
            ",group_no =#{groupNo,jdbcType=VARCHAR}",
            "</when>",
            "<when test='deptCode!=null'>",
            ",dept_code =#{deptCode,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='groupName!=null'>",
            ",group_name =#{groupName,jdbcType=VARCHAR}",
            "</when>",
            "<when test='mzFlag!=null'>",
            ",mz_flag =#{mzFlag,jdbcType=CHAR}",
            "</when>",
            "<when test='yfFlag!=null'>",
            ",yf_flag =#{yfFlag,jdbcType=CHAR}",
            "</when>",
            "</trim>"
            , "</script>"})
    int updateYpZdGroupName(YpZdGroupNameVo ypZdGroupNameVo);

    /**
     * 根据药房编码删除药房
     *
     * @param groupNo
     * @return
     */
    @Delete("delete from yp_zd_group_name where group_no = #{groupNo}")
    int deleteByGroupNo(String groupNo);
}
