package handler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"youngee_b_api/model/http_model"
	"youngee_b_api/service"
)

func WrapAddToListHandler(ctx *gin.Context) {
	handler := newAddToListHandler(ctx)
	baseRun(handler)
}

func newAddToListHandler(ctx *gin.Context) *AddToListHandler {
	return &AddToListHandler{
		req:  http_model.NewAddToListRequest(),
		resp: http_model.NewAddToListResponse(),
		ctx:  ctx,
	}
}

type AddToListHandler struct {
	req  *http_model.AddToListRequest
	resp *http_model.CommonResponse
	ctx  *gin.Context
}

func (h *AddToListHandler) getRequest() interface{} {
	return h.req
}
func (h *AddToListHandler) getContext() *gin.Context {
	return h.ctx
}
func (h *AddToListHandler) getResponse() interface{} {
	return h.resp
}
func (h *AddToListHandler) run() {
	var data *http_model.AddToListRequest
	data = h.req

	// 1. 加入商单
	createSProjectErr := service.SProject.CreateSProject(h.ctx, *data)
	if createSProjectErr != nil {
		h.resp.Status = 50000
		h.resp.Message = "加入商单失败"
		h.resp.Data = createSProjectErr
		fmt.Println(createSProjectErr)
	}

	// 2. 创建合作关系
	createCooperateErr := service.Cooperate.CreateCooperate(h.ctx, h.req.EnterpriseId, h.req.SupplierId, h.req.SubAccountId)
	if createCooperateErr != nil {
		fmt.Println(createCooperateErr)
		h.resp.Status = 50000
		h.resp.Message = "创建合作关系失败"
		h.resp.Data = createCooperateErr
	} else {
		h.resp.Status = 20000
		h.resp.Message = "加入商单成功"
	}
}

func (h *AddToListHandler) checkParam() error {
	return nil
}
