|
|
|
|
package callback
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"base/app/common"
|
|
|
|
|
"base/app/constant"
|
|
|
|
|
"base/app/lib/tencent"
|
|
|
|
|
"base/app/logic/broker/contract"
|
|
|
|
|
"base/app/logic/file"
|
|
|
|
|
"base/app/model"
|
|
|
|
|
"database/sql"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"git.oa00.com/go/logger"
|
|
|
|
|
"git.oa00.com/go/mysql"
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Tencent struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type EssCallBack struct {
|
|
|
|
|
MsgId string
|
|
|
|
|
MsgType string
|
|
|
|
|
MsgData msgData
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type msgData struct {
|
|
|
|
|
ApplicationId string
|
|
|
|
|
ProxyOrganizationOpenId string
|
|
|
|
|
CustomerData string
|
|
|
|
|
FlowId string
|
|
|
|
|
FlowName string
|
|
|
|
|
FlowType string
|
|
|
|
|
FlowStatus string
|
|
|
|
|
FlowMessage string
|
|
|
|
|
CreateOn int64
|
|
|
|
|
Deadline int64
|
|
|
|
|
FlowApproverInfo []flowApproverInfo
|
|
|
|
|
OccurTime int64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type flowApproverInfo struct {
|
|
|
|
|
ProxyOrganizationOpenId string
|
|
|
|
|
ProxyOperatorOpenId string
|
|
|
|
|
recipientId string
|
|
|
|
|
PhoneNumber string
|
|
|
|
|
ProxyOrganizationName string
|
|
|
|
|
SignOrder uint
|
|
|
|
|
ApproveName string
|
|
|
|
|
ApproveStatus string
|
|
|
|
|
ApproveMessage string
|
|
|
|
|
ApproveTime int64
|
|
|
|
|
CaSign string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sign @Title 电子签回调
|
|
|
|
|
func (t *Tencent) Sign(c *gin.Context) {
|
|
|
|
|
body, _ := common.GetRequestBody(c.Request)
|
|
|
|
|
logger.Logger.Info("电子签回调: " + string(body))
|
|
|
|
|
var res EssCallBack
|
|
|
|
|
json.Unmarshal(body, &res)
|
|
|
|
|
switch res.MsgType {
|
|
|
|
|
case "FlowStatusChange":
|
|
|
|
|
flowInfo := res.MsgData
|
|
|
|
|
var essFlowStatus, contractStatus, essFlowKind uint
|
|
|
|
|
switch flowInfo.FlowStatus {
|
|
|
|
|
case "INIT": // 合同创建
|
|
|
|
|
essFlow := model.ThirdTencentEssFlow{
|
|
|
|
|
FlowId: flowInfo.FlowId,
|
|
|
|
|
ContractSn: flowInfo.CustomerData,
|
|
|
|
|
Name: flowInfo.FlowName,
|
|
|
|
|
Status: model.EssFlowStatusInit,
|
|
|
|
|
CreateOn: sql.NullTime{Time: time.Unix(flowInfo.CreateOn, 0), Valid: true},
|
|
|
|
|
Deadline: sql.NullTime{Time: time.Unix(flowInfo.Deadline, 0), Valid: true},
|
|
|
|
|
UpdatedAt: time.Unix(flowInfo.OccurTime, 0),
|
|
|
|
|
}
|
|
|
|
|
// 寻找合同类型,关联合同id
|
|
|
|
|
essFlowKind = contract.ContractLogic.ContractSnKind(essFlow.ContractSn)
|
|
|
|
|
switch essFlowKind {
|
|
|
|
|
case model.EssFlowConsignment:
|
|
|
|
|
contract := model.ContractConsignment{ContractSn: essFlow.ContractSn}
|
|
|
|
|
if mysql.Db.Where(&contract).First(&contract).Error == nil {
|
|
|
|
|
essFlow.ContractId = contract.Id
|
|
|
|
|
}
|
|
|
|
|
case model.EssFLowPurchase:
|
|
|
|
|
contract := model.ContractPurchase{ContractSn: essFlow.ContractSn}
|
|
|
|
|
if mysql.Db.Where(&contract).First(&contract).Error == nil {
|
|
|
|
|
essFlow.ContractId = contract.Id
|
|
|
|
|
}
|
|
|
|
|
case model.EssFlowSale:
|
|
|
|
|
contract := model.ContractSale{ContractSn: essFlow.ContractSn}
|
|
|
|
|
if mysql.Db.Where(&contract).First(&contract).Error == nil {
|
|
|
|
|
essFlow.ContractId = contract.Id
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 下载合同初始pdf
|
|
|
|
|
url, filename, err := tencent.Ess.GetUrlByFlowId(essFlow.FlowId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Logger.Error("电子签文件下载地址获取出错:" + err.Error())
|
|
|
|
|
}
|
|
|
|
|
path, err := file.UploadLogic.WebFile(url, "contract", filename)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Logger.Error("电子签文件下载出错")
|
|
|
|
|
}
|
|
|
|
|
essFlow.Url = path
|
|
|
|
|
if mysql.Db.Create(&essFlow).Error != nil {
|
|
|
|
|
logger.Logger.Error("电子签回调信息保存出错")
|
|
|
|
|
}
|
|
|
|
|
// 未找到合同id时取消电子签流程
|
|
|
|
|
if essFlow.ContractId == 0 {
|
|
|
|
|
tencent.Ess.ChannelCancelFlow([]*string{&essFlow.FlowId})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "PART": // 合同签署中
|
|
|
|
|
case "ALL": // 合同签署完成
|
|
|
|
|
essFlowStatus = model.EssFlowStatusALL
|
|
|
|
|
contractStatus = constant.ContractStatusSign
|
|
|
|
|
case "REJECT": // 合同拒签
|
|
|
|
|
essFlowStatus = model.EssFlowStatusREJECT
|
|
|
|
|
contractStatus = constant.ContractStatusReject
|
|
|
|
|
case "CANCEL": // 合同撤回
|
|
|
|
|
essFlowStatus = model.EssFlowStatusCANCEL
|
|
|
|
|
contractStatus = constant.ContractStatusCancel
|
|
|
|
|
case "DEADLINE": // 合同过期
|
|
|
|
|
essFlowStatus = model.EssFlowStatusDEADLINE
|
|
|
|
|
contractStatus = constant.ContractStatusDeadline
|
|
|
|
|
case "EXCEPTION": // 合同异常
|
|
|
|
|
essFlowStatus = model.EssFlowStatusEXCEPTION
|
|
|
|
|
contractStatus = constant.ContractStatusException
|
|
|
|
|
}
|
|
|
|
|
// 电子签状态变动
|
|
|
|
|
if essFlowStatus > 0 {
|
|
|
|
|
essFlow := model.ThirdTencentEssFlow{FlowId: flowInfo.FlowId}
|
|
|
|
|
if mysql.Db.Where(&essFlow).First(&essFlow).Error != nil {
|
|
|
|
|
logger.Logger.Error("电子签回调信息更新出错")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
upd := map[string]interface{}{}
|
|
|
|
|
upd["status"] = essFlowStatus
|
|
|
|
|
// 完成时下载电子签合同文件
|
|
|
|
|
switch essFlowStatus {
|
|
|
|
|
case model.EssFlowStatusALL:
|
|
|
|
|
url, filename, err := tencent.Ess.GetUrlByFlowId(essFlow.FlowId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Logger.Error("电子签文件下载地址获取出错:" + err.Error())
|
|
|
|
|
}
|
|
|
|
|
path, err := file.UploadLogic.WebFile(url, "contract", filename)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Logger.Error("电子签文件下载出错")
|
|
|
|
|
}
|
|
|
|
|
upd["url"] = path
|
|
|
|
|
case model.EssFlowStatusREJECT:
|
|
|
|
|
upd["reject_at"] = time.Now()
|
|
|
|
|
upd["reason"] = res.MsgData.FlowMessage
|
|
|
|
|
case model.EssFlowStatusCANCEL, model.EssFlowStatusDEADLINE, model.EssFlowStatusEXCEPTION:
|
|
|
|
|
upd["cancel_at"] = time.Now()
|
|
|
|
|
}
|
|
|
|
|
if mysql.Db.Model(&model.ThirdTencentEssFlow{}).Where(&essFlow).Updates(upd).Error != nil {
|
|
|
|
|
logger.Logger.Error("电子签回调信息更新出错")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据电子签状态改变合同
|
|
|
|
|
if essFlow.ContractId <= 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
essFlowKind = contract.ContractLogic.ContractSnKind(essFlow.ContractSn)
|
|
|
|
|
err := contract.ContractLogic.ContractStatusChange(mysql.Db, essFlowKind, essFlow.ContractId, contractStatus)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Logger.Error("电子签回调信息更新出错:" + err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|