package project

import (
	"server/dao"
	"server/model/common/request"
	"server/model/project"
)

type ProjectService struct{}

func (s ProjectService) AddProject(project dao.Project) error {
	err := project.AddProject()
	return err
}

func (s ProjectService) DelProject(id int) error {
	err := dao.Project{}.DelProject(id)
	return err
}

func (s ProjectService) UpdateProject(project dao.Project) error {
	err := project.UpdateProject()
	return err
}

func (s ProjectService) GetProjectAndDetails(info request.PageInfo) (list interface{}, total int64, err error) {
	projectList, total, err := dao.Project{}.GetProjectAndDetails(info)

	var details []project.ProjectDetails

	for _, Pjc := range projectList {
		detail := project.ProjectDetails{Project: Pjc}
		screensList, _ := dao.QueryScreensToDetail(Pjc.ID)

		detail.RelationDev.Screens = screensList

		details = append(details, detail)
	}
	return details, total, nil
}

func (s ProjectService) GetProjectList() (list interface{}, err error) {
	list, err = dao.GetProjectList()
	return list, err
}

func (s ProjectService) ChangeProjects(change project.ReqChangeProjects) error {
	//先删除原来记录
	err := dao.DelOldProjectsByUserId(change.ID)
	if err != nil {
		return err
	}

	if len(change.ProIds) != 0 {
		//组装对象批量新增
		var UserProjects []request.UserProject

		for _, pid := range change.ProIds {
			UserProjects = append(UserProjects, request.UserProject{
				SysUserId: change.ID,
				ProjectId: pid,
			})
		}
		err = dao.ChangeProjects(UserProjects)
		if err != nil {
			return err
		}
		return nil
	}
	return nil
}

func (s ProjectService) GetProjectListByUserID(id uint) (interface{}, error) {
	list, err := dao.GetProjectListByUserIDNoPage(id)
	return list, err
}
