791 lines
20 KiB
Go
791 lines
20 KiB
Go
package order
|
||
|
||
import (
|
||
"crypto/sha256"
|
||
"errors"
|
||
"fmt"
|
||
"github.com/fonchain_enterprise/fonchain-main/api/account"
|
||
"github.com/fonchain_enterprise/fonchain-main/api/order"
|
||
"github.com/fonchain_enterprise/fonchain-main/pkg/e"
|
||
"github.com/fonchain_enterprise/fonchain-main/pkg/model/login"
|
||
"github.com/fonchain_enterprise/fonchain-main/pkg/service"
|
||
"github.com/gin-gonic/gin"
|
||
"github.com/gin-gonic/gin/binding"
|
||
"github.com/shopspring/decimal"
|
||
"github.com/tealeg/xlsx"
|
||
"io/ioutil"
|
||
"net/url"
|
||
"os"
|
||
"strconv"
|
||
"strings"
|
||
"time"
|
||
)
|
||
|
||
func GetWeeklyStaffList(c *gin.Context) {
|
||
req := &order.StaffWeeklyListRequest{}
|
||
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
||
service.Error(c, e.InvalidParams, err)
|
||
return
|
||
}
|
||
weeklyReq := &order.StaffWeeklyListRequest{
|
||
StartDate: req.StartDate,
|
||
EndDate: req.EndDate,
|
||
Status: 1,
|
||
//UserIds: userIds.UserIds,
|
||
UserName: req.UserName,
|
||
SiteName: req.SiteName,
|
||
JobNumber: req.JobNumber,
|
||
Page: req.Page,
|
||
PageSize: req.PageSize,
|
||
}
|
||
res, err := service.OrderProvider.StaffWeeklyList(c, weeklyReq)
|
||
if err != nil {
|
||
service.Error(c, e.InvalidParams, err)
|
||
return
|
||
}
|
||
service.Success(c, res)
|
||
return
|
||
}
|
||
func GetWeeklyStaffExcel(c *gin.Context) {
|
||
req := &order.StaffWeeklyListRequest{}
|
||
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
||
service.Error(c, e.InvalidParams, err)
|
||
return
|
||
}
|
||
weeklyReq := &order.StaffWeeklyListRequest{
|
||
StartDate: req.StartDate,
|
||
EndDate: req.EndDate,
|
||
Status: 1,
|
||
UserName: req.UserName,
|
||
SiteName: req.SiteName,
|
||
JobNumber: req.JobNumber,
|
||
}
|
||
res, err := service.OrderProvider.StaffWeeklyList(c, weeklyReq)
|
||
if err != nil {
|
||
service.Error(c, e.InvalidParams, err)
|
||
return
|
||
}
|
||
|
||
// 创建临时文件
|
||
tmpfile, err := ioutil.TempFile("", "StaffWeeklyReport_*.xlsx")
|
||
if err != nil {
|
||
service.Error(c, e.Error, err)
|
||
return
|
||
}
|
||
// 将数据写入 Excel
|
||
err = CreateWeeklyStaffExcel(res, tmpfile)
|
||
if err != nil {
|
||
service.Error(c, e.Error, err)
|
||
return
|
||
}
|
||
defer os.Remove(tmpfile.Name()) // 删除临时文件
|
||
|
||
// 返回文件流
|
||
fileName := "员工汇报.xlsx"
|
||
escapedFileName := url.QueryEscape(fileName)
|
||
c.Header("Content-Disposition", "attachment; filename*=UTF-8''"+escapedFileName)
|
||
c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
||
c.File(tmpfile.Name())
|
||
}
|
||
func GetWeeklyBossList(c *gin.Context) {
|
||
req := &order.WeeklyListRequest{}
|
||
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
||
service.Error(c, e.InvalidParams, err)
|
||
return
|
||
}
|
||
if len(req.Status) != 0 {
|
||
for _, i := range req.Status {
|
||
if i == 1 {
|
||
req.Status = append(req.Status, 2)
|
||
}
|
||
}
|
||
} else {
|
||
req.Status = append(req.Status, 1, 2, 3)
|
||
}
|
||
res, err := service.OrderProvider.WeeklyList(c, req)
|
||
if err != nil {
|
||
service.Error(c, e.InvalidParams, err)
|
||
return
|
||
}
|
||
service.Success(c, res)
|
||
return
|
||
}
|
||
func GetWeeklyBossInfo(c *gin.Context) {
|
||
type WeeklyInfoRequest struct {
|
||
Id uint64 `json:"id"`
|
||
}
|
||
req := &WeeklyInfoRequest{}
|
||
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
||
service.Error(c, e.InvalidParams, err)
|
||
return
|
||
}
|
||
infoReq := &order.WeeklyInfoRequest{
|
||
Id: strconv.FormatUint(req.Id, 10),
|
||
}
|
||
res, err := service.OrderProvider.WeeklyInfo(c, infoReq)
|
||
if err != nil {
|
||
service.Error(c, e.InvalidParams, err)
|
||
return
|
||
}
|
||
service.Success(c, res)
|
||
return
|
||
}
|
||
func GetWeeklyComment(c *gin.Context) {
|
||
req := &order.GetWeeklyCommentRequest{}
|
||
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
||
service.Error(c, e.InvalidParams, err)
|
||
return
|
||
}
|
||
if req.WeeklyId == 0 {
|
||
service.Error(c, e.InvalidParams, errors.New("id不能为空"))
|
||
return
|
||
}
|
||
res, err := service.OrderProvider.GetWeeklyComment(c, req)
|
||
if err != nil {
|
||
service.Error(c, e.InvalidParams, err)
|
||
return
|
||
}
|
||
service.Success(c, res)
|
||
return
|
||
}
|
||
func CreateWeeklyComment(c *gin.Context) {
|
||
req := &order.WeeklyCommentRequest{}
|
||
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
||
service.Error(c, e.InvalidParams, err)
|
||
return
|
||
}
|
||
userInfo := login.GetUserInfoFromC(c)
|
||
req.UserId = userInfo.ID
|
||
req.UserName = userInfo.NickName
|
||
req.ReportTime = time.Now().Format("2006-01-02 15:04:05")
|
||
if req.WeeklyId == 0 {
|
||
service.Error(c, e.InvalidParams, errors.New("id不能为空"))
|
||
return
|
||
}
|
||
res, err := service.OrderProvider.CreateWeeklyComment(c, req)
|
||
if err != nil {
|
||
service.Error(c, e.InvalidParams, err)
|
||
return
|
||
}
|
||
service.Success(c, res)
|
||
return
|
||
}
|
||
|
||
// 将数据写入 Excel 文件
|
||
func CreateWeeklyStaffExcel(data *order.StaffWeeklyResponseList, file *os.File) error {
|
||
// 创建 Excel 文件
|
||
xlsFile := xlsx.NewFile()
|
||
sheet, err := xlsFile.AddSheet("员工汇报列表")
|
||
if err != nil {
|
||
return err
|
||
}
|
||
// 添加表头
|
||
row := sheet.AddRow()
|
||
row.AddCell().SetValue("姓名")
|
||
row.AddCell().SetValue("工号")
|
||
row.AddCell().SetValue("站点")
|
||
row.AddCell().SetValue("提交日期")
|
||
row.AddCell().SetValue("当前业务金额")
|
||
// 将数据写入 Excel
|
||
for _, req := range data.WeeklyList {
|
||
row := sheet.AddRow()
|
||
row.AddCell().SetValue(req.StaffName)
|
||
row.AddCell().SetValue(req.JobNumber)
|
||
row.AddCell().SetValue(req.SiteName)
|
||
row.AddCell().SetValue(req.FillingDate)
|
||
row.AddCell().SetValue(req.BusinessAmount)
|
||
}
|
||
// 保存 Excel 文件
|
||
err = xlsFile.Write(file)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
return nil
|
||
}
|
||
func GetWeeklyStaffInfo(c *gin.Context) {
|
||
req := &order.ReportUserDetail{}
|
||
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
||
service.Error(c, e.InvalidParams, err)
|
||
return
|
||
}
|
||
infoReq := &order.ReportUserDetail{
|
||
ID: req.ID,
|
||
}
|
||
res, err := service.OrderProvider.StaffWeeklyInfo(c, infoReq)
|
||
if err != nil {
|
||
service.Error(c, e.InvalidParams, err)
|
||
return
|
||
}
|
||
service.Success(c, res)
|
||
return
|
||
}
|
||
|
||
// GetEntrust 提交的报表列表
|
||
func GetEntrust(c *gin.Context) {
|
||
req := &order.CommonRequest{}
|
||
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
||
service.Error(c, e.InvalidParams, err)
|
||
return
|
||
}
|
||
|
||
res, err := service.OrderProvider.GetEntrust(c, req)
|
||
if err != nil {
|
||
service.Error(c, e.Error, err)
|
||
return
|
||
}
|
||
|
||
service.Success(c, res)
|
||
return
|
||
}
|
||
|
||
func EntrustDelete(c *gin.Context) {
|
||
req := &order.CommonRequest{}
|
||
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
||
service.Error(c, e.InvalidParams, err)
|
||
return
|
||
}
|
||
|
||
res, err := service.OrderProvider.EntrustDelete(c, req)
|
||
if err != nil {
|
||
service.Error(c, e.Error, err)
|
||
return
|
||
}
|
||
|
||
service.Success(c, res)
|
||
return
|
||
}
|
||
|
||
func EntrustBatchDelete(c *gin.Context) {
|
||
req := &order.UpDateOrderEntrustsRequest{}
|
||
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
||
service.Error(c, e.InvalidParams, err)
|
||
return
|
||
}
|
||
res, err := service.OrderProvider.EntrustBatchDelete(c, req)
|
||
if err != nil {
|
||
service.Error(c, e.Error, err)
|
||
return
|
||
}
|
||
|
||
service.Success(c, res)
|
||
return
|
||
}
|
||
func EntrustCreate(c *gin.Context) {
|
||
req := &order.EntrustRequest{}
|
||
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
||
service.Error(c, e.InvalidParams, err)
|
||
return
|
||
}
|
||
|
||
res, err := service.OrderProvider.EntrustCreate(c, req)
|
||
if err != nil {
|
||
service.Error(c, e.Error, err)
|
||
return
|
||
}
|
||
|
||
service.Success(c, res)
|
||
return
|
||
}
|
||
|
||
// GetEntrusts 列表
|
||
func GetEntrusts(c *gin.Context) {
|
||
req := &order.EntrustList{}
|
||
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
||
service.Error(c, e.InvalidParams, err)
|
||
return
|
||
}
|
||
|
||
res, err := service.OrderProvider.GetEntrusts(c, req)
|
||
if err != nil {
|
||
service.Error(c, e.Error, err)
|
||
return
|
||
}
|
||
|
||
service.Success(c, res)
|
||
return
|
||
}
|
||
|
||
// ImportEntrusts 导入
|
||
func ImportEntrusts(c *gin.Context) {
|
||
|
||
file, err := c.FormFile("file")
|
||
if err != nil {
|
||
service.Error(c, e.Error, err)
|
||
}
|
||
|
||
c.SaveUploadedFile(file, file.Filename)
|
||
|
||
err = importReal(c, file.Filename)
|
||
|
||
if err != nil {
|
||
service.Error(c, e.Error, err)
|
||
return
|
||
}
|
||
|
||
service.Success(c, nil)
|
||
return
|
||
}
|
||
|
||
func isNum(s string) bool {
|
||
_, err := strconv.ParseFloat(s, 64)
|
||
return err == nil
|
||
}
|
||
|
||
func importReal(c *gin.Context, filename string) error {
|
||
err := ExcelParse(c, filename)
|
||
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
/*
|
||
if len(list) >= 2 {
|
||
entrusts := getListFromRaw(list)
|
||
if len(entrusts) > 0 {
|
||
req := &order.EntrustListResponse{
|
||
Data: entrusts,
|
||
}
|
||
fmt.Println("1--------", req)
|
||
_, err := service.OrderProvider.EntrustCreates(c, req)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
}
|
||
}
|
||
*/
|
||
|
||
return nil
|
||
}
|
||
|
||
func getListFromRaw(list []map[int]string) ([]*order.EntrustRequest, error) {
|
||
|
||
var entrusts []*order.EntrustRequest
|
||
|
||
kkMap := map[string]string{
|
||
"序号": "index",
|
||
"委托人": "clientName",
|
||
"客户服务编码": "serviceCode",
|
||
"是否关联员工": "associatedEmployee",
|
||
"身份证后8位": "clientId",
|
||
"联系电话": "clientTel",
|
||
"开户行": "bank",
|
||
"银行账号": "bankNo",
|
||
"委托申请单号": "entrustNo",
|
||
"画家": "artistName",
|
||
"画作编号": "artworkNum",
|
||
"版权编号": "artworkNum",
|
||
"画作名称": "artworkName",
|
||
"画作尺寸(平尺)": "artworkSize",
|
||
"成交价格": "price",
|
||
"委托价格最低": "entrustPriceLow",
|
||
"委托价格(最低价)": "entrustPriceLow",
|
||
"委托价格(最低价)": "entrustPriceLow",
|
||
"委托价格(最高价)": "entrustPrice",
|
||
"委托价格(最高价)": "entrustPrice",
|
||
"服务费最低": "serviceChargeLow",
|
||
"服务费最低金额": "serviceChargeLow",
|
||
"服务费(最低金额)": "serviceChargeLow",
|
||
"服务费最高": "serviceCharge",
|
||
"服务费最高金额": "serviceCharge",
|
||
"服务费(最高金额)": "serviceCharge",
|
||
"个税最低": "personTaxLow",
|
||
"代扣个税最低金额": "personTaxLow",
|
||
"代扣个税(最低金额)": "personTaxLow",
|
||
"代扣个税最低": "personTaxLow",
|
||
"代扣个税最高": "personTax",
|
||
"个税最高": "personTax",
|
||
"代扣个税最高金额": "personTax",
|
||
"代扣个税(最高金额)": "personTax",
|
||
"实际支付最低": "realPayLow",
|
||
"实际支付(最低金额)": "realPayLow",
|
||
"实际支付最低金额": "realPayLow",
|
||
"实际支付金额最高": "realPay",
|
||
"实际支付最高": "realPay",
|
||
"实际支付(最高金额)": "realPay",
|
||
"保真证明": "realProve",
|
||
"销售公司": "saleSite",
|
||
"委托人所属销售公司": "saleSite",
|
||
"业务员": "sellerName",
|
||
"日期": "optionDate",
|
||
"受委托期限开始日期": "optionStartDate",
|
||
"受委托期限结束日期": "optionEndDate",
|
||
"委托押金款付款日期": "entrustDate",
|
||
"是否有居间服务协议": "brokerage",
|
||
"备注": "remark",
|
||
"是否长期": "isLong",
|
||
"数据是否有误": "mistake",
|
||
}
|
||
|
||
keyMap := list[0]
|
||
fmt.Println("第一行头部", keyMap)
|
||
|
||
for index, tt := range list {
|
||
if index == 0 {
|
||
continue
|
||
}
|
||
|
||
temp := &order.EntrustRequest{}
|
||
|
||
fmt.Println("当前是行:", index)
|
||
fmt.Println(tt)
|
||
|
||
for i, r := range tt {
|
||
var err error
|
||
t := strings.TrimSpace(r)
|
||
|
||
if _, ok := keyMap[i]; !ok {
|
||
//fmt.Println("跳出", i)
|
||
continue
|
||
}
|
||
|
||
keyString := strings.TrimSpace(keyMap[i])
|
||
|
||
//fmt.Println("字段i:", i)
|
||
//fmt.Println("字段keystring", keyString)
|
||
//fmt.Println("字段内容", t)
|
||
if _, ok := kkMap[keyString]; !ok {
|
||
//return entrusts, errors.New(fmt.Sprintf("行数:%d字段信息(%s)没有匹配,请以模版为准", i, keyString))
|
||
fmt.Println(fmt.Sprintf("行数:%d字段信息(%s)没有匹配,请以模版为准", i, keyString))
|
||
continue
|
||
}
|
||
|
||
switch kkMap[keyString] {
|
||
|
||
case "index":
|
||
temp.Index = t
|
||
case "serviceCode":
|
||
temp.ServiceCode = t
|
||
case "clientName":
|
||
temp.ClientName = t
|
||
case "associatedEmployee":
|
||
temp.AssociatedEmployee = t
|
||
case "clientId":
|
||
temp.ClientId = t
|
||
case "clientTel":
|
||
temp.ClientTel = t
|
||
case "bank":
|
||
temp.Bank = t
|
||
case "bankNo":
|
||
temp.BankNo = t
|
||
case "entrustNo":
|
||
temp.EntrustNo = t
|
||
case "artistName":
|
||
temp.ArtistName = t
|
||
case "artworkNum":
|
||
temp.ArtworkNum = t
|
||
case "artworkName":
|
||
temp.ArtworkName = t
|
||
//case "artworkSize":
|
||
// temp.ArtworkSize, err = stringToNumString(t)
|
||
case "price":
|
||
temp.Price, err = stringToNumString(t)
|
||
case "entrustPriceLow":
|
||
temp.EntrustPriceLow, err = stringToNumString(t)
|
||
case "entrustPrice":
|
||
temp.EntrustPrice, err = stringToNumString(t)
|
||
case "serviceChargeLow":
|
||
temp.ServiceChargeLow, err = stringToNumString(t)
|
||
case "serviceCharge":
|
||
temp.ServiceCharge, err = stringToNumString(t)
|
||
case "personTaxLow":
|
||
temp.PersonTaxLow, err = stringToNumString(t)
|
||
case "personTax":
|
||
temp.PersonTax, err = stringToNumString(t)
|
||
case "realPayLow":
|
||
temp.RealPayLow, err = stringToNumString(t)
|
||
case "realPay":
|
||
temp.RealPay, err = stringToNumString(t)
|
||
case "realProve":
|
||
temp.RealProve = t
|
||
case "saleSite":
|
||
temp.SaleSite = t
|
||
case "sellerName":
|
||
temp.SellerName = t
|
||
case "optionDate":
|
||
temp.OptionDate = t
|
||
case "optionStartDate":
|
||
temp.OptionStartDate = TimeProcessing(t)
|
||
case "optionEndDate":
|
||
temp.OptionEndDate = TimeProcessing(t)
|
||
case "entrustDate":
|
||
//时间格式做处理
|
||
temp.EntrustDate = TimeProcessing(t)
|
||
|
||
case "brokerage":
|
||
temp.Brokerage = t
|
||
case "remark":
|
||
temp.Remark = t
|
||
case "isLong":
|
||
temp.IsLong = t
|
||
}
|
||
|
||
if err != nil {
|
||
return entrusts, errors.New(fmt.Sprintf("字段:%s提示错误:%s", keyString, err.Error()))
|
||
}
|
||
|
||
}
|
||
if temp.ClientName == "" {
|
||
//if !isNum(temp.Index) || temp.ClientName == "" {
|
||
continue
|
||
}
|
||
EntrustPriceNum, err1 := strconv.ParseFloat(temp.EntrustPrice, 64)
|
||
ServiceChargeNum, err2 := strconv.ParseFloat(temp.ServiceCharge, 64)
|
||
PriceNum, err3 := strconv.ParseFloat(temp.Price, 64)
|
||
PersonTaxNum, err4 := strconv.ParseFloat(temp.PersonTax, 64)
|
||
RealPayNum, err5 := strconv.ParseFloat(temp.RealPay, 64)
|
||
|
||
if err1 != nil || err2 != nil || err3 != nil || err4 != nil || err5 != nil {
|
||
temp.Mistake = "1"
|
||
fmt.Println("转换失败:", err1, err2, err3, err4, err5)
|
||
}
|
||
|
||
// 计算最高金额
|
||
var maxPersonTax int64
|
||
|
||
maxServiceCharge := int64(EntrustPriceNum) * 6 / 100
|
||
if temp.IsLong == "是" {
|
||
maxPersonTax = (int64(EntrustPriceNum) - int64(PriceNum) - maxServiceCharge) * 3 / 100
|
||
} else {
|
||
maxPersonTax = (int64(EntrustPriceNum) - int64(PriceNum) - maxServiceCharge) / 5
|
||
}
|
||
maxRealPay := int64(EntrustPriceNum) - maxServiceCharge - maxPersonTax
|
||
|
||
// 核对测算逻辑
|
||
if int64(EntrustPriceNum) != maxServiceCharge+maxPersonTax+maxRealPay ||
|
||
int64(ServiceChargeNum) != maxServiceCharge ||
|
||
int64(PersonTaxNum) != maxPersonTax ||
|
||
int64(RealPayNum) != maxRealPay {
|
||
temp.Mistake = "1"
|
||
} else {
|
||
temp.Mistake = "0"
|
||
}
|
||
temp.IsCopyright = 2
|
||
if find := strings.Contains(temp.ArtworkNum, "IP"); find {
|
||
temp.IsCopyright = 1
|
||
}
|
||
|
||
entrusts = append(entrusts, temp)
|
||
}
|
||
sha256.New()
|
||
fmt.Println("等待插入数据", entrusts)
|
||
return entrusts, nil
|
||
}
|
||
func TimeProcessing(t string) (res string) {
|
||
dayNum, err := strconv.Atoi(t)
|
||
if err == nil {
|
||
currentTime, _ := time.Parse("2006-01-02", "1899-12-31")
|
||
oldTime := currentTime.AddDate(0, 0, dayNum-1) //若要获取3天前的时间,则应将-2改为-3
|
||
res = oldTime.Format("2006-01-02")
|
||
} else {
|
||
times, err := time.Parse("01-02-06", t)
|
||
if err == nil {
|
||
res = times.Format("2006-01-02")
|
||
} else {
|
||
times, err := time.Parse("2006.1.2", t)
|
||
if err == nil {
|
||
res = times.Format("2006-01-02")
|
||
}
|
||
}
|
||
}
|
||
return res
|
||
}
|
||
|
||
// UpdateEntrustKeys 批量更新
|
||
func UpdateEntrustKeys(c *gin.Context) {
|
||
req := &order.EntrustListResponse{}
|
||
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
||
service.Error(c, e.InvalidParams, err)
|
||
return
|
||
}
|
||
|
||
res, err := service.OrderProvider.UpdateEntrustKeys(c, req)
|
||
if err != nil {
|
||
service.Error(c, e.Error, err)
|
||
return
|
||
}
|
||
|
||
service.Success(c, res)
|
||
return
|
||
}
|
||
|
||
// ExcelParseToList xlsx文件解析
|
||
func ExcelParseToList(filePath string) ([]map[int]string, error) {
|
||
//func ExcelParse(filePath string) ([][]string, error) {
|
||
//filePath := "upload/" + fileName
|
||
xlFile, err := xlsx.OpenFile(filePath)
|
||
|
||
if err != nil {
|
||
return []map[int]string{}, err
|
||
}
|
||
|
||
//开辟除表头外的行数的数组内存
|
||
//遍历sheet
|
||
var resourceArr []map[int]string
|
||
for _, sheet := range xlFile.Sheets {
|
||
//遍历每一行
|
||
//for rowIndex, row := range sheet.Rows {
|
||
for _, row := range sheet.Rows {
|
||
//跳过第一行表头信息
|
||
/*
|
||
if rowIndex == 0 {
|
||
continue
|
||
}
|
||
|
||
*/
|
||
//遍历每一个单元
|
||
|
||
//开辟除表头外的行数的数组内存
|
||
objMap := make(map[int]string)
|
||
|
||
if len(row.Cells) <= 0 || row.Cells[0].String() == "" {
|
||
continue
|
||
}
|
||
|
||
for cellIndex, cell := range row.Cells {
|
||
text := cell.String()
|
||
//如果是每一行的第一个单元格
|
||
objMap[cellIndex] = text
|
||
/*
|
||
if cellIndex == 0 {
|
||
resourceArr[rowIndex-1][cellIndex] = text
|
||
}
|
||
*/
|
||
}
|
||
resourceArr = append(resourceArr, objMap)
|
||
}
|
||
|
||
}
|
||
|
||
return resourceArr, nil
|
||
}
|
||
|
||
// ExcelParse xlsx文件解析
|
||
// func ExcelParse(filePath string) ([]map[int]string, error) {
|
||
func ExcelParse(c *gin.Context, filePath string) error {
|
||
//func ExcelParse(filePath string) ([][]string, error) {
|
||
//filePath := "upload/" + fileName
|
||
xlFile, err := xlsx.OpenFile(filePath)
|
||
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
//开辟除表头外的行数的数组内存
|
||
//遍历sheet
|
||
for sheetIndex, sheet := range xlFile.Sheets {
|
||
var resourceArr []map[int]string
|
||
|
||
//遍历每一行
|
||
//for rowIndex, row := range sheet.Rows {
|
||
for _, row := range sheet.Rows {
|
||
//跳过第一行表头信息
|
||
/*
|
||
if rowIndex == 0 {
|
||
continue
|
||
}
|
||
|
||
*/
|
||
//遍历每一个单元
|
||
|
||
//开辟除表头外的行数的数组内存
|
||
objMap := make(map[int]string)
|
||
|
||
if len(row.Cells) <= 0 || row.Cells[0].String() == "" {
|
||
continue
|
||
}
|
||
|
||
for cellIndex, cell := range row.Cells {
|
||
text := cell.String()
|
||
//如果是每一行的第一个单元格
|
||
objMap[cellIndex] = text
|
||
/*
|
||
if cellIndex == 0 {
|
||
resourceArr[rowIndex-1][cellIndex] = text
|
||
}
|
||
*/
|
||
}
|
||
resourceArr = append(resourceArr, objMap)
|
||
}
|
||
|
||
if len(resourceArr) >= 2 {
|
||
entrusts, err := getListFromRaw(resourceArr)
|
||
if err != nil {
|
||
return errors.New(fmt.Sprintf("页码:%d,文件读取错误信息%s", sheetIndex+1, err.Error()))
|
||
}
|
||
var sellerNames []string
|
||
if len(entrusts) > 0 {
|
||
for _, entrust := range entrusts {
|
||
sellerNames = append(sellerNames, entrust.SellerName)
|
||
}
|
||
sellerNameList, _ := service.AccountProvider.QueryPersonnelWithTheSameName(c, &account.QueryPersonnelWithTheSameNameRequest{
|
||
Names: sellerNames,
|
||
Domain: "fontree",
|
||
//Status: "notactive",
|
||
})
|
||
duplicateNameSet := make(map[string]struct{})
|
||
for _, name := range sellerNameList.Names {
|
||
duplicateNameSet[name] = struct{}{}
|
||
}
|
||
for i := range entrusts {
|
||
if _, exists := duplicateNameSet[entrusts[i].SellerName]; exists {
|
||
entrusts[i].DuplicateName = true
|
||
} else {
|
||
entrusts[i].DuplicateName = false
|
||
}
|
||
}
|
||
req := &order.EntrustListResponse{
|
||
Data: entrusts,
|
||
}
|
||
_, err = service.OrderProvider.EntrustCreates(c, req)
|
||
if err != nil {
|
||
return errors.New(fmt.Sprintf("页码:%d,数据生成错误信息%s", sheetIndex+1, err.Error()))
|
||
}
|
||
}
|
||
}
|
||
break
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
func stringToNumString(str string) (string, error) {
|
||
|
||
if str == "" {
|
||
return "0", nil
|
||
}
|
||
|
||
if str == "-" {
|
||
return "0", nil
|
||
}
|
||
|
||
num, err := decimal.NewFromString(str)
|
||
if err != nil {
|
||
return "0", err
|
||
}
|
||
roundedNum := num.Floor()
|
||
return roundedNum.String(), nil
|
||
|
||
}
|
||
|
||
func stringTofloat64(str string) (float64, error) {
|
||
|
||
if str == "" {
|
||
return 0, nil
|
||
}
|
||
|
||
vv, err := strconv.ParseFloat(str, 64)
|
||
|
||
if err != nil {
|
||
return 0, err
|
||
}
|
||
|
||
res, err := strconv.ParseFloat(fmt.Sprintf("%.2f", vv), 64)
|
||
|
||
if err != nil {
|
||
return 0, err
|
||
}
|
||
|
||
return res, nil
|
||
}
|