/*
 * @description:附件相关
 * @Author: Lanjianrong
 * @Date: 2020-12-04 16:13:32
 * @FilePath: \construction_management\services\annex_service.go
 */
package services

import (
	"fmt"
	"log"
	"time"

	"go.mod/comm"
	"go.mod/conf"
	"go.mod/models"

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

type AnnexService interface {
	ValidRule(ctx iris.Context) (viewmodels.Annex, error)
	ValidCreate(ctx iris.Context) (viewmodels.AnnexCreate, error)
	Create(uid int, dataType int, dataId int, list []viewmodels.AnnexList) error
	Delete(id int) error
	GetCounts(dataType int, dataId int) (int64, error)
	Get(dataType int, dataId int, pageNo int, pageSize int) ([]viewmodels.AnnexListView, int64)
}

// //返回service操作类
type annexService struct {
	daoAnnex  *dao.AnnexDao
	valideGet string
	// validDetail       string
}

// 创建项目用户service
func NewAnnexService() AnnexService {
	return &annexService{
		valideGet: "/api/file",
		daoAnnex:  dao.NewAnnexDao(datasource.InstanceDbMaster()),
	}
}

// 附件列表
func (s *annexService) Get(dataType int, dataId int, pageNo int, pageSize int) ([]viewmodels.AnnexListView, int64) {
	fileData := make([]viewmodels.AnnexListView, 0)
	list, total := s.daoAnnex.GetList(dataType, dataId, pageNo, pageSize)
	for _, item := range list {
		annexVM := viewmodels.AnnexListView{}
		uid, _ := comm.AesEncrypt(item.AccountId, conf.SignSecret)
		annexVM.AccountId = uid
		id, _ := comm.AesEncrypt(item.Id, conf.SignSecret)
		annexVM.Id = id
		annexVM.AccountName = item.AccountName
		annexVM.CreateTime = item.CreateTime
		annexVM.FileName = item.FileName
		annexVM.FilePath = item.FilePath
		fileData = append(fileData, annexVM)
	}
	return fileData, total
}

// 计算附件总数
func (s *annexService) GetCounts(dataType int, dataId int) (int64, error) {
	count, err := s.daoAnnex.GetCount(dataType, dataId)
	return count, err
}

// 插入数据
func (s *annexService) Create(uid int, dataType int, dataId int, list []viewmodels.AnnexList) error {
	fileData := make([]models.CmAnnex, 0)
	fmt.Println("-------------------------", list)
	for _, file := range list {
		fileVM := models.CmAnnex{}
		fileVM.DataType = dataType
		fileVM.DataId = dataId
		fileVM.AccountId = uid
		fileVM.Name = file.FileName
		fileVM.OssUrl = file.FilePath
		// fileVM.CreateTime = time.Unix(file.CreateTime, 0)
		// time, _ := time.Parse("2020-11-11 20:11:11", file.CreateTime)
		fileVM.CreateTime = file.CreateTime
		fileVM.UpdateTime = time.Now()
		// fmt.Println("------------------", file.CreateTime)
		fileData = append(fileData, fileVM)
	}
	fmt.Println("fileData", fileData)
	err := s.daoAnnex.InsertByList(fileData)
	return err
}

// 删除数据
func (s *annexService) Delete(id int) error {
	err := s.daoAnnex.DeleteById(id)
	return err
}

// 规则校验
func (s *annexService) ValidRule(ctx iris.Context) (viewmodels.Annex, error) {
	annexVaild := viewmodels.Annex{}
	if ctx.Method() == "GET" {
		err := ctx.ReadForm(&annexVaild)
		if err != nil {
			log.Println("safe-ValidRule-ReadForm转换异常, error=", err)
			return annexVaild, err
		}
		if ctx.Path() == s.valideGet {
			err = annexVaild.ValidateGet()

		}
		return annexVaild, err
	}
	return annexVaild, nil
}

func (s *annexService) ValidCreate(ctx iris.Context) (viewmodels.AnnexCreate, error) {
	annexVaild := viewmodels.AnnexCreate{}
	err := ctx.ReadJSON(&annexVaild)
	if err != nil {
		log.Println("safe-ValidRule-ReadJSON转换异常, error=", err)
		return annexVaild, err
	}
	return annexVaild, nil
}
