/*
 * @description: 项目相关数据处理
 * @Author: CP
 * @Date: 2020-09-04 14:42:27
 * @FilePath: \construction_management\services\project_service.go
 */
package services

import (
	"html"
	"log"
	"strconv"

	"github.com/kataras/iris/v12"
	"go.mod/comm"
	"go.mod/conf"
	"go.mod/dao"
	"go.mod/datasource"
	"go.mod/models"
	"go.mod/web/viewmodels"
)

//定义项目Service接口
type ProjectService interface {
	//ValidManager(code string, account string, password string) error
	ValidRule(iris.Context) (viewmodels.Project, error)
	Get(projectId int) *viewmodels.Project
	GetList(viewmodels.Project) []viewmodels.Project
	GetName(code string) []viewmodels.Project
	Save(projectId int, projectVM viewmodels.Project) error
}

//返回service操作类
type projectService struct {
	dao       *dao.ProjectDao
	validSave string
}

//创建项目service
func NewProjectService() ProjectService {
	return &projectService{
		dao:       dao.NewProjectDao(datasource.InstanceDbMaster()),
		validSave: "/api/projectSetting/project/save",
	}
}

// 验证项目项的内容
func (s *projectService) ValidRule(ctx iris.Context) (viewmodels.Project, error) {
	projectVaild := viewmodels.Project{}
	var err error
	if ctx.Method() == "POST" {
		err = ctx.ReadJSON(&projectVaild)
		if err != nil {
			log.Println("project-ValidRule-ReadForm转换异常, error=", err)
			return projectVaild, err
		}
	} else {
		err = ctx.ReadForm(&projectVaild)
		if err != nil {
			log.Println("project-ValidRule-ReadForm转换异常, error=", err)
			return projectVaild, err
		}
	}

	if ctx.Path() == s.validSave {
		err = projectVaild.ValidateName()
	} else {
		err = projectVaild.Validate()
	}

	if err != nil {
		log.Println("项目信息验证, error=", err)
		return projectVaild, err
	}

	projectVaild.Code = html.EscapeString(projectVaild.Code)
	projectVaild.Name = html.EscapeString(projectVaild.Name)

	return projectVaild, nil
}

// ID获得项目信息-TODO
func (s *projectService) Get(projectId int) *viewmodels.Project {

	return nil
}

// 获得一组项目信息 -项目编号
func (s *projectService) GetList(projectVM viewmodels.Project) []viewmodels.Project {
	datalist := make([]viewmodels.Project, 0)
	// 项目编号
	if projectVM.Code != "" {

		projectData := s.dao.GetListByCode(projectVM.Code)
		for _, data := range projectData {
			projectVM = makeProjectVM(&data)
			datalist = append(datalist, projectVM)
		}
		return datalist
	}
	return datalist
}

// 获得项目名称
func (s *projectService) GetName(code string) []viewmodels.Project {
	datalist := make([]viewmodels.Project, 0)
	if code != "" {
		projectData := s.dao.GetListByCode(code)
		for _, data := range projectData {
			projectVM := makeProjectNameVM(&data)
			datalist = append(datalist, projectVM)
		}
		return datalist
	}
	return datalist
}

// 保存项目信息
func (s *projectService) Save(id int, projectVM viewmodels.Project) error {
	project := models.CmProject{}
	project.Id = id
	project.Name = projectVM.Name

	err := s.dao.Update(&project, []string{"Name"})

	return err
}

// 构造视图层models
func makeProjectVM(projectCM *models.CmProject) viewmodels.Project {
	projectVM := viewmodels.Project{}
	id, _ := comm.AesEncrypt(strconv.Itoa(projectCM.Id), conf.SignSecret)
	projectVM.Id = id
	projectVM.Code = projectCM.Code
	projectVM.Name = projectCM.Name
	return projectVM
}

func makeProjectNameVM(projectCM *models.CmProject) viewmodels.Project {
	projectVM := viewmodels.Project{}
	projectVM.Code = projectCM.Code
	projectVM.Name = projectCM.Name
	return projectVM
}

// 获得单个项目信息-ID 编号
// func (s *projectService) Get(projectVM *viewmodels.Project) {
// 	projectCM := models.CmProject{}

// 	// 项目ID或编号
// 	if projectVM.Id == "" && projectVM.Code == "" {
// 		projectVM.Id = "0"
// 		return
// 		//return errors.New("项目ID或者项目编号不存在")
// 	}

// 	if projectVM.Id != "" {
// 		projectId, err := comm.AesDecrypt(projectVM.Id, conf.SignSecret)
// 		if err != nil {
// 			projectVM.Id = "0"
// 			return
// 		}
// 		projectIdInt, err := strconv.Atoi(projectId)
// 		if err != nil {
// 			projectVM.Id = "0"
// 			return
// 		}
// 		if projectIdInt != 0 {
// 			projectCM.Id = projectIdInt
// 		}
// 	} else if projectVM.Code != "" {
// 		projectCM.Code = projectVM.Code
// 	} else {
// 		projectVM.Id = "0"
// 		return
// 	}

// 	s.dao.Get(&projectCM)
// 	if projectCM.Id != 0 {
// 		// 项目信息补充完整
// 		projectB := makeProjectVM(&projectCM)
// 		projectVM = &projectB
// 	} else {
// 		projectVM.Id = "0"
// 		return
// 	}
// }
