package thyyxxk.webserver.dao.his.zhuyuanyisheng;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.zhuyuanyisheng.orderquash.ReqOrderQuash;
import thyyxxk.webserver.entity.zhuyuanyisheng.orderquash.ReturnReqOrderQuash;

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

@Mapper
public interface OrderQuashDao extends BaseMapper<ReqOrderQuash> {

    @Select("select start_time from yz_act_order where act_order_no = #{orderNo}")
    Date selectOrderStartTime(BigDecimal orderNo);

    @Select("select count(1) from yz_yp_zy_order where act_order_no = #{orderNo} " +
            "and status_flag = '1' ")
    int selectYpOrder(BigDecimal orderNo);

    @Select("select count(1) from yz_zy_patient_fee where act_order_no = #{orderNo}")
    int selectZyPatientFee(BigDecimal orderNo);

    @Select("select count(1) from zy_detail_charge where order_no = #{orderNo} " +
            "and inpatient_no = #{patNo} and admiss_times = #{times} and trans_flag_yb <> '2' ")
    int selectZyDetailCharge(BigDecimal orderNo,
                             String patNo,
                             Integer times);

    @Select("select rtrim(pat_no)                    as pat_no, " +
            "       (select rtrim(name) " +
            "        from view_zy_patient_all pat " +
            "        where pat.inpatient_no = pat_no " +
            "          and pat.admiss_times = times) as name, " +
            "       times, " +
            "       count(1)                         as count,state," +
            "       ward_name = (select rtrim(name) from zd_unit_code where ward = code)  " +
            "from req_order_quash,zy_actpatient b " +
            "  ${ew.customSqlSegment} and pat_no = inpatient_no " +
            "  and times = admiss_times " +
            "group by pat_no, times,state,ward ")
    List<ReturnReqOrderQuash> selectReqListGroupBy(@Param("ew") QueryWrapper<ReturnReqOrderQuash> queryWrapper);

    @Select("select a.act_order_no, " +
            "       req_datetime, " +
            "       req_id, " +
            "       req_remark, " +
            "       approver_id," +
            "       approver_name = (select rtrim(name) from a_employee_mi where code = approver_id), " +
            "       approver_datatime, " +
            "       approver_remark, " +
            "       state, " +
            "       pat_no, " +
            "       times, " +
            "       b.order_name, " +
            "       b.frequ_code, " +
            "       enter_oper_name = (select rtrim(name) from a_employee_mi where code = enter_oper)," +
            "       order_time, " +
            "       start_time, " +
            "       end_time," +
            "       a.act_order_no " +
            "from req_order_quash a " +
            "         left join view_xc_act_and_erase_order b on (a.act_order_no = b.act_order_no) " +
            "where pat_no = #{patNo} " +
            "  and times = #{times} " +
            "  and state = #{state} ")
    List<ReturnReqOrderQuash> selectReqListByPat(String patNo, String times, int state);

    @Update("update req_order_quash " +
            "set state             = #{req.state}, " +
            "    approver_id       = #{req.approverId}, " +
            "    approver_datatime = #{req.approverDatatime}, " +
            "    approver_remark   = #{req.approverRemark} " +
            "where act_order_no = #{req.actOrderNo}")
    void updateReqOrderQuash(@Param("req") ReqOrderQuash req);

    @Delete("delete yz_zy_patient_fee where act_order_no = #{orderNo} and charge_status ='3' ")
    void delPatientFee(@Param("orderNo") BigDecimal orderNo);

    @Delete("delete yz_yp_zy_order where act_order_no =#{orderNo} and status_flag ='1' ")
    void delYpZyOrder(@Param("orderNo") BigDecimal orderNo);

    @Insert("insert into yz_erase_order select * from yz_act_order a where a.act_order_no = #{orderNo} ")
    void eraseOrder(@Param("orderNo") BigDecimal orderNo);

    @Update("update yz_erase_order set modifier = enter_oper , " +
            "status_time = getdate()," +
            " print_order_change =case when isnull ( print_order_change , '' ) ='' then 'd' else '' end where act_order_no =#{orderNo} ")
    void upErase(@Param("orderNo") BigDecimal orderNo);

    @Delete("delete yz_act_order where act_order_no = #{orderNo};" +
            "delete  yz_act_occ where act_order_no = #{orderNo};")
    void delYzOrder(@Param("orderNo") BigDecimal orderNo);

    @Update("update yz_act_order set end_time = getdate() " +
            "where act_order_no = #{orderNo} and end_time is null" +
            " and frequ_code <> 'ONCE' ")
    void stopOrder(@Param("orderNo") BigDecimal orderNo);

}
