/*
 * @description: 安全巡检审批相关
 * @Author: LanJianRong
 * @Date: 2020-12-18
 * @FilePath: \construction_management\web\api\safe_rpc_api.go
 */
package services

import (
	"errors"
	"log"
	"time"

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

type SafeAuditService interface {
	ValidRule(ctx iris.Context) (viewmodels.SafeAudit, error)
	ValidStart(ctx iris.Context) (viewmodels.AuditStart, error)
	// AddAuditor(safeId int, bId int, auditId int, times int) error
	CloseAudit(id int, opinion string, curUid int, saveId int) error
	BackAudit(id int, opinion string, curUid int, saveId int, auditId int) error
	PassAudit(id int, uid int, auditId int, opinion string, rectifiedInfo string, rectifiedTime string) error
	StartAudit(safeId int, bidsectionId int, auditors []int, reAuditors []int, uid int, pid int, inspection string, inspectionDetail string, demand string, createTime time.Time) error
}

type safeAuditService struct {
	validAdd     string
	validPass    string
	validClose   string
	validBack    string
	daoSafeAudit *dao.SafeAuditDao
	daoSafe      *dao.SafeDao
	daoApprover  *dao.ApproverDao
	daoTree      *dao.TreeDao
}

//创建项目用户service
func NewSafeAuditService() SafeAuditService {
	return &safeAuditService{
		validBack:    "/api/safe_audit/back",
		validAdd:     "/api/safe_audit/add",
		validClose:   "/api/safe_audit/close",
		validPass:    "/api/safe_audit/pass",
		daoSafeAudit: dao.NewSafeAuditDao(datasource.InstanceDbMaster()),
		daoSafe:      dao.NewSafeDao(datasource.InstanceDbMaster()),
		daoApprover:  dao.NewApproverDao(datasource.InstanceDbMaster()),
		daoTree:      dao.NewTreeDao(datasource.InstanceDbMaster()),
	}
}

// 关闭审批流程
func (s *safeAuditService) CloseAudit(id int, opinion string, curUid int, saveId int) error {
	err := s.daoApprover.CloseHandler("safe", id, opinion, curUid, saveId)
	return err
}

// 审批退回
func (s *safeAuditService) BackAudit(id int, opinion string, curUid int, saveId int, auditId int) error {
	auditor, err := s.daoApprover.FindApproverById(id)
	if auditor.AuditId != curUid {
		return errors.New("该用户没有审批权限")
	}
	safe := s.daoSafe.FindById(saveId)

	// 退回到了检查人
	if auditId == 0 {
		// 初始化审批流程
		err = s.daoApprover.InitStatus("safe", auditor.BidsectionId, auditor.DataType, auditor.DataId, auditor.AuditId, safe.Times, auditor.Progress+1, opinion)
	} else {
		// 退回到审批流程中的某一个人
		err = s.daoApprover.BackHandlerWithId("safe", auditId, auditor.AuditId, safe.Times, auditor.Progress+1, opinion)
	}
	return err

}

// 审批通过
func (s *safeAuditService) PassAudit(id int, uid int, auditId int, opinion string, rectifiedInfo string, rectifiedTime string) error {
	err := s.daoApprover.PassHandler("safe", id, uid, auditId, opinion, rectifiedInfo, rectifiedTime)
	return err
}

func (s *safeAuditService) StartAudit(safeId int, bidsectionId int, auditors []int, reAuditors []int, uid int, pid int, inspection string, inspectionDetail string, demand string, createTime time.Time) error {
	safe := s.daoSafe.FindById(safeId)
	if safe.Uid != uid {
		return errors.New("该用户没有操作权限")
	}
	// 清除可能有的旧的审批流程
	err := s.daoApprover.DeleteOldAuditors(bidsectionId, 1, safeId)
	if err != nil {
		return err
	}
	// 增加审批日志
	err = s.daoSafeAudit.AddAuditRecord(safeId, bidsectionId, uid, safe.Times, 0, 0, "", "")
	if err != nil {
		return err
	}
	// 改变安全巡检表的状态
	err = s.daoSafe.ChangeStatus(safeId, 1, inspection, inspectionDetail, demand, createTime)
	if err != nil {
		return err
	}
	// 创建审批流程, 同时添加推送消息
	err = s.daoApprover.InsertData(uid, bidsectionId, pid, 1, safeId, auditors, reAuditors)
	return err
}

// 规则校验
func (s *safeAuditService) ValidRule(ctx iris.Context) (viewmodels.SafeAudit, error) {
	safeAuditVaild := viewmodels.SafeAudit{}
	if ctx.Method() == "POST" {
		err := ctx.ReadJSON(&safeAuditVaild)
		if err != nil {
			log.Println("safe-ValidRule-ReadJson转换异常, error=", err)
			return safeAuditVaild, err
		}
		if ctx.Path() == s.validAdd {
			err = safeAuditVaild.ValidateAddAuditor()
			return safeAuditVaild, err
		} else if ctx.Path() == s.validClose {
			err = safeAuditVaild.ValidateClose()
			return safeAuditVaild, err
		} else if ctx.Path() == s.validBack {
			err = safeAuditVaild.ValidateBack()
			return safeAuditVaild, err
		} else if ctx.Path() == s.validPass {
			err = safeAuditVaild.ValidatePass()
			return safeAuditVaild, err
		}
		return safeAuditVaild, err
	}
	return safeAuditVaild, nil
}

// 校验审批开始
func (s *safeAuditService) ValidStart(ctx iris.Context) (viewmodels.AuditStart, error) {
	startAuditValid := viewmodels.AuditStart{}
	err := ctx.ReadJSON(&startAuditValid)
	if err != nil {
		log.Println("safe-ValidRule-ReadJson转换异常, error=", err)
		return startAuditValid, err
	}
	err = startAuditValid.ValidateStart()
	return startAuditValid, err
}
