/*
 * @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 QualityAuditService interface {
	ValidRule(ctx iris.Context) (viewmodels.QualityAudit, error)
	ValidStart(ctx iris.Context) (viewmodels.QualityAuditStart, 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) 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 qualityAuditService struct {
	validAdd        string
	validPass       string
	validClose      string
	validBack       string
	daoQualityAudit *dao.QualityAuditDao
	daoQuality      *dao.QualityDao
	daoApprover     *dao.ApproverDao
}

//创建项目用户service
func NewQualityAuditService() QualityAuditService {
	return &qualityAuditService{
		validBack:       "/api/quality_audit/back",
		validAdd:        "/api/quality_audit/add",
		validClose:      "/api/quality_audit/close",
		validPass:       "/api/quality_audit/pass",
		daoQualityAudit: dao.NewQualityAuditDao(datasource.InstanceDbMaster()),
		daoQuality:      dao.NewQualityDao(datasource.InstanceDbMaster()),
		daoApprover:     dao.NewApproverDao(datasource.InstanceDbMaster()),
	}
}

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

// 审批退回
func (s *qualityAuditService) 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.daoQuality.FindById(saveId)

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

}

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

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

// 规则校验
func (s *qualityAuditService) ValidRule(ctx iris.Context) (viewmodels.QualityAudit, error) {
	qualityAuditVaild := viewmodels.QualityAudit{}
	if ctx.Method() == "POST" {
		err := ctx.ReadJSON(&qualityAuditVaild)
		if err != nil {
			log.Println("quality-ValidRule-ReadJson转换异常, error=", err)
			return qualityAuditVaild, err
		}
		if ctx.Path() == s.validAdd {
			err = qualityAuditVaild.ValidateQualityAddAuditor()
			return qualityAuditVaild, err
		} else if ctx.Path() == s.validClose {
			err = qualityAuditVaild.ValidateQualityClose()
			return qualityAuditVaild, err
		} else if ctx.Path() == s.validBack {
			err = qualityAuditVaild.ValidateQualityBack()
			return qualityAuditVaild, err
		} else if ctx.Path() == s.validPass {
			err = qualityAuditVaild.ValidateQualityPass()
			return qualityAuditVaild, err
		}
		return qualityAuditVaild, err
	}
	return qualityAuditVaild, nil
}

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