You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
8.3 KiB

package contract
import (
"base/app/config"
"base/app/constant"
"base/app/lib/tencent"
"base/app/logic/broker/message"
"base/app/logic/broker/user/wallet"
"base/app/logic/file"
"base/app/model"
"errors"
"git.oa00.com/go/mysql"
essbasic "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/essbasic/v20210526"
"gorm.io/gorm"
"strings"
"time"
)
var ContractLogic = &contractLogic{}
type contractLogic struct {
}
func (c *contractLogic) EssFlowByTemplate(tx *gorm.DB, brokerId, contractId uint, contractSn, flowName, templateId string, formFields []*essbasic.FormField, flowApproverInfos []*essbasic.FlowApproverInfo) (result interface{}, err error) {
//if err := wallet.AssessLogic.Use(tx, model.WalletAssessHistoryKindContract, brokerId, contractId, 1); err != nil {
// return result, err
//}
if !config.IsProd() {
flowIds, err := tencent.Ess.CreateFlowsByTemplates(formFields, flowApproverInfos, contractSn, flowName, templateId)
if err != nil {
return result, err
}
if len(flowIds) == 0 {
return result, nil
}
urls, err := tencent.Ess.CreateSignUrls(0, flowIds)
if err != nil {
result = map[string]interface{}{
"flowIds": flowIds,
}
return result, nil
}
result = urls[0]
}
return
}
// 合同状态修改和订单变化
func (c *contractLogic) ContractStatusChange(tx *gorm.DB, kind, contractId, status uint) error {
messageItem := message.MessageItem{
Title: "合同提示",
LinkedId: contractId,
Content: "您的合同状态发生变化,请注意查看"}
switch kind {
case model.EssFlowConsignment:
contract := model.ContractConsignment{Id: contractId}
if tx.Where(&contract).First(&contract).Error != nil {
return errors.New("未找到电子签对应的寄售合同")
}
updContract := map[string]interface{}{}
updContract["status"] = status
switch status {
case constant.ContractStatusSign:
updContract["sign_at"] = time.Now()
if tx.Model(&model.OrderConsignment{}).Where("contract_id = ?", contract.Id).UpdateColumns(map[string]interface{}{
"status": model.OrderConsignmentStatusUnPublish,
}).RowsAffected != 1 {
return errors.New("寄售订单状态修改失败")
}
if tx.Model(&model.Customer{}).Where("id = ? and level < ?", contract.CustomerId, model.CustomerLevelDone).UpdateColumn("level", model.CustomerLevelDone).Error != nil {
return errors.New("提交失败")
}
messageItem.Content = "您的寄售合同已被用户签字"
case constant.ContractStatusReject, constant.ContractStatusCancel, constant.ContractStatusDeadline, constant.ContractStatusException:
updContract["cancel_at"] = time.Now()
if tx.Model(&model.OrderConsignment{}).Where("contract_id = ?", contract.Id).UpdateColumns(map[string]interface{}{
"status": model.OrderConsignmentStatusCancel,
"cancel_at": time.Now(),
}).RowsAffected != 1 {
return errors.New("寄售订单状态修改失败")
}
messageItem.Content = "您的寄售合同已取消"
}
if tx.Model(&model.ContractConsignment{}).Where(contract).UpdateColumns(updContract).Error != nil {
return errors.New("寄售合同信息更新出错")
}
message.MessageLogic.MessageToBroker(mysql.Db, []uint{contract.BrokerId}, 0, model.BrokerMessageTypeNormal, messageItem)
case model.EssFLowPurchase:
contract := model.ContractPurchase{Id: contractId}
if tx.Where(&contract).First(&contract).Error != nil {
return errors.New("未找到电子签对应的收购合同")
}
if tx.Model(&model.ContractPurchase{}).Where(&contract).Updates(map[string]interface{}{
"status": status,
"cancel_at": time.Now(),
}).RowsAffected != 1 {
return errors.New("收购合同信息更新出错")
}
message.MessageLogic.MessageToBroker(mysql.Db, []uint{contract.BrokerId}, 0, model.BrokerMessageTypeNormal, messageItem)
case model.EssFlowSale:
contract := model.ContractSale{Id: contractId}
if tx.Preload("Car.CarBase").Where(&contract).First(&contract).Error != nil {
return errors.New("未找到电子签对应的出售合同")
}
updContract := map[string]interface{}{}
updContract["status"] = status
var orderSaleStatus, carStatus, consignmentStatus uint
// 根据出售ess状态修改合同、订单、车辆、或有关联寄售订单的状态
switch status {
case constant.ContractStatusSign:
updContract["sign_at"] = time.Now()
orderSaleStatus = model.OrderSaleStatusSign
carStatus = model.CarStatusSigned
//consignmentStatus = model.OrderConsignmentStatusSale
messageItem.Content = "您的出售合同已被用户签字"
if contract.CustomerId > 0 {
if tx.Model(&model.Customer{}).Where("id = ? and level < ?", contract.CustomerId, model.CustomerLevelDone).UpdateColumn("level", model.CustomerLevelDone).Error != nil {
return errors.New("提交失败")
}
}
case constant.ContractStatusReject, constant.ContractStatusCancel, constant.ContractStatusDeadline, constant.ContractStatusException:
updContract["cancel_at"] = time.Now()
orderSaleStatus = model.OrderSaleStatusCancel
carStatus = model.CarStatusNormal
consignmentStatus = model.OrderConsignmentStatusTheUpper
messageItem.Content = "您的出售合同已取消"
}
if tx.Model(&model.ContractSale{}).Where(&contract).UpdateColumns(updContract).Error != nil {
return errors.New("出售合同信息更新出错")
}
if orderSaleStatus > 0 {
if tx.Model(&model.OrderSale{}).Where("contract_id = ?", contract.Id).UpdateColumn("status", orderSaleStatus).RowsAffected != 1 {
return errors.New("出售订单状态修改失败")
}
}
if carStatus > 0 {
if tx.Model(&model.Car{}).Where("id = ?", contract.CarId).UpdateColumn("status", carStatus).Error != nil {
return errors.New("线上车辆状态修改失败")
}
}
switch contract.Car.CarBase.Source {
case model.CarBaseSourceConsignment:
if consignmentStatus > 0 {
if tx.Model(&model.OrderConsignment{}).Where("id = ?", contract.Car.CarBase.OrderId).UpdateColumn("status", consignmentStatus).RowsAffected != 1 {
return errors.New("寄售订单状态修改失败")
}
}
}
message.MessageLogic.MessageToBroker(mysql.Db, []uint{contract.BrokerId}, 0, model.BrokerMessageTypeNormal, messageItem)
}
// 钱包次数退款
if status == constant.ContractStatusCancel {
var assessHistoryKind uint
switch kind {
case model.EssFlowConsignment:
assessHistoryKind = model.WalletAssessHistoryKindContractConsignment
case model.EssFLowPurchase:
assessHistoryKind = model.WalletAssessHistoryKindContractPurchase
case model.EssFlowSale:
assessHistoryKind = model.WalletAssessHistoryKindContractSale
}
his := model.WalletAssessHistory{Kind: assessHistoryKind, OrderId: contractId}
if tx.Where(&his).First(&his).Error != nil {
return errors.New("未找到合同使用钱包次数记录")
}
if err := wallet.AssessLogic.Refund(tx, model.WalletAssessKindContract, his.UserType, his.UserId, his.Kind, his.OrderId, 1); err != nil {
return err
}
}
return nil
}
// GetEssSignUrl @Title 获取合同签署地址
func (c *contractLogic) GetEssSignUrl(flowId string) (url string, err error) {
urls, err := tencent.Ess.CreateSignUrls(0, []*string{&flowId})
if err != nil {
return url, nil
}
url = *urls[0].SignUrl
return
}
// SaveEssFlowFile @Title 获取最新ess合同文件
func (c *contractLogic) SaveEssFlowFile(contractSn string) (path string, err error) {
essFlow := model.ThirdTencentEssFlow{ContractSn: contractSn}
if mysql.Db.Where(&essFlow).First(&essFlow).Error != nil {
return path, errors.New("查找ess合同失败")
}
url, filename, err := tencent.Ess.GetUrlByFlowId(essFlow.FlowId)
if err != nil {
return path, err
}
path, err = file.UploadLogic.WebFile(url, "contract", filename)
if err != nil {
return path, err
}
if mysql.Db.Model(&model.ThirdTencentEssFlow{}).Where(&essFlow).Updates(map[string]interface{}{
"url": path,
}).Error != nil {
return path, errors.New("保存合同文件出错")
}
return path, nil
}
// 根据合同编号获取合同类型
func (c *contractLogic) ContractSnKind(contractSn string) uint {
if strings.Contains(contractSn, constant.NoPrefixContractConsignment) {
return model.EssFlowConsignment
} else if strings.Contains(contractSn, constant.NoPrefixContractPurchase) {
return model.EssFLowPurchase
} else if strings.Contains(contractSn, constant.NoPrefixContractSale) {
return model.EssFlowSale
} else {
return 0
}
}