package seal

import (
	"encoding/json"
	"errors"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"hongze/hz_crm_api/models/contract"
	"hongze/hz_crm_api/models/seal"
	"hongze/hz_crm_api/models/system"
	"hongze/hz_crm_api/services"
	"hongze/hz_crm_api/services/flow"
	"strings"
	"time"
)

// SubmitApproval 提交审批
func SubmitApproval(sealInfo *seal.Seal) (err error) {
	// 发起审批
	approval, approvalRecordList, err := addApproval(sealInfo)
	if err != nil {
		return
	}
	// 待办通知
	{
		go MessageToNodeUser(approvalRecordList[0].NodeId, approval.ApplyUserId, approval.ContractApprovalId, 1, 1, sealInfo.CompanyName, sealInfo.Use)
	}

	return
}

// GetFlowTypeBySealType 根据盖章类型(多选,逗号拼接)获取审批流的类型
func GetFlowTypeBySealType(sealType string) (flowType int) {
	flowType = 5         // 合同章
	longFlow := "公章,法人章" // 盖章类型包含其中之一则审批流变成6
	sealTypeArr := strings.Split(sealType, ",")
	for _, itemType := range sealTypeArr {
		if strings.Contains(longFlow, itemType) {
			flowType = 6
		}
	}

	return
}

// addApproval 发起审批
func addApproval(sealInfo *seal.Seal) (approval *contract.ContractApproval, contractApprovalRecordList []*contract.ContractApprovalRecord, err error) {
	//用印状态判断
	ignoreStatus := []string{"待提交", "已撤回", "已驳回"}
	if !strings.Contains(strings.Join(ignoreStatus, ","), sealInfo.Status) {
		err = errors.New("用印单状态异常，不允许提交审批，当前用印单状态：" + sealInfo.Status)
		return
	}

	//将用印数据存为快照
	sealDetailByte, err := json.Marshal(sealInfo)
	if err != nil {
		err = errors.New(fmt.Sprint("用印单异常，当前用印单数据格式化失败：", err))
		return
	}

	// 获取审批流ID
	flowId := GetFlowTypeBySealType(sealInfo.SealType)
	flowItemInfo, err := flow.GetApprovalFlow(flowId)
	if err != nil {
		return
	}
	if len(flowItemInfo.NodeList) <= 0 {
		err = errors.New("审批流程未配置")
		return
	}

	//校验是否存在待审批的审批单（其实没有多大意义，只是为了 异常数据校验）
	approval = &contract.ContractApproval{
		ContractId:     sealInfo.SealId,
		ApprovalType:   "seal",
		Status:         "待审批",
		ApplyContent:   sealInfo.Use,
		ContractDetail: string(sealDetailByte),
		ApplyUserId:    sealInfo.UserId,
		ApplyUserName:  sealInfo.UserName,
		FlowId:         flowItemInfo.FlowId,
		FlowVersion:    flowItemInfo.CurrVersion,
		CurrNodeId:     flowItemInfo.NodeList[0].NodeId,
		StartNodeId:    flowItemInfo.NodeList[0].NodeId,
		ModifyTime:     time.Now(),
		CreateTime:     time.Now(),
	}
	has, err := approval.CheckPendingByContractId(sealInfo.SealId, "seal")
	if err != nil {
		return
	}
	if has {
		err = errors.New("用印异常，不允许提交审批，存在待审核的审批单")
		return
	}

	//ApproveUserMap := make(map[int]system.User)
	for _, nodeItem := range flowItemInfo.NodeList {
		//审批流记录
		if len(nodeItem.UserList) <= 0 {
			err = errors.New(fmt.Sprint("审批流程异常，没有可审批的人员,Err:", err))
			return
		}
		for _, userItem := range nodeItem.UserList {
			contractApprovalRecord := &contract.ContractApprovalRecord{
				Status:              "待审批",
				ApproveRemark:       "",
				ApproveUserId:       userItem.AdminId,
				ApproveUserName:     userItem.Name,
				ApproveRoleTypeCode: userItem.RoleTypeCode,
				NodeId:              nodeItem.NodeId,
				NodeType:            nodeItem.NodeType,
				NextNodeId:          nodeItem.NextNodeId,
				PrevNodeId:          nodeItem.PrevNodeId,
				CreateTime:          time.Now(),
				ModifyTime:          time.Now(),
			}
			contractApprovalRecordList = append(contractApprovalRecordList, contractApprovalRecord)
			//ApproveUserMap[userItem.AdminId] = userItem
		}

	}

	err = approval.Apply2(approval, contractApprovalRecordList)
	if err != nil {
		return
	}
	return
}

// CheckApprovalAuth 校验审批操作权限
func CheckApprovalAuth(sealId int, sysUser *system.Admin) (sealInfo *seal.Seal, approvalInfo *contract.ContractApproval, approvalRecord *contract.ContractApprovalRecord, err error) {
	// 用印详情
	sealInfo, err = seal.GetSealInfoById(sealId)
	if err != nil {
		err = errors.New("获取用印失败，Err:" + err.Error())
		return
	}
	// 合同状态判断
	if sealInfo.Status != "待审批" {
		err = errors.New("用印状态异常，不允许驳回申请，当前合同状态：" + sealInfo.Status)
		return
	}

	// 获取该最近一条审批单详情
	approvalInfo, err = contract.GetLastContractApprovalByContractId(sealInfo.SealId, "seal")
	if err != nil {
		err = errors.New("获取审批单失败，Err:" + err.Error())
		return
	}
	if approvalInfo.Status != "待审批" {
		err = errors.New("当前审批单状态异常，不允许审批，审批单状态：" + approvalInfo.Status)
		return
	}

	// 审批流
	approvalRecordList, err := contract.GetContractApprovalRecordListByContractApprovalIdAndNodeId(approvalInfo.ContractApprovalId, approvalInfo.CurrNodeId)
	if err != nil {
		err = errors.New("获取审批失败，Err:" + err.Error())
		return
	}
	for _, tmpApprovalRecord := range approvalRecordList {
		if tmpApprovalRecord.NodeType == "check" && tmpApprovalRecord.ApproveUserId == sysUser.AdminId {
			approvalRecord = tmpApprovalRecord
		}
	}
	if approvalRecord == nil {
		err = errors.New("当前账号没有审批权限")
		return
	}

	return
}

// ApprovedApproval 通过审批
func ApprovedApproval(sealId int, sysUser *system.Admin, remark string) (err error) {
	// 校验审批权限
	sealInfo, approvalInfo, approvalRecord, err := CheckApprovalAuth(sealId, sysUser)
	if err != nil {
		return
	}
	if approvalRecord == nil {
		err = errors.New("审批流异常，没有审批流信息")
		return
	}

	// 用印状态判断
	if sealInfo.Status != "待审批" {
		err = errors.New("用印状态异常，不允许审批，当前用印状态：" + sealInfo.Status)
		return
	}
	// 审批单状态判断
	if approvalInfo.Status != "待审批" {
		err = errors.New("审批单状态异常，不允许审批，当前审批单状态：" + approvalInfo.Status)
		return
	}
	// 审批流状态判断
	if approvalRecord.Status != "待审批" {
		err = errors.New("审批流状态异常，不允许审批，当前审批流状态：" + approvalRecord.Status)
		return
	}
	// 判断是否审批类型，如果不是审批类型，那么就没有审批权限
	if approvalRecord.NodeType != "check" {
		err = errors.New("当前账号没有审批权限")
		return
	}
	// 操作权限校验
	if sysUser.RoleTypeCode != approvalRecord.ApproveRoleTypeCode {
		err = errors.New("当前账号没有审批权限")
		return
	}
	if approvalRecord.ApproveUserId > 0 && approvalRecord.ApproveUserId != sysUser.AdminId {
		err = errors.New("当前账号没有审批权限，需要指定人操作")
		return
	}

	o := orm.NewOrm()
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()

	// 通过审批
	err = approvalInfo.Approved(approvalInfo, approvalRecord, remark)
	if err != nil {
		return
	}
	// 添加操作日志
	err = seal.AddSealOperationRecord(sealInfo.SealId, sysUser.AdminId, approvalRecord.ContractApprovalRecordId, "approval", sysUser.RealName, "通过申请", "")
	if err != nil {
		err = errors.New("添加操作日志失败")
		return
	}

	// 推送消息给抄送人
	content := sealInfo.CompanyName + " 用印已审核"

	// 如果下一个节点属于结束节点，那么通知对应的销售
	if approvalRecord.NextNodeId == 0 {
		// 待办通知（通知销售已经审核通过了）
		{
			approvalSysUser, _ := system.GetSysAdminById(sealInfo.UserId)
			go services.AddCompanyApprovalMessage(sysUser.AdminId, sealInfo.UserId, 0, approvalRecord.ContractApprovalRecordId, 2, sourceType, 2, sealInfo.CompanyName, content, content, approvalSysUser.Mobile)
		}
	} else {
		// 获取下级节点信息
		flowNodeInfo, tmpErr := system.GetByNodeId(approvalRecord.NextNodeId)
		if tmpErr != nil {
			err = tmpErr
			return
		}

		// 如果该级节点是抄送类型，那么需要将该节点给处理掉
		if flowNodeInfo.NodeType == "cc" {
			go approvedByCc(remark, approvalRecord, sysUser)
		} else {
			//发送消息下级审批人
			go MessageToNodeUser(approvalRecord.NextNodeId, sealInfo.UserId, approvalInfo.ContractApprovalId, 1, 1, sealInfo.CompanyName, content)
		}
	}

	return
}

// RejectApproval 驳回审批
func RejectApproval(sealId int, sysUser *system.Admin, remark string) (err error) {
	// 校验审批权限
	sealInfo, approvalInfo, approvalRecord, err := CheckApprovalAuth(sealId, sysUser)
	if err != nil {
		return
	}

	o := orm.NewOrm()
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()

	// 驳回审批
	err = reject(sealInfo, approvalInfo, approvalRecord, sysUser, remark)
	if err != nil {
		return
	}
	// 待办通知
	{
		content := sealInfo.CompanyName + " 用印已驳回"
		approvalSysUser, _ := system.GetSysAdminById(sealInfo.UserId)
		go services.AddCompanyApprovalMessage(sysUser.AdminId, sealInfo.UserId, 0, approvalRecord.ContractApprovalRecordId, 2, sourceType, 3, sealInfo.CompanyName, content, content, approvalSysUser.Mobile)
	}

	return
}

// reject 驳回操作
func reject(sealInfo *seal.Seal, approvalInfo *contract.ContractApproval, approvalRecord *contract.ContractApprovalRecord, opUser *system.Admin, rejectRemark string) (err error) {
	if approvalRecord == nil {
		err = errors.New("审批流异常，没有审批流信息")
		return
	}
	//审批流状态判断
	if approvalRecord.Status != "待审批" {
		err = errors.New("审批流状态异常，不允许驳回申请，当前审批流状态：" + approvalRecord.Status)
		return
	}
	//判断是否审批类型，如果不是审批类型，那么就没有审批权限
	if approvalRecord.NodeType != "check" {
		err = errors.New("当前账号没有审批权限")
		return
	}
	//操作人
	opUserId := opUser.AdminId
	opUserName := opUser.RealName
	//操作权限校验
	if opUser.RoleTypeCode != approvalRecord.ApproveRoleTypeCode {
		err = errors.New("当前账号没有审批权限")
		return
	}
	if approvalRecord.ApproveUserId > 0 && approvalRecord.ApproveUserId != opUserId {
		err = errors.New("当前账号没有审批权限，需要指定人操作")
		return
	}
	//审批单状态判断
	if approvalInfo.Status != "待审批" {
		err = errors.New("审批单状态异常，不允许驳回申请，当前审批单状态：" + approvalInfo.Status)
		return
	}
	//用印状态判断
	if sealInfo.Status != "待审批" {
		err = errors.New("用印状态异常，不允许驳回申请，当前用印状态：" + sealInfo.Status)
		return
	}
	// 驳回
	err = approvalInfo.Reject(approvalInfo, approvalRecord, opUserId, opUserName, rejectRemark)
	if err != nil {
		return
	}
	// 添加操作日志
	err = seal.AddSealOperationRecord(sealInfo.SealId, opUserId, approvalRecord.ContractApprovalRecordId, "reject", opUserName, "驳回申请", rejectRemark)
	if err != nil {
		err = errors.New("添加操作日志失败")
		return
	}

	return
}

// approvedByCc 审批通过（抄送节点）
func approvedByCc(approvedRemark string, sourceApprovalRecord *contract.ContractApprovalRecord, sysUser *system.Admin) (err error) {
	// 下个流程节点id
	nextNodeId := 0
	// 获取审批单中抄送节点的所有数据列表
	approvalRecordList, err := contract.GetContractApprovalRecordListByContractIdAndNode(sourceApprovalRecord.ContractApprovalId, sourceApprovalRecord.NextNodeId)
	if err != nil {
		return
	}
	// 遍历所有的抄送单
	for _, approvalRecord := range approvalRecordList {
		nextNodeId = approvalRecord.NextNodeId
		// 审批流状态判断
		if approvalRecord.Status != "待审批" {
			err = errors.New("审批流状态异常，不允许审批，当前审批流状态：" + approvalRecord.Status)
			return
		}
		// 判断是否审批类型，如果不是审批类型，那么就没有审批权限
		if approvalRecord.NodeType != "cc" {
			err = errors.New("当前账号不是抄送权限")
			return
		}
	}

	// 获取审批单详情
	approvalInfo, err := contract.GetContractApprovalById(sourceApprovalRecord.ContractApprovalId)
	if err != nil {
		return
	}

	// 获取用印信息
	sealInfo, err := seal.GetSealInfoById(approvalInfo.ContractId)
	if err != nil {
		return
	}

	//if contractApprovalRecord
	err = approvalInfo.ApprovedByCc(approvalInfo, approvalRecordList, approvedRemark, nextNodeId)
	if err != nil {
		return
	}

	content := sealInfo.CompanyName + " 用印已审核"

	// 发送消息给抄送人
	go MessageToNodeUser(approvalRecordList[0].NodeId, sealInfo.UserId, approvalInfo.ContractApprovalId, 2, 2, sealInfo.CompanyName, content)

	// 如果下一个节点属于结束节点，那么通知对应的销售
	if nextNodeId == 0 {
		// 待办通知
		{
			content = sealInfo.CompanyName + " 用印已审核"
			approvalSysUser, _ := system.GetSysAdminById(sealInfo.UserId)
			go services.AddCompanyApprovalMessage(sourceApprovalRecord.ApproveUserId, sealInfo.UserId, 0, sourceApprovalRecord.ContractApprovalRecordId, 2, sourceType, 2, sealInfo.CompanyName, content, content, approvalSysUser.Mobile)
		}
	} else {
		// 获取下级节点信息
		flowNodeInfo, tmpErr := system.GetByNodeId(nextNodeId)
		if tmpErr != nil {
			err = tmpErr
			return
		}
		// 如果下级节点是抄送类型，那么还是需要处理抄送节点逻辑
		if flowNodeInfo.NodeType == "cc" {
			go approvedByCc(approvedRemark, sourceApprovalRecord, sysUser)
		} else {
			//如果下级级节点是审批类型，发送消息下级审批人
			go MessageToNodeUser(nextNodeId, sealInfo.UserId, approvalInfo.ContractApprovalId, 1, 1, sealInfo.CompanyName, approvalInfo.ApplyContent)
		}
	}

	return
}
