package thyyxxk.webserver.dao.his.medicaladvice.common;

import com.baomidou.mybatisplus.core.metadata.IPage;
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.medicaladvice.medicamanage.common.YzSequence;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.common.YzYpPageNo;

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

/**
 * @Description:
 * @Author:lihong
 * @Date: 2023/9/22
 */
@Mapper
public interface MedicalCommonDao {
    /**
     * @description:查询药房下拉数据
     * @author: lihong
     * @date: 2023/9/22 11:04
     * @param: params
     * @return: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    @Select(" select group_no code ,group_name name  from yp_zd_group_name where group_no in (${params})")
    List<Map<String,Object>> selectYpZdGroupName(String params);
    /**
     * @description:查询药单
     * @author: lihong
     * @date: 2023/9/22 11:22
     * @param: wardCode
     * @return: java.util.List<thyyxxk.webserver.entity.medicaladvice.medicamanage.common.YzYpPageNo>
     **/
    @Select(" select * from  ( " +
            " SELECT page_no," +
            "       dept_code," +
            "       ward_code," +
            "       submit_time," +
            "       submit_name," +
            "       cancel_submit_time," +
            "       cancel_submit_name," +
            "       submit_flag," +
            "       page_class," +
            "       page_name," +
            "       start_date," +
            "       end_date," +
            "       confirm_flag," +
            "       confirm_time," +
            "       contirm_id," +
            "       pay_self," +
            "       group_no  ," +
            "       sort_no=1" +
            " FROM yz_yp_page_no" +
            " where    "+
            "        ward_code=#{wardCode} and page_class <> 'T'" +
            " union" +
            " select page_no," +
            "       dept_code," +
            "       ward_code," +
            "       isnull(submit_date,input_date)," +
            "       null," +
            "       null," +
            "       null," +
            "       case a.status_flag when '3' then 1 else 0 end," +
            "       'Z'," +
            "       '中草药单'," +
            "       null," +
            "       null," +
            "       null," +
            "       null," +
            "       null," +
            "       null," +
            "       group_no = b.group_no," +
            "       sort_no =2" +
            " from   yz_act_order_cy a,yz_act_order_cy_detail b" +
            " where a.order_no = b.order_no and" +
            "        a.serial_no = b.serial_no and" +
            "        a.ward_code= #{wardCode} and" +
            "        a.status_flag in('2','3')" +
            " union "+
            " SELECT page_no=0,   " +
            "         null,   " +
            "         null," +
            "         null,   " +
            "         null,   " +
            "         null,   " +
            "         null,    " +
            "         null,   " +
            "         null,   " +
            "         null,   " +
            "         null,   " +
            "         null,   " +
            "         null,   " +
            "         null,   " +
            "         null,   " +
            "         null,   " +
            "         null," +
            "         sort_no=0" +
            "    FROM zy_init"+
            " )t ${condition} ")
    List<YzYpPageNo> selectYzYpPageNo(@Param("page")IPage page, @Param("wardCode") String wardCode,@Param("condition") String condition);
    /**
     * @description:查询序列表药单号
     * @author: lihong
     * @date: 2023/9/27 14:20
     * @return: java.lang.Integer
     **/
    @Select(" select page_no from yz_sequence  WITH (TABLOCKX)")
    Integer selectPageNo();
    @Update(" update yz_sequence set page_no =#{newPageNo} where page_no =#{oldPageNo}")
    int updatePageNo(@Param("oldPageNo") Integer oldPageNo,@Param("newPageNo") Integer newPageNo);

    /**
     * @description:查询退药单
     * @author: lihong
     * @date: 2023/9/28 15:31
     * @param: page
     * @param: wardCode
     * @param: condition
     * @return: java.util.List<thyyxxk.webserver.entity.medicaladvice.medicamanage.common.YzYpPageNo>
     **/
    @Select(" select  * from (" +
            "                   SELECT page_no," +
            "                          dept_code," +
            "                          ward_code," +
            "                          submit_time," +
            "                          submit_name," +
            "                          cancel_submit_time," +
            "                          cancel_submit_name," +
            "                          submit_flag," +
            "                          page_class," +
            "                          page_name," +
            "                          start_date," +
            "                          end_date," +
            "                          confirm_flag," +
            "                          confirm_time," +
            "                          contirm_id," +
            "                          pay_self," +
            "                          group_no  ," +
            "                          sort_no=1" +
            "                   FROM yz_yp_page_no" +
            "                   where    " +
            "                           ward_code=#{wardCode} and" +
            "                           page_class='T'" +
            "                   union" +
            "                   SELECT page_no=0," +
            "                          null," +
            "                          null," +
            "                          null," +
            "                          null," +
            "                          null," +
            "                          null," +
            "                          null," +
            "                          'T'," +
            "                          null," +
            "                          null," +
            "                          null," +
            "                          null," +
            "                          null," +
            "                          null," +
            "                          null," +
            "                          null," +
            "                          sort_no=0" +
            "                   FROM zy_init" +
            "                   )t ${condition}")
    List<YzYpPageNo> selectTydYzYpPageNo(@Param("page")IPage page, @Param("wardCode") String wardCode,@Param("condition") String condition);
    @Select(" select top 1  *  from yz_sequence ")
    YzSequence selectYzSequence();
    @Select(" <script> " +
            "select distinct a.py_code," +
            "                a.d_code," +
            "                rtrim(a.supply_code) code," +
            "                rtrim(a.supply_name) supply_name," +
            "                a.class," +
            "                a.sort_code" +
            " from yz_supply_type a,yz_act_order b" +
            " where a.supply_code = b.supply_code and" +
            "        isnull(a.del_flag,'0') != '1' and" +
            "        b.ward_code =  #{wardCode} " +
            " <if test=\" keyWard !=null and  keyWard !=''   \">"+
            " and ( a.py_code like '${keyWard}%' or a.d_code like '${keyWard}%' or a.supply_name like '${keyWard}%'  )"+
            "  </if>"+
            "</script>")
    List<Map<String, Object>> selectItemsSubclass(Map<String,String> params);
    @Select(" SELECT rtrim(code) code , name, py_code, d_code" +
            " FROM zd_charge_item_type" +
            " union all" +
            " select top 1 '#','药品','#','#' from zd_charge_item_type" +
            " union all" +
            " select top 1 '%','全部诊疗项目','%','%' from zd_charge_item_type ")
    List<Map<String, Object>> selectItemsList();
    @Select(" <script>" +
            "  SELECT rtrim(a.order_code) code," +
            "       a.order_name name " +
            " FROM yz_act_order a," +
            "     yz_order_item b (nolock)" +
            " where a.ward_code = #{wardCode}" +
            "  and a.status_flag &gt; '2'" +
            "<if test = \"  code =='%'.toString  \">"+
            "  and a.status_flag &lt; '5' " +
            " </if>"+
            "  and a.order_code = b.order_code" +
            "  and a.group_no = '00'" +
            "<if test = \"  code !='%'.toString  \">"+
            "  and b.order_class like '${code}%' " +
            " </if>"+
            " group by a.order_code," +
            "         a.order_name" +
            " </script>")
    List<Map<String, Object>> selectItemDetail(@Param("code") String code,@Param("wardCode") String wardCode);
    @Select(" SELECT rtrim(a.order_code) code ," +
            "       a.order_name name ," +
            "       a.drug_specification" +
            " FROM yz_act_order a" +
            " where a.ward_code = #{wardCode}" +
            "  and a.status_flag > 2" +
            "  and a.status_flag <= '5'" +
            "  and group_no <> '00'" +
            " group by a.order_code," +
            "         a.order_name," +
            "         a.drug_specification")
    List<Map<String, Object>> selectDrugDetail(String wardCode);

    @Select(" SELECT code=zy_adtward.ward_code, zy_adtward.dept_code, name=zy_adtward.ward_name, zy_adtward.dept_name " +
            " FROM zy_adtward " +
            " where PATINDEX('8%',ward_code)>0 " +
            " order by dept_code, ward_code")
    List<Map<String, Object>> selectZyWard();

    @Select("  select *  " +
            " from (  " +
            "         SELECT zd_unit_code.code,  " +
            "                zd_unit_code.name,  " +
            "                parent_dept_code = zd_dept_all.dept  " +
            "         FROM zd_dept_all,  " +
            "              zd_unit_code  " +
            "         where zd_dept_all.small_dept = zd_unit_code.code  " +
            "     ) t  " +
            " where t.parent_dept_code = #{code} ")
    List<Map<String, Object>> selectSmallWard(String code);
}
