298 lines
8.3 KiB
Go
298 lines
8.3 KiB
Go
package supplier
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"fonchain-fiee/api/supplier"
|
|
"fonchain-fiee/pkg/logic"
|
|
"fonchain-fiee/pkg/model/login"
|
|
"fonchain-fiee/pkg/service"
|
|
"fonchain-fiee/pkg/service/upload"
|
|
"fonchain-fiee/pkg/utils/excel"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin/binding"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func GetSupplier(c *gin.Context) {
|
|
req := &supplier.GetSupplierRequest{}
|
|
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
|
service.Error(c, err)
|
|
return
|
|
}
|
|
res, err := service.SupplierProvider.GetSupplier(c, req)
|
|
if err != nil {
|
|
service.Error(c, err)
|
|
return
|
|
}
|
|
|
|
service.Success(c, res)
|
|
return
|
|
}
|
|
func CreateSupplier(c *gin.Context) {
|
|
req := &supplier.CreateSupplierRequest{}
|
|
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
|
service.Error(c, err)
|
|
return
|
|
}
|
|
res, err := service.SupplierProvider.CreateSupplier(c, req)
|
|
if err != nil {
|
|
service.Error(c, err)
|
|
return
|
|
}
|
|
service.Success(c, res)
|
|
return
|
|
}
|
|
|
|
func UpdateSupplier(c *gin.Context) {
|
|
req := &supplier.UpdateSupplierRequest{}
|
|
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
|
service.Error(c, err)
|
|
return
|
|
}
|
|
info, err := service.SupplierProvider.GetSupplier(c, &supplier.GetSupplierRequest{Id: req.Id})
|
|
if err != nil {
|
|
service.Error(c, err)
|
|
return
|
|
}
|
|
if info.Status == 3 {
|
|
service.Error(c, errors.New("审核中不可修改")) //todo 修改:审核中并且存在审核人 不可修改
|
|
return
|
|
}
|
|
if req.Status != 1 && req.Status != 2 {
|
|
service.Error(c, errors.New("审批状态错误")) //todo 只允许前端暂存或提交审核申请
|
|
}
|
|
res, err := service.SupplierProvider.UpdateSupplier(c, req)
|
|
if err != nil {
|
|
service.Error(c, err)
|
|
return
|
|
}
|
|
service.Success(c, res)
|
|
return
|
|
}
|
|
|
|
func GetSupplierList(c *gin.Context) {
|
|
req := &supplier.GetSupplierListRequest{}
|
|
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
|
service.Error(c, err)
|
|
return
|
|
}
|
|
res, err := service.SupplierProvider.GetSupplierList(c, req)
|
|
if err != nil {
|
|
service.Error(c, err)
|
|
return
|
|
}
|
|
service.Success(c, res)
|
|
return
|
|
}
|
|
|
|
func CreateOrganizeDictionary(c *gin.Context) {
|
|
req := &supplier.CreateOrganizeDictionaryRequest{}
|
|
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
|
service.Error(c, err)
|
|
return
|
|
}
|
|
res, err := service.SupplierProvider.CreateOrganizeDictionary(c, req)
|
|
if err != nil {
|
|
service.Error(c, err)
|
|
return
|
|
}
|
|
service.Success(c, res)
|
|
return
|
|
}
|
|
|
|
func GetOrganizeDictionaryList(c *gin.Context) {
|
|
req := &supplier.GetOrganizeDictionaryListRequest{}
|
|
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
|
service.Error(c, err)
|
|
return
|
|
}
|
|
res, err := service.SupplierProvider.GetOrganizeDictionaryList(c, req)
|
|
if err != nil {
|
|
service.Error(c, err)
|
|
return
|
|
}
|
|
service.Success(c, res)
|
|
return
|
|
}
|
|
|
|
func GetCountryRegionList(c *gin.Context) {
|
|
req := &supplier.GetCountryRegionListRequest{}
|
|
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
|
service.Error(c, err)
|
|
return
|
|
}
|
|
res, err := service.SupplierProvider.GetCountryRegionList(c, req)
|
|
if err != nil {
|
|
service.Error(c, err)
|
|
return
|
|
}
|
|
service.Success(c, res)
|
|
return
|
|
}
|
|
|
|
func CreateImportRecord(c *gin.Context) {
|
|
file, err := c.FormFile("file")
|
|
if err != nil {
|
|
service.Error(c, err)
|
|
}
|
|
safeFilename := fmt.Sprintf("%d", time.Now().Unix()) + "_" + file.Filename
|
|
userInfo := login.GetUserInfoFromC(c)
|
|
fileDir := fmt.Sprintf("/fiee/supplier/%s/%s_%s", time.Now().Format("2006-01-02"), userInfo.Name, safeFilename)
|
|
// 保存上传的文件到本地
|
|
if err = c.SaveUploadedFile(file, safeFilename); err != nil {
|
|
service.Error(c, err)
|
|
return
|
|
}
|
|
defer func() {
|
|
if err := os.Remove(safeFilename); err != nil {
|
|
// 处理删除文件失败的情况
|
|
fmt.Println("Failed to delete file:", err)
|
|
}
|
|
}()
|
|
inputUrl, err := upload.PutBosWithName(safeFilename, false, fileDir)
|
|
if err != nil {
|
|
service.Error(c, err)
|
|
return
|
|
}
|
|
supplierList, err := logic.ImportSupplier(safeFilename)
|
|
if err != nil {
|
|
service.Error(c, err)
|
|
return
|
|
}
|
|
if len(supplierList) == 0 {
|
|
service.Error(c, errors.New("导入数据为空"))
|
|
return
|
|
}
|
|
uuid, _ := uuid.NewUUID()
|
|
var SuccessNum uint64
|
|
var SupplierCodes []string
|
|
var ErrSupplierRes []*logic.ErrSupplierRes
|
|
for idx, i := range supplierList {
|
|
rowNum := idx + 1
|
|
if i.OwningEntityName == "" || i.LegalName == "" || i.CountryOrRegionName == "" || i.CompanyRegistrationNumber == "" || i.SupplierType == "" || i.ApprovalStatus == "" {
|
|
ErrSupplierRes = append(ErrSupplierRes, &logic.ErrSupplierRes{
|
|
ID: rowNum,
|
|
LegalName: i.LegalName,
|
|
Remark: "必填项存在空值",
|
|
})
|
|
continue
|
|
}
|
|
OwningEntityRes, _ := service.SupplierProvider.GetOrganizeDictionaryInfo(c, &supplier.CreateOrganizeDictionaryRequest{Name: i.OwningEntityName})
|
|
if OwningEntityRes == nil || OwningEntityRes.Id == 0 {
|
|
ErrSupplierRes = append(ErrSupplierRes, &logic.ErrSupplierRes{
|
|
ID: rowNum,
|
|
LegalName: i.LegalName,
|
|
Remark: "所属组织不存在",
|
|
})
|
|
continue
|
|
}
|
|
countryRegionRes, _ := service.SupplierProvider.GetCountryRegionInfo(c, &supplier.GetCountryRegionInfoRequest{ZhAndCode: i.CountryOrRegionName})
|
|
if countryRegionRes == nil || countryRegionRes.Id == 0 {
|
|
ErrSupplierRes = append(ErrSupplierRes, &logic.ErrSupplierRes{
|
|
ID: rowNum,
|
|
LegalName: i.LegalName,
|
|
Remark: "所属国家或地区不存在",
|
|
})
|
|
continue
|
|
}
|
|
var LastReviewDate string
|
|
if i.ApprovalDate != "" {
|
|
t, err := time.Parse("2006-01-02", i.ApprovalDate)
|
|
if err != nil {
|
|
LastReviewDate = ""
|
|
i.ApprovalDate = ""
|
|
} else {
|
|
LastReviewDate = t.AddDate(0, 1, 0).Format("2006-01-02")
|
|
}
|
|
}
|
|
createRes, err := service.SupplierProvider.CreateSupplier(c, &supplier.CreateSupplierRequest{
|
|
OwningEntityId: OwningEntityRes.Id,
|
|
LegalName: i.LegalName,
|
|
LocalName: i.LocalName,
|
|
AbbreviationName: i.AbbreviationName,
|
|
CountryOrRegionId: countryRegionRes.Id,
|
|
CompanyRegistrationNumber: i.CompanyRegistrationNumber,
|
|
SupplierType: i.SupplierType,
|
|
ApprovalStatus: i.ApprovalStatus,
|
|
ApprovalDate: i.ApprovalDate,
|
|
LastReviewDate: LastReviewDate,
|
|
LegalEntityType: i.LegalEntityType,
|
|
SanctionsCountryScreeningResult: i.SanctionsCountryScreeningResult,
|
|
KeyFinancial: i.KeyFinancial,
|
|
CompanyAddress: i.CompanyAddress,
|
|
PrimaryContact: i.PrimaryContact,
|
|
DataOwnerDepartment: i.DataOwnerDepartment,
|
|
BasicCompanyInformation: i.BasicCompanyInformation,
|
|
SupplementaryText: i.SupplementaryText,
|
|
Status: 1,
|
|
})
|
|
if err != nil {
|
|
ErrSupplierRes = append(ErrSupplierRes, &logic.ErrSupplierRes{
|
|
ID: rowNum,
|
|
LegalName: i.LegalName,
|
|
Remark: err.Error(),
|
|
})
|
|
continue
|
|
} else {
|
|
SupplierCodes = append(SupplierCodes, createRes.SupplierCode)
|
|
SuccessNum++
|
|
}
|
|
|
|
}
|
|
status := 2
|
|
outUrl := ""
|
|
if len(ErrSupplierRes) == 0 {
|
|
status = 1
|
|
} else {
|
|
var sheet1Columns = []string{"行数", "供应商法定全称", "错误信息"}
|
|
var sheet1 = excel.NewSheet("Sheet1", ErrSupplierRes, sheet1Columns)
|
|
ex, err := excel.NewExcelCreatorFromTemplate("./mistake.xlsx", "./", "", sheet1)
|
|
ex.UseOption(excel.OptionFileNameSuffixWithUnixTime)
|
|
path, _, err := ex.WriteToFile()
|
|
outUrl, err = upload.PutBos(path, "", false)
|
|
if err != nil {
|
|
service.Error(c, err)
|
|
return
|
|
}
|
|
defer func() {
|
|
os.Remove(path)
|
|
}()
|
|
}
|
|
req := &supplier.CreateImportRecordRequest{
|
|
SupplierCodes: SupplierCodes,
|
|
Status: uint64(status),
|
|
UserId: userInfo.ID,
|
|
ToLeadUrl: inputUrl,
|
|
DeriveUrl: outUrl,
|
|
Uuid: uuid.String(),
|
|
SuccessNum: SuccessNum,
|
|
FailNum: uint64(len(ErrSupplierRes)),
|
|
}
|
|
_, err = service.SupplierProvider.CreateImportRecord(c, req)
|
|
if err != nil {
|
|
service.Error(c, err)
|
|
return
|
|
}
|
|
service.Success(c, req)
|
|
return
|
|
}
|
|
|
|
func GetImportRecordInfo(c *gin.Context) {
|
|
req := &supplier.GetImportRecordRequest{}
|
|
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
|
service.Error(c, err)
|
|
return
|
|
}
|
|
res, err := service.SupplierProvider.GetImportRecordInfo(c, req)
|
|
if err != nil {
|
|
service.Error(c, err)
|
|
return
|
|
}
|
|
service.Success(c, res)
|
|
return
|
|
}
|