package controller

import (
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	"strings"
	"youngee_b_api/app/service"
	"youngee_b_api/app/vo"
)

type CooperationController struct{}

// 服务商端链接
func (o CooperationController) GetSupplierLink(c *gin.Context) {
	supplierLink := "服务商端链接"
	resultMap := make(map[string]string)
	resultMap["supplierLink"] = supplierLink
	returnSuccess(c, 20000, resultMap)
}

// 服务商搜索
func (o CooperationController) SearchSupplier(c *gin.Context) {
	param := &vo.SupplierSearchParam{}
	err := c.BindJSON(param)
	if err != nil || "" == param.FieldName {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	reSupplierPreviews, err := service.CooperationService{}.SearchSupplier(param)
	if err != nil {
		logrus.Errorf("[SearchSupplier] call Show err:%+v\n", err)
		returnError(c, 40000, err.Error())
		return
	}
	returnSuccess(c, 20000, reSupplierPreviews)
}

// 服务商入库批量邀请
func (o CooperationController) InviteSupplier(c *gin.Context) {
	param := &vo.SupplierInviteParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	err1 := service.CooperationService{}.InviteSupplier(param)
	if err1 != nil {
		if strings.Contains(err1.Error(), "Duplicate entry") {
			logrus.Errorf("[InviteSupplier] call Show err:%+v\n", err)
			returnError(c, 40000, "请勿邀请已入库服务商")
		} else {
			logrus.Errorf("[InviteSupplier] call Show err:%+v\n", err)
			returnError(c, 40000, err1.Error())
		}
		return
	}
	returnSuccess(c, 20000, nil)
}

// 在库服务商列表
func (o CooperationController) GetEnterprisePoolList(c *gin.Context) {
	param := &vo.SupplierSearchInPoolParam{}
	err := c.BindJSON(param)
	if err != nil || "" == param.EnterpriseId {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	result, err1 := service.CooperationService{}.GetEnterprisePoolList(param)
	if err1 != nil {
		logrus.Errorf("[GetEnterprisePoolList] call Show err:%+v\n", err)
		returnError(c, 40000, err.Error())
		return
	}
	returnSuccess(c, 20000, result)
}

// 服务商邀请待确认列表
func (o CooperationController) GetSupplierConfirmingList(c *gin.Context) {
	param := &vo.SupplierConfirmingParam{}
	err := c.BindJSON(param)
	if err != nil || "" == param.EnterpriseId {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	result, err1 := service.CooperationService{}.GetSupplierConfirmingList(param)
	if err1 != nil {
		logrus.Errorf("[GetSupplierConfirmingList] call Show err:%+v\n", err)
		returnError(c, 40000, err.Error())
		return
	}
	returnSuccess(c, 20000, result)
}

// 服务商管理-角标
func (o CooperationController) GetSupplierCount(c *gin.Context) {
	param := &vo.SupplierConfirmingParam{}
	err := c.BindJSON(param)
	if err != nil || "" == param.EnterpriseId {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	result := service.CooperationService{}.GetSupplierCount(param)

	returnSuccess(c, 20000, result)
}

// 门店详情
func (o CooperationController) GetStoreDetail(c *gin.Context) {
	param := &vo.StoreSearchParam{}
	err := c.BindJSON(param)
	if err != nil || 0 == param.StoreId {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	reStoreInfo, err := service.StoreService{}.GetStoreDetail(param)
	if err != nil {
		logrus.Errorf("[GetStoreDetail] call Show err:%+v\n", err)
		returnError(c, 40000, err.Error())
		return
	}
	returnSuccess(c, 20000, reStoreInfo)
}

// 更新门店
func (o CooperationController) UpdateStore(c *gin.Context) {
	param := &vo.StoreUpdateParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	storeId, err := service.StoreService{}.UpdateStore(param)
	if err != nil {
		logrus.Errorf("[UpdateStore] call Show err:%+v\n", err)
		returnError(c, 40000, err.Error())
		return
	}
	resultMap := make(map[string]int64)
	resultMap["storeId"] = storeId
	returnSuccess(c, 20000, resultMap)
}

// 删除门店
func (o CooperationController) DeleteStore(c *gin.Context) {
	param := &vo.StoreUpdateParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	storeId, err := service.StoreService{}.DeleteStore(param)
	if err != nil {
		logrus.Errorf("[DeleteStore] call Show err:%+v\n", err)
		returnError(c, 40000, err.Error())
		return
	}
	resultMap := make(map[string]int64)
	resultMap["storeId"] = storeId
	returnSuccess(c, 20000, resultMap)
}

// 团购详情
func (o CooperationController) GetTeamBuyingDetail(c *gin.Context) {
	param := &vo.TeamBuyingSearchParam{}
	err := c.BindJSON(param)
	if err != nil || 0 == param.TeamBuyingId {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	reTeamBuyingInfo, err := service.TeamBuyingService{}.GetTeamBuyingDetail(param)
	if err != nil {
		logrus.Errorf("[GetTeamBuyingDetail] call Show err:%+v\n", err)
		returnError(c, 40000, err.Error())
		return
	}
	returnSuccess(c, 20000, reTeamBuyingInfo)
}

// 更新团购
func (o CooperationController) UpdateTeamBuying(c *gin.Context) {
	param := &vo.TeamBuyingUpdateParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	teamBuyingId, err := service.TeamBuyingService{}.UpdateTeamBuying(param)
	if err != nil {
		logrus.Errorf("[UpdateTeamBuying] call Show err:%+v\n", err)
		returnError(c, 40000, err.Error())
		return
	}
	resultMap := make(map[string]int64)
	resultMap["teamBuyingId"] = teamBuyingId
	returnSuccess(c, 20000, resultMap)
}

// 删除团购
func (o CooperationController) DeleteTeamBuying(c *gin.Context) {
	param := &vo.TeamBuyingUpdateParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	teamBuyingId, err := service.TeamBuyingService{}.DeleteTeamBuying(param)
	if err != nil {
		logrus.Errorf("[DeleteTeamBuying] call Show err:%+v\n", err)
		returnError(c, 40000, err.Error())
		return
	}
	resultMap := make(map[string]int64)
	resultMap["teamBuyingId"] = teamBuyingId
	returnSuccess(c, 20000, resultMap)
}

// 商品详情
func (o CooperationController) GetProductDetail(c *gin.Context) {
	param := &vo.ProductSearchParam{}
	err := c.BindJSON(param)
	if err != nil || 0 == param.ProductId {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	reStoreInfo, err := service.ProductService{}.GetProductDetail(param)
	if err != nil {
		logrus.Errorf("[GetProductDetail] call Show err:%+v\n", err)
		returnError(c, 40000, err.Error())
		return
	}
	returnSuccess(c, 20000, reStoreInfo)
}

// 更新商品
func (o CooperationController) UpdateProduct(c *gin.Context) {
	param := &vo.ProductUpdateParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	productId, err := service.ProductService{}.UpdateProduct(param)
	if err != nil {
		logrus.Errorf("[UpdateProduct] call Show err:%+v\n", err)
		returnError(c, 40000, err.Error())
		return
	}
	resultMap := make(map[string]int64)
	resultMap["productId"] = productId
	returnSuccess(c, 20000, resultMap)
}

// 删除商品
func (o CooperationController) DeleteProduct(c *gin.Context) {
	param := &vo.ProductUpdateParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	productId, err := service.ProductService{}.DeleteProduct(param)
	if err != nil {
		logrus.Errorf("[DeleteProduct] call Show err:%+v\n", err)
		returnError(c, 40000, err.Error())
		return
	}
	resultMap := make(map[string]int64)
	resultMap["productId"] = productId
	returnSuccess(c, 20000, resultMap)
}

// 商品列表角标
func (o CooperationController) CountProduct(c *gin.Context) {
	param := &vo.GetAllProductParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	resMap := service.ProductService{}.CountProduct(param)

	returnSuccess(c, 20000, resMap)
}
