package controllers

import (
	"XT_New/enums"
	"XT_New/models"
	"XT_New/service"
	"XT_New/utils"
	"github.com/astaxie/beego"
	"time"
)

type PharmacyController struct {
	BaseAuthAPIController
}

func PharmacyApiRegistRouters() {
	beego.Router("/api/pharmacy/ceshili", &PharmacyController{}, "get:Tlili")
	beego.Router("/api/pharmacy/todaynumber", &PharmacyController{}, "get:TodayNumber")                   //查询今天的待发药，已发药人数（
	beego.Router("/api/pharmacy/waitingdrug", &PharmacyController{}, "get:WaitingDrug")                   //获取当天待发药的所有患者（
	beego.Router("/api/pharmacy/issueddrugs", &PharmacyController{}, "get:IssuedDrug")                    //获取当天已发药的所有患者（
	beego.Router("/api/pharmacy/getpharmacycontent", &PharmacyController{}, "get:GetPharmacyContent")     //获取当天该患者的所有信息（
	beego.Router("/api/pharmacy/dispensingmedicine", &PharmacyController{}, "get:DispensingMedicine")     //患者发药按钮点击（
	beego.Router("/api/pharmacy/drugwithdrawal", &PharmacyController{}, "get:DrugWithdrawal")             //退药按钮点击
	beego.Router("/api/pharmacy/dispensingdetails", &PharmacyController{}, "get:DispensingDetails")       //获取发药明细的患者列表(
	beego.Router("/api/pharmacy/prescriptiondetails", &PharmacyController{}, "get:PrescriptionDetails")   //发药明细-详情(
	beego.Router("/api/pharmacy/dispensemedicine", &PharmacyController{}, "get:DispenseMedicine")         //获取当天已发药的药品(
	beego.Router("/api/pharmacy/waitingmedicine", &PharmacyController{}, "get:WaitingMedicine")           //获取当天待发药的药品(
	beego.Router("/api/pharmacy/getpatientswithdrugs", &PharmacyController{}, "get:GetPatientsWithDrugs") //获取当天该药品的所有患者(
	beego.Router("/api/pharmacy/medicinedeparture", &PharmacyController{}, "get:MedicineDeparture")       //药品发药按钮点击(
	beego.Router("/api/pharmacy/getcurrentname", &PharmacyController{}, "get:GetCurrentName")             //获取当前登录账号的名字(
}
func (this *PharmacyController) Tlili() {
	times := this.GetString("time", "")
	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	var stime, etime int64
	if times == "" {
		stime, etime = service.GetNowTime()
	} else {
		stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", times+" 00:00:00", loc)
		stime = stmp.Unix()
		etime = stime + 86399
	}
	orgid := this.GetAdminUserInfo().CurrentOrgId
	service.InitDrugidIsNil(orgid, stime, etime)
	this.ServeSuccessJSON(map[string]interface{}{
		"list": "err",
	})
	return
}
func (this *PharmacyController) GetCurrentName() {
	create := this.GetAdminUserInfo().AdminUser.Id
	this.ServeSuccessJSON(map[string]interface{}{
		"list": create,
	})
	return
}

//查询今天的待发药，已发药人数
func (this *PharmacyController) TodayNumber() {
	var err error
	defer func() {
		if err != nil {
			service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
		}
	}()
	orgid := this.GetAdminUserInfo().CurrentOrgId
	times := this.GetString("time", "")
	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	var stime, etime int64
	if times == "" {
		stime, etime = service.GetNowTime()
	} else {
		stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", times+" 00:00:00", loc)
		stime = stmp.Unix()
		etime = stime + 86399
	}
	//查询表里当天的数据
	var wtotal int
	wtotal, err = service.GetTodayPharmacy(stime, etime, orgid, 1)
	if err != nil {
		utils.ErrorLog(err.Error())
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	var itotal int
	itotal, err = service.GetTodayPharmacy(stime, etime, orgid, 0)
	if err != nil {
		utils.ErrorLog(err.Error())
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"wtotal": wtotal,
		"itotal": itotal,
	})
	return
}
func (this *PharmacyController) IssuedDrug() {
	var err error
	defer func() {
		if err != nil {
			service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
		}
	}()
	keyword := this.GetString("keyword", "")
	times := this.GetString("time", "")
	orgid := this.GetAdminUserInfo().CurrentOrgId
	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	var stime, etime int64
	if times == "" {
		stime, etime = service.GetNowTime()
	} else {
		stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", times+" 00:00:00", loc)
		stime = stmp.Unix()
		etime = stime + 86399
	}
	//查询表里当天的数据
	var flist []*models.TmpPatient
	flist, err = service.GetTodayDrug(stime, etime, orgid, 1, keyword)
	if err != nil {
		utils.ErrorLog(err.Error())
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"list": flist,
	})
	return
}
func (this *PharmacyController) WaitingDrug() {
	var err error
	defer func() {
		if err != nil {
			service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
		}
	}()
	keyword := this.GetString("keyword", "")
	times := this.GetString("time", "")
	orgid := this.GetAdminUserInfo().CurrentOrgId
	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	var stime, etime int64
	if times == "" {
		stime, etime = service.GetNowTime()
	} else {
		stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", times+" 00:00:00", loc)
		stime = stmp.Unix()
		etime = stime + 86399
	}
	//查询表里当天的数据
	var flist []*models.TmpPatient
	flist, err = service.GetTodayDrug(stime, etime, orgid, 0, keyword)
	if err != nil {
		utils.ErrorLog(err.Error())
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"list": flist,
	})
	return
}

//
func (this *PharmacyController) GetPharmacyContent() {
	var err error
	defer func() {
		if err != nil {
			service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
		}
	}()
	patient_id, _ := this.GetInt64("patient_id", 0)
	is_medicine, _ := this.GetInt64("is_medicine", 0) //0：待发药，1：已发药
	times := this.GetString("time", "")
	orgid := this.GetAdminUserInfo().CurrentOrgId
	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	var stime, etime int64
	if times == "" {
		stime, etime = service.GetNowTime()
	} else {
		stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", times+" 00:00:00", loc)
		stime = stmp.Unix()
		etime = stime + 86399
	}
	var list []*models.PharmacyContent
	list, err = service.GetPatientMedication(orgid, patient_id, stime, etime, is_medicine)
	if err != nil {
		utils.ErrorLog(err.Error())
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	return_value := make(map[string]interface{})
	return_value["list"] = list
	//if is_medicine == 1{发药时间先不展示
	//	return_value["time"] = time
	//}
	this.ServeSuccessJSON(return_value)
	return

}

//发药按钮点击
func (this *PharmacyController) DispensingMedicine() {
	var err error
	defer func() {
		if err != nil {
			service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
		}
	}()
	patient_id, _ := this.GetInt64("patient_id", 0)
	times := this.GetString("time", "")
	orgid := this.GetAdminUserInfo().CurrentOrgId
	creater := this.GetAdminUserInfo().AdminUser.Id
	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	var stime, etime int64
	if times == "" {
		stime, etime = service.GetNowTime()
	} else {
		stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", times+" 00:00:00", loc)
		stime = stmp.Unix()
		etime = stime + 86399
	}
	err = service.DispensingMedicine(orgid, patient_id, stime, etime, creater)
	if err != nil {
		utils.ErrorLog(err.Error())
		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"list": "操作成功",
	})
	return
}

//退药按钮点击
func (this *PharmacyController) DrugWithdrawal() {
	var err error
	defer func() {
		if err != nil {
			service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
		}
	}()
	patient_id, _ := this.GetInt64("patient_id", 0)
	times := this.GetString("time", "")
	orgid := this.GetAdminUserInfo().CurrentOrgId
	creater := this.GetAdminUserInfo().AdminUser.Id
	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	var stime, etime int64
	if times == "" {
		stime, etime = service.GetNowTime()
	} else {
		stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", times+" 00:00:00", loc)
		stime = stmp.Unix()
		etime = stime + 86399
	}
	err = service.DrugWithdrawal(orgid, patient_id, stime, etime, creater)
	if err != nil {
		utils.ErrorLog(err.Error())
		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"list": "操作成功",
	})
	return

}

//发药明细列表
func (this *PharmacyController) DispensingDetails() {
	var err error
	defer func() {
		if err != nil {
			service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
		}
	}()
	keyword := this.GetString("keyword", "")       //患者名称
	start_time := this.GetString("start_time", "") //开始时间
	end_time := this.GetString("end_time", "")     //结束时间
	page, _ := this.GetInt64("page", 1)            //页码
	limit, _ := this.GetInt64("limit", 10)         //每一页查出来的条数
	orgid := this.GetAdminUserInfo().CurrentOrgId
	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	var stime, etime int64
	if start_time == "" {
		stime = 1
	} else {
		stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
		stime = stmp.Unix()
	}
	if end_time == "" {
		_, etime = service.GetNowTime()
	} else {
		etmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
		etime = etmp.Unix()
	}
	var dislist []*models.DispensingList
	var total int64
	dislist, total, err = service.DispensingDetailsList(stime, etime, orgid, page, limit, keyword)
	if err != nil {
		utils.ErrorLog(err.Error())
		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
		return
	}

	this.ServeSuccessJSON(map[string]interface{}{
		"list":  dislist,
		"total": total,
	})
	return
}

//处方详情
func (this *PharmacyController) PrescriptionDetails() {
	var err error
	defer func() {
		if err != nil {
			service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
		}
	}()
	//患者姓名
	patient_id, _ := this.GetInt64("patient_id", 0)
	//发药时间
	record_date, _ := this.GetInt64("record_date", 0)
	orgid := this.GetAdminUserInfo().CurrentOrgId
	if record_date == 0 || patient_id == 0 {
		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数不足")
		return
	}
	var list []*models.PrescripDetails
	list, err = service.PrescriptionDetails(patient_id, record_date, orgid)
	if err != nil {
		utils.ErrorLog(err.Error())
		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"list": list,
	})
	return
}

//已发药品的信息
func (this *PharmacyController) DispenseMedicine() {
	var err error
	defer func() {
		if err != nil {
			service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
		}
	}()
	keyword := this.GetString("keyword", "")
	times := this.GetString("time", "")
	orgid := this.GetAdminUserInfo().CurrentOrgId
	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	var stime, etime int64
	if times == "" {
		stime, etime = service.GetNowTime()
	} else {
		stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", times+" 00:00:00", loc)
		stime = stmp.Unix()
		etime = stime + 86399
	}
	//查询表里当天的数据
	var flist []*models.ListOfDrugs
	flist, err = service.GetTodayMedicine(stime, etime, orgid, 1, keyword)
	if err != nil {
		utils.ErrorLog(err.Error())
		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"list": flist,
	})
	return
}

//待发药的药品信息
func (this *PharmacyController) WaitingMedicine() {
	var err error
	defer func() {
		if err != nil {
			service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
		}
	}()
	keyword := this.GetString("keyword", "")
	times := this.GetString("time", "")
	orgid := this.GetAdminUserInfo().CurrentOrgId
	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	var stime, etime int64
	if times == "" {
		stime, etime = service.GetNowTime()
	} else {
		stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", times+" 00:00:00", loc)
		stime = stmp.Unix()
		etime = stime + 86399
	}
	//查询表里当天的数据
	var flist []*models.ListOfDrugs
	flist, err = service.GetTodayMedicine(stime, etime, orgid, 0, keyword)
	if err != nil {
		utils.ErrorLog(err.Error())
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"list": flist,
	})
	return
}

//获取药品的所有患者信息
func (this *PharmacyController) GetPatientsWithDrugs() {
	var err error
	defer func() {
		if err != nil {
			service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
		}
	}()
	drug_id, _ := this.GetInt64("drug_id", 0)
	is_medicine, _ := this.GetInt64("is_medicine", 0) //0：待发药，1：已发药
	times := this.GetString("time", "")
	orgid := this.GetAdminUserInfo().CurrentOrgId
	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	var stime, etime int64
	if times == "" {
		stime, etime = service.GetNowTime()
	} else {
		stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", times+" 00:00:00", loc)
		stime = stmp.Unix()
		etime = stime + 86399
	}
	var list []*models.PatientInformation
	list, err = service.FindMedicationList(orgid, drug_id, stime, etime, is_medicine)
	if err != nil {
		utils.ErrorLog(err.Error())
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"list": list,
	})
	return
}

//药品管理发药按钮点击
func (this *PharmacyController) MedicineDeparture() {
	var err error
	defer func() {
		if err != nil {
			service.SaveErrs(this.GetAdminUserInfo().CurrentOrgId, this.Ctx.Input, err)
		}
	}()
	creater, _ := this.GetInt64("creater", 0) //领药人
	ids := this.GetString("ids", "")          //发药的数据
	orgid := this.GetAdminUserInfo().CurrentOrgId
	if ids == "" {
		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数不足")
		return
	}
	err = service.MedicineDeparture(ids, creater, orgid)
	if err != nil {
		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"list": "修改成功",
	})
	return
}
