package controllers

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

type HisChargeApiController struct {
	BaseAuthAPIController
}

func HisChargeApiRegistRouters() {
	beego.Router("/api/his/chargestatistics/detail", &HisChargeApiController{}, "get:GetChargeStatisticsDetail")
	beego.Router("/api/his/chargestatistics/settle", &HisChargeApiController{}, "get:GetChargeStatisticsSettle")

	beego.Router("/api/his/patient", &HisChargeApiController{}, "get:GetAllPatient")
	//beego.Router("/api/his/chargestatistics/settle", &HisChargeApiController{}, "get:GetChargeStatisticsSettle")

	beego.Router("/api/his/inspectionlist/get", &HisChargeApiController{}, "get:GetHisInspectionList")
	beego.Router("/api/his/inspectioninfo/get", &HisChargeApiController{}, "get:GetHisInspectionInfo")

	//发票
	beego.Router("/api/fapiao/create", &HisChargeApiController{}, "post:CreateFaPiaoRecord")
	beego.Router("/api/fapiao/modify", &HisChargeApiController{}, "post:ModifyFaPiaoRecord")
	beego.Router("/api/fapiao/list", &HisChargeApiController{}, "get:GetFaPiaoRecordList")
	beego.Router("/api/fapiao/delete", &HisChargeApiController{}, "post:DeleteFaPiaoRecord")
	beego.Router("/api/fapiao", &HisChargeApiController{}, "get:GetFaPiaoRecord")
	beego.Router("/api/fapiao/is_use", &HisChargeApiController{}, "post:UpdateFaPiaoRecordIsUse")

	beego.Router("/api/his/getyidiclear", &HisChargeApiController{}, "get:GetHisYidiClearRecord")

}

func (c *HisChargeApiController) UpdateFaPiaoRecordIsUse() {
	id, _ := c.GetInt64("id", 0)
	is_use, _ := c.GetInt64("is_use")
	if id <= 0 {
		utils.ErrorLog("id == 0")
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}

	record, _ := service.FindFapiaoById(id)
	adminUserInfo := c.GetAdminUserInfo()
	service.UpdateFapiaoIsUse(adminUserInfo.CurrentOrgId)
	records := models.HisFapiaoRecord{
		ID:           id,
		FapiaoCode:   record.FapiaoCode,
		FapiaoNumber: record.FapiaoNumber,
		Ctime:        time.Now().Unix(),
		Mtime:        time.Now().Unix(),
		UserOrgId:    adminUserInfo.CurrentOrgId,
		Status:       1,
		IsUse:        is_use,
	}
	err := service.ModifyFapiao(&records)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"fapiao_record": records,
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}
}
func (c *HisChargeApiController) CreateFaPiaoRecord() {
	fapiao_code := c.GetString("fapiao_code")
	fapiao_number := c.GetString("fapiao_number")
	if len(fapiao_code) <= 0 {
		utils.ErrorLog("len(fapiao_code) == 0")

		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}

	if len(fapiao_number) <= 0 {
		utils.ErrorLog("len(fapiao_number) == 0")
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}

	adminUserInfo := c.GetAdminUserInfo()

	record := models.HisFapiaoRecord{
		FapiaoCode:   fapiao_code,
		FapiaoNumber: fapiao_number,
		Ctime:        time.Now().Unix(),
		Mtime:        time.Now().Unix(),
		UserOrgId:    adminUserInfo.CurrentOrgId,
		Status:       1,
		IsUse:        0,
	}

	err, records := service.AddSigleFapiaoRecord(&record)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"fapiao_record": records,
		})

	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)

	}

}
func (c *HisChargeApiController) ModifyFaPiaoRecord() {
	id, _ := c.GetInt64("id", 0)
	fapiao_code := c.GetString("fapiao_code")
	fapiao_number := c.GetString("fapiao_number")

	if id <= 0 {
		utils.ErrorLog("id == 0")
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}

	if len(fapiao_code) <= 0 {
		utils.ErrorLog("len(fapiao_code) == 0")
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}

	if len(fapiao_number) <= 0 {
		utils.ErrorLog("len(fapiao_number) == 0")
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}

	record, _ := service.FindFapiaoById(id)
	adminUserInfo := c.GetAdminUserInfo()
	records := models.HisFapiaoRecord{
		ID:           id,
		FapiaoCode:   fapiao_code,
		FapiaoNumber: fapiao_number,
		Ctime:        time.Now().Unix(),
		Mtime:        time.Now().Unix(),
		UserOrgId:    adminUserInfo.CurrentOrgId,
		Status:       1,
		IsUse:        record.IsUse,
	}

	err := service.ModifyFapiao(&records)

	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"fapiao_record": records,
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}
}
func (c *HisChargeApiController) GetFaPiaoRecordList() {
	page, _ := c.GetInt64("page", 1)
	limit, _ := c.GetInt64("limit", 7)
	adminUserInfo := c.GetAdminUserInfo()
	fapiaoRecord, total, err := service.FindAllFapiaoList(adminUserInfo.CurrentOrgId, page, limit)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"fapiao_record": fapiaoRecord,
			"total":         total,
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}
}
func (c *HisChargeApiController) DeleteFaPiaoRecord() {
	id, _ := c.GetInt64("id", 0)
	err := service.DeleteFapiaoById(id)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"msg": "删除成功",
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}
}
func (c *HisChargeApiController) GetFaPiaoRecord() {
	id, _ := c.GetInt64("id", 0)
	fapiaoRecord, err := service.FindFapiaoById(id)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"fapiao_record": fapiaoRecord,
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}
}

func (c *HisChargeApiController) GetChargeStatisticsDetail() {
	start_time := c.GetString("start_time")
	end_time := c.GetString("end_time")
	keyword := c.GetString("keyword")
	item_type, _ := c.GetInt64("type")

	adminUser := c.GetAdminUserInfo()

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	startTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
	if err != nil {

	}
	startRecordDateTime := startTime.Unix()

	endTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
	if err != nil {

	}
	endRecordDateTime := endTime.Unix()

	chargePatient, err := service.GetAllPatientChargeDetails(adminUser.CurrentOrgId, startRecordDateTime, endRecordDateTime, keyword, item_type)

	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"patients": chargePatient,
		})
		return
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return

	}

}

func (c *HisChargeApiController) GetChargeStatisticsSettle() {
	start_time := c.GetString("start_time")
	end_time := c.GetString("end_time")
	keyword := c.GetString("keyword")
	item_type, _ := c.GetInt64("type")
	adminUser := c.GetAdminUserInfo()

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	startTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
	if err != nil {

	}
	startRecordDateTime := startTime.Unix()

	endTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
	if err != nil {

	}
	endRecordDateTime := endTime.Unix()
	chargePatient, err := service.GetAllPatientChargeSettle(adminUser.CurrentOrgId, startRecordDateTime, endRecordDateTime, keyword, item_type)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"patients": chargePatient,
		})
		return
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return

	}

}

func (c *HisChargeApiController) GetHisInspectionList() {
	record_date := c.GetString("record_date")
	keyword := c.GetString("keyword")
	is_print, _ := c.GetInt64("is_print")
	page, _ := c.GetInt64("page")
	limit, _ := c.GetInt64("limit")

	adminUser := c.GetAdminUserInfo()

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	startTime, err := time.ParseInLocation(timeLayout+" 15:04:05", record_date+" 00:00:00", loc)
	if err != nil {

	}
	record_time := startTime.Unix()
	labels, total, err := service.GetLabelPrintList(page, limit, adminUser.CurrentOrgId, record_time, is_print, keyword)
	if err == nil {

		c.ServeSuccessJSON(map[string]interface{}{
			"labels": labels,
			"total":  total,
		})
		return

	} else {

		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return

	}

}
func (c *HisChargeApiController) GetHisInspectionInfo() {
	id, _ := c.GetInt64("id")
	label, err := service.GetLabelPrintInfo(id)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"label": label,
		})
		return
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
}

func (c *HisChargeApiController) GetHisYidiClearRecord() {
	records, err := service.FindHisYidiClearRecord(c.GetAdminUserInfo().CurrentOrgId)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"list": records,
		})
		return
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
}

func (c *HisChargeApiController) GetAllPatient() {
	patients, err := service.GetAllPatientTwo(c.GetAdminUserInfo().CurrentOrgId)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"list": patients,
		})
		return
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
}

func (c *HisChargeApiController) GetStatisticsDetail() {
	start_time := c.GetString("start_time")
	end_time := c.GetString("end_time")
	keyword := c.GetString("keyword")
	item_type, _ := c.GetInt64("type")
	p_type, _ := c.GetInt64("p_type")
	patinet_id, _ := c.GetInt64("patinet_id")

	adminUser := c.GetAdminUserInfo()

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	startTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
	if err != nil {

	}
	startRecordDateTime := startTime.Unix()

	endTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
	if err != nil {

	}
	endRecordDateTime := endTime.Unix()

	chargePatient, err := service.GetPatientChargeDetails(patinet_id, adminUser.CurrentOrgId, startRecordDateTime, endRecordDateTime, keyword, item_type, p_type)

	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"patients": chargePatient,
		})
		return
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return

	}

}

func (c *HisChargeApiController) GetStatisticsGather() {
	start_time := c.GetString("start_time")
	end_time := c.GetString("end_time")
	keyword := c.GetString("keyword")
	item_type, _ := c.GetInt64("type")
	adminUser := c.GetAdminUserInfo()
	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	startTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
	if err != nil {

	}
	startRecordDateTime := startTime.Unix()

	endTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
	if err != nil {

	}
	endRecordDateTime := endTime.Unix()

	chargePatient, err := service.GetAllPatientChargeDetails(adminUser.CurrentOrgId, startRecordDateTime, endRecordDateTime, keyword, item_type)

	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"patients": chargePatient,
		})
		return
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return

	}

}
