package erpordermanage

import (
	"encoding/json"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	model "go-admin/app/admin/models"
	orm "go-admin/common/global"
	"go-admin/logger"
	"go-admin/tools"
	"go-admin/tools/app"
	"io"
	"net/http"
	"time"
)

// ErpOrderCreate 新建零售订单
// @Summary 新建零售订单
// @Tags 零售订单
// @Produce json
// @Accept json
// @Param request body models.ErpOrderCreateReq true "新建零售订单模型"
// @Success 200 {object} models.ErpOrder
// @Router /api/v1/erp_order/create [post]
func ErpOrderCreate(c *gin.Context) {
	var req = new(model.ErpOrderCreateReq)
	if err := c.ShouldBindJSON(&req); err != nil {
		logger.Error("ShouldBindJSON err:", logger.Field("err", err))
		app.Error(c, http.StatusBadRequest, err, "参数错误:"+err.Error())
		return
	}

	err := tools.Validate(req) //必填参数校验
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, err.Error())
		return
	}

	tools.RoundFloatFields(req)

	// 如果用户是会员，手机号不能为空
	if req.MemberType == model.ErpOrderMemberTypeMember && req.Tel == "" {
		app.Error(c, http.StatusBadRequest, errors.New("参数错误：缺少会员手机号"), "参数错误：缺少会员手机号")
	}

	erpOrder, err := model.CreateErpOrder(req, c)
	if err != nil {
		logger.Error("CreateErpOrder err:", logger.Field("err", err))
		app.Error(c, http.StatusInternalServerError, err, err.Error())
		return
	}

	app.OK(c, erpOrder, "新增成功")
	return
}

// ErpOrderEdit 编辑零售订单
// @Summary 编辑零售订单
// @Tags 零售订单
// @Produce json
// @Accept json
// @Param request body models.ErpOrderCreateReq true "编辑零售订单模型"
// @Success 200 {object} models.ErpOrder
// @Router /api/v1/erp_order/edit [post]
func ErpOrderEdit(c *gin.Context) {
	var req = new(model.ErpOrderCreateReq)
	if err := c.ShouldBindJSON(&req); err != nil {
		logger.Error("ShouldBindJSON err:", logger.Field("err", err))
		app.Error(c, http.StatusBadRequest, err, "参数错误:"+err.Error())
		return
	}

	err := tools.Validate(req) //必填参数校验
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, err.Error())
		return
	}

	// 如果用户是会员，手机号不能为空
	if req.MemberType == model.ErpOrderMemberTypeMember && req.Tel == "" {
		app.Error(c, http.StatusBadRequest, errors.New("参数错误：缺少会员手机号"), "参数错误：缺少会员手机号")
	}

	erpOrder, err := model.EditErpOrder(req, c)
	if err != nil {
		logger.Error("EditErpOrder err:", logger.Field("err", err))
		app.Error(c, http.StatusInternalServerError, err, err.Error())
		return
	}

	app.OK(c, erpOrder, "编辑成功")
	return
}

// ErpOrderList 查询零售订单列表
// @Summary 查询零售订单列表
// @Tags 零售订单
// @Produce json
// @Accept json
// @Param request body models.ErpOrderListReq true "查询零售订单列表模型"
// @Success 200 {object} models.ErpOrderListResp
// @Router /api/v1/erp_order/list [post]
func ErpOrderList(c *gin.Context) {
	req := &model.ErpOrderListReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		logger.Error("ShouldBindJSON err:", logger.Field("err", err))
		app.Error(c, http.StatusBadRequest, err, "参数错误:"+err.Error())
		return
	}

	resp, err := req.List(c)
	if err != nil {
		logger.Error("erp commodity list err:", logger.Field("err", err))
		if err.Error() == "库存已有该串码商品" {
			app.Error(c, http.StatusInternalServerError, err, err.Error())
		} else if err.Error() == "非当前门店所售商品，需前往对应门店退货" {
			app.Error(c, http.StatusInternalServerError, err, err.Error())
		} else {
			app.Error(c, http.StatusInternalServerError, err, "查询失败:"+err.Error())
		}
		return
	}

	app.OK(c, resp, "")
	return
}

// ErpOrderAudit 审核零售订单（审核、取消审核）
// @Summary 审核零售订单
// @Tags 零售订单
// @Produce json
// @Accept json
// @Param request body models.ErpOrderAuditReq true "审核零售订单模型"
// @Success 200 {object} app.Response
// @Router /api/v1/erp_order/audit [post]
func ErpOrderAudit(c *gin.Context) {
	var req = new(model.ErpOrderAuditReq)
	if err := c.ShouldBindJSON(&req); err != nil {
		logger.Error("ShouldBindJSON err:", logger.Field("err", err))
		app.Error(c, http.StatusBadRequest, err, "参数错误:"+err.Error())
		return
	}

	err := tools.Validate(req) //必填参数校验
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, err.Error())
		return
	}

	var erpOrder model.ErpOrder
	err = orm.Eloquent.Table("erp_order").Where("bill_sn = ?", req.BillSn).Find(&erpOrder).Error
	if err != nil {
		logger.Error("order err:", logger.Field("err", err))
		app.Error(c, http.StatusInternalServerError, err, "审核失败:"+err.Error())
		return
	}

	sysUser, err := model.GetSysUserByCtx(c)
	if err != nil {
		logger.Errorf("err:%#v", err)
		app.Error(c, http.StatusInternalServerError, err, "审核失败:"+err.Error())
		return
	}
	if !(tools.GetRoleName(c) == "admin" || tools.GetRoleName(c) == "系统管理员") {
		// 校验入参门店是否包含在用户所有门店中，是否过期
		if !model.CheckUserStore(erpOrder.StoreId, sysUser) {
			app.Error(c, http.StatusInternalServerError, err, "审核失败:您没有该门店权限")
			return
		}
	}

	// 未审核订单取消审核直接返回
	if erpOrder.State == model.ErpOrderStateUnAudit && req.State == 2 {
		app.OK(c, nil, "")
		return
	}
	// 已审核订单审核直接返回
	if erpOrder.State == model.ErpOrderStateAudited && req.State == 1 {
		app.OK(c, nil, "")
		return
	}
	// 如果订单已审核而且是已支付状态，则不能取消审核
	if erpOrder.State == model.ErpOrderStateAudited && erpOrder.PayStatus == model.HavePaid && req.State == 2 {
		app.Error(c, http.StatusInternalServerError, err, "操作失败:已支付订单不能取消审核")
		return
	}

	orderState := model.ErpOrderStateAudited
	stockState := model.SoldOut
	switch req.State {
	case 1: // 审核
		orderState = model.ErpOrderStateAudited
		stockState = model.SoldOut                           // 库存-已售
		if erpOrder.RetailType == model.RetailTypeRejected { // 退货单
			stockState = model.InStock // 库存-在库
		}
	case 2: // 取消审核
		orderState = model.ErpOrderStateUnAudit
		stockState = model.InStock                           // 库存-在库
		if erpOrder.RetailType == model.RetailTypeRejected { // 退货单反审核
			if erpOrder.State == model.ErpOrderStateUnAudit {
				app.OK(c, nil, "")
				return
			}
			// 校验商品是否有库存,是否是对应门店库存商品
			var commodity []model.ErpOrderCommodity
			err = orm.Eloquent.Table("erp_order_commodity").Where("erp_order_id = ?", erpOrder.ID).Find(&commodity).Error
			if err != nil && err != model.RecordNotFound {
				app.Error(c, http.StatusInternalServerError, err, "操作失败:未查询到订单数据")
				return
			}

			var checkReq model.ErpOrderCreateReq
			checkReq.StoreId = erpOrder.StoreId
			checkReq.ErpOrderCommodities = commodity
			err = model.CheckOrderCommodityStock(&checkReq)
			if err != nil {
				app.Error(c, http.StatusInternalServerError, err, "此订单部分商品已出库，需先反审核相关订单返还库存")
				return
			}

			erpOrder.RetailType = model.RetailTypeSale // 订单改为销售，方便后面流程更新库存
			stockState = model.SoldOut                 // 状态为已售
		}
	default:
		logger.Error("req.State no in (1,2)", logger.Field("req.State", req.State))
		app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误,req.State no in (1,2)")
		return
	}

	nPayStatus := model.NoCreatePayOrder
	if req.State == 1 && erpOrder.RetailType == model.RetailTypeSale { // 审核:销售订单才更新支付状态，退货订单不用更新
		var cashiers []model.ErpOrderCashier
		err = json.Unmarshal([]byte(erpOrder.CashierList), &cashiers)
		if err != nil {
			logger.Error("unmarshal CashierList err:", logger.Field("err", err))
		}

		nPayStatus = model.WaitForPaying // 待支付
		// 检查支付方式，如果没有线上支付则默认为支付已完成
		if !model.CheckIsOnlinePay(cashiers) {
			nPayStatus = model.HavePaid // 已完成
			stockState = model.SoldOut  // 库存-已售
		}
	}

	begin := orm.Eloquent.Begin()
	err = begin.Table("erp_order").Where("id = ?", erpOrder.ID).Updates(map[string]interface{}{
		"state":        orderState,
		"pay_status":   nPayStatus,
		"audit_time":   time.Now(),
		"auditor_name": sysUser.NickName,
		"auditor_id":   sysUser.UserId,
		"updated_at":   time.Now(),
	}).Error
	if err != nil {
		begin.Rollback()
		logger.Error("order err:", logger.Field("err", err))
		app.Error(c, http.StatusInternalServerError, err, "审核失败:"+err.Error())
		return
	}

	// 更新库存
	err = model.UpdateStock(begin, erpOrder, stockState, nPayStatus, req.State)
	if err != nil {
		begin.Rollback()
		logger.Error("UpdateStock err:", logger.Field("err", err))
		app.Error(c, http.StatusInternalServerError, err, "审核失败:"+err.Error())
		return
	}

	// 更新对应优惠券的状态为已使用状态
	err = model.UpdateCoupon(begin, erpOrder, stockState)
	if err != nil {
		begin.Rollback()
		logger.Error("UpdateStock err:", logger.Field("err", err))
		app.Error(c, http.StatusInternalServerError, err, "审核失败:"+err.Error())
		return
	}

	err = begin.Commit().Error
	if err != nil {
		begin.Rollback()
		logger.Error("UpdateStock err:", logger.Field("err", err))
		app.Error(c, http.StatusInternalServerError, err, "审核失败:"+err.Error())
		return
	}

	if req.State == 1 && (nPayStatus == model.HavePaid || erpOrder.RetailType == model.RetailTypeRejected) { // 已完成支付的零售订单判断是否发优惠券
		// 发放尊享会员优惠券
		model.UpdateSwitchUserCoupon(erpOrder.ID, erpOrder.Uid, erpOrder.RetailType)
	}

	app.OK(c, nil, "")
	return
}

// ErpOrderDelete 删除零售订单
// @Summary 删除零售订单
// @Tags 零售订单
// @Produce json
// @Accept json
// @Param request body models.ErpOrderDeleteReq true "删除零售订单模型"
// @Success 200 {object} app.Response
// @Router /api/v1/erp_order/delete [post]
func ErpOrderDelete(c *gin.Context) {
	var req = new(model.ErpOrderDeleteReq)
	if err := c.ShouldBindJSON(&req); err != nil {
		logger.Error("ShouldBindJSON err:", logger.Field("err", err))
		app.Error(c, http.StatusBadRequest, err, "参数错误:"+err.Error())
		return
	}

	err := tools.Validate(req) //必填参数校验
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, err.Error())
		return
	}

	err = model.DeleteOrder(req, c)
	if err != nil {
		logger.Error("AddInvoice err:", logger.Field("err", err))
		app.Error(c, http.StatusInternalServerError, err, err.Error())
		return
	}

	app.OK(c, nil, "")
	return
}

// ErpOrderAddInvoice 开发票
// @Summary 开发票
// @Tags 零售订单
// @Produce json
// @Accept json
// @Param request body models.ErpOrderAddInvoiceReq true "开发票模型"
// @Success 200 {object} app.Response
// @Router /api/v1/erp_order/add_invoice [post]
func ErpOrderAddInvoice(c *gin.Context) {
	var req = new(model.ErpOrderAddInvoiceReq)
	if err := c.ShouldBindJSON(&req); err != nil {
		logger.Error("ShouldBindJSON err:", logger.Field("err", err))
		app.Error(c, http.StatusBadRequest, err, "参数错误:"+err.Error())
		return
	}

	err := tools.Validate(req) //必填参数校验
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, err.Error())
		return
	}

	err = model.SetInvoice(req, c)
	if err != nil {
		logger.Error("AddInvoice err:", logger.Field("err", err))
		app.Error(c, http.StatusInternalServerError, err, "操作失败:"+err.Error())
		return
	}

	app.OK(c, nil, "")
	return
}

// ErpOrderPay 收款
// @Summary 收款
// @Tags 零售订单
// @Produce json
// @Accept json
// @Param request body models.ErpOrderPayReq true "收款模型"
// @Success 200 {object} models.ErpOrderPayResp
// @Router /api/v1/erp_order/pay [post]
func ErpOrderPay(c *gin.Context) {
	var req = new(model.ErpOrderPayReq)
	if err := c.ShouldBindJSON(&req); err != nil {
		logger.Error("ShouldBindJSON err:", logger.Field("err", err))
		app.Error(c, http.StatusBadRequest, err, "参数错误:"+err.Error())
		return
	}

	err := tools.Validate(req) //必填参数校验
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, err.Error())
		return
	}

	resp, err := model.ErpOrderPay(req, c)
	if err != nil {
		logger.Error("ErpOrderPay err:", logger.Field("err", err))
		app.Error(c, http.StatusInternalServerError, err, err.Error())
		return
	}

	app.OK(c, resp, "")
	return
}

// ErpOrderQueryPayStatus 查询付款状态
// @Summary 查询付款状态
// @Tags 零售订单
// @Produce json
// @Accept json
// @Param request body models.ErpOrderDeleteReq true "查询付款状态模型"
// @Success 200 {object} models.ErpOrderPayResp
// @Router /api/v1/erp_order/query_pay_status [post]
func ErpOrderQueryPayStatus(c *gin.Context) {
	var req = new(model.ErpOrderDeleteReq)
	if err := c.ShouldBindJSON(&req); err != nil {
		logger.Error("ShouldBindJSON err:", logger.Field("err", err))
		app.Error(c, http.StatusBadRequest, err, "参数错误:"+err.Error())
		return
	}

	err := tools.Validate(req) //必填参数校验
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, err.Error())
		return
	}

	resp, err := model.QueryErpOrderPayStatus(req.BillSn)
	if err != nil {
		logger.Error("QueryErpOrderPayStatus err:", logger.Field("err", err))
		app.Error(c, http.StatusInternalServerError, err, "查询失败:"+err.Error())
		return
	}

	app.OK(c, resp, "")
	return
}

// ErpOrderStoreManageData 查询门店经营数据
// @Summary 查询门店经营数据
// @Tags 零售报表
// @Produce json
// @Accept json
// @Param request body models.ErpOrderStoreManageDataReq true "查询门店经营数据模型"
// @Success 200 {object} models.ErpOrderStoreManageDataResp
// @Router /api/v1/erp_order/store_manage_data [post]
func ErpOrderStoreManageData(c *gin.Context) {
	var req = new(model.ErpOrderStoreManageDataReq)
	if err := c.ShouldBindJSON(&req); err != nil {
		logger.Error("ShouldBindJSON err:", logger.Field("err", err))
		app.Error(c, http.StatusBadRequest, err, "参数错误:"+err.Error())
		return
	}

	err := tools.Validate(req) //必填参数校验
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, err.Error())
		return
	}

	resp, err := model.QueryStoreManageData(req, c)
	if err != nil {
		logger.Error("QueryStoreManageData err:", logger.Field("err", err))
		app.Error(c, http.StatusInternalServerError, err, "查询失败:"+err.Error())
		return
	}

	app.OK(c, resp, "")
	return
}

// ErpOrderRetailMargin 查询商品零售毛利汇总
// @Summary 查询商品零售毛利汇总
// @Tags 零售报表
// @Produce json
// @Accept json
// @Param request body models.ErpOrderRetailMarginReq true "查询商品零售毛利汇总数据模型"
// @Success 200 {object} models.ErpOrderRetailMarginResp
// @Router /api/v1/erp_order/retail_margin [post]
func ErpOrderRetailMargin(c *gin.Context) {
	var req = new(model.ErpOrderRetailMarginReq)
	if err := c.ShouldBindJSON(&req); err != nil {
		logger.Error("ShouldBindJSON err:", logger.Field("err", err))
		app.Error(c, http.StatusBadRequest, err, "参数错误:"+err.Error())
		return
	}

	err := tools.Validate(req) //必填参数校验
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, err.Error())
		return
	}

	resp, err := model.QueryRetailMargin(req, c)
	if err != nil {
		logger.Error("QueryRetailMargin err:", logger.Field("err", err))
		app.Error(c, http.StatusInternalServerError, err, "查询失败:"+err.Error())
		return
	}

	app.OK(c, resp, "")
	return
}

// ErpOrderRetailDetail 查询零售明细
// @Summary 查询零售明细
// @Tags 零售报表
// @Produce json
// @Accept json
// @Param request body models.ErpOrderRetailDetailReq true "查询零售明细数据模型"
// @Success 200 {object} models.ErpOrderRetailDetailResp
// @Router /api/v1/erp_order/retail_detail [post]
func ErpOrderRetailDetail(c *gin.Context) {
	var req = new(model.ErpOrderRetailDetailReq)
	if err := c.ShouldBindJSON(&req); err != nil {
		logger.Error("ShouldBindJSON err:", logger.Field("err", err))
		app.Error(c, http.StatusBadRequest, err, "参数错误:"+err.Error())
		return
	}

	err := tools.Validate(req) //必填参数校验
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, err.Error())
		return
	}

	resp, err := model.QueryRetailDetail(req, c)
	if err != nil {
		logger.Error("QueryRetailDetail err:", logger.Field("err", err))
		app.Error(c, http.StatusInternalServerError, err, "查询失败:"+err.Error())
		return
	}

	app.OK(c, resp, "")
	return
}

// ErpOrderReceiptData 查询小票数据
// @Summary 查询小票数据
// @Tags 零售订单
// @Produce json
// @Accept json
// @Param request body models.ErpOrderDeleteReq true "查询小票数据模型"
// @Success 200 {object} models.ErpOrderReceiptDataResp
// @Router /api/v1/erp_order/receipt_data [post]
func ErpOrderReceiptData(c *gin.Context) {
	var req = new(model.ErpOrderDeleteReq)
	if err := c.ShouldBindJSON(&req); err != nil {
		logger.Error("ShouldBindJSON err:", logger.Field("err", err))
		app.Error(c, http.StatusBadRequest, err, "参数错误:"+err.Error())
		return
	}

	err := tools.Validate(req) //必填参数校验
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, err.Error())
		return
	}

	resp, err := model.QueryReceiptData(req, c)
	if err != nil {
		logger.Error("QueryRetailDetail err:", logger.Field("err", err))
		app.Error(c, http.StatusInternalServerError, err, "查询失败:"+err.Error())
		return
	}

	app.OK(c, resp, "")
	return
}

// ErpOrderShowAllData 展示所有订单
// @Summary 展示所有订单
// @Tags 零售订单
// @Produce json
// @Accept json
// @Param request body models.ErpOrderShowConfig true "展示所有订单模型"
// @Success 200 {object} app.Response
// @Router /api/v1/erp_order/show_all_data [post]
func ErpOrderShowAllData(c *gin.Context) {
	var req = new(model.ErpOrderShowConfig)
	if err := c.ShouldBindJSON(&req); err != nil {
		logger.Error("ShouldBindJSON err:", logger.Field("err", err))
		app.Error(c, http.StatusBadRequest, err, "参数错误:"+err.Error())
		return
	}

	err := tools.Validate(req) //必填参数校验
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, err.Error())
		return
	}

	//if !(tools.GetRoleName(c) == "admin" || tools.GetRoleName(c) == "系统管理员") {
	//	app.Error(c, http.StatusBadRequest, errors.New("您没有该接口权限"), "您没有该接口权限")
	//	return
	//}

	err = model.SetErpOrderShowConfig(req)
	if err != nil {
		logger.Error("SetErpOrderShowConfig err:", logger.Field("err", err))
		app.Error(c, http.StatusInternalServerError, err, "设置失败:"+err.Error())
		return
	}

	app.OK(c, nil, "")
	return
}

// ErpOrderDailyReport 经营日报表
// @Summary 经营日报表
// @Tags 零售报表
// @Produce json
// @Accept json
// @Param request body models.ErpOrderDailyReportReq true "经营日报表模型"
// @Success 200 {object} models.ErpOrderDailyReportResp
// @Router /api/v1/erp_order/daily_report [post]
func ErpOrderDailyReport(c *gin.Context) {
	var req = new(model.ErpOrderDailyReportReq)
	if err := c.ShouldBindJSON(&req); err != nil {
		logger.Error("ShouldBindJSON err:", logger.Field("err", err))
		app.Error(c, http.StatusBadRequest, err, "参数错误:"+err.Error())
		return
	}

	err := tools.Validate(req) //必填参数校验
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, err.Error())
		return
	}

	resp, err := model.QueryOrderDailyReport(req, c)
	if err != nil {
		logger.Error("QueryOrderDailyReport err:", logger.Field("err", err))
		app.Error(c, http.StatusInternalServerError, err, "查询失败:"+err.Error())
		return
	}

	app.OK(c, resp, "")
	return
}

// ErpOrderSaleDetail 查询销售明细
// @Summary 查询销售明细
// @Tags 零售报表
// @Produce json
// @Accept json
// @Param request body models.ErpOrderSaleDetailReq true "查询销售明细数据模型"
// @Success 200 {object} models.ErpOrderRetailDetailResp
// @Router /api/v1/erp_order/sale_detail [post]
func ErpOrderSaleDetail(c *gin.Context) {
	var req = new(model.ErpOrderSaleDetailReq)
	if err := c.ShouldBindJSON(&req); err != nil {
		logger.Error("ShouldBindJSON err:", logger.Field("err", err))
		app.Error(c, http.StatusBadRequest, err, "参数错误:"+err.Error())
		return
	}

	err := tools.Validate(req) //必填参数校验
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, err.Error())
		return
	}

	resp, err := model.QuerySaleDetail(req, c)
	if err != nil {
		logger.Error("QueryRetailDetail err:", logger.Field("err", err))
		app.Error(c, http.StatusInternalServerError, err, "查询失败:"+err.Error())
		return
	}

	app.OK(c, resp, "")
	return
}

// ErpOrderBatchImport 批量导入零售数据
// @Summary 批量导入零售数据
// @Tags 零售订单
// @Produce json
// @Accept json
// @Param file body string true "上传excel文件" models.ErpOrderBatchImportReq
// @Success 200 {object} models.ErpOrderBatchImportResp
// @Router /api/v1/erp_order/import [post]
func ErpOrderBatchImport(c *gin.Context) {
	jsonData := c.Request.FormValue("jsonData") // "jsonData" 是键名
	if jsonData == "" {
		app.Error(c, http.StatusBadRequest, errors.New("参数错误，门店id为空"), "参数错误，门店id为空")
		return
	}

	var req = new(model.ErpOrderBatchImportReq)
	err := json.Unmarshal([]byte(jsonData), &req)
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, "参数错误:"+err.Error())
		return
	}

	file, header, err := c.Request.FormFile("file")
	if err != nil {
		logger.Error("form file err:", logger.Field("err", err))
		app.Error(c, http.StatusInternalServerError, err, "预览失败")
		return
	}

	readAll, err := io.ReadAll(file)
	if err != nil {
		logger.Error("read all err:", logger.Field("err", err))
		app.Error(c, http.StatusInternalServerError, err, "预览失败")
		return
	}

	fmt.Println("header:", header.Filename)
	_, colsMap, err := model.FileExcelImport(c, readAll, nil, 4, int(req.StoreId))
	if err != nil {
		logger.Errorf("file excel reader err:", err)
		app.Error(c, http.StatusInternalServerError, err, err.Error())
		return
	}

	fmt.Println("colsMap:", colsMap)
	if len(colsMap) != 0 {
		colsMap = colsMap[1:]
	}

	var orderCommodities []model.ErpOrderCommodity
	orderCommodities, err = model.ImportOrderData(c, colsMap)
	if err != nil {
		app.Error(c, http.StatusInternalServerError, err, err.Error())
		return
	}

	resp := model.ErpOrderBatchImportResp{
		Commodities: orderCommodities,
	}

	app.OK(c, resp, "导入成功")
	return
}
