package controllers

import (
	"encoding/json"
	"errors"
	"fmt"
	"git.rosy.net.cn/baseapi/platformapi/mtwmapi"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/jxutils"
	"git.rosy.net.cn/jx-callback/business/model"
	"git.rosy.net.cn/jx-callback/business/model/dao"
	"git.rosy.net.cn/jx-callback/business/partner"
	"git.rosy.net.cn/jx-callback/business/partner/purchase/jx/localjx"
	"git.rosy.net.cn/jx-callback/business/partner/purchase/mtwm"
	"github.com/astaxie/beego/server/web"
	"net"
	"strings"
	"time"
)

type JxOrderController struct {
	web.Controller
}

// @Title 创建京西商城订单
// @Description 创建京西商城订单
// @Param	token			header		string	true	"认证token"
// @Param	jxOrder			formData	string	true	"订单信息"
// @Param	addressID		formData	int64	true	"配送地址ID"
// @Param	fromStoreID		formData	int		fasle	"物料配送门店"
// @Param	couponIDs		formData	string	fasle	"优惠券IDs"
// @Param	createType		formData	int		false	"创建类型, 0:预创建, 1:创建"
// @Param	isDeliverySelf	formData	bool	false	"是否是自提单"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /CreateOrder [post]
func (c *JxOrderController) CreateOrder() {
	c.callCreateOrder(func(params *tJxorderCreateOrderParams) (retVal interface{}, errCode string, err error) {
		var jxOrder *localjx.JxOrderInfo
		if err = utils.UnmarshalUseNumber([]byte(params.JxOrder), &jxOrder); err == nil {
			var couponIDs []int
			if err = jxutils.Strings2Objs(params.CouponIDs, &couponIDs); err == nil {
				retVal, err = localjx.CreateOrder(params.Ctx, jxOrder, int64(params.AddressID), params.CreateType, params.FromStoreID, params.IsDeliverySelf, couponIDs)
			}
		}
		return retVal, "", err
	})
}

// @Title 京西门店订单扫码枪扫码支付
// @Description 京西门店订单扫码枪扫码支付
// @Param	token			header		string	true	"认证token"
// @Param	vendorOrderID	formData	string	true	"订单ID"
// @Param	paymentLabel	formData	string	true	"支付身份标示"
// @Param	payType			formData	string	true	"扫码方式[tonglian/lakala]"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /Pay4OrderByBarCodeScanner [post]
func (c *JxOrderController) Pay4OrderByBarCodeScanner() {
	c.callPay4OrderByBarCodeScanner(func(params *tJxorderPay4OrderByBarCodeScannerParams) (retVal interface{}, errCode string, err error) {
		switch params.PayType {
		case "tonglian":
			retVal, err = localjx.BarCodeScannerPay(params.VendorOrderID, params.PaymentLabel)
		case "lakala":
			ip, _, err := net.SplitHostPort(c.Ctx.Request.RemoteAddr)
			if err != nil {
				return retVal, "", err
			}
			retVal, err = localjx.BarCodeScannerPayByLaKaLa(params.Ctx, params.VendorOrderID, params.PaymentLabel, ip)
		}
		return retVal, "", err
	})
}

// @Title 到店扫码支付订单退款
// @Description 到店扫码支付订单退款
// @Param	token			header		string	true	"认证token"
// @Param	vendorOrderID	formData	string	true	"订单ID"
// @Param	skuIds			formData	string	true	"[key:value]退款商品 skuId:count,int"
// @Param	Reason			formData	string	false	"退单原因"
// @Param	payType			formData	string	true	"扫码方式[tl/lkl]"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /RefundOnlineOrder [post]
func (c *JxOrderController) RefundOnlineOrder() {
	c.callRefundOnlineOrder(func(params *tJxorderRefundOnlineOrderParams) (retVal interface{}, errCode string, err error) {
		skuIds := make(map[int]int, 0)
		if err = json.Unmarshal([]byte(params.SkuIds), &skuIds); err != nil {
			return nil, "", err
		}
		switch params.PayType {
		case "tonglian":
			err = localjx.RefundBarCodeScannerOrder(params.Ctx, params.VendorOrderID, skuIds, params.Reason)
		case "lakala":
			retVal, err = localjx.RefundBarCodeScannerOrderByLaKaLa(params.Ctx, params.VendorOrderID, skuIds, params.Reason, "toStore")
		}

		return retVal, errCode, err
	})
}

// @Title 扫码订单刷新售后信息
// @Description 扫码订单刷新售后信息
// @Param	token			header		string	true	"认证token"
// @Param	afsOrderId		query		string	true	"售后订单id"
// @Param	payType			formData	string	true	"扫码方式[tl/lkl]"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /QueryBarCodeRefundStatus [get]
func (c *JxOrderController) QueryBarCodeRefundStatus() {
	c.callQueryBarCodeRefundStatus(func(params *tJxorderQueryBarCodeRefundStatusParams) (retVal interface{}, errCode string, err error) {
		switch params.PayType {
		case "tonglian":
			//err = localjx.RefundBarCodeScannerOrder(params.AfsOrderId)
		case "lakala":
			retVal, err = localjx.QueryBarCodeRefundStatus(params.AfsOrderId)
		}
		return retVal, errCode, err
	})
}

// @Title 手动刷新扫码枪扫码支付订单状态
// @Description 手动刷新扫码枪扫码支付订单状态
// @Param	token			header		string	true	"认证token"
// @Param	vendorOrderID	formData	string	true	"订单ID"
// @Param	payType			formData	string	true	"扫码方式[tl/lkl]"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /RefreshPayStatus [post]
func (c *JxOrderController) RefreshPayStatus() {
	c.callRefreshPayStatus(func(params *tJxorderRefreshPayStatusParams) (retVal interface{}, errCode string, err error) {
		switch params.PayType {
		case "tonglian":
			retVal, err = localjx.QueryBarCodeScannerStatus(params.VendorOrderID)
		case "lakala":
			retVal, err = localjx.LaKaLaBarCodeScannerStatus(params.VendorOrderID)
		}
		return retVal, "", err
	})
}

// @Title 注册和注销门店扫码枪设备
// @Description 注册和注销门店扫码枪设备
// @Param	token			header		string	true	"认证token"
// @Param	storeId			formData	int		true	"京西门店ID"
// @Param	operation		formData	string	true	"当前操作00-新增,02-注销"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /TerminalRegister [post]
func (c *JxOrderController) TerminalRegister() {
	c.callTerminalRegister(func(params *tJxorderTerminalRegisterParams) (retVal interface{}, errCode string, err error) {
		err = localjx.AddStoreTerm(params.StoreId, params.Operation)
		return retVal, errCode, err
	})
}

// @Title 查询门店扫码枪设备状态
// @Description 查询门店扫码枪设备状态
// @Param	token			header		string	true	"认证token"
// @Param	storeId			formData	int		true	"京西门店ID"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /TerminalQuery [post]
func (c *JxOrderController) TerminalQuery() {
	c.callTerminalQuery(func(params *tJxorderTerminalQueryParams) (retVal interface{}, errCode string, err error) {
		retVal, err = localjx.QueryStoreTerm(params.StoreId)
		return retVal, errCode, err
	})
}

// @Title 请求支付京西商城订单
// @Description 请求支付京西商城订单
// @Param	token			header		string	true	"认证token"
// @Param	subAppID		formData	string	false	"appID"
// @Param	vendorOrderID	formData	string	true	"订单ID"
// @Param	payType			formData	int		true	"支付类型"
// @Param	vendorPayType	formData	string	true	"平台支付类型"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /Pay4Order [post]
func (c *JxOrderController) Pay4Order() {
	c.callPay4Order(func(params *tJxorderPay4OrderParams) (retVal interface{}, errCode string, err error) {
		retVal, err = localjx.Pay4Order(params.Ctx, utils.Str2Int64(params.VendorOrderID), params.PayType, params.VendorPayType, params.SubAppID)
		return retVal, "", err
	})
}

// @Title 请求支付京西商城订单,门店自提订单
// @Description 请求支付京西商城订单,门店自提订单
// @Param	token			header		string	true	"认证token"
// @Param	subAppID		formData	string	false	"appID"
// @Param	payType			formData	int		true	"支付类型"
// @Param	vendorPayType	formData	string	true	"平台支付类型"
// @Param	payMoney		formData	int		true	"自提支付金额"
// @Param	storeId			formData	int		true	"提货门店id"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /Pay4StoreMyselfDeliverOrder [post]
func (c *JxOrderController) Pay4StoreMyselfDeliverOrder() {
	c.callPay4StoreMyselfDeliverOrder(func(params *tJxorderPay4StoreMyselfDeliverOrderParams) (interface{}, string, error) {
		orderDetail, _ := dao.GetStoreDetail(dao.GetDB(), params.StoreId, 0, "")
		jxOrder := &localjx.JxOrderInfo{
			BuyerComment: "门店自提单",
			StoreID:      params.StoreId,
			Skus: []*localjx.JxSkuInfo{
				&localjx.JxSkuInfo{
					SkuID:       8030743,
					Count:       params.PayMoney,
					Price:       1,
					SalePrice:   1,
					Name:        "自定义支付购买",
					Weight:      1,
					GroupSign:   false,
					DefendPrice: 0,
				},
			},
			ExpectedDeliveredTimestamp: 0,
			TotalPrice:                 int64(params.PayMoney),
			FreightPrice:               0,
			OrderPrice:                 int64(params.PayMoney),
			ActualPayPrice:             int64(params.PayMoney),
			OrderID:                    jxutils.GenOrderNo(),
			StoreName:                  orderDetail.Name,
			Weight:                     500,
			FromStoreID:                0,
			EarningType:                model.EarningTypePoints,
			OrderType:                  model.OrderTypeNormal,
			IsBuyNowPrice:              0,
			IsPriceDefend:              model.YES,
			OrderID2:                   "",
			UserID:                     params.Ctx.GetUserID(),
		}
		outJxOrder, err := localjx.CreateOrder(params.Ctx, jxOrder, int64(142), 1, 0, true, nil)
		if err != nil {
			return nil, "", err
		}
		if outJxOrder.OrderID == 0 {
			return nil, "", errors.New("orderId 不能为空")
		}
		time.Sleep(200 * time.Millisecond)
		retVal, err := localjx.Pay4Order(params.Ctx, outJxOrder.OrderID, params.PayType, params.VendorPayType, params.SubAppID)
		return retVal, "", err
	})
}

// @Title 请求支付京西商城相关用户支付项目
// @Description 请求支付京西商城相关用户支付项目
// @Param	token			header		string	true	"认证token"
// @Param	thingID			formData	int		false	"项目ID"
// @Param	subAppID		formData	string	false	"appID"
// @Param	vendorOrderID	formData	string	false	"订单ID"
// @Param	payType			formData	int		true	"支付类型"
// @Param	vendorPayType	formData	string	true	"平台支付类型"
// @Param	code			formData	string	false	"app吊起微信支付时,需要code获取openID"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /Pay4User [post]
func (c *JxOrderController) Pay4User() {
	c.callPay4User(func(params *tJxorderPay4UserParams) (retVal interface{}, errCode string, err error) {
		retVal, err = localjx.Pay4User(params.Ctx, params.ThingID, params.VendorOrderID, params.PayType, params.VendorPayType, params.SubAppID, params.Code)
		return retVal, "", err
	})
}

// @Title 买家取消（或申请取消）订单
// @Description 买家取消（或申请取消）订单
// @Param	token			header		string	true	"认证token"
// @Param	vendorOrderID	formData	string	true	"订单ID"
// @Param	reason			formData	string	true	"原因"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /BuyerCancelOrder [post]
func (c *JxOrderController) BuyerCancelOrder() {
	c.callBuyerCancelOrder(func(params *tJxorderBuyerCancelOrderParams) (retVal interface{}, errCode string, err error) {
		retVal, err = localjx.BuyerCancelOrder(params.Ctx, utils.Str2Int64(params.VendorOrderID), params.Reason)
		return retVal, "", err
	})
}

// @Title 得到门店营业时间配送列表
// @Description 得到门店营业时间配送列表
// @Param	token			header		string	true	"认证token"
// @Param	storeID			query		int		true	"门店ID"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetAvailableDeliverTime [get]
func (c *JxOrderController) GetAvailableDeliverTime() {
	c.callGetAvailableDeliverTime(func(params *tJxorderGetAvailableDeliverTimeParams) (retVal interface{}, errCode string, err error) {
		retVal, err = localjx.GetAvailableDeliverTime(params.Ctx, params.StoreID)
		return retVal, "", err
	})
}

// @Title 得到一个订单的支付信息
// @Description 得到一个订单的支付信息
// @Param	token			header	string	true	"认证token"
// @Param	vendorOrderID	query	string	true	"订单号，如果此项不为空，忽略其它所有查询条件"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetOrderPay [get]
func (c *JxOrderController) GetOrderPay() {
	c.callGetOrderPay(func(params *tJxorderGetOrderPayParams) (retVal interface{}, errCode string, err error) {
		retVal, err = localjx.GetOrderPay(params.Ctx, params.VendorOrderID)
		return retVal, "", err
	})
}

// @Title 查询自己的订单
// @Description 查询自己的订单
// @Param	token			header	string	true	"认证token"
// @Param	vendorOrderID	query	string	false	"订单号，如果此项不为空，忽略其它所有查询条件"
// @Param	keyword			query	string	false	"查询关键字"
// @Param	fromDate		query	string	false	"开始日期（包含），格式（2006-01-02），如果订单号为空此项必须要求"
// @Param	toDate			query	string	false	"结束日期（包含），格式（2006-01-02），如果订单号为空此项必须要求"
// @Param	statuss			query	string	false	"订单状态列表[1,2,3]，缺省不限制"
// @Param	offset			query	int		false	"结果起始序号（以0开始，缺省为0）"
// @Param	pageSize		query	int		false	"结果页大小（缺省为50，-1表示全部）"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetMyOrders [get]
func (c *JxOrderController) GetMyOrders() {
	c.callGetMyOrders(func(params *tJxorderGetMyOrdersParams) (retVal interface{}, errCode string, err error) {
		retVal, err = localjx.GetMyOrders(params.Ctx, params.FromDate, params.ToDate, params.MapData, params.Offset, params.PageSize)
		return retVal, "", err
	})
}

// @Title 查询自己的售后单
// @Description 查询自己的售后单
// @Param	token			header	string	true	"认证token"
// @Param	vendorOrderID	query	string	false	"订单号，如果此项不为空，忽略其它所有查询条件"
// @Param	afsOrderID		query	string	false	"售后单号"
// @Param	userID			query	string	false	"用户ID"
// @Param	fromTime 		query	string	false	"开始日期（包含），格式（2006-01-02），如果订单号为空此项必须要求"
// @Param	toTime 			query	string	false	"结束日期（包含），格式（2006-01-02），如果订单号为空此项必须要求"
// @Param	offset			query	int		false	"结果起始序号（以0开始，缺省为0）"
// @Param	pageSize		query	int		false	"结果页大小（缺省为50，-1表示全部）"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetMyAfsOrders [get]
func (c *JxOrderController) GetMyAfsOrders() {
	c.callGetMyAfsOrders(func(params *tJxorderGetMyAfsOrdersParams) (retVal interface{}, errCode string, err error) {
		retVal, err = localjx.GetMyAfsOrders(params.Ctx, params.VendorOrderID, params.AfsOrderID, params.UserID, params.FromTime, params.ToTime, params.Offset, params.PageSize)
		return retVal, "", err
	})
}

// @Title 查询自己的订单状态数量信息
// @Description 查询自己的订单状态数量信息
// @Param	token			header	string	true	"认证token"
// @Param	fromDate		query	string	false	"开始日期（包含），格式（2006-01-02），如果订单号为空此项必须要求"
// @Param	toDate			query	string	false	"结束日期（包含），格式（2006-01-02），如果订单号为空此项必须要求"
// @Param	statuss			query	string	false	"订单状态列表[1,2,3]，缺省不限制"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetMyOrderCountInfo [get]
func (c *JxOrderController) GetMyOrderCountInfo() {
	c.callGetMyOrderCountInfo(func(params *tJxorderGetMyOrderCountInfoParams) (retVal interface{}, errCode string, err error) {
		timeList, err := jxutils.BatchStr2Time(params.FromDate, params.ToDate)
		if err == nil {
			var statuss []int
			if err = jxutils.Strings2Objs(params.Statuss, &statuss); err == nil {
				retVal, err = localjx.GetMyOrderCountInfo(params.Ctx, timeList[0], timeList[1], statuss)
			}
		}
		return retVal, "", err
	})
}

// @Title 每日订单打款
// @Description 每日订单打款
// @Param	token				header		string	true	"认证token"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /AutoPayForPopluarMan [post]
func (c *JxOrderController) AutoPayForPopluarMan() {
	c.callAutoPayForPopluarMan(func(params *tJxorderAutoPayForPopluarManParams) (retVal interface{}, errCode string, err error) {
		err = localjx.AutoPayForPopluarMan(params.Ctx)
		return retVal, "", err
	})
}

// @Title 订单打款
// @Description 订单打款
// @Param	token				header		string	true	"认证token"
// @Param	vendorOrderID		formData	string	true	"订单ID"
// @Param	userID				formData	string	true	"userID"
// @Param	price				formData	int		true	"钱"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /PayForPopluarMan [post]
func (c *JxOrderController) PayForPopluarMan() {
	c.callPayForPopluarMan(func(params *tJxorderPayForPopluarManParams) (retVal interface{}, errCode string, err error) {
		err = localjx.PayForPopluarMan(params.Ctx, params.VendorOrderID, params.UserID, params.Price)
		return retVal, "", err
	})
}

// @Title 查询物料订单状态
// @Description 查询物料订单状态
// @Param	token				header		string	true	"认证token"
// @Param	vendorOrderID		query		string	true	"订单ID"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetMatterOrderStatus [get]
func (c *JxOrderController) GetMatterOrderStatus() {
	c.callGetMatterOrderStatus(func(params *tJxorderGetMatterOrderStatusParams) (retVal interface{}, errCode string, err error) {
		retVal, err = localjx.GetMatterOrderStatus(params.Ctx, params.VendorOrderID)
		return retVal, "", err
	})
}

// @Title 刷新所有物料订单状态
// @Description 刷新所有物料订单状态
// @Param	token				header		string	true	"认证token"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /RefreshAllMatterOrderStatus [put]
func (c *JxOrderController) RefreshAllMatterOrderStatus() {
	c.callRefreshAllMatterOrderStatus(func(params *tJxorderRefreshAllMatterOrderStatusParams) (retVal interface{}, errCode string, err error) {
		err = localjx.RefreshAllMatterOrderStatus(params.Ctx)
		return retVal, "", err
	})
}

// @Title 重发物料订单
// @Description 重发物料订单
// @Param	token				header		string	true	"认证token"
// @Param	vendorOrderID		formData	string	true	"订单ID"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /SendFailedMatterOrder [post]
func (c *JxOrderController) SendFailedMatterOrder() {
	c.callSendFailedMatterOrder(func(params *tJxorderSendFailedMatterOrderParams) (retVal interface{}, errCode string, err error) {
		err = localjx.SendFailedMatterOrder(params.Ctx, params.VendorOrderID)
		return retVal, "", err
	})
}

// @Title 根据时间获取进货辅助工具里的商品，用于加入购物车
// @Description 根据时间获取进货辅助工具里的商品，用于加入购物车
// @Param	token		header	string	true	"认证token"
// @Param	fromDate	query	string	false	"开始日期（包含），格式（2006-01-02），如果订单号为空此项必须要求"
// @Param	toDate		query	string	false	"结束日期（包含），格式（2006-01-02），如果订单号为空此项必须要求"
// @Param	fromStoreID	query	int		false	"进货门店ID"
// @Param	storeID		query	int		false	"货源门店ID"
// @Param	percentage	query	string	false	"销量比例，1，1.05，1.1"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetSupplySupportStoreSkus [get]
func (c *JxOrderController) GetSupplySupportStoreSkus() {
	c.callGetSupplySupportStoreSkus(func(params *tJxorderGetSupplySupportStoreSkusParams) (retVal interface{}, errCode string, err error) {
		retVal, err = localjx.GetSupplySupportStoreSkus(params.Ctx, params.FromDate, params.ToDate, params.FromStoreID, params.StoreID, params.Percentage)
		return retVal, "", err
	})
}

// @Title 得到自己的守价订单列表
// @Description 得到自己的守价订单列表
// @Param	token		header	string	true	"认证token"
// @Param	fromTime	query	string	false	"开始日期（包含），格式（2006-01-02 00:00:00）"
// @Param	toTime		query	string	false	"结束日期（包含），格式（2006-01-02 00:00:00）"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetMyPriceDefendOrders [get]
func (c *JxOrderController) GetMyPriceDefendOrders() {
	c.callGetMyPriceDefendOrders(func(params *tJxorderGetMyPriceDefendOrdersParams) (retVal interface{}, errCode string, err error) {
		retVal, err = localjx.GetMyPriceDefendOrders(params.Ctx, params.FromTime, params.ToTime)
		return retVal, "", err
	})
}

// @Title 测试立马生成守价订单
// @Description 测试立马生成守价订单
// @Param	token		header	string	true	"认证token"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /TestDefend [get]
func (c *JxOrderController) TestDefend() {
	c.callTestDefend(func(params *tJxorderTestDefendParams) (retVal interface{}, errCode string, err error) {
		err = localjx.CreateOrderByPriceDefend(params.Ctx)
		return retVal, "", err
	})
}

// @Title 查询优惠券
// @Description 查询优惠券
// @Param	token			header	string	true	"认证token"
// @Param	couponType		query	int		false	"优惠券类型，1为商品总额满减，2为运费优惠"
// @Param	couponStatuss	query	string	false	"优惠券状态，0为正常可使用，-1为已过期， -2为还没开始"
// @Param	storeIDs		query	string	false	"门店IDs"
// @Param	keyword			query	string	false	"关键字"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetCoupons [get]
func (c *JxOrderController) GetCoupons() {
	c.callGetCoupons(func(params *tJxorderGetCouponsParams) (retVal interface{}, errCode string, err error) {
		var statuss, storeIDs []int
		if err = jxutils.Strings2Objs(params.CouponStatuss, &statuss, params.StoreIDs, &storeIDs); err == nil {
			retVal, err = localjx.GetCoupons(params.Ctx, params.CouponType, statuss, storeIDs, params.Keyword)
		}
		return retVal, "", err
	})
}

// @Title 添加优惠券
// @Description 添加优惠券
// @Param	token		header		string		true	"认证token"
// @Param	payload 	formData	string		true	"优惠券类型 payload"
// @Param	storeIDs	formData	string		false	"门店IDs"
// @Param	count 		formData	int			false	"优惠券数量，默认1"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /AddCoupons [post]
func (c *JxOrderController) AddCoupons() {
	c.callAddCoupons(func(params *tJxorderAddCouponsParams) (retVal interface{}, errCode string, err error) {
		coupons := &model.Coupons{}
		var storeIDs []int
		if err = utils.UnmarshalUseNumber([]byte(params.Payload), coupons); err == nil {
			if err = jxutils.Strings2Objs(params.StoreIDs, &storeIDs); err == nil {
				err = localjx.AddCoupons(params.Ctx, coupons, params.Count, storeIDs)
			}
		}
		return retVal, "", err
	})
}

// @Title 修改优惠券
// @Description 修改优惠券
// @Param	token		header		string		true	"认证token"
// @Param	payload 	formData	string		true	"优惠券类型 payload"
// @Param	storeIDs	formData	string		false	"门店IDs"
// @Param	isDel		formData	bool		false	"是否是删除"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /UpdateCoupons [post]
func (c *JxOrderController) UpdateCoupons() {
	c.callUpdateCoupons(func(params *tJxorderUpdateCouponsParams) (retVal interface{}, errCode string, err error) {
		payload := make(map[string]interface{})
		var storeIDs []int
		if err = utils.UnmarshalUseNumber([]byte(params.Payload), &payload); err == nil {
			if err = jxutils.Strings2Objs(params.StoreIDs, &storeIDs); err == nil {
				err = localjx.UpdateCoupons(params.Ctx, payload, storeIDs, params.IsDel)
			}
		}
		return retVal, "", err
	})
}

// @Title 领取优惠券
// @Description 领取优惠券
// @Param	token		header		string		true	"认证token"
// @Param	couponID	formData	int			true	"券ID"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /ReceiveCoupons [post]
func (c *JxOrderController) ReceiveCoupons() {
	c.callReceiveCoupons(func(params *tJxorderReceiveCouponsParams) (retVal interface{}, errCode string, err error) {
		err = localjx.ReceiveCoupons(params.Ctx, params.CouponID)
		return retVal, "", err
	})
}

// @Title 创建门店账户订单
// @Description 创建门店账户订单
// @Param	token			header		string	true	"认证token"
// @Param	vendorOrderID	formData	string	false	"订单ID"
// @Param	orderType		formData	int		true	"订单类型，4为门店账户订单"
// @Param	storeID			formData	int		true	"门店ID"
// @Param	price			formData	int		true	"支付金额"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /CreateStoreAcctOrder [post]
func (c *JxOrderController) CreateStoreAcctOrder() {
	c.callCreateStoreAcctOrder(func(params *tJxorderCreateStoreAcctOrderParams) (retVal interface{}, errCode string, err error) {
		retVal, err = localjx.CreateStoreAcctOrder(params.Ctx, params.OrderType, params.StoreID, params.Price, params.VendorOrderID)
		return retVal, "", err
	})
}

// @Title 创建品牌账户订单
// @Description 创建品牌账户订单
// @Param	token			header		string	true	"认证token"
// @Param	brandID			formData	int		false	"品牌ID"
// @Param	price			formData	int		true	"支付金额"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /CreateBrandOrder [post]
func (c *JxOrderController) CreateBrandOrder() {
	c.callCreateBrandOrder(func(params *tJxorderCreateBrandOrderParams) (retVal interface{}, errCode string, err error) {
		retVal, err = localjx.CreateBrandOrder(params.Ctx, params.BrandID, params.Price)
		return retVal, "", err
	})
}

// @Title 上传订单拣货图片
// @Description 上传订单拣货图片
// @Param	token			header		string	true	"认证token"
// @Param	orderId			formData	string  true	"订单ID"
// @Param	vendorID		formData	int		true	"平台ID"
// @Param	mediaList		formData	string	true	"返图参数"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /UploadOrderPicking [post]
func (c *JxOrderController) UploadOrderPicking() {
	c.callUploadOrderPicking(func(params *tJxorderUploadOrderPickingParams) (retVal interface{}, errCode string, err error) {
		var mediaList = make([]*mtwmapi.UploadMedia, 0, 0)

		if err = json.Unmarshal([]byte(params.MediaList), &mediaList); err != nil {
			return nil, "", err
		}

		var imgList []string
		for _, v := range mediaList {
			if v.Url != "" {
				imgList = append(imgList, v.Url)
			}
		}

		localOrder, err := partner.CurOrderManager.LoadOrder(params.OrderId, params.VendorID)
		if err != nil {
			return retVal, "", err
		}
		handler, _ := partner.GetPurchasePlatformFromVendorID(params.VendorID).(partner.IPurchasePlatformStoreSkuHandler)
		if handler == nil {
			return retVal, "", fmt.Errorf("不存在的厂商ID:%d", params.VendorID)
		}
		err = handler.UploadPickingImg(localOrder, mediaList)
		if err == nil {
			originalOrder := &model.GoodsOrderImg{
				VendorOrderID: localOrder.VendorOrderID,
				VendorID:      localOrder.VendorID,
				OriginalData:  string(utils.MustMarshal(imgList)),
				CreatedAt:     time.Now(),
			}
			dao.CreateEntity(dao.GetDB(), originalOrder)
		} else {
			for _, v := range imgList {
				aa := strings.Split(v, "/")[3]
				jxutils.DeleteExportContent(aa)
			}

		}
		return retVal, "", err
	})
}

// @Title 获取订单返图详情
// @Description 获取订单返图详情
// @Param	token			header		string	true	"认证token"
// @Param	orderId			formData	string  true	"订单ID"
// @Param	vendorID		formData	int		false	"平台ID"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetOrderRequirePicture [get]
func (c *JxOrderController) GetOrderRequirePicture() {
	c.callGetOrderRequirePicture(func(params *tJxorderGetOrderRequirePictureParams) (retVal interface{}, errCode string, err error) {
		localOrder, err := partner.CurOrderManager.LoadOrder(params.OrderId, params.VendorID)
		if err != nil {
			return nil, "", err
		}
		retVal, err = mtwm.GetVendorDetail(localOrder)
		return retVal, errCode, err
	})
}
