package new_mobile_api_controllers

import (
	"Xcx_New/controllers/mobile_api_controllers"
	"Xcx_New/enums"
	"Xcx_New/service"
	"fmt"
	"github.com/jinzhu/gorm"
	"time"
)

type DialysisParamerterApiController struct {
	mobile_api_controllers.MobileBaseAPIAuthController
}

func (this *DialysisParamerterApiController) GetAllMobileZon() {

	adminUserInfo := this.GetMobileAdminUserInfo()
	orgid := adminUserInfo.Org.Id
	err, zone := service.GetAllDeviceZone(orgid)
	if err == nil {
		this.ServeSuccessJSON(map[string]interface{}{
			"zone": zone,
		})
	}
}

func (this *DialysisParamerterApiController) GetMobileDialysisParameters() {
	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	adminUserInfo := this.GetMobileAdminUserInfo()
	orgid := adminUserInfo.Org.Id
	fmt.Println(orgid)
	keywords := this.GetString("keyword")
	start_time := this.GetString("start_time")
	fmt.Println("start_time", start_time)
	limit, _ := this.GetInt64("limit")
	fmt.Println(limit)
	page, _ := this.GetInt64("page")
	fmt.Println(page)
	partitionType, _ := this.GetInt64("partitionType")
	fmt.Println("partiontype", partitionType)
	scheduleType, _ := this.GetInt64("scheduleType")
	fmt.Println(scheduleType)

	theTime, _ := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
	if len(keywords) > 0 {

		//dialysisSchedule, err, total := service.GetDialysisParametersByKeyword(orgid, keywords, scheduleType, partitionType, page, limit, theTime.Unix())
		//查询今日排班的病人
		patient, err := service.GetDialysisTodaySchedulePatient(orgid, theTime.Unix(), page, limit, keywords, scheduleType, partitionType)
		var vlist []interface{}
		var elist []interface{}
		for _, item := range patient {
			prescription, _ := service.GetDialysisPrescriptionList(item.PatientId, orgid, theTime.Unix())
			vlist = append(vlist, prescription)
			dialysis, _ := service.GetAssessmentBeforDialysisByStartime(item.PatientId, orgid, theTime.Unix())
			elist = append(elist, dialysis)
			if err == nil {
				this.ServeSuccessJSON(map[string]interface{}{
					//"schedule":     dialysisSchedule,
					//"total":        total,
					"prescription": vlist,
					"dialysbefor":  elist,
					"patient":      patient,
				})
			} else {
				this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
			}
		}
	} else {
		dialysisSchedule, err, total := service.GetDialysisParameter(orgid, theTime.Unix(), scheduleType, partitionType, page, limit)
		//查询今日排班的病人
		patient, err := service.GetDialysisTodaySchedulePatient(orgid, theTime.Unix(), page, limit, keywords, scheduleType, partitionType)
		var vlist []interface{}
		var elist []interface{}
		for _, item := range patient {
			prescription, _ := service.GetDialysisPrescriptionList(item.PatientId, orgid, theTime.Unix())
			vlist = append(vlist, prescription)
			dialysis, _ := service.GetAssessmentBeforDialysisByStartime(item.PatientId, orgid, theTime.Unix())
			elist = append(elist, dialysis)
		}
		if err == nil {
			this.ServeSuccessJSON(map[string]interface{}{
				"schedule":     dialysisSchedule,
				"total":        total,
				"prescription": vlist,
				"dialysbefor":  elist,
				"patient":      patient,
			})
		} else {
			this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
		}
	}
}

func (this *DialysisParamerterApiController) GetMobileWareHouseOutList() {

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	start_time := this.GetString("start_time")
	startime, _ := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
	end_time := this.GetString("end_time")
	endtime, _ := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
	fmt.Println(start_time, end_time)
	orgId := this.GetMobileAdminUserInfo().Org.Id
	wareoutlist, err := service.GetWareHouseOutList(startime.Unix(), endtime.Unix(), orgId)
	count, err := service.GetAnticoagulantCount(startime.Unix(), endtime.Unix(), orgId)
	if err == nil {
		this.ServeSuccessJSON(map[string]interface{}{
			"wareoutlist": wareoutlist,
			"count":       count,
		})
	}

}

func (this *DialysisParamerterApiController) GetAllMobileMaterial() {

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	start_time := this.GetString("startime")
	startime, _ := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
	fmt.Println("开始时间", startime)
	endtime, _ := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 23:59:59", loc)
	fmt.Println("结束时间", endtime)
	adminUserInfo := this.GetMobileAdminUserInfo()
	orgId := adminUserInfo.Org.Id
	material, err := service.GetAllMaterial(startime.Unix(), endtime.Unix(), orgId)
	if err == nil {
		this.ServeSuccessJSON(map[string]interface{}{
			"material": material,
		})
	}
}

func (this *DialysisParamerterApiController) GetMobileCollectList() {

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	limit, _ := this.GetInt64("limit")
	fmt.Println(limit)
	page, _ := this.GetInt64("page")
	fmt.Println("page", page)
	partitiontype, _ := this.GetInt64("partitionType")
	fmt.Println(partitiontype)
	scheduletype, _ := this.GetInt64("scheduleType")
	fmt.Println(scheduletype)
	start_time := this.GetString("start_time")
	fmt.Println(start_time)
	startime, _ := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
	orgId := this.GetMobileAdminUserInfo().Org.Id
	keyword := this.GetString("keyword")
	//获取透析参数数据
	//schedule, err, _ := service.GetCollectList(limit, page, partitiontype, scheduletype, startime.Unix(), orgId, keyword)
	//查询今日排班的病人
	patient, err := service.GetDialysisTodaySchedulePatient(orgId, startime.Unix(), page, limit, keyword, scheduletype, partitiontype)
	var vlist []interface{}
	var clist []interface{}
	for _, item := range patient {
		prescription, _ := service.GetDialysisPrescriptionList(item.PatientId, orgId, startime.Unix())
		vlist = append(vlist, prescription)
	}
	_, errors := service.FindStockOutByIsSys(orgId, 1, startime.Unix())

	goodTypes, _ := service.FindAllGoodType(orgId) //two, err, i := service.PCGetDialysisGoodsTwo(orgId, startime.Unix(), scheduletype, partitiontype, page, limit, keyword)

	if errors == gorm.ErrRecordNotFound {
		dialysisGoods, _, total := service.PCGetDialysisGoodsTwo(orgId, startime.Unix(), scheduletype, partitiontype, page, limit, keyword)
		for _, item := range dialysisGoods { //获取当天排班的每个患者的最后日期的库存使用情况
			goodUser, _ := service.GetLastDialysisGoods(item.PatientId, orgId, startime.Unix())
			lastGoodUserDetial, _ := service.GetLastDialysisBeforePrepare(item.PatientId, orgId, startime.Unix())
			item.LastAutomaticReduceDetail = goodUser
			item.LastDialysisBeforePrepare = lastGoodUserDetial
		}
		this.ServeSuccessJSON(map[string]interface{}{
			"dialysis_goods": dialysisGoods,
			"good_type":      goodTypes,
			"total":          total,
			//"schedule":       schedule,
			"prescription": vlist,
			"patient":      patient,
		})
		return

	} else if err == nil {
		//获取当天排班的每个患者的库存使用情况
		dialysisGoods, err, total := service.PCGetDialysisGoodsTwo(orgId, startime.Unix(), scheduletype, partitiontype, page, limit, keyword)
		for _, item := range dialysisGoods { //获取当天排班的每个患者的最后日期的库存使用情况
			goodUser, _ := service.GetLastDialysisGoods(item.PatientId, orgId, startime.Unix())
			lastGoodUserDetial, _ := service.GetLastDialysisBeforePrepare(item.PatientId, orgId, startime.Unix())

			fmt.Println(goodUser)
			fmt.Println(lastGoodUserDetial)

			item.LastAutomaticReduceDetail = goodUser
			item.LastDialysisBeforePrepare = lastGoodUserDetial
		}
		if err == nil {
			this.ServeSuccessJSON(map[string]interface{}{
				"dialysis_goods": dialysisGoods,
				"good_type":      goodTypes,
				"total":          total,
				//"schedule":       schedule,
				//"consumables":    consumables,
				"prescription": vlist,
				"reducelist":   clist,
			})
			return

		} else {
			this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
			return
		}
	} else if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return

	}
	//if err == nil {
	//	this.ServeSuccessJSON(map[string]interface{}{
	//		"schedule":     schedule,
	//		"total":        total,
	//		"consumables":  consumables,
	//		"prescription": vlist,
	//		"reducelist":   clist,
	//	})
	//}
}

func (this *DialysisParamerterApiController) GetMobileGatherList() {

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	start_time := this.GetString("start_time")
	end_time := this.GetString("end_time")
	startime, _ := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
	endtime, _ := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
	adminUserInfo := this.GetMobileAdminUserInfo()
	orgId := adminUserInfo.Org.Id
	fmt.Println("开始时间", startime)
	fmt.Println("结束时间", endtime)
	warehouselist, err := service.GetWareHouseOutList(startime.Unix(), endtime.Unix(), orgId)

	//获取透析耗材数据
	consumables, err := service.GetDialysisConsumables(startime.Unix(), endtime.Unix(), orgId)

	//统计抗凝剂数据
	count, err := service.GetAnticoagulantCount(startime.Unix(), endtime.Unix(), orgId)

	if err == nil {
		this.ServeSuccessJSON(map[string]interface{}{
			"warehouselist": warehouselist,
			"consumables":   consumables,
			"count":         count,
		})
	}
}
