package controllers

import (
	"encoding/json"
	"hongze/hongze_cygx/models"
	"strconv"
	"time"
)

//优化建议
type AdviceController struct {
	BaseAuthController
}

// @Title 新增优化建议
// @Description 新增优化建议接口
// @Param	request	body models.AddCygxAdviceReq true "type json string"
// @Success Ret=200 新增成功
// @router /add [post]
func (this *AdviceController) ApplyApprove() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	user := this.User
	if user == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req models.AddCygxAdviceReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.Advice == "" {
		br.Msg = "建议内容不可为空"
		return
	}

	companyDetail, err := models.GetCompanyDetailById(user.CompanyId)
	if err != nil {
		br.Msg = "新增优化建议失败！"
		br.ErrMsg = "获取客户详情失败，Err:" + err.Error()
		return
	}
	if companyDetail == nil {
		br.Msg = "新增优化建议失败！"
		br.ErrMsg = "客户不存在，uid:" + strconv.Itoa(user.UserId)
		return
	}
	item := new(models.CygxAdvice)
	item.UserId = user.UserId
	item.UserRealName = user.RealName
	item.CompanyId = user.CompanyId
	item.CompanyName = companyDetail.CompanyName
	item.SalesRealName = companyDetail.SellerName
	item.Advice = req.Advice
	item.AdviceImgUrl = req.AdviceImgUrl
	item.CreateTime = time.Now()
	item.Mobile = user.Mobile
	item.Email = user.Email
	_, err = models.AddCygxAdvice(item)
	if err != nil {
		br.Msg = "新增优化建议失败"
		br.ErrMsg = "新增优化建议失败,Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "新增成功"
}
