/*
 * @description:标段账号相关数据操作
 * @Author: CP
 * @Date: 2020-10-22 16:13:32
 * @FilePath: \construction_management\services\bid_account_service.go
 */
package services

import (
	"errors"
	"log"

	"github.com/kataras/iris/v12"
	"go.mod/dao"
	"go.mod/datasource"
	"go.mod/web/utils"
	"go.mod/web/viewmodels"
)

//定义项目用户Service接口
type BidAccountService interface {
	ValidRule(ctx iris.Context) (viewmodels.BidAccount, error)
	Create(viewBidAccount viewmodels.BidAccount, projectId int) error
	Delete(viewBidAccount viewmodels.BidAccount, projectId int) error
}

//返回service操作类
type bidAccountService struct {
	projectAccountDao *dao.ProjectAccountDao
	projectDao        *dao.ProjectDao
	bidsectionDao     *dao.BidsectionDao
	treeDao           *dao.TreeDao
	bidAccountDao     *dao.BidAccountDao
}

//创建项目用户service
func NewBidAccountService() BidAccountService {
	return &bidAccountService{
		projectAccountDao: dao.NewProjectAccountDao(datasource.InstanceDbMaster()),
		projectDao:        dao.NewProjectDao(datasource.InstanceDbMaster()),
		bidsectionDao:     dao.NewBidsectionDao(datasource.InstanceDbMaster()),
		treeDao:           dao.NewTreeDao(datasource.InstanceDbMaster()),
		bidAccountDao:     dao.NewBidAccountDao(datasource.InstanceDbMaster()),
	}
}

// 登陆验证
func (s *bidAccountService) ValidRule(ctx iris.Context) (viewmodels.BidAccount, error) {
	bidAccountVaild := viewmodels.BidAccount{}
	err := ctx.ReadJSON(&bidAccountVaild)
	if err != nil {
		log.Println("ReadForm转换异常, error=", err)
		return bidAccountVaild, err
	}

	err = bidAccountVaild.Validate()
	if err != nil {
		log.Println("登录验证, error=", err)
		return bidAccountVaild, err
	}
	return bidAccountVaild, nil
}

// 新增标段于账号的关系
func (s *bidAccountService) Create(viewBidAccount viewmodels.BidAccount, projectId int) error {
	// 写入关系表-标段的成员数量-账号表中标段ID
	// 1.检查账号合法性
	accountId, err := utils.GetDecryptId(viewBidAccount.AccountId)
	if err != nil {
		return err
	}
	accountData := s.projectAccountDao.Get(accountId, projectId)
	if accountData.Id == 0 {
		return errors.New("添加的账号不合法")
	}
	// 2.检查标段合法性
	bidsectionId, err := utils.GetDecryptId(viewBidAccount.BidsectionId)
	if err != nil {
		return err
	}
	bidsection := s.bidsectionDao.Get(bidsectionId, projectId)
	if bidsection.Id == 0 {
		return errors.New("标段不合法")
	}
	// 3.检查目录的合法性
	// 3-1标段ID,,项目ID
	treeData := s.treeDao.GetBidParentId(bidsectionId, projectId)
	// 3-2获得目录ID
	if treeData.Id == 0 {
		return errors.New("目录不合法")
	}
	treeId := treeData.Id

	// 新增成员到标段
	err = s.bidAccountDao.Create(bidsectionId, accountData, treeId, projectId)
	if err != nil {
		return err
	}
	return nil
}

//移除标段成员
func (s *bidAccountService) Delete(viewBidAccount viewmodels.BidAccount, projectId int) error {
	// 1.检查账号合法性
	accountId, err := utils.GetDecryptId(viewBidAccount.AccountId)
	if err != nil {
		return err
	}
	accountData := s.projectAccountDao.Get(accountId, projectId)
	if accountData.Id == 0 {
		return errors.New("添加的账号不合法")
	}
	// 2.检查标段合法性
	bidsectionId, err := utils.GetDecryptId(viewBidAccount.BidsectionId)
	if err != nil {
		return err
	}
	bidsection := s.bidsectionDao.Get(bidsectionId, projectId)
	if bidsection.Id == 0 {
		return errors.New("标段不合法")
	}
	// 3.检查目录的合法性
	// 3-1标段ID,,项目ID
	treeData := s.treeDao.GetBidParentId(bidsectionId, projectId)
	// 3-2获得目录ID
	if treeData.Id == 0 {
		return errors.New("目录不合法")
	}
	treeId := treeData.Id

	// 删除成员到标段
	err = s.bidAccountDao.Delete(bidsectionId, accountData, treeId, projectId)
	if err != nil {
		return err
	}
	return nil
}
