package thyyxxk.webserver.dao.his.inpatient;

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.inpatient.conventions.ConventionDetail;
import thyyxxk.webserver.entity.inpatient.conventions.RoughConvention;

import java.util.List;

@Mapper
public interface ConventionsDao {

    @Select("SELECT distinct " +
            "ltrim(rtrim(str(a.act_order_no))) actOrderNo, " +
            "a.occ_time occTime, " +
            "a.inpatient_no, " +
            "a.admiss_times, " +
            "name=rtrim(b.name), " +
            "rtrim(b.bed_no) bedNo, " +
            "rtrim(d.order_name) orderName, " +
            "rtrim(e.name) doctorName " +
            "FROM yz_zy_patient_fee a ,zy_actpatient b , " +
            "yz_act_order d left join a_employee_mi e on d.physician=e.code " +
            "where a.charge_status='3' and " +
            "a.inpatient_no=#{inpatientNo} and " +
            "a.admiss_times=#{admissTimes} and " +
            "b.inpatient_no=#{inpatientNo} and " +
            "b.admiss_times=#{admissTimes} and " +
            "a.exec_unit='2010000' and " +
            "d.inpatient_no=#{inpatientNo} and " +
            "d.admiss_times=#{admissTimes} and " +
            "a.act_order_no=d.act_order_no and " +
            "convert(varchar(20),a.occ_time,23)<=convert(varchar(20),getdate(),23) " +
            "order by a.inpatient_no,a.admiss_times,a.occ_time")
    List<RoughConvention> getRoughConventions(@Param("inpatientNo") String inpatientNo,
                                              @Param("admissTimes") Integer admissTimes);

    @Select("SELECT a.inpatient_no inpatientNo,"+
            "a.admiss_times admissTimes,"+
            "rtrim(b.name) name, "+
            "rtrim(d.order_name) orderName, "+
            "rtrim(e.name) doctorName, "+
            "a.occ_time occTime, "+
            "rtrim(a.charge_code) xmCode, "+
            "rtrim(c.name) xmName, "+
            "rtrim(a.order_count) amount, "+
            "rtrim(a.charge_fee) fee, "+
            "ltrim(rtrim(str(a.act_order_no))) actOrderNo "+
            "FROM yz_zy_patient_fee a ,zy_actpatient b ,zd_charge_item c, "+
            "yz_act_order d left join a_employee_mi e on d.physician=e.code "+
            "where a.charge_status='3' and "+
            "a.inpatient_no =#{inpatientNo} and "+
            "a.inpatient_no=b.inpatient_no and "+
            "a.admiss_times=b.admiss_times and "+
            "a.exec_unit ='2010000' and "+
            "a.charge_code=c.code and "+
            "d.act_order_no=#{actOrderNo} "+
            "and a.inpatient_no=d.inpatient_no "+
            "and a.admiss_times=d.admiss_times "+
            "and a.act_order_no=d.act_order_no "+
            "and convert(varchar(20),a.occ_time,23)<=convert(varchar(20),getdate(),23) "+
            "order by a.inpatient_no,a.admiss_times,a.occ_time ")
    List<ConventionDetail> getConventionDetail(@Param("inpatientNo") String inpatientNo,
                                               @Param("actOrderNo") String actOrderNo);

    @Update("<script>" +
            "update yz_zy_patient_fee set charge_status='2'," +
            "op_id=#{staffId}, op_date=getDate() " +
            "where act_order_no in " +
            "<foreach collection='actOrderNos' item='actOrderNo' open='(' separator=',' close=')'>" +
            "#{actOrderNo}" +
            "</foreach>" +
            "</script>")
    void retractConvention(@Param("staffId") String staffId, List<String> actOrderNos);
}
