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

981 lines
29 KiB
Go

package service
import (
"encoding/json"
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/xuri/excelize/v2"
"gorm.io/gorm"
"insure/dto/request"
"insure/dto/request/manage"
"insure/dto/response"
"insure/e"
"insure/global"
"insure/model"
"insure/third_party/signer"
"insure/third_party/suixing"
"insure/third_party/zking"
"insure/utils"
"insure/utils/excel"
"math"
"net/http"
"strconv"
"strings"
"time"
)
var OrderService = orderService{}
type orderService struct {
}
func (s *orderService) CreateOrder(param suixing.CreateParam) (*model.Order, error) {
orderNo := utils.GenerateNo("order")
order, err := s.saveOrder(orderNo, param.RequestID)
if err != nil {
return nil, err
}
user, err := s.saveUser(orderNo, param.Data.User)
if err != nil {
return nil, err
}
project, err := s.saveProject(orderNo, param.Data.Project)
if err != nil {
return nil, err
}
applicant, err := s.saveApplicant(orderNo, param.Data.Applicant)
if err != nil {
return nil, err
}
assured, err := s.saveAssured(orderNo, param.Data.Assured)
if err != nil {
return nil, err
}
order.User = user
order.Applicant = applicant
order.Project = project
order.Assured = assured
return order, nil
}
func (s *orderService) saveOrder(orderNo string, requestID string) (*model.Order, error) {
o := model.Order{}
o.Token = utils.Md5(orderNo + utils.GenerateRandomString(10) + strconv.Itoa(int(time.Now().UnixMicro())))
o.OrderNo = orderNo
o.RequestID = requestID
o.Status = "initial"
err := global.DB.Save(&o).Error
if err != nil {
return nil, err
}
return &o, nil
}
func (s *orderService) saveUser(orderNo string, user suixing.User) (*model.User, error) {
u := model.User{}
u.OrderNo = orderNo
u.UserID = user.UserID
u.Username = user.Username
u.Mobile = user.Mobile
u.UnitID = user.UnitID
u.UnitName = user.UnitName
u.BankUserName = user.BankUserName
u.BankName = user.BankName
u.BankCardNum = user.BankCardNum
u.BeiandiquCode = user.BeiandiquCode
err := global.DB.Save(&u).Error
if err != nil {
return nil, err
}
return &u, nil
}
func (s *orderService) saveProject(orderNo string, project suixing.Project) (*model.Project, error) {
projectAmount, _ := strconv.ParseFloat(project.Amount, 64)
insureAmount := math.Round(projectAmount * 0.003)
if insureAmount < 200 {
insureAmount = 200.00
}
insureAmountStr := fmt.Sprintf("%.2f", insureAmount)
p := model.Project{}
p.OrderNo = orderNo
p.Name = project.Name
p.TpID = project.TpID
p.BdID = project.BdID
p.BdName = project.BdName
p.StartDate = project.StartDate
p.Amount = project.Amount
p.InsureAmount = insureAmountStr
p.Province = project.Province
p.City = project.City
p.District = project.District
p.BzjEtime = project.BzjEtime
p.CreateTime = project.CreateTime
p.PlanDate = project.PlanDate
p.BuildPrice = project.BuildPrice
p.BuildPlace = project.BuildPlace
p.TenderProjectType = project.TenderProjectType
p.ValidPeriod = project.ValidPeriod
p.TenderNoticeUrl = project.TenderNoticeUrl
p.TenderFileUrl = project.TenderFileUrl
p.TenderFileUrlMd5 = project.TenderFileUrlMd5
p.ProjectApprovalNo = project.ProjectApprovalNo
err := global.DB.Save(&p).Error
if err != nil {
return nil, err
}
return &p, nil
}
func (s *orderService) saveApplicant(orderNo string, applicant suixing.Applicant) (*model.Applicant, error) {
licenseFiles, err := json.Marshal(applicant.LicenseFiles)
if err != nil {
return nil, err
}
a := model.Applicant{}
a.OrderNo = orderNo
a.ApplicantName = applicant.ApplicantName
a.CreditVld = applicant.CreditVld
a.CreditCode = applicant.CreditCode
a.ContactName = applicant.ContactName
a.ContactMobile = applicant.ContactMobile
a.ComTelArea = applicant.ComTelArea
a.Province = applicant.Province
a.City = applicant.City
a.District = applicant.District
a.Address = applicant.Address
a.Email = applicant.Email
a.LegalName = applicant.LegalName
a.LegalNum = applicant.LegalNum
a.ComTelNum = applicant.ComTelNum
a.LicenseFiles = string(licenseFiles)
a.SignUrl = applicant.SignUrl
err = global.DB.Save(&a).Error
if err != nil {
return nil, err
}
return &a, nil
}
func (s *orderService) saveAssured(orderNo string, assured suixing.Assured) (*model.Assured, error) {
a := model.Assured{}
a.OrderNo = orderNo
a.AssuredName = assured.AssuredName
a.CreditVld = assured.CreditVld
a.CreditCode = assured.CreditCode
a.ContactName = assured.ContactName
a.ContactMobile = assured.ContactMobile
a.ComTelArea = assured.ComTelArea
a.Province = assured.Province
a.City = assured.City
a.District = assured.District
a.Address = assured.Address
a.TenderBankAccount = assured.TenderBankAccount
a.TenderBankOpenbank = assured.TenderBankOpenbank
a.TenderAccountName = assured.TenderAccountName
a.Agency = assured.Agency
a.ComTelNum = assured.ComTelNum
err := global.DB.Save(&a).Error
if err != nil {
return nil, err
}
return &a, nil
}
func (s *orderService) GetOrderByToken(token string) (*model.Order, error) {
order := model.Order{}
err := global.DB.
Preload("User").
Preload("Project").
Preload("Applicant").
Preload("Assured").
Preload("Invoice").
Where("token", token).
First(&order).
Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, e.NewError("记录不存在")
} else {
return nil, e.NewError("数据异常")
}
}
return &order, nil
}
func (s *orderService) GetOrderByOrderNo(orderNo string) (*model.Order, error) {
order := model.Order{}
err := global.DB.
Preload("User").
Preload("Project").
Preload("Applicant").
Preload("Assured").
Preload("Invoice").
Preload("OrderResult").
Where("order_no", orderNo).
First(&order).
Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, e.NewError("记录不存在")
} else {
return nil, e.NewError("数据异常")
}
}
return &order, nil
}
func (s *orderService) GetOrderByOrderSn(orderSn string) (*model.Order, error) {
order := model.Order{}
err := global.DB.
Preload("User").
Preload("Project").
Preload("Applicant").
Preload("Assured").
Preload("Invoice").
Where("order_sn", orderSn).
First(&order).
Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, e.NewError("记录不存在")
} else {
return nil, e.NewError("数据异常")
}
}
return &order, nil
}
func (s *orderService) GetOrderResultByPolicyNo(policyNo string) (*model.OrderResult, error) {
orderResult := model.OrderResult{}
err := global.DB.
Where("policy_no", policyNo).
First(&orderResult).
Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, e.NewError("记录不存在")
} else {
return nil, e.NewError("数据异常")
}
}
return &orderResult, nil
}
func (s *orderService) AddSignLog(orderNo string, param signer.UploadParam) (*model.SignLog, error) {
var err error
isUploadPdf := 1
if param.IsUploadPdf != "" {
isUploadPdf, err = strconv.Atoi(param.IsUploadPdf)
if err != nil {
return nil, err
}
}
l := model.SignLog{}
l.OrderNo = orderNo
l.Params = param.Params
l.CaInfo = param.CaInfo
l.IsUploadPdf = isUploadPdf
l.FileInfo = param.FileInfo
l.PcInfo = param.PcInfo
err = global.DB.Save(&l).Error
if err != nil {
return nil, err
}
return &l, nil
}
func (s *orderService) PostToZking(orderNo string) (*zking.Result, error) {
order := model.Order{}
err := global.DB.
Preload("User").
Preload("Project").
Preload("Applicant").
Preload("Assured").
Where("order_no", orderNo).
First(&order).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, e.NewError("记录不存在")
} else {
return nil, e.NewError("数据异常")
}
}
user := suixing.User{
Mobile: order.User.Mobile,
Username: order.User.Username,
UserID: order.User.UserID,
UnitID: order.User.UnitID,
UnitName: order.User.UnitName,
BeiandiquCode: order.User.BeiandiquCode,
BankName: order.User.BankName,
BankCardNum: order.User.BankCardNum,
BankUserName: order.User.BankUserName,
}
var licenseFiles []string
if order.Applicant.LicenseFiles != "" {
err := json.Unmarshal([]byte(order.Applicant.LicenseFiles), &licenseFiles)
if err != nil {
return nil, err
}
}
applicant := suixing.Applicant{
ApplicantName: order.Applicant.ApplicantName,
CreditCode: order.Applicant.CreditCode,
CreditVld: order.Applicant.CreditVld,
ContactName: order.Applicant.ContactName,
ContactMobile: order.Applicant.ContactMobile,
ComTelArea: order.Applicant.ComTelArea,
Province: order.Applicant.Province,
City: order.Applicant.City,
District: order.Applicant.District,
Address: order.Applicant.Address,
Email: order.Applicant.Email,
LegalName: order.Applicant.LegalName,
LegalNum: order.Applicant.LegalNum,
ComTelNum: order.Applicant.ComTelNum,
LicenseFiles: licenseFiles,
SignUrl: order.Applicant.SignedUrl,
}
project := suixing.Project{
Name: order.Project.Name,
TpID: order.Project.TpID,
BdID: order.Project.BdID,
BdName: order.Project.BdName,
StartDate: order.Project.StartDate,
Amount: order.Project.Amount,
Province: order.Project.Province,
City: order.Project.City,
District: order.Project.District,
BzjEtime: order.Project.BzjEtime,
CreateTime: order.Project.CreateTime,
PlanDate: order.Project.PlanDate,
BuildPrice: order.Project.BuildPrice,
BuildPlace: order.Project.BuildPlace,
TenderProjectType: order.Project.TenderProjectType,
ValidPeriod: order.Project.ValidPeriod,
TenderNoticeUrl: order.Project.TenderNoticeUrl,
TenderFileUrl: order.Project.TenderFileUrl,
TenderFileUrlMd5: order.Project.TenderFileUrlMd5,
ProjectApprovalNo: order.Project.ProjectApprovalNo,
}
assured := suixing.Assured{
AssuredName: order.Assured.AssuredName,
ComTelArea: order.Assured.ComTelArea,
CreditCode: order.Assured.CreditCode,
CreditVld: order.Assured.CreditVld,
ContactName: order.Assured.ContactName,
ContactMobile: order.Assured.ContactMobile,
TenderBankAccount: order.Assured.TenderBankAccount,
TenderBankOpenbank: order.Assured.TenderBankOpenbank,
TenderAccountName: order.Assured.TenderAccountName,
Province: order.Assured.Province,
City: order.Assured.City,
District: order.Assured.District,
Address: order.Assured.Address,
ComTelNum: order.Assured.ComTelNum,
Agency: order.Assured.Agency,
}
createData := suixing.CreateData{
User: user,
Project: project,
Applicant: applicant,
Assured: assured,
}
data, _ := json.Marshal(createData)
param := zking.CommonParam{
AppID: global.Config.Zking.AppID,
RequestID: order.RequestID,
Timestamp: strconv.Itoa(int(time.Now().Unix())),
Version: "1.0.0",
Data: string(data),
}
result, err := zking.InsureApi.SuixingCreate(param)
if err != nil {
return nil, err
}
if result.Code != "200" {
return nil, e.NewError(result.Msg)
}
return result, nil
}
func (s *orderService) SaveClaim(param suixing.ClaimParam) (*model.Claim, error) {
claim := model.Claim{}
err := global.DB.Where("policy_no", param.Data.PolicyNo).First(&claim).Error
if err != nil && !e.Is(err, gorm.ErrRecordNotFound) {
return nil, err
}
fileList, err := json.Marshal(param.Data.FileList)
if err != nil {
return nil, err
}
claim.PolicyNo = param.Data.PolicyNo
claim.BankName = param.Data.BankName
claim.AccountName = param.Data.AccountName
claim.ContactName = param.Data.ContactName
claim.ContactPhone = param.Data.ContactPhone
claim.FileList = string(fileList)
claim.BankCardNum = param.Data.BankCardNum
claim.BackPremium = param.Data.BackPremium
claim.Reason = param.Data.Reason
err = global.DB.Save(&claim).Error
if err != nil {
return nil, err
}
return &claim, nil
}
func (s *orderService) ZkingClaim(claim *model.Claim) (*zking.Result, error) {
var fileList []map[string]string
err := json.Unmarshal([]byte(claim.FileList), &fileList)
if err != nil {
return nil, err
}
createData := suixing.ClaimData{
PolicyNo: claim.PolicyNo,
BankName: claim.BankName,
AccountName: claim.AccountName,
ContactName: claim.ContactName,
ContactPhone: claim.ContactPhone,
FileList: fileList,
BankCardNum: claim.BankCardNum,
BackPremium: claim.BackPremium,
Reason: claim.Reason,
}
data, _ := json.Marshal(createData)
param := zking.CommonParam{
AppID: global.Config.Zking.AppID,
RequestID: utils.GenerateNo("requestId"),
Timestamp: strconv.Itoa(int(time.Now().Unix())),
Version: "1.0.0",
Data: string(data),
}
result, err := zking.InsureApi.SuixingClaim(param)
if err != nil {
return nil, err
}
if result.Code != "200" {
return nil, e.NewError(result.Msg)
}
return result, nil
}
func (s *orderService) SaveRevoke(param suixing.RevokeParam) (*model.Revoke, error) {
revoke := model.Revoke{}
err := global.DB.Where("policy_no", param.Data.PolicyNo).First(&revoke).Error
if err != nil && !e.Is(err, gorm.ErrRecordNotFound) {
return nil, err
}
fileList, err := json.Marshal(param.Data.FileList)
if err != nil {
return nil, err
}
revoke.PolicyNo = param.Data.PolicyNo
revoke.BankName = param.Data.BankName
revoke.AccountName = param.Data.AccountName
revoke.ContactName = param.Data.ContactName
revoke.ContactPhone = param.Data.ContactPhone
revoke.FileList = string(fileList)
revoke.BankCardNum = param.Data.BankCardNum
revoke.BackPremium = param.Data.BackPremium
revoke.Reason = param.Data.Reason
err = global.DB.Save(&revoke).Error
if err != nil {
return nil, err
}
return &revoke, nil
}
func (s *orderService) ZkingRevoke(revoke *model.Revoke) (*zking.Result, error) {
var fileList []map[string]string
err := json.Unmarshal([]byte(revoke.FileList), &fileList)
if err != nil {
return nil, err
}
createData := suixing.ClaimData{
PolicyNo: revoke.PolicyNo,
BankName: revoke.BankName,
AccountName: revoke.AccountName,
ContactName: revoke.ContactName,
ContactPhone: revoke.ContactPhone,
FileList: fileList,
BankCardNum: revoke.BankCardNum,
BackPremium: revoke.BackPremium,
Reason: revoke.Reason,
}
data, _ := json.Marshal(createData)
param := zking.CommonParam{
AppID: global.Config.Zking.AppID,
RequestID: utils.GenerateNo("requestId"),
Timestamp: strconv.Itoa(int(time.Now().Unix())),
Version: "1.0.0",
Data: string(data),
}
result, err := zking.InsureApi.SuixingRevoke(param)
if err != nil {
return nil, err
}
if result.Code != "200" {
return nil, e.NewError(result.Msg)
}
return result, nil
}
func (s *orderService) GetOrderResultByToken(token string) (*model.OrderResult, error) {
order := model.Order{}
err := global.DB.
Preload("User").
Preload("Project").
Preload("Applicant").
Preload("Assured").
Where("token", token).
First(&order).
Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, e.NewError("记录不存在")
} else {
return nil, e.NewError("数据异常")
}
}
orderResult := model.OrderResult{}
err = global.DB.Where("order_sn", order.OrderSn).First(&orderResult).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
} else {
return nil, e.NewError("数据异常")
}
}
return &orderResult, nil
}
func (s *orderService) GetOrders(req manage.OrderQueryRequest) ([]model.Order, int64) {
orders := make([]model.Order, req.PageSize)
offset := (req.Page - 1) * req.PageSize
var total int64
s.buildQuery(req).Preload("User").
Preload("Project").
Preload("Applicant").
Preload("Assured").
Preload("OrderResult").
Preload("Invoice").
Order("id desc").Offset(offset).Limit(req.PageSize).Find(&orders)
s.buildQuery(req).Model(&model.Order{}).Count(&total)
return orders, total
}
func (s *orderService) buildQuery(req manage.OrderQueryRequest) *gorm.DB {
tx := global.DB
if req.OrderSn != "" {
tx = tx.Where("order_sn", req.OrderSn)
}
if req.OrderNo != "" {
tx = tx.Where("order_no", req.OrderNo)
}
if req.InsureTimeStart != "" && req.InsureTimeEnd != "" {
startInsureTime := req.InsureTimeStart + " 00:00:00"
endInsureTime := req.InsureTimeEnd + " 23:59:59"
var orderSns []string
global.DB.Model(&model.OrderResult{}).Where("created_at >= ?", startInsureTime).Where("created_at <= ?", endInsureTime).Pluck("order_sn", &orderSns)
tx = tx.Where("order_sn in (?)", orderSns)
}
if req.AssuredName != "" {
var orderSns []string
global.DB.Model(&model.Assured{}).Where("assured_name", req.AssuredName).Pluck("order_sn", &orderSns)
tx = tx.Where("order_sn in (?)", orderSns)
}
if req.AssuredCreditCode != "" {
var orderSns []string
global.DB.Model(&model.Assured{}).Where("credit_code", req.AssuredCreditCode).Pluck("order_sn", &orderSns)
tx = tx.Where("order_sn in (?)", orderSns)
}
if req.AssuredContactName != "" {
var orderSns []string
global.DB.Model(&model.Assured{}).Where("contact_name", req.AssuredContactName).Pluck("order_sn", &orderSns)
tx = tx.Where("order_sn in (?)", orderSns)
}
if req.AssuredContactMobile != "" {
var orderSns []string
global.DB.Model(&model.Assured{}).Where("contact_mobile", req.AssuredContactMobile).Pluck("order_sn", &orderSns)
tx = tx.Where("order_sn in (?)", orderSns)
}
if req.ProjectName != "" {
var orderSns []string
global.DB.Model(&model.Project{}).Where("name", req.ProjectName).Pluck("order_sn", &orderSns)
tx = tx.Where("order_sn in (?)", orderSns)
}
if req.TpId != "" {
var orderSns []string
global.DB.Model(&model.Project{}).Where("tp_id", req.TpId).Pluck("order_sn", &orderSns)
tx = tx.Where("order_sn in (?)", orderSns)
}
if req.ApplicantName != "" {
var orderSns []string
global.DB.Model(&model.Applicant{}).Where("applicant_name", req.ApplicantName).Pluck("order_sn", &orderSns)
tx = tx.Where("order_sn in (?)", orderSns)
}
if req.ApplicantName != "" {
var orderSns []string
global.DB.Model(&model.Applicant{}).Where("applicant_name", req.ApplicantName).Pluck("order_sn", &orderSns)
tx = tx.Where("order_sn in (?)", orderSns)
}
if req.AssuredCreditCode != "" {
var orderSns []string
global.DB.Model(&model.Applicant{}).Where("credit_code", req.AssuredCreditCode).Pluck("order_sn", &orderSns)
tx = tx.Where("order_sn in (?)", orderSns)
}
if req.AssuredContactName != "" {
var orderSns []string
global.DB.Model(&model.Applicant{}).Where("contact_name", req.AssuredContactName).Pluck("order_sn", &orderSns)
tx = tx.Where("order_sn in (?)", orderSns)
}
if req.AssuredContactMobile != "" {
var orderSns []string
global.DB.Model(&model.Applicant{}).Where("contact_mobile", req.AssuredContactMobile).Pluck("order_sn", &orderSns)
tx = tx.Where("order_sn in (?)", orderSns)
}
if req.PolicyNo != "" {
var orderSns []string
global.DB.Model(&model.OrderResult{}).Where("policy_no", req.PolicyNo).Pluck("order_sn", &orderSns)
tx = tx.Where("order_sn in (?)", orderSns)
}
if req.Status != "" {
statusMap := map[string][]string{
"payed": []string{"payed"},
"wait_submit": []string{"initial", "generated", "signed"},
"wait_pay": []string{"submitted"},
"claimed": []string{"claimed"},
"revoked": []string{"revoked"},
}
statusList := statusMap[req.Status]
tx = tx.Where("status in (?)", statusList)
}
return tx
}
func (s *orderService) GenerateOrdersExcel(req manage.OrderQueryRequest) *excelize.File {
pageSize := 200
lastID := 0
f := excel.NewFile()
headers := []string{
"订单号",
"项目订单编号",
"项目来源",
"招标项目编号",
"项目名称",
"标段编号",
"标段名称",
"保险公司",
"保险产品",
"保/批单号",
"招标人",
"招标人-联系人姓名",
"招标人-联系人手机号",
"代理机构",
"投标人",
"统一社会信用代码",
"投标人-联系人姓名",
"投标人-联系人手机号",
"投标人-开户行(基本户)",
"投标人-银行账号",
"接收发票的邮箱",
"出函时间",
"起保时间",
"保函天数",
"保费",
"保额",
"保单状态",
"发票抬头",
"税号",
}
excel.SetSimpleHeaders(headers, "Sheet1", f)
var allOrders []model.Order
for {
orders := make([]model.Order, pageSize)
s.buildQuery(req).Preload("User").
Preload("Project").
Preload("Applicant").
Preload("Assured").
Preload("OrderResult").
Where("id > ?", lastID).
Order("id asc").
Limit(pageSize).
Find(&orders)
count := len(orders)
if count == 0 {
break
}
lastIndex := count - 1
lastID = orders[lastIndex].ID
fmt.Println("lastID", lastID)
allOrders = append(allOrders, orders...)
}
s.buildOrdersExcel(allOrders, "Sheet1", f)
return f
}
func (orderService) buildOrdersExcel(orders []model.Order, sheet string, f *excelize.File) {
row := 2
for _, order := range orders {
col := 0
policyNo := ""
policyCreatedAt := ""
if order.OrderResult != nil {
policyNo = order.OrderResult.PolicyNo
policyCreatedAt = order.OrderResult.CreatedAt.Format(time.DateTime)
}
assuredName := ""
assuredContactMobile := ""
assuredContactName := ""
assuredAgency := ""
if order.Assured != nil {
assuredName = order.Assured.AssuredName
assuredContactMobile = order.Assured.ContactMobile
assuredContactName = order.Assured.ContactName
assuredAgency = order.Assured.Agency
}
applicantName := ""
applicantContactMobile := ""
applicantContactName := ""
applicantCreditCode := ""
invoiceAcceptEmail := ""
if order.Applicant != nil {
applicantName = order.Applicant.ApplicantName
applicantContactMobile = order.Applicant.ContactMobile
applicantContactName = order.Applicant.ContactName
applicantCreditCode = order.Applicant.CreditCode
invoiceAcceptEmail = order.Applicant.InvoiceAcceptEmail
}
invoiceHeader := ""
invoiceTaxNumber := ""
if order.Invoice != nil {
invoiceHeader = order.Invoice.Header
invoiceTaxNumber = order.Invoice.TaxNumber
}
if order.Project == nil {
order.Project = &model.Project{}
}
f.SetCellValue(sheet, excel.CellKey(row, &col), order.OrderNo)
f.SetCellValue(sheet, excel.CellKey(row, &col), order.OrderSn)
f.SetCellValue(sheet, excel.CellKey(row, &col), "易交易电子信息交易中心")
f.SetCellValue(sheet, excel.CellKey(row, &col), order.Project.TpID)
f.SetCellValue(sheet, excel.CellKey(row, &col), order.Project.Name)
f.SetCellValue(sheet, excel.CellKey(row, &col), order.Project.BdID)
f.SetCellValue(sheet, excel.CellKey(row, &col), order.Project.BdName)
f.SetCellValue(sheet, excel.CellKey(row, &col), "紫金财产保险股份有限公司福建分公司")
f.SetCellValue(sheet, excel.CellKey(row, &col), "投标保证保险")
f.SetCellValue(sheet, excel.CellKey(row, &col), policyNo)
f.SetCellValue(sheet, excel.CellKey(row, &col), assuredName)
f.SetCellValue(sheet, excel.CellKey(row, &col), assuredContactName)
f.SetCellValue(sheet, excel.CellKey(row, &col), assuredContactMobile)
f.SetCellValue(sheet, excel.CellKey(row, &col), assuredAgency)
f.SetCellValue(sheet, excel.CellKey(row, &col), applicantName)
f.SetCellValue(sheet, excel.CellKey(row, &col), applicantCreditCode)
f.SetCellValue(sheet, excel.CellKey(row, &col), applicantContactName)
f.SetCellValue(sheet, excel.CellKey(row, &col), applicantContactMobile)
f.SetCellValue(sheet, excel.CellKey(row, &col), order.User.BankName)
f.SetCellValue(sheet, excel.CellKey(row, &col), order.User.BankCardNum)
f.SetCellValue(sheet, excel.CellKey(row, &col), invoiceAcceptEmail)
f.SetCellValue(sheet, excel.CellKey(row, &col), policyCreatedAt)
f.SetCellValue(sheet, excel.CellKey(row, &col), order.Project.StartDate)
f.SetCellValue(sheet, excel.CellKey(row, &col), "180天")
f.SetCellValue(sheet, excel.CellKey(row, &col), order.Project.InsureAmount)
f.SetCellValue(sheet, excel.CellKey(row, &col), order.Project.Amount)
f.SetCellValue(sheet, excel.CellKey(row, &col), order.GetStatusText())
f.SetCellValue(sheet, excel.CellKey(row, &col), invoiceHeader)
f.SetCellValue(sheet, excel.CellKey(row, &col), invoiceTaxNumber)
row++
}
}
func (s *orderService) SaveOrderResult(data zking.PushCallbackData) (*model.OrderResult, error) {
if data.OrderSn == "" {
return nil, e.NewError("参数错误")
}
order, err := s.GetOrderByOrderSn(data.OrderSn)
if err != nil {
return nil, err
}
orderResult := model.OrderResult{}
err = global.DB.Where("order_sn", data.OrderSn).First(&orderResult).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return nil, err
}
orderResult.OrderNo = order.OrderNo
orderResult.OrderSn = data.OrderSn
orderResult.PolicyNo = data.PolicyNo
orderResult.Status = data.Status
orderResult.Remark = data.Remark
orderResult.ConfirmReceiptLink = data.ConfirmReceiptLink
orderResult.ConfirmReceiptLinkMd5 = data.ConfirmReceiptLinkMd5
orderResult.GuranteeLink = data.GuranteeLink
orderResult.GuranteeLinkMd5 = data.GuranteeLinkMd5
orderResult.GuranteeOfdLink = data.GuranteeOfdLink
orderResult.GuranteeOfdLinkMd5 = data.GuranteeOfdLinkMd5
orderResult.InvoiceLink = data.InvoiceLink
orderResult.InvoiceLinkMd5 = data.InvoiceLinkMd5
orderResult.MinPremium = data.MinPremium
orderResult.Rate = data.Rate
orderResult.ServiceAmount = data.ServiceAmount
err = global.DB.Save(&orderResult).Error
if err != nil {
return nil, err
}
return &orderResult, nil
}
func (orderService) UploadPdf(fileParam string, c *gin.Context) (string, error) {
file, err := c.FormFile(fileParam)
if err != nil {
return "", e.NewError("文件上传失败")
}
reader, err := file.Open()
if err != nil {
return "", e.NewError("文件打开失败:" + err.Error())
}
b := make([]byte, 512)
reader.Read(b)
contentType := http.DetectContentType(b)
if contentType != "application/pdf" && contentType != "application/zip" {
return "", e.NewError("文件格式错误:" + contentType)
}
fileNameItems := strings.Split(file.Filename, ".")
filePath := time.Now().Format("20060102") + "/" + utils.Md5(file.Filename+strconv.Itoa(int(time.Now().UnixMicro()))+utils.GenerateRandomString(10)) + "." + fileNameItems[len(fileNameItems)-1]
uploadPath := "."
if global.Config.Server.RootPath != "" {
uploadPath = global.Config.Server.RootPath
}
dest := uploadPath + "/static/uploads/" + filePath
err = c.SaveUploadedFile(file, dest)
if err != nil {
return "", e.NewError("文件保存失败:" + err.Error())
}
return global.Config.Server.Domain + "/uploads/" + filePath, err
}
func (s *orderService) UpdateApplicant(req request.UpdateApplicantRequest) (*model.Order, error) {
order, err := s.GetOrderByToken(req.Token)
if err != nil {
return nil, err
}
user := order.User
user.BankName = req.BankName
user.BankCardNum = req.BankCardNum
err = global.DB.Save(&user).Error
if err != nil {
return nil, err
}
applicant := order.Applicant
applicant.ContactName = req.ContactName
applicant.ContactMobile = req.ContactMobile
applicant.CreditCode = req.CreditCode
applicant.ApplicantName = req.ApplicantName
applicant.InvoiceType = req.InvoiceType
applicant.InvoiceAcceptEmail = req.InvoiceAcceptEmail
err = global.DB.Save(&applicant).Error
if err != nil {
return nil, err
}
order.User = user
order.Applicant = applicant
return order, nil
}
func (s *orderService) UpdateInvoice(req request.UpdateInvoiceRequest) (*model.Order, error) {
order, err := s.GetOrderByToken(req.Token)
if err != nil {
return nil, err
}
invoice := order.Invoice
if invoice == nil {
invoice = &model.Invoice{}
}
invoice.OrderNo = order.OrderNo
invoice.Header = req.Header
invoice.TaxNumber = req.TaxNumber
err = global.DB.Save(&invoice).Error
if err != nil {
return nil, err
}
order.Invoice = invoice
return order, nil
}
func (s *orderService) UpdateOrderAfterPostToInsure(orderNo, payUrl string, orderSn string) {
global.DB.Model(&model.Order{}).Where("order_no", orderNo).UpdateColumns(model.Order{Status: "submitted", PayUrl: payUrl, OrderSn: orderSn})
}
func (s *orderService) UpdateOrderAfterClaim(orderNo string) {
global.DB.Model(&model.Order{}).Where("order_no", orderNo).UpdateColumns(model.Order{Status: "claimed"})
}
func (s *orderService) UpdateOrderAfterRevoke(orderNo string) {
global.DB.Model(&model.Order{}).Where("order_no", orderNo).UpdateColumns(model.Order{Status: "revoked"})
}
func (s *orderService) GetPayInfo(token string) (*response.PayInfoResponse, error) {
order, err := s.GetOrderByToken(token)
if err != nil {
return nil, err
}
if order.Status != "submitted" {
return nil, e.NewError("订单状态异常")
}
payInfo := response.PayInfoResponse{
TpID: order.Project.TpID,
PayAmount: order.Project.InsureAmount,
OrderSn: order.OrderSn,
OrderCreateTime: order.CreatedAt.Format("2006-01-02 15:04:05"),
ProductName: "投标保证保险",
Remark: order.Project.TpID,
Payee: response.Payee{
BankName: "中国工商银行福州下渡支行",
BankCardNum: "1402024519601002227",
BankUserName: "紫金财产保险股份有限公司福建分公司",
},
Payer: response.Payer{
BankName: order.User.BankName,
BankCardNum: order.User.BankCardNum,
BankUserName: order.User.BankUserName,
},
}
return &payInfo, nil
}