package controllers

import (
	"errors"
	"eta/eta_mini_crm_ht/models"
)

type MerchantController struct {
	BaseAuthController
}

//
//// AddProduct
//// @Title 新增产品
//// @Description 获取报告作者
//// @Success 200 {object} models.ReportAuthorResp
//// @router /addProduct [post]
//func (this *MerchantController) AddProduct() {
//	br := new(models.BaseResponse).Init()
//	defer func() {
//		this.Data["json"] = br
//		this.ServeJSON()
//	}()
//
//	var req request.ProductReq
//	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
//		br.Msg = "参数解析失败"
//		br.ErrMsg = "参数解析失败,Err:" + err.Error()
//		return
//	}
//	if !checkProductRiskLevel(req.RiskLevel) {
//		br.Msg = "产品风险等级不合法"
//		br.ErrMsg = "产品风险等级不合法:" + req.RiskLevel
//		return
//	}
//	if !checkProductType(req.Type) {
//		br.Msg = "产品类型不合法"
//		br.ErrMsg = "产品类型不合法:" + req.Type
//		return
//	}
//	if req.ProductName == "" {
//		br.Msg = "产品名称不能为空"
//		br.ErrMsg = "产品名称不能为空"
//		return
//	}
//	price, err := decimal.NewFromString(req.Price)
//	if err != nil {
//		br.Msg = "产品价格格式不正确"
//		br.ErrMsg = "产品价格格式不正确,err:" + err.Error()
//		return
//	}
//	productType, err := transProductType(req.Type)
//	if err != nil {
//		br.Msg = "产品类型不正确"
//		br.ErrMsg = "产品类型不正确,err:" + err.Error()
//		return
//	}
//	product := models.MerchantProduct{
//		Deleted:     false,
//		SourceId:    req.SourceId,
//		Title:       req.ProductName,
//		Price:       price,
//		RiskLevel:   req.RiskLevel,
//		Type:        productType,
//		IsPermanent: true,
//		SaleStatus:  models.OnSale,
//	}
//	err = product.Insert()
//	if err != nil {
//		br.Msg = "新增产品失败"
//		br.ErrMsg = "新增产品失败,err:" + err.Error()
//		return
//	}
//	br.Msg = "新增产品成功"
//	br.Ret = 200
//	br.Success = true
//}
//
//// AddPackage
//// @Title 新增产品
//// @Description 获取报告作者
//// @Success 200 {object} models.ReportAuthorResp
//// @router /addPackage [post]
//func (this *MerchantController) AddPackage() {
//	br := new(models.BaseResponse).Init()
//	defer func() {
//		this.Data["json"] = br
//		this.ServeJSON()
//	}()
//
//	var req request.PackageReq
//	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
//		br.Msg = "参数解析失败"
//		br.ErrMsg = "参数解析失败,Err:" + err.Error()
//		return
//	}
//	if !checkProductRiskLevel(req.RiskLevel) {
//		br.Msg = "产品风险等级不合法"
//		br.ErrMsg = "产品风险等级不合法:" + req.RiskLevel
//		return
//	}
//	if req.ProductName == "" {
//		br.Msg = "产品名称不能为空"
//		br.ErrMsg = "产品名称不能为空"
//		return
//	}
//	price, err := decimal.NewFromString(req.Price)
//	if err != nil {
//		br.Msg = "产品价格格式不正确"
//		br.ErrMsg = "产品价格格式不正确,err:" + err.Error()
//		return
//	}
//	//开始事务
//	product := models.MerchantProduct{
//		Deleted:     false,
//		Title:       req.ProductName,
//		Price:       price,
//		RiskLevel:   req.RiskLevel,
//		Type:        models.ProductPackage,
//		IsPermanent: false,
//		ValidDays:   req.ValidDays,
//		SaleStatus:  models.OnSale,
//	}
//	err = product.Insert()
//	if err != nil {
//		br.Msg = "新增产品失败"
//		br.ErrMsg = "新增产品失败,err:" + err.Error()
//		return
//	}
//	br.Msg = "新增产品成功"
//	br.Ret = 200
//	br.Success = true
//}
func checkProductType(productType string) bool {
	if productType == "" {
		return false
	}
	if productType != Report && productType != Video && productType != Audio {
		return false
	}
	return true
}

func transProductType(tansType string) (productType models.MerchantProductType, err error) {
	if tansType == "" {
		err = errors.New("产品类型为空")
		return
	}
	switch tansType {
	case Report:
		productType = models.ProductReport
		return
	case Video:
		productType = models.ProductVideo
		return
	case Audio:
		productType = models.ProductAudio
		return
	default:
		err = errors.New("产品类型不合法")
		return
	}
}