package thyyxxk.webserver.dao.his.datamodify;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.datamodify.TYzActOrderModify;
import thyyxxk.webserver.entity.datamodify.YzActOrder;

import java.math.BigDecimal;
import java.util.List;


/**
 * <p>
 * 医嘱修改 接口
 * </p>
 *
 * @author 肖蟾
 * @since 2021-03-08
 */
@Mapper
public interface YzActOrderDao extends BaseMapper<YzActOrder> {

    /**
     * 查询全部医嘱
     *
     * @param queryWrapper 传入医嘱号和住院号
     * @return 返回指定的医嘱号和医嘱名称  多个
     */
    @Select("select rtrim(a.inpatient_no) inpatient_no,rtrim(frequ_code) frequ_code,start_time,end_time,act_order_no, " +
            " order_name,rtrim(b.name) patient_name,order_time, " +
            "rtrim((select name from zy_ward_code where code = ward_code)) ward_code from " +
            "yz_act_order as a,a_patient_mi as b " +
            "${ew.customSqlSegment} and isnull(del_flag,0)=0 and a.inpatient_no=b.inpatient_no")
    List<YzActOrder> listQueryYz(@Param(Constants.WRAPPER) Wrapper<YzActOrder> queryWrapper);


    /**
     * 插入医嘱修改信息
     *
     * @param param    需要修改的字段
     * @param dataList 原本的信息
     */
    @Insert("<script>" +
            "insert into t_yz_act_order_modify (act_order_no, inpatient_no, start_time, end_time, new_start_time, new_end_time, " +
            "proposer,propose_remark, patient_name, frequ_code, " +
            "ward_code, order_name) values " +
            "<foreach collection='dataList' item='list' separator=','>" +
            "(#{list.actOrderNo},#{list.inpatientNo},#{list.startTime},#{list.endTime},#{param.startTime},#{param.endTime}," +
            "#{param.proposer},#{param.proposeRemark},#{list.patientName},#{list.frequCode},#{list.wardCode},#{list.orderName})" +
            "</foreach>" +
            "</script>")
    void yzActOrderModifyInsert(@Param("param") TYzActOrderModify param,
                                @Param("dataList") List<TYzActOrderModify> dataList);

    @Select("<script>" +
            "select cast(act_order_no as decimal(15, 0))  from t_yz_act_order_modify where act_order_no in " +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            " #{item}" +
            "</foreach>" +
            "</script>")
    List<BigDecimal> chaKansfCunZai(@Param("list") List<BigDecimal> actOrderNoList);


    @Select("select count(1) from t_yz_act_order_modify where act_order_no = #{actOrderNo}")
    Integer chaKanDanGeShenQing(BigDecimal actOrderNo);

    /**
     * 如果数据库中有了的话就更新
     *
     * @param param    要更新的字段
     * @param dataList 原本的信息
     */
    @Update("<script>" +
            "<foreach collection='dataList' item='item' separator=';'>" +
            "update t_yz_act_order_modify " +
            "<set>" +
            "new_start_time=#{param.startTime}," +
            "new_end_time=#{param.endTime},audit_flag =1," +
            "proposer = #{param.proposer},propose_time=getdate(), " +
            "propose_remark=#{param.proposeRemark},patient_name=#{item.patientName},frequ_code=#{item.frequCode}" +
            ",ward_code=#{item.wardCode},order_name=#{item.orderName} " +
            "</set>" +
            "where act_order_no = #{item.actOrderNo} " +
            "</foreach>" +
            "</script>")
    void yzActOrderModifyUpdate(@Param("param") TYzActOrderModify param,
                                @Param("dataList") List<TYzActOrderModify> dataList);

    /**
     * 根据人员的 code 来查询医嘱申请信息
     *
     * @param page        分页条件
     * @param code        操作人的code
     * @param actOrderNo  医嘱号
     * @param inpatientNo 住院号
     * @return 返回分页的数据
     */
    @Select("<script>" +
            "select act_order_no,audit_flag,audit_remark,propose_remark,inpatient_no," +
            "audit_staff=(select name from a_employee_mi where code=audit_staff),audit_time from t_yz_act_order_modify  " +
            "where proposer = #{code}" +
            "<if test=\"actOrderNo != null and actOrderNo !=''\">" +
            "and act_order_no = #{actOrderNo}" +
            "</if>" +
            "<if test=\"inpatientNo !=null and inpatientNo != '' \">" +
            "and inpatient_no = #{inpatientNo}" +
            "</if>" +
            "</script>")
    List<TYzActOrderModify> queryZhuangTai(IPage<TYzActOrderModify> page,
                                           @Param("code") String code,
                                           @Param("actOrderNo") String actOrderNo,
                                           @Param("inpatientNo") String inpatientNo);

    /**
     * 查询出有医嘱修改审核权限的人的 code 用这个来发送通知
     *
     * @return 返回code
     */
    @Select("select user_code from dj_user_role where role_id=28")
    List<String> selectAdmins();

    @Insert("insert into t_yz_act_order_modify (act_order_no, inpatient_no, start_time, end_time, new_start_time, new_end_time, " +
            "proposer,propose_remark,  " +
            "patient_name, frequ_code, ward_code, order_name) " +
            "values (#{actOrderNo},#{inpatientNo},#{startTime},#{endTime},#{newStartTime},#{newEndTime},#{proposer},#{proposeRemark},#{patientName}," +
            "#{frequCode},#{wardCode},#{orderName})")
    void danGeShenQingInsert(TYzActOrderModify param);

    @Update("update t_yz_act_order_modify set new_start_time=#{newStartTime}," +
            "new_end_time=#{newEndTime},proposer=#{proposer}," +
            "propose_time=getdate(),propose_remark=#{proposeRemark} ,audit_flag = 1 " +
            "where act_order_no = #{actOrderNo}")
    void danGeShenQingUpdate(TYzActOrderModify param);

    @Select("select name from a_employee_mi where code = #{code}")
    String getUserName(String code);
}
