package thyyxxk.webserver.dao.his.zhuyuanyiji;

import com.alibaba.fastjson.JSONObject;
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.datamodify.MzChargeDetail;

import java.util.List;

/**
 * <p>
 * 描述： 门诊项目确认或取消
 * </p>
 *
 * @author xc
 * @date 2021-10-09 15:16
 */
@Mapper
public interface MenZhengXiangMuDao {


    @Select("select rtrim(patient_id) patient_id,rtrim(name) name from mz_charge_detail " +
            "where charge_date >= #{startTime} and charge_date <= #{endTime} " +
            "and exec_dept = #{execDept} and group_no = '00' and confirm_flag = 0 " +
            "and charge_item_code  <>  'BILL99' " +
            "group by patient_id ,name")
    List<MzChargeDetail> kuaiSuChaZhao(@Param("startTime") String startTime,
                                       @Param("endTime") String endTime,
                                       @Param("execDept") String execDept);

    @Select("SELECT mz_patient_mi.name, " +
            "       mz_patient_mi.sex, " +
            "       rtrim(mz_patient_mi.patient_id) patient_id, " +
            "       ic_card_no = cast(mz_patient_mi.ic_card_no as varchar(50)), " +
            "       mz_patient_mi.birth_day, " +
            "       mz_patient_mi.phone_no, " +
            "       mz_patient_mi.adress  , " +
            "       age=space(20), " +
            "       account_balance=0.00, " +
            "       account_status =space(2), " +
            "       times, " +
            "       response_type, " +
            "       yb_flag=space(1), " +
            "       by_flag=space(1), " +
            "       contract_code=contract_code, " +
            "       charge_type " +
            "FROM mz_patient_mi " +
            "where patient_id=#{patientId} or ic_card_no = #{patientId}")
    MzChargeDetail getMzPatient(String patientId);


    @Select("<script>" +
            "select charge_date,real_no,rtrim(patient_id) patient_id,times  " +
            "from mz_charge_detail where  charge_date &gt;= #{startTime} and charge_date &lt;= #{endTime} and " +
            "patient_id = #{patientId} and pay_mark = '0' " +
            "<if test=\"confirmFlag == 0 \">" +
            "and (confirm_flag = 0 or confirm_flag = 3) " +
            "</if>" +
            "<if test=\"confirmFlag != 0 \">" +
            "and confirm_flag = 1" +
            "</if>" +
            "and group_no = '00' and charge_item_code  &lt;&gt; 'BILL99' " +
            "group by charge_date,real_no,patient_id,times " +
            "</script>")
    List<MzChargeDetail> huoQuMenZhenQueRenShuJu(@Param("patientId") String patientId,
                                                 @Param("startTime") String startTime,
                                                 @Param("endTime") String endTime,
                                                 @Param("confirmFlag") Integer confirmFlag);

    //   父节点 根据  patient_id times receipt_no order_no charge_item_code
    //   子节点 根据  patient_id times receipt_no order_no tc_no
    @Select("<script>" +
            "select confirm_flag,rtrim(patient_id) patient_id ,times,item_no,receipt_no,order_no,req_no, " +
            "       rtrim(charge_item_code) charge_item_code,rtrim(isnull(tc_no,'')) tc_no,rtrim(tc_name) tc_name," +
            "       rtrim(bill_item_code) bill_item_code, (select rtrim(code) from zd_unit_code where code = exec_dept)  exec_dept," +
            "       charge_name = (select rtrim(name) from zd_charge_item where charge_item_code = code)," +
            "       quantity,drug_win,unit_price, " +
            "       exec_dept_name= (select rtrim(name) from zd_unit_code where code = exec_dept), " +
            "       confirm_id,confirm_id_name = (select rtrim(name) from a_employee_mi where code = confirm_id), " +
            "       confirm_time " +
            "       from mz_charge_detail " +
            "where  real_no = #{realNo} and patient_id = #{patientNo} and times = #{times} and req_no is not null  " +
            "and pay_mark = '0' and group_no = '00' and charge_item_code  &lt;&gt; 'BILL99' " +
            "<if test=\"xiangMuZhuangTai != null and xiangMuZhuangTai == 0 \">" +
            "and (confirm_flag = 0 or confirm_flag = 3) " +
            "</if>" +
            "<if test=\"xiangMuZhuangTai !=null and xiangMuZhuangTai == 1 \">" +
            "and confirm_flag = 1" +
            "</if>" +
            "</script>")
    List<MzChargeDetail> feiYongXinXi(@Param("realNo") Integer realNo,
                                      @Param("patientNo") String patientNo,
                                      @Param("times") Integer times,
                                      @Param("xiangMuZhuangTai") Integer xiangMuZhuangTai);

    @Select("select tc_name,quantity,group_order, drug_quan, drugUnitName = (select name from yp_zd_unit where drug_unit = code) " +
            " from mz_charge_detail where patient_id=#{patientNo} " +
            "and serial_no=#{realNo} and times=#{times} and group_no!='00'")
    List<MzChargeDetail> yaoPinXinxi(@Param("realNo") Integer realNo,
                                     @Param("patientNo") String patientNo,
                                     @Param("times") Integer times);

    /**
     * @param patientId 门诊号
     * @param times     次数
     * @param reqNo     申请号
     * @return 缴费的详细信息
     */
    @Select("select (unit_price * quantity * drug_win) charge_fee,quantity charge_amount, " +
            "(select rtrim(name) from zd_charge_item where code = charge_item_code) " +
            "from mz_charge_detail where " +
            "req_no = #{reqNo} and unit_price <> 0 and patient_id = #{patientId} and times = #{times} " +
            "and group_no = 00 and charge_item_code  <> 'BILL99' ")
    List<MzChargeDetail> getMingXi(@Param("patientId") String patientId,
                                   @Param("times") Integer times,
                                   @Param("reqNo") Integer reqNo);

    @Update("<script>" +
            "<foreach collection='list' item='item' separator=';'> " +
            "update mz_charge_detail " +
            "<set>" +
            " confirm_flag = #{item.confirmFlag} , receive_flag = #{item.confirmFlag}  " +
            "<choose>" +
            "<when test=\"item.confirmFlag == 1\">" +
            ",confirm_time =getdate() , exec_dept =#{execDept},confirm_id =#{confirmId}" +
            "</when>" +
            "<otherwise>" +
            ",confirm_time = null,confirm_id = null" +
            "</otherwise>" +
            "</choose>" +
            "</set>" +
            "where " +
            "patient_id = #{item.patientId} and times =#{item.times} " +
            "and item_no = #{item.itemNo} and charge_item_code = #{item.chargeItemCode} " +
            "and receipt_no = #{item.receiptNo} and order_no = #{item.orderNo} " +
            "and req_no =#{item.reqNo} " +
            "</foreach>" +
            "</script>")
    void xiangMuQueRenHuoQuXiao(@Param("list") List<MzChargeDetail> list,
                                @Param("confirmId") String confirmId,
                                @Param("execDept") String execDept);

    @Update("<script>" +
            "<foreach collection='list' item='item' separator=';'> " +
            "update mz_yj_req " +
            "<set>" +
            " confirm_flag = #{item.confirmFlag} , receive_flag = #{item.confirmFlag}  " +
            "<choose>" +
            "<when test=\"item.confirmFlag == 1\">" +
            ",confirm_time =getdate() , exec_dept =#{execDept},confirm_id =#{confirmId}" +
            "</when>" +
            "<otherwise>" +
            ",confirm_time = null ,confirm_id = null" + // , exec_dept = null
            "</otherwise>" +
            "</choose>" +
            "</set>" +
            "where " +
            "patient_id = #{item.patientId} and times =#{item.times} " +
            "and item_no = #{item.itemNo} " +
            "and receipt_no = #{item.receiptNo} and order_no = #{item.orderNo} " +
            "and req_no =#{item.reqNo} " +
            "</foreach>" +
            "</script>")
    void xiangMuQueRenYjReq(@Param("list") List<MzChargeDetail> list,
                            @Param("confirmId") String confirmId,
                            @Param("execDept") String execDept);


    @Select("select req_no ,\n" +
            "       order_name   ,\n" +
            "       req_comment  ,\n" +
            "       b.emr_hpi    ,\n" +
            "       b.emr_pe     ,\n" +
            "       a.req_date   ,\n" +
            "       a.req_doctor ,\n" +
            "       a.req_ward," +
            "   req_doctor_name = (select rtrim(name) from a_employee_mi c where c.code = a.req_doctor)," +
            "    req_ward_name   = (select rtrim(name) from zd_unit_code c where c.code = a.req_ward), jy_remark  \n" +
            "from mz_yj_req a,\n" +
            "     mz_bl_record b\n" +
            "where a.patient_id = b.patient_id\n" +
            "  and a.times = b.times\n" +
            "  and a.patient_id = #{patientId}\n" +
            "  and a.req_no = #{reqNo}")
    JSONObject getReqDetailsByNo(String patientId, Integer reqNo);


}
