package menu

import (
	"github.com/tiptok/gocomm/common"
	"github.com/tiptok/gocomm/pkg/log"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/factory"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol"
	protocolx "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol/menu"
	"time"
)

type MenuService struct {
}

func (svr *MenuService) CreateMenu(header *protocol.RequestHeader, request *protocolx.CreateMenuRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.CreateMenuResponse{}
	if err = request.ValidateCommand(); err != nil {
		err = protocol.NewCustomMessage(2, err.Error())
	}
	if err = transactionContext.StartTransaction(); err != nil {
		log.Error(err)
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	newMenu := &domain.Menu{
		MenuName:    request.MenuName,
		MenuCode:    request.MenuCode,
		Icon:        request.Icon,
		Sort:        request.Sort,
		Hidden:      request.Hidden,
		CreatedTime: time.Now(),
		UpdateTime:  time.Now(),
	}

	var MenuRepository, _ = factory.CreateMenuRepository(transactionContext)
	if m, err := MenuRepository.Save(newMenu); err != nil {
		return nil, err
	} else {
		rsp = m
	}
	err = transactionContext.CommitTransaction()
	return
}

func (svr *MenuService) UpdateMenu(header *protocol.RequestHeader, request *protocolx.UpdateMenuRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.UpdateMenuResponse{}
	if err = request.ValidateCommand(); err != nil {
		err = protocol.NewCustomMessage(2, err.Error())
	}
	if err = transactionContext.StartTransaction(); err != nil {
		log.Error(err)
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	var MenuRepository, _ = factory.CreateMenuRepository(transactionContext)
	var menu *domain.Menu
	if menu, err = MenuRepository.FindOne(common.ObjectToMap(request)); err != nil {
		return
	}
	if err = menu.Update(common.ObjectToMap(request)); err != nil {
		return
	}
	if menu, err = MenuRepository.Save(menu); err != nil {
		return
	}
	err = transactionContext.CommitTransaction()
	return
}

func (svr *MenuService) GetMenu(header *protocol.RequestHeader, request *protocolx.GetMenuRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.GetMenuResponse{}
	if err = request.ValidateCommand(); err != nil {
		err = protocol.NewCustomMessage(2, err.Error())
	}
	if err = transactionContext.StartTransaction(); err != nil {
		log.Error(err)
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	var MenuRepository, _ = factory.CreateMenuRepository(transactionContext)
	var menu *domain.Menu
	if menu, err = MenuRepository.FindOne(common.ObjectToMap(request)); err != nil {
		return
	}
	rsp = menu
	err = transactionContext.CommitTransaction()
	return
}

func (svr *MenuService) DeleteMenu(header *protocol.RequestHeader, request *protocolx.DeleteMenuRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.DeleteMenuResponse{}
	if err = request.ValidateCommand(); err != nil {
		err = protocol.NewCustomMessage(2, err.Error())
	}
	if err = transactionContext.StartTransaction(); err != nil {
		log.Error(err)
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	var MenuRepository, _ = factory.CreateMenuRepository(transactionContext)
	var menu *domain.Menu
	if menu, err = MenuRepository.FindOne(common.ObjectToMap(request)); err != nil {
		return
	}
	if menu, err = MenuRepository.Remove(menu); err != nil {
		return
	}
	rsp = menu
	err = transactionContext.CommitTransaction()
	return
}

func (svr *MenuService) ListMenu(header *protocol.RequestHeader, request *protocolx.ListMenuRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.ListMenuResponse{}
	if err = request.ValidateCommand(); err != nil {
		err = protocol.NewCustomMessage(2, err.Error())
	}
	if err = transactionContext.StartTransaction(); err != nil {
		log.Error(err)
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	var MenuRepository, _ = factory.CreateMenuRepository(transactionContext)
	var menu []*domain.Menu
	var total int64
	if total, menu, err = MenuRepository.Find(common.ObjectToMap(request)); err != nil {
		return
	}
	rsp = map[string]interface{}{
		"total": total,
		"list":  menu,
	}
	err = transactionContext.CommitTransaction()
	return
}

func NewMenuService(options map[string]interface{}) *MenuService {
	svr := &MenuService{}
	return svr
}