package admin_api_controllers

import (
	"XT_Admin_Api/enums"
	"XT_Admin_Api/models/admin_models"
	"XT_Admin_Api/service"
	"XT_Admin_Api/utils"
	"fmt"
	"time"
)

type OrgAPIController struct {
	AdminBaseAPIAuthController
}

func (this *OrgAPIController) GetOrgList() {
	active_status,_ := this.GetInt64("active_status")
	depth_active_status,_ := this.GetInt64("depth_active_status")
	start_time := this.GetString("start_time")
	end_time := this.GetString("end_time")
	province,_ := this.GetInt64("province")
	city,_ := this.GetInt64("city")
	district,_ := this.GetInt64("district")
	follow,_ := this.GetInt64("follow")
	keyword := this.GetString("keyword")
	page, _ := this.GetInt64("page", 0)
	limit, _ := this.GetInt64("limit", 0)
	if page <= 0 {
		page = 1
	}
	if limit <= 0 {
		limit = 7
	}

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	var startTime int64
	if len(start_time) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
		if err != nil {
			fmt.Println(err)
			this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		startTime = theTime.Unix()
	}
	var endTime int64
	if len(end_time) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
		if err != nil {
			utils.ErrorLog(err.Error())
			this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		endTime = theTime.Unix()
	}

	adminInfo := this.GetAdminInfo()
	if adminInfo.Admin.IsSuperAdmin == 1 {
		list, err, total := service.GetAllOrgList(active_status,depth_active_status,startTime,endTime,province,city,district,follow,keyword,page,limit)
		if err != nil {

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

	}
}

func (this *OrgAPIController) GetOrgInfo() {
	org_id, _:=this.GetInt64("org_id",0)
	info, err := service.GetOrgDetailInfo(org_id)
	province, _ := service.GetDistrictsById(info.Province)
	city, _ := service.GetDistrictsById(info.City)
	district, _ := service.GetDistrictsById(info.District)

	orgType, err := service.GetAllOrgType()

	if err != nil {
		this.ErrorLog("错误：%v", err)
		return
	} else {
		this.ServeSuccessJSON(map[string]interface{}{
			"info": info,
			"area":province.Name+city.Name+district.Name,
			"org_type":orgType,
		})
	}
}

func (this *OrgAPIController) GetOrgDialysisOrderStatistics() {
	org_id, _:=this.GetInt64("org_id",0)
	range_data := this.GetString("range_data")
	range_type, _ := this.GetInt64("range_type")
	statistics_type, _ := this.GetInt64("type")
	var rangeDate *time.Time
	var parseDateErr error
	var rangeTimeStamp int64




	var orgDialysisOrderStatistics []*admin_models.Statistics

	switch range_type{
	case 1: //今天
		if len(range_data) > 0 {
			rangeDate, parseDateErr = utils.ParseTimeStringToTime("2006-01-02", range_data)
			if parseDateErr != nil {
				this.ErrorLog("日期(%v)解析错误：%v", range_data, parseDateErr)
				return
			}
			rangeTimeStamp = rangeDate.Unix()
		} else {
			rangeTimeStamp = 0
		}
		now := time.Now().Unix()
		count , err := service.GetOrgDialysisCount(statistics_type,org_id,rangeTimeStamp,now)
		if err != nil {
			this.ErrorLog("错误：%v", err)
			return

		} else {
			this.ServeSuccessJSON(map[string]interface{}{
				"count": count,
			})
		}
		break
	case 2:  //昨天
		if len(range_data) > 0 {
			rangeDate, parseDateErr = utils.ParseTimeStringToTime("2006-01-02", range_data)
			if parseDateErr != nil {
				this.ErrorLog("日期(%v)解析错误：%v", range_data, parseDateErr)
				return
			}
			rangeTimeStamp = rangeDate.Unix()
		} else {
			rangeTimeStamp = 0
		}
		now := time.Now().Unix()
		count , err := service.GetOrgDialysisCount(statistics_type,org_id,rangeTimeStamp,now)
		if err != nil {
			this.ErrorLog("错误：%v", err)
			return

		} else {
			this.ServeSuccessJSON(map[string]interface{}{
				"count": count,
			})
		}
		break
	case 3://一周
		var err error
		orgDialysisOrderStatistics, err = service.GetWeekOrgDialysisCount(statistics_type, org_id)
		if err != nil {
			this.ErrorLog("错误：%v", err)
			return
		} else {
			this.ServeSuccessJSON(map[string]interface{}{
				"list": orgDialysisOrderStatistics,
			})
		}
		break
	case 4://一个月
		var err error
		orgDialysisOrderStatistics, err = service.GetMonthOrgDialysisCount(statistics_type, org_id)
		if err != nil {
			this.ErrorLog("错误：%v", err)
			return
		} else {
			this.ServeSuccessJSON(map[string]interface{}{
				"list": orgDialysisOrderStatistics,
			})
		}
		break
	case 5://三个月
		var err error
		orgDialysisOrderStatistics, err = service.GetThreeMonthOrgDialysisCount(statistics_type, org_id)
		if err != nil {
			this.ErrorLog("错误：%v", err)
			return
		} else {
			this.ServeSuccessJSON(map[string]interface{}{
				"list": orgDialysisOrderStatistics,
			})
		}
		break
	case 6://半年
		var err error
		orgDialysisOrderStatistics, err = service.GetSixMonthOrgDialysisCount(statistics_type, org_id)
		if err != nil {
			this.ErrorLog("错误：%v", err)
			return
		} else {
			this.ServeSuccessJSON(map[string]interface{}{
				"list": orgDialysisOrderStatistics,
			})
		}
		break
	case 7://一年
		var err error
		orgDialysisOrderStatistics, err = service.GetYearOrgDialysisCount(statistics_type, org_id)
		if err != nil {
			this.ErrorLog("错误：%v", err)
			return
		} else {
			this.ServeSuccessJSON(map[string]interface{}{
				"list": orgDialysisOrderStatistics,
			})
		}
		break
	}
}

func (c *OrgAPIController) GetDistrictsByUpid() {
	id, _ := c.GetInt64("id", 0)
	citys, _ := service.GetDistrictsByUpid(id)
	c.ServeSuccessJSON(map[string]interface{}{
		"citys": citys,
	})
	return
}


func (this *OrgAPIController)GetInitData(){
	provinces, _ := service.GetDistrictsByUpid(0)
	this.ServeSuccessJSON(map[string]interface{}{
		"provinces": provinces,
	})
}
