package controllers

import (
	"cc-officialweb/models"
	"cc-officialweb/service"
	"cc-officialweb/unity"
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"github.com/go-playground/validator/v10"
	"strconv"
)

type DataModelController struct {
	beego.Controller
}

// GetData 获取数据
func (d *DataModelController) GetData() {
	data, err := service.GetDataModel()
	if err != nil {
		d.Data["json"] = JSON{Code: 103, Msg: "获取失败", Data: ""}
		d.ServeJSON()
		return
	} else {
		d.Data["json"] = JSON{Code: 200, Msg: "获取成功", Data: data}
		d.ServeJSON()
	}
}

// GetDatas 获取数据
func (d *DataModelController) GetDatas() {
	var page unity.PageParams
	err := json.Unmarshal(d.Ctx.Input.RequestBody, &page)
	if err != nil {
		d.Data["json"] = &JSON{Code: 101, Msg: "参数错误"}
		d.ServeJSON()
		return
	}
	result, total, current, err := unity.Paginate(page, models.Data{})
	if err != nil {
		d.Data["json"] = &JSON{Code: 101, Msg: "查询失败"}
		d.ServeJSON()
		return
	} else {
		d.Data["json"] = &JSON{Code: 200, Msg: "查询成功", Data: &JSONS{
			Current: current,
			Size:    total,
			Data:    result,
		}}
		d.ServeJSON()
		return
	}
}

// GetDataType 获得数据类型
func (d *DataModelController) GetDataType() {
	data, err := service.GetDataType()
	if err == nil {
		d.Data["json"] = &JSON{Code: 200, Msg: "查询成功", Data: data}
		d.ServeJSON()
	} else {
		d.Data["json"] = &JSON{Code: 101, Msg: "查询失败"}
		d.ServeJSON()
		return
	}
}

// GetDatasByType 获取数据类型
func (d *DataModelController) GetDatasByType() {
	types := d.GetString("types")
	err2 := validator.New().Var(types, "required")
	if err2 != nil {
		d.Data["json"] = JSON{Code: 103, Msg: "获取失败缺少必要参数", Data: ""}
	}
	data, err := service.GetDataByType(types)
	if err != nil {
		d.Data["json"] = JSON{Code: 103, Msg: "获取失败", Data: ""}
		d.ServeJSON()
		return
	} else {
		d.Data["json"] = JSON{Code: 200, Msg: "获取成功", Data: data}
		d.ServeJSON()
	}
}

// UpdataData 更新数据
func (d *DataModelController) UpdataData() {
	var dataDto models.DataDto
	var data models.Data
	err := json.Unmarshal(d.Ctx.Input.RequestBody, &dataDto)
	if err != nil {
		d.Data["json"] = JSON{Code: 103, Msg: "json解析失败", Data: ""}
		d.ServeJSON()
		return
	}
	data.Title = dataDto.Title
	data.Types = dataDto.Types
	data.Nums = dataDto.Nums
	data.IsIndex = dataDto.IsIndex
	id, err := unity.UpdateById(dataDto.ID, &data)
	if err == nil {
		d.Data["json"] = JSON{Code: 200, Msg: "更新成功", Data: id}
		d.ServeJSON()
	} else {
		d.Data["json"] = JSON{Code: 103, Msg: "更新失败", Data: ""}
		d.ServeJSON()
	}
}

// DeleteDataById 根据id删除数据
func (d *DataModelController) DeleteDataById() {
	getString := d.GetString("id")
	err := validator.New().Var("id", "required")
	if err != nil {
		d.Data["json"] = JSON{Code: 104, Msg: "id不能为空", Data: ""}
		d.ServeJSON()
	}
	atoi, _ := strconv.Atoi(getString)
	id, err := unity.DeleteById(atoi, models.Data{})
	if err != nil {
		d.Data["json"] = JSON{Code: 104, Msg: "删除失败", Data: ""}
		d.ServeJSON()
	} else {
		d.Data["json"] = JSON{Code: 200, Msg: "删除成功", Data: id}
		d.ServeJSON()
	}
}

// AddData 添加数据
func (d *DataModelController) AddData() {
	var data models.Data
	var dataDto models.DataDto
	err := json.Unmarshal(d.Ctx.Input.RequestBody, &dataDto)
	if err != nil {
		d.Data["json"] = JSON{Code: 103, Msg: "json解析失败", Data: ""}
		d.ServeJSON()
		return
	}
	data.Nums = dataDto.Nums
	data.Title = dataDto.Title
	data.Types = dataDto.Types
	data.IsIndex = dataDto.IsIndex
	add, err := unity.Add(&data)
	if err != nil {
		d.Data["json"] = JSON{Code: 104, Msg: "添加失败", Data: ""}
		d.ServeJSON()
	} else {
		d.Data["json"] = JSON{Code: 200, Msg: "添加成功", Data: add}
		d.ServeJSON()
	}
}

// UpdataModel 更新数据
func (d *DataModelController) UpdataModel() {
	var dataModel models.DataMOdelDto
	var data models.DataMOdel
	err := json.Unmarshal(d.Ctx.Input.RequestBody, &dataModel)
	if err != nil {
		d.Data["json"] = JSON{Code: 103, Msg: "json解析失败", Data: ""}
		d.ServeJSON()
		return
	}
	data.AfterSalesEarlyWarningService = dataModel.AfterSalesEarlyWarningService
	data.Airpacing = dataModel.Airpacing
	data.BigDataManagementPlatform = dataModel.BigDataManagementPlatform
	data.Cdc = dataModel.Cdc
	data.CdcInjectionSites = dataModel.CdcInjectionSites
	data.ChainPharmacies = dataModel.ChainPharmacies
	data.ColdChainLogisticsCompany = dataModel.ColdChainLogisticsCompany
	data.ColdChainTraining = dataModel.ColdChainTraining
	data.ColdStorage = dataModel.ColdStorage
	data.Counties = dataModel.Counties
	data.Dehumidifiers = dataModel.Dehumidifiers
	data.DvisualizationPlatform = dataModel.DvisualizationPlatform
	data.Freezer = dataModel.Freezer
	data.Hospital = dataModel.Hospital
	data.Incubator = dataModel.Incubator
	data.IncubatorSafetyPlatform = dataModel.IncubatorSafetyPlatform
	data.NumberOfProbes = dataModel.NumberOfProbes
	data.NumberOfStores = dataModel.NumberOfStores
	data.NumberOfValidationReports = dataModel.NumberOfValidationReports
	data.NumberOfValidationScenarios = dataModel.NumberOfValidationScenarios
	data.Pharmaceutical = dataModel.Pharmaceutical
	data.PharmaceuticalCompanies = dataModel.PharmaceuticalCompanies
	data.Prefecture = dataModel.Prefecture
	data.Provinces = dataModel.Provinces
	data.RefrigeratedCabinets = dataModel.RefrigeratedCabinets
	data.RefrigeratedTrucks = dataModel.RefrigeratedTrucks
	data.SafetyPlatformForRefrigeratedTrucks = dataModel.SafetyPlatformForRefrigeratedTrucks
	data.ShadedCabinet = dataModel.ShadedCabinet
	data.ShadedStorage = dataModel.ShadedStorage
	data.ID = dataModel.ID
	model := service.UpdateDataModel(data)
	if model {
		d.Data["json"] = JSON{Code: 200, Msg: "更新成功", Data: ""}
		d.ServeJSON()
	} else {
		d.Data["json"] = JSON{Code: 103, Msg: "更新失败", Data: ""}
		d.ServeJSON()
	}
}

// DeleteDataModel 删除数据
func (d *DataModelController) DeleteDataModel() {
	getString := d.GetString("id")
	err := validator.New().Var("id", "required")
	if err != nil {
		d.Data["json"] = JSON{Code: 104, Msg: "id不能为空", Data: ""}
		d.ServeJSON()
	}
	atoi, _ := strconv.Atoi(getString)
	id, err := unity.DeleteById(atoi, models.Data{})
	if err != nil {
		d.Data["json"] = JSON{Code: 104, Msg: "删除失败", Data: ""}
		d.ServeJSON()
	} else {
		d.Data["json"] = JSON{Code: 200, Msg: "删除成功", Data: id}
		d.ServeJSON()
	}
}

// AddDataModel 添加数据模型
func (d *DataModelController) AddDataModel() {
	var data models.DataMOdel
	var dataModel models.DataMOdelDto
	err := json.Unmarshal(d.Ctx.Input.RequestBody, &dataModel)
	if err != nil {
		d.Data["json"] = JSON{Code: 103, Msg: "json解析失败", Data: ""}
		d.ServeJSON()
		return
	}
	data.AfterSalesEarlyWarningService = dataModel.AfterSalesEarlyWarningService
	data.Airpacing = dataModel.Airpacing
	data.BigDataManagementPlatform = dataModel.BigDataManagementPlatform
	data.Cdc = dataModel.Cdc
	data.CdcInjectionSites = dataModel.CdcInjectionSites
	data.ChainPharmacies = dataModel.ChainPharmacies
	data.ColdChainLogisticsCompany = dataModel.ColdChainLogisticsCompany
	data.ColdChainTraining = dataModel.ColdChainTraining
	data.ColdStorage = dataModel.ColdStorage
	data.Counties = dataModel.Counties
	data.Dehumidifiers = dataModel.Dehumidifiers
	data.DvisualizationPlatform = dataModel.DvisualizationPlatform
	data.Freezer = dataModel.Freezer
	data.Hospital = dataModel.Hospital
	data.Incubator = dataModel.Incubator
	data.IncubatorSafetyPlatform = dataModel.IncubatorSafetyPlatform
	data.NumberOfProbes = dataModel.NumberOfProbes
	data.NumberOfStores = dataModel.NumberOfStores
	data.NumberOfValidationReports = dataModel.NumberOfValidationReports
	data.NumberOfValidationScenarios = dataModel.NumberOfValidationScenarios
	data.Pharmaceutical = dataModel.Pharmaceutical
	data.PharmaceuticalCompanies = dataModel.PharmaceuticalCompanies
	data.Prefecture = dataModel.Prefecture
	data.Provinces = dataModel.Provinces
	data.RefrigeratedCabinets = dataModel.RefrigeratedCabinets
	data.RefrigeratedTrucks = dataModel.RefrigeratedTrucks
	data.SafetyPlatformForRefrigeratedTrucks = dataModel.SafetyPlatformForRefrigeratedTrucks
	data.ShadedCabinet = dataModel.ShadedCabinet
	data.ShadedStorage = dataModel.ShadedStorage
	add, err := unity.Add(&data)
	if err != nil {
		d.Data["json"] = JSON{Code: 104, Msg: "添加失败", Data: ""}
		d.ServeJSON()
	} else {
		d.Data["json"] = JSON{Code: 200, Msg: "添加成功", Data: add}
		d.ServeJSON()
	}
}
