package cross_variety

import (
	"encoding/json"
	"eta/eta_api/controllers"
	"eta/eta_api/models"
	"eta/eta_api/models/data_manage/cross_variety"
	"eta/eta_api/models/data_manage/cross_variety/request"
	"eta/eta_api/models/data_manage/cross_variety/response"
	"eta/eta_api/utils"
	"github.com/rdlucklib/rdluck_tools/paging"
	"time"
)

// VarietyController
// @Description: 品种列表
type VarietyController struct {
	controllers.BaseAuthController
}

// Add
// @Title 新增品种
// @Description 新增品种接口
// @Param	request	body request.AddVarietyReq true "type json string"
// @Success 200 Ret=200 添加成功
// @router /variety/add [post]
func (c *VarietyController) Add() {
	br := new(models.BaseResponse).Init()
	defer func() {
		c.Data["json"] = br
		c.ServeJSON()
	}()
	var req request.AddVarietyReq
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.VarietyName == "" {
		br.Msg = "请输入品种名称"
		br.IsSendEmail = false
		return
	}
	varietyName := utils.TrimStr(req.VarietyName)
	item, err := cross_variety.GetVarietyByName(varietyName)
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "添加失败"
		br.ErrMsg = "添加失败，Err:" + err.Error()
		return
	}
	if item != nil {
		br.Msg = "添加失败，品种名称不能重复"
		br.IsSendEmail = false
		return
	}

	variety := &cross_variety.ChartVariety{
		ChartVarietyId:     0,
		ChartVarietyName:   varietyName,
		ChartVarietyNameEn: varietyName,
		SysUserId:          c.SysUser.AdminId,
		SysUserRealName:    c.SysUser.RealName,
		ModifyTime:         time.Now(),
		CreateTime:         time.Now(),
	}
	err = cross_variety.AddVariety(variety)
	if err != nil {
		br.Msg = "添加品种失败"
		br.ErrMsg = "添加品种失败,Err:" + err.Error()
		return
	}

	br.Ret = 200
	br.Msg = "添加成功"
	br.IsAddLog = true
	br.Success = true
}

// Edit
// @Title 编辑品种接口
// @Description 编辑品种接口
// @Param	request	body request.EditVarietyReq true "type json string"
// @Success 200 Ret=200 修改成功
// @router /variety/edit [post]
func (c *VarietyController) Edit() {
	br := new(models.BaseResponse).Init()
	defer func() {
		c.Data["json"] = br
		c.ServeJSON()
	}()
	var req request.EditVarietyReq
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.ChartVarietyId <= 0 {
		br.Msg = "请选择品种"
		br.IsSendEmail = false
		return
	}
	if req.VarietyName == "" {
		br.Msg = "请输入品种名称"
		br.IsSendEmail = false
		return
	}
	varietyName := utils.TrimStr(req.VarietyName)

	item, err := cross_variety.GetVarietyByName(varietyName)
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "添加失败"
		br.ErrMsg = "添加失败，Err:" + err.Error()
		return
	}
	if item != nil && item.ChartVarietyId != req.ChartVarietyId {
		br.Msg = "添加失败，品种名称不能重复"
		br.IsSendEmail = false
		return
	}

	// 获取品种详情
	varietyInfo, err := cross_variety.GetVarietyById(req.ChartVarietyId)
	if err != nil {
		br.Msg = "查询品种失败"
		br.ErrMsg = "查询品种失败;ERR:" + err.Error()
		return
	}

	// 编辑
	switch c.Lang {
	case utils.EnLangVersion:
		varietyInfo.ChartVarietyNameEn = varietyName
	default:
		varietyInfo.ChartVarietyName = varietyName
	}
	varietyInfo.ModifyTime = time.Now()
	err = varietyInfo.Update([]string{"ChartVarietyName", "ChartVarietyNameEn", "ModifyTime"})
	if err != nil {
		br.Msg = "修改品种失败"
		br.ErrMsg = "修改品种失败,Err:" + err.Error()
		return
	}

	br.Ret = 200
	br.Msg = "修改成功"
	br.IsAddLog = true
	br.Success = true
}

// DeleteCheck
// @Title 删除品种检测接口
// @Description 删除品种检测接口
// @Param	request	body request.DelVarietyReq true "type json string"
// @Success 200 Ret=200 检测成功
// @router /variety/delete/check [post]
func (c *VarietyController) DeleteCheck() {
	br := new(models.BaseResponse).Init()
	defer func() {
		c.Data["json"] = br
		c.ServeJSON()
	}()
	var req request.DelVarietyReq
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	if req.ChartVarietyId <= 0 {
		br.Msg = "请选择品种"
		br.IsSendEmail = false
		return
	}
	var deleteStatus int
	var tipsMsg string

	// 获取该品种关联的图表数
	count, err := cross_variety.GetCountChartByVarietyId(req.ChartVarietyId)
	if err != nil {
		br.Msg = "检测异常"
		br.ErrMsg = "检测异常,err:" + err.Error()
		br.IsSendEmail = false
		return
	}
	if count > 0 {
		deleteStatus = 1
		tipsMsg = "已关联图表的品种，不允许删除！"
	}

	if deleteStatus == 0 {
		tipsMsg = "可删除，进行删除操作"
	}
	resp := response.VarietyDeleteCheckResp{
		DeleteStatus: deleteStatus,
		TipsMsg:      tipsMsg,
	}
	br.Ret = 200
	br.Msg = "检测成功"
	br.Success = true
	br.Data = resp
}

// Delete
// @Title 删除品种
// @Description 删除品种接口
// @Param	request	body request.DeleteChartClassifyReq true "type json string"
// @Success 200 Ret=200 删除成功
// @router /variety/delete [post]
func (c *VarietyController) Delete() {
	br := new(models.BaseResponse).Init()
	defer func() {
		c.Data["json"] = br
		c.ServeJSON()
	}()
	var req request.DelVarietyReq
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	if req.ChartVarietyId <= 0 {
		br.Msg = "请选择品种"
		br.IsSendEmail = false
		return
	}

	// 获取该品种关联的图表数
	count, err := cross_variety.GetCountChartByVarietyId(req.ChartVarietyId)
	if err != nil {
		br.Msg = "检测异常"
		br.ErrMsg = "检测异常,err:" + err.Error()
		br.IsSendEmail = false
		return
	}
	if count > 0 {
		br.Msg = "已关联图表的品种，不允许删除！"
		br.ErrMsg = "已关联图表的品种，不允许删除！"
		br.IsSendEmail = false
		return
	}

	varietyInfo, err := cross_variety.GetVarietyById(req.ChartVarietyId)
	if err != nil {
		if err.Error() == utils.ErrNoRow() {
			br.Msg = "该品种不存在或已删除"
			br.IsSendEmail = false
		} else {
			br.Msg = "删除失败"
			br.ErrMsg = "查找品种失败，ERR：" + err.Error()
		}
		return
	}
	err = varietyInfo.Delete()
	if err != nil {
		br.Msg = "删除失败"
		br.ErrMsg = "删除品种失败，ERR：" + err.Error()
		return
	}

	br.Ret = 200
	br.Msg = "删除成功"
	br.Success = true
	br.IsAddLog = true
}

// List
// @Title 获取标签中的指标与品种的映射关系
// @Description 获取标签中的指标与品种的映射关系
// @Success 200 Ret=200 保存成功
// @router /variety/list [get]
func (c *VarietyController) List() {
	br := new(models.BaseResponse).Init()
	defer func() {
		c.Data["json"] = br
		c.ServeJSON()
	}()
	// 获取数据
	list, err := cross_variety.GetVarietyList()
	if err != nil {
		br.Msg = "保存失败"
		br.ErrMsg = "保存失败，ERR：" + err.Error()
		return
	}
	dataCount := len(list)
	page := paging.GetPaging(1, dataCount, dataCount)

	resp := response.VarietyListResp{
		List:   list,
		Paging: page,
	}

	br.Ret = 200
	br.Msg = "获取成功"
	br.Success = true
	br.Data = resp
}
