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.
798 lines
23 KiB
Go
798 lines
23 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/xuri/excelize/v2"
|
|
"gorm.io/gorm"
|
|
"insure/dto/request"
|
|
"insure/dto/request/manage"
|
|
"insure/e"
|
|
"insure/global"
|
|
"insure/model"
|
|
"insure/third_party/signer"
|
|
"insure/third_party/suixing"
|
|
"insure/third_party/zking"
|
|
"insure/utils"
|
|
"insure/utils/excel"
|
|
"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) {
|
|
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.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) 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) 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) {
|
|
fileList, err := json.Marshal(param.Data.FileList)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
revoke := model.Revoke{}
|
|
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.SuixingClaim(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) {
|
|
fileList, err := json.Marshal(param.Data.FileList)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
claim := model.Claim{}
|
|
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) 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
|
|
tx := s.buildQuery(req)
|
|
tx.Preload("User").
|
|
Preload("Project").
|
|
Preload("Applicant").
|
|
Preload("Assured").
|
|
Preload("OrderResult").
|
|
Order("id desc").Offset(offset).Limit(req.PageSize).Find(&orders)
|
|
tx.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.AssuredName != "" {
|
|
var orderSns []string
|
|
global.DB.Model(&model.Assured{}).Where("assured_name", req.AssuredName).Pluck("order_sn", &orderSns)
|
|
tx = tx.Where("order_sn", 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", 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", 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", 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", 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", 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", 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", 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", 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", 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", 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", orderSns)
|
|
}
|
|
return tx
|
|
}
|
|
|
|
func (s *orderService) GenerateOrdersExcel(req manage.OrderQueryRequest) *excelize.File {
|
|
pageSize := 200
|
|
lastID := 0
|
|
tx := s.buildQuery(req)
|
|
|
|
f := excel.NewFile()
|
|
headers := []string{"订单号", "项目编号", "项目名称", "招标人名称", "招标联系人", "招标人身份证", "招标联系手机", "投标人名称", "投标联系人", "投标人身份证", "投标联系手机", "保单号", "下单时间"}
|
|
excel.SetSimpleHeaders(headers, "Sheet1", f)
|
|
for {
|
|
orders := make([]model.Order, pageSize)
|
|
tx.Preload("User").
|
|
Preload("Project").
|
|
Preload("Applicant").
|
|
Preload("Assured").Where("id > ?", lastID).Limit(pageSize).Find(&orders)
|
|
count := len(orders)
|
|
if count == 0 {
|
|
break
|
|
}
|
|
s.buildOrdersExcel(orders, "Sheet1", f)
|
|
lastIndex := count - 1
|
|
lastID = orders[lastIndex].ID
|
|
}
|
|
return f
|
|
}
|
|
|
|
func (orderService) buildOrdersExcel(orders []model.Order, sheet string, f *excelize.File) {
|
|
row := 2
|
|
for _, order := range orders {
|
|
col := 0
|
|
policyNo := ""
|
|
if order.OrderResult != nil {
|
|
policyNo = order.OrderResult.PolicyNo
|
|
}
|
|
f.SetCellValue(sheet, excel.CellKey(row, &col), order.OrderSn)
|
|
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.Assured.AssuredName)
|
|
f.SetCellValue(sheet, excel.CellKey(row, &col), order.Assured.ContactName)
|
|
f.SetCellValue(sheet, excel.CellKey(row, &col), order.Assured.CreditCode)
|
|
f.SetCellValue(sheet, excel.CellKey(row, &col), order.Assured.ContactMobile)
|
|
f.SetCellValue(sheet, excel.CellKey(row, &col), order.Applicant.ApplicantName)
|
|
f.SetCellValue(sheet, excel.CellKey(row, &col), order.Applicant.ContactName)
|
|
f.SetCellValue(sheet, excel.CellKey(row, &col), order.Applicant.CreditCode)
|
|
f.SetCellValue(sheet, excel.CellKey(row, &col), order.Applicant.ContactMobile)
|
|
f.SetCellValue(sheet, excel.CellKey(row, &col), policyNo)
|
|
f.SetCellValue(sheet, excel.CellKey(row, &col), order.CreatedAt.Format(time.DateTime))
|
|
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})
|
|
}
|