/*
 * @description:项目用户相关数据处理
 * @Author: CP
 * @Date: 2020-08-27 17:23:58
 * @FilePath: \construction_management\services\project_account_service.go
 */
package services

import (
	"errors"
	"html"
	"log"

	"github.com/kataras/iris/v12"
	"go.mod/comm"
	"go.mod/web/viewmodels"

	"go.mod/dao"
	"go.mod/datasource"
)

//定义项目用户Service接口
type ProjectAccountService interface {
	ValidRule(ctx iris.Context) (viewmodels.ProjectAccount, error)
	ValidRuleChangePassword(ctx iris.Context) (viewmodels.AccountPassword, error)
	ValidRulePermission(ctx iris.Context) (viewmodels.Permission, error)
	ValidRuleAccount(ctx iris.Context) (viewmodels.ProjectAccount, error)
	ValidGetPermission(ctx iris.Context) (viewmodels.Permission, error)
	ValidRuleProjectId(ctx iris.Context) (viewmodels.ProjectAccount, error)
	Get(accountId int) *viewmodels.ProjectAccount
}

//返回service操作类
type projectAccountService struct {
	dao                  *dao.ProjectAccountDao
	bidAccountDao        *dao.BidAccountDao
	permissionAccountDao *dao.PermissionAccountDao
	validSave            string
	validAdd             string
	validPassword        string
}

//创建项目用户service
func NewProjectAccountService() ProjectAccountService {
	return &projectAccountService{
		dao:                  dao.NewProjectAccountDao(datasource.InstanceDbMaster()),
		bidAccountDao:        dao.NewBidAccountDao(datasource.InstanceDbMaster()),
		permissionAccountDao: dao.NewPermissionAccountDao(datasource.InstanceDbMaster()),
		validSave:            "/api/projectSetting/account/save",
		validAdd:             "/api/projectSetting/account/create",
		validPassword:        "/api/projectSetting/account/change",
	}
}

// 用户规则验证
func (s *projectAccountService) ValidRule(ctx iris.Context) (viewmodels.ProjectAccount, error) {
	accountVaild := viewmodels.ProjectAccount{}
	err := ctx.ReadJSON(&accountVaild)
	if err != nil {
		log.Println("account-ValidRule-ReadForm转换异常, error=", err)
		return accountVaild, err
	}

	if ctx.Path() == s.validAdd {
		err = accountVaild.Validate()
	} else if ctx.Path() == s.validSave {
		err = accountVaild.ValidateUpdate()
	} else if ctx.Path() == s.validPassword {
		err = accountVaild.ValidatePassword()
	} else {
		log.Println("请求路径找不到对应的验证规则")
		return accountVaild, errors.New("验证错误-未找到验证规则")
	}

	if err != nil {
		log.Println("用户验证, error=", err)
		return accountVaild, err
	}

	// 验证账号组
	if ctx.Path() != s.validPassword {
		accountGroup := comm.NewAccountGroup()
		err = accountGroup.ValidRule(accountVaild.AccountGroup)
		if err != nil {
			log.Println("用户验证, error=", err)
			return accountVaild, err
		}
	}
	// xss
	accountVaild.Account = html.EscapeString(accountVaild.Account)
	accountVaild.Password = html.EscapeString(accountVaild.Password)
	accountVaild.Name = html.EscapeString(accountVaild.Name)
	accountVaild.Company = html.EscapeString(accountVaild.Company)
	accountVaild.Position = html.EscapeString(accountVaild.Position)
	accountVaild.Mobile = html.EscapeString(accountVaild.Mobile)
	accountVaild.Telephone = html.EscapeString(accountVaild.Telephone)

	return accountVaild, nil
}

// 验证密码
func (s *projectAccountService) ValidRuleChangePassword(ctx iris.Context) (viewmodels.AccountPassword, error) {
	accountVaild := viewmodels.AccountPassword{}
	err := ctx.ReadJSON(&accountVaild)
	if err != nil {
		log.Println("account-ValidRule-ReadForm转换异常, error=", err)
		return accountVaild, err
	}

	err = accountVaild.ValidateChangePassword()

	if err != nil {
		log.Println("权限验证, error=", err)
		return accountVaild, err
	}

	return accountVaild, nil
}

// 用户规则验证
func (s *projectAccountService) ValidRulePermission(ctx iris.Context) (viewmodels.Permission, error) {
	accountVaild := viewmodels.Permission{}
	err := ctx.ReadJSON(&accountVaild)
	if err != nil {
		log.Println("account-ValidRule-ReadForm转换异常, error=", err)
		return accountVaild, err
	}

	err = accountVaild.Validate()

	if err != nil {
		log.Println("权限验证, error=", err)
		return accountVaild, err
	}

	return accountVaild, nil
}

// 用户规则验证
func (s *projectAccountService) ValidGetPermission(ctx iris.Context) (viewmodels.Permission, error) {
	accountVaild := viewmodels.Permission{}
	err := ctx.ReadForm(&accountVaild)
	if err != nil {
		log.Println("account-ValidRule-ReadForm转换异常, error=", err)
		return accountVaild, err
	}

	err = accountVaild.ValidateSinglePermission()

	if err != nil {
		log.Println("权限验证, error=", err)
		return accountVaild, err
	}

	return accountVaild, nil
}

// 验证编辑用户
func (s *projectAccountService) ValidRuleAccount(ctx iris.Context) (viewmodels.ProjectAccount, error) {
	accountVaild := viewmodels.ProjectAccount{}
	err := ctx.ReadJSON(&accountVaild)
	if err != nil {
		log.Println("account-ValidRule-ReadForm转换异常, error=", err)
		return accountVaild, err
	}

	err = accountVaild.ValidateAccount()

	if err != nil {
		log.Println("权限验证, error=", err)
		return accountVaild, err
	}

	return accountVaild, nil
}

// 验证用户项目ID
func (s *projectAccountService) ValidRuleProjectId(ctx iris.Context) (viewmodels.ProjectAccount, error) {
	accountVaild := viewmodels.ProjectAccount{}
	err := ctx.ReadForm(&accountVaild)
	if err != nil {
		log.Println("account-ValidRule-ReadForm转换异常, error=", err)
		return accountVaild, err
	}

	err = accountVaild.ValidateProjectId()

	if err != nil {
		log.Println("权限验证, error=", err)
		return accountVaild, err
	}

	return accountVaild, nil
}

// 获得一个项目用户
func (s *projectAccountService) Get(id int) *viewmodels.ProjectAccount {
	modelsAccount := s.dao.Get(id)
	viewAccountData := viewmodels.ProjectAccount{}
	if modelsAccount.Id == 0 {
		viewAccountData.Id = "0"
		return &viewAccountData
	}

	viewAccountData = comm.MakeProjectAccountVM(modelsAccount)
	return &viewAccountData
}
