/*
 * @description: 管理员数据处理
 * @Author: CP
 * @Date: 2020-08-20 17:08:38
 * @FilePath: \construction_management\services\manager_service.go
 */
package services

import (
	"encoding/json"
	"errors"
	"fmt"
	"strconv"

	"go.mod/comm"
	"go.mod/conf"
	"go.mod/dao"
	"go.mod/datasource"
	"go.mod/models"
	"go.mod/web/viewmodels"
)

//定义管理员Service接口
type ManagerService interface {
	GetAll() (dataList []viewmodels.Manager, total int64)
	CountAll() int64
	Get(id int) *viewmodels.Manager
	GetCurrentManager(staffName string) (*viewmodels.CurrentManager, error)
	Update(data viewmodels.Manager, id int, groupId int) error
	Create(data *models.CmManager) error
	Enable(id int, canLogin int) error
}

//返回service操作类
type managerService struct {
	dao *dao.ManagerDao
}

//创建管理员service
func NewManagerService() ManagerService {
	return &managerService{
		dao: dao.NewManagerDao(datasource.InstanceDbMaster()),
	}
}

//实现getall接口
func (s *managerService) GetAll() (data []viewmodels.Manager, total int64) {

	datalist := make([]viewmodels.Manager, 0)
	result, total := s.dao.GetAll()

	for _, item := range result {
		projectVM := makeManagerVM(&item)
		datalist = append(datalist, projectVM)
	}

	return datalist, total
}

func (s *managerService) CountAll() int64 {
	return s.dao.CountAll()
}

func (s *managerService) GetCurrentManager(staffName string) (*viewmodels.CurrentManager, error) {
	managerData := s.dao.GetUserName(staffName)
	groupDao := dao.NewGroupDao(datasource.InstanceDbMaster())

	groupData := groupDao.Get(managerData.GroupId)
	if groupData.Id == 0 {
		return nil, errors.New("未找到管理组")
	}

	cmData := viewmodels.CurrentManager{}
	id, _ := comm.AesEncrypt(strconv.Itoa(managerData.Id), conf.SignSecret)
	StaffId, _ := comm.AesEncrypt(strconv.Itoa(managerData.StaffId), conf.SignSecret)
	permission := []string{}
	json.Unmarshal([]byte(groupData.Permission), &permission)

	cmData.Id = id
	cmData.Permission = permission
	cmData.IsAdmin = managerData.IsAdmin
	cmData.Username = managerData.Username
	cmData.StaffId = StaffId
	return &cmData, nil
}

// 获得管理员详情
func (s *managerService) Get(id int) *viewmodels.Manager {
	result := s.dao.Get(id)
	data := makeManagerVM(result)
	return &data
}

func (s *managerService) Update(data viewmodels.Manager, id int, groupId int) error {

	// 1.查询是否有该权限组
	groupDao := dao.NewGroupDao(datasource.InstanceDbMaster())
	groupData := groupDao.Get(groupId)
	if groupData.Id == 0 {
		return errors.New("未找到管理组")
	}

	updateData := models.CmManager{}
	updateData.Id = id
	updateData.GroupId = groupId
	updateData.CanLogin = data.CanLogin
	updateData.IsAdmin = data.IsAdmin
	updateData.Telephone = data.Telephone
	updateData.Fixedphone = data.Fixedphone
	updateData.Email = data.Email
	updateData.Qq = data.Qq
	updateData.Position = data.Position

	// 要填写字段名称必须要更新的字段
	// , "telephone", "fixedphone", "email", "qq", "position"
	if err := s.dao.Update(&updateData, []string{"group_id", "is_admin", "can_login"}); err != nil {
		fmt.Println(err)
		return err
	}
	return nil
}

func (s *managerService) Enable(id int, canLogin int) error {
	Manager := models.CmManager{}
	Manager.Id = id
	Manager.CanLogin = canLogin
	// 要填写字段名称必须要更新的字段
	if err := s.dao.Update(&Manager, []string{"can_login"}); err != nil {
		fmt.Println(err)
		return err
	}
	return nil
}

func (s *managerService) Create(data *models.CmManager) error {
	return s.dao.Create(data)
}

// 构造视图层models
func makeManagerVM(projectCM *models.CmManager) viewmodels.Manager {
	modelsVM := viewmodels.Manager{}
	id, _ := comm.AesEncrypt(strconv.Itoa(projectCM.Id), conf.SignSecret)
	groupId, _ := comm.AesEncrypt(strconv.Itoa(projectCM.GroupId), conf.SignSecret)

	modelsVM.Id = id
	modelsVM.Username = projectCM.Username
	modelsVM.Category = projectCM.Category
	modelsVM.GroupId = groupId
	modelsVM.IsAdmin = projectCM.IsAdmin
	modelsVM.RealName = projectCM.RealName
	modelsVM.Telephone = projectCM.Telephone
	modelsVM.LoginIp = projectCM.LoginIp
	modelsVM.CanLogin = projectCM.CanLogin
	modelsVM.Office = projectCM.Office

	modelsVM.Category = projectCM.Category

	if !projectCM.LastLogin.IsZero() {
		modelsVM.LastLogin = projectCM.LastLogin.Format(conf.SysTimeform)
	}
	if !projectCM.CreateTime.IsZero() {
		modelsVM.CreateTime = projectCM.CreateTime.Format(conf.SysTimeform)
	}

	return modelsVM
}
