|
|
package controllers
|
|
|
|
|
|
import (
|
|
|
"crypto/md5"
|
|
|
"encoding/hex"
|
|
|
"encoding/json"
|
|
|
"errors"
|
|
|
"fmt"
|
...
|
...
|
@@ -618,11 +620,14 @@ func (c *OrderInfoController) ListOrderForExcel() { |
|
|
* @return
|
|
|
**/
|
|
|
func (c *OrderInfoController) ImportOrderFromExcel() {
|
|
|
// 获取参数
|
|
|
where := c.GetString("where")
|
|
|
file, h, _ := c.GetFile("file")
|
|
|
|
|
|
jsonMap := make(map[string]interface{})
|
|
|
companyId := c.GetUserCompany()
|
|
|
|
|
|
// Json数据解析
|
|
|
jsonMap := make(map[string]interface{})
|
|
|
err := json.Unmarshal([]byte(where), &jsonMap)
|
|
|
if err != nil {
|
|
|
logs.Error(err)
|
...
|
...
|
@@ -632,8 +637,7 @@ func (c *OrderInfoController) ImportOrderFromExcel() { |
|
|
// 返回字段定义
|
|
|
ret := map[string]interface{}{}
|
|
|
|
|
|
// 返回信息表头定义
|
|
|
// 0: 订单号, 1: 发货单号, 3: 客户名称, 3: 订单区域, 4: 编号, 5: 合伙人, 6: 类型, 7: 业务抽成比例, 8: 产品名称, 9: 数量, 10: 单价, 11: 合伙人分红比例
|
|
|
// 返回信息表头定义 0: 订单号, 1: 发货单号, 2: 客户名称, 3: 订单区域, 4: 编号, 5: 合伙人, 6: 类型, 7: 业务抽成比例, 8: 产品名称, 9: 数量, 10: 单价, 11: 合伙人分红比例
|
|
|
var tableHeader = []string{"错误详情", "行号", "订单号", "发货单号", "客户名称", "订单区域", "编号", "合伙人", "类型", "业务抽成比例", "产品名称", "数量", "单价", "合伙人分红比例"}
|
|
|
|
|
|
// 文件后缀名校验
|
...
|
...
|
@@ -642,14 +646,14 @@ func (c *OrderInfoController) ImportOrderFromExcel() { |
|
|
".xlsx": true,
|
|
|
}
|
|
|
if _, ok := AllowExtMap[ext]; !ok {
|
|
|
c.ResponseError(errors.New("文件后缀名不符合上传要求,请上传正确的文件"))
|
|
|
c.ResponseError(errors.New("文件后缀名不符合上传要求,请上传正确格式的文件"))
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// 打开文件
|
|
|
xlsx, err := excelize.OpenReader(file)
|
|
|
if err != nil {
|
|
|
c.ResponseError(errors.New("文件打开失败"))
|
|
|
c.ResponseError(errors.New("文件打开失败,请确定文件能够正常打开"))
|
|
|
return
|
|
|
}
|
|
|
|
...
|
...
|
@@ -669,13 +673,14 @@ func (c *OrderInfoController) ImportOrderFromExcel() { |
|
|
nullLine := make([]interface{}, 0)
|
|
|
nullFlag := false
|
|
|
for i, row := range rows {
|
|
|
if i > 2 {
|
|
|
if i > 2 && row != nil {
|
|
|
if len(row) == constant.EXCEL_COLUMN { // 中间空字符校验
|
|
|
var myRow = row
|
|
|
var tmpRow = row
|
|
|
var myRow []string
|
|
|
for j, cell := range row {
|
|
|
if j != 8 { // 业务员抽成比例不校验
|
|
|
if cell == "" || cell == " " { // 空字符串填充
|
|
|
myRow[j] = "null"
|
|
|
tmpRow[j] = "null"
|
|
|
nullFlag = true
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -683,14 +688,14 @@ func (c *OrderInfoController) ImportOrderFromExcel() { |
|
|
if nullFlag {
|
|
|
myRow = append(myRow, "必填项不能为空") // 错误信息
|
|
|
s := strconv.Itoa(i + 1)
|
|
|
myRow = append(myRow, s) // 行号
|
|
|
myRow = append(myRow, row...) // 错误行数据
|
|
|
myRow = append(myRow, s) // 行号
|
|
|
myRow = append(myRow, tmpRow...) // 错误行数据
|
|
|
nullLine = append(nullLine, myRow)
|
|
|
nullFlag = false
|
|
|
}
|
|
|
} else if len(row) < constant.EXCEL_COLUMN && len(row) > 0 { // 尾部空字符校验
|
|
|
} else if len(row) > 0 && len(row) < constant.EXCEL_COLUMN { // 尾部空字符校验
|
|
|
var myRow []string
|
|
|
for i := 0; i < constant.EXCEL_COLUMN-len(row); i++ {
|
|
|
for i := 0; i < constant.EXCEL_COLUMN-len(row); i++ { // null补位
|
|
|
myRow = append(myRow, "null")
|
|
|
}
|
|
|
myRow = append(myRow, "必填项不能为空") // 错误信息
|
...
|
...
|
@@ -702,6 +707,7 @@ func (c *OrderInfoController) ImportOrderFromExcel() { |
|
|
}
|
|
|
}
|
|
|
|
|
|
// 空单元格返回
|
|
|
if len(nullLine) > 0 {
|
|
|
ret = map[string]interface{}{
|
|
|
"successCount": 0,
|
...
|
...
|
@@ -714,11 +720,11 @@ func (c *OrderInfoController) ImportOrderFromExcel() { |
|
|
return
|
|
|
}
|
|
|
|
|
|
// 单元格长度、内容校验
|
|
|
// 内容校验
|
|
|
errorLine := make([]interface{}, 0)
|
|
|
var partnerType = []string{"事业合伙", "业务合伙", "研发合伙", "业务-产品应用合伙"}
|
|
|
for i, row := range rows {
|
|
|
if i > 2 && len(row) == constant.EXCEL_COLUMN { // 数据行
|
|
|
if i > 2 && row != nil && len(row) == constant.EXCEL_COLUMN { // 数据行
|
|
|
var myRow []string
|
|
|
for j, cell := range row {
|
|
|
switch j {
|
...
|
...
|
@@ -829,10 +835,13 @@ func (c *OrderInfoController) ImportOrderFromExcel() { |
|
|
}
|
|
|
}
|
|
|
}
|
|
|
errorLine = append(errorLine, myRow)
|
|
|
if myRow != nil {
|
|
|
errorLine = append(errorLine, myRow)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 内容错误行返回
|
|
|
if len(errorLine) > 0 {
|
|
|
ret = map[string]interface{}{
|
|
|
"successCount": 0,
|
...
|
...
|
@@ -845,20 +854,134 @@ func (c *OrderInfoController) ImportOrderFromExcel() { |
|
|
return
|
|
|
}
|
|
|
|
|
|
// 归类订单
|
|
|
// 创建订单服务
|
|
|
orderSrv := orderService.NewOrderInfoService(nil)
|
|
|
|
|
|
// 聚合订单产品
|
|
|
var orderCommands = make(map[string]*orderCmd.CreateOrderCommand, 0)
|
|
|
for i, row := range rows {
|
|
|
if i > 2 && len(row) == 13 {
|
|
|
if i > 2 && len(row) == constant.EXCEL_COLUMN {
|
|
|
hashValue := md5.Sum([]byte(row[0] + row[1] + row[4] + row[6])) // 根据:订单号+发货单号+合伙人编号+合伙类型计算哈希值
|
|
|
hashString := hex.EncodeToString(hashValue[:])
|
|
|
|
|
|
if _, ok := orderCommands[hashString]; !ok {
|
|
|
//订单相关,0: 订单号, 1: 发货单号, 2: 客户名称, 3: 订单区域, 4: 编号, 5: 合伙人, 6: 类型, 7: 业务抽成比例,
|
|
|
sbPercent, _ := strconv.ParseFloat(row[7], 64) //业务抽成比例
|
|
|
|
|
|
//产品相关,8: 产品名称, 9: 数量, 10: 单价, 11: 合伙人分红比例
|
|
|
amount, _ := strconv.ParseInt(row[9], 10, 64) // 数量
|
|
|
price, _ := strconv.ParseFloat(row[10], 64) // 单价
|
|
|
percent, _ := strconv.ParseFloat(row[11], 64) // 合伙人分红比例
|
|
|
|
|
|
// 初始化建订单命令集
|
|
|
orderCommands[hashString] = &orderCmd.CreateOrderCommand{
|
|
|
OrderType: 0,
|
|
|
OrderCode: row[0],
|
|
|
DeliveryCode: row[1],
|
|
|
BuyerName: row[2],
|
|
|
OrderRegion: row[3],
|
|
|
PartnerId: 0, // 根据合伙人类型+合伙人编号查找合伙人id
|
|
|
SalesmanBonusPercent: sbPercent,
|
|
|
Goods: []orderCmd.OrderGoodData{
|
|
|
{
|
|
|
GoodName: row[8],
|
|
|
PlanGoodNumber: int(amount),
|
|
|
Price: price,
|
|
|
PartnerBonusPercent: percent,
|
|
|
},
|
|
|
},
|
|
|
CompanyId: companyId,
|
|
|
PartnerCategory: 1,
|
|
|
LineNumbers: []int{i},
|
|
|
}
|
|
|
|
|
|
// 获取partnerId
|
|
|
var partnerInfo *domain.PartnerInfo
|
|
|
partnerInfo, err = orderSrv.GetPartnerIdByCodeAndCategory(orderQuery.GetPartnerIdQuery{
|
|
|
Code: row[4],
|
|
|
PartnerCategory: 0,
|
|
|
CompanyId: companyId,
|
|
|
})
|
|
|
if err != nil {
|
|
|
|
|
|
}
|
|
|
if partnerInfo != nil {
|
|
|
orderCommands[hashString].PartnerId = partnerInfo.Partner.Id
|
|
|
// 1: 事业合伙、2: 业务合伙、3: 研发合伙、4: 业务-产品应用合伙
|
|
|
switch row[6] {
|
|
|
case "事业合伙":
|
|
|
partnerInfo.PartnerCategory = 1
|
|
|
case "业务合伙":
|
|
|
partnerInfo.PartnerCategory = 2
|
|
|
case "研发合伙":
|
|
|
partnerInfo.PartnerCategory = 3
|
|
|
case "业务-产品应用合伙":
|
|
|
partnerInfo.PartnerCategory = 4
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
//产品相关,8: 产品名称, 9: 数量, 10: 单价, 11: 合伙人分红比例
|
|
|
amount, _ := strconv.ParseInt(row[9], 10, 64) // 数量
|
|
|
price, _ := strconv.ParseFloat(row[10], 64) // 单价
|
|
|
percent, _ := strconv.ParseFloat(row[11], 64) // 合伙人分红比例
|
|
|
|
|
|
// 记录同一笔订单产品
|
|
|
orderCommands[hashString].Goods = append(orderCommands[hashString].Goods, orderCmd.OrderGoodData{
|
|
|
GoodName: row[8],
|
|
|
PlanGoodNumber: int(amount),
|
|
|
Price: price,
|
|
|
PartnerBonusPercent: percent,
|
|
|
LineNumber: i, // 记录行号
|
|
|
})
|
|
|
|
|
|
// 记录聚合行号
|
|
|
orderCommands[hashString].LineNumbers = append(orderCommands[hashString].LineNumbers, i)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 批量创建订单命令集
|
|
|
var createOrderCommands []*orderCmd.CreateOrderCommand
|
|
|
for _, orderCommand := range orderCommands {
|
|
|
createOrderCommands = append(createOrderCommands, orderCommand)
|
|
|
}
|
|
|
|
|
|
// 新增失败记录
|
|
|
failureDataList := make([]interface{}, 0)
|
|
|
|
|
|
// 新增成功记录计数
|
|
|
//var successDataCount int64
|
|
|
var successDataCount int64
|
|
|
|
|
|
c.ResponseData(failureDataList)
|
|
|
// 新增错误信息
|
|
|
var createError error
|
|
|
|
|
|
// 批量新增订单
|
|
|
failureDataList, createError = orderSrv.CreateNewOrderByImport(createOrderCommands)
|
|
|
if createError != nil {
|
|
|
c.ResponseError(createError)
|
|
|
return
|
|
|
} else {
|
|
|
if len(failureDataList) > 0 { // 导入失败返回
|
|
|
successDataCount = 0
|
|
|
ret = map[string]interface{}{
|
|
|
"successCount": successDataCount,
|
|
|
"fail": map[string]interface{}{
|
|
|
"tableHeader": tableHeader,
|
|
|
"tableData": failureDataList,
|
|
|
},
|
|
|
}
|
|
|
} else { // 导入成功返回
|
|
|
successDataCount = int64(len(rows) - 3 - len(failureDataList))
|
|
|
if successDataCount == int64(len(rows)-3) {
|
|
|
ret = map[string]interface{}{
|
|
|
"successCount": successDataCount,
|
|
|
"fail": nil,
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 返回错误详情
|
|
|
c.ResponseData(ret)
|
|
|
return
|
|
|
} |
...
|
...
|
|