package controllers

import (
	"io"

	"git.rosy.net.cn/jx-callback/business/jxstore/yonghui"
	"git.rosy.net.cn/jx-callback/business/jxutils"
	"github.com/astaxie/beego/server/web"
)

//读取永辉excelAPI
type YongHuiController struct {
	web.Controller
}

// @Title 读取永辉excel文件
// @Description 读取永辉excel文件
// @Param	token				header		string	true	"认证token"
// @Param	isAsync				query		bool	true	"是否异步，缺省是同步"
// @Param	isContinueWhenError	query		bool	true	"单个同步失败是否继续，缺省false"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /LoadExcelByYongHui [post,get]
func (c *YongHuiController) LoadExcelByYongHui() {
	if c.Ctx.Input.IsGet() {
		w := c.Ctx.ResponseWriter
		// 上传页面
		w.Header().Add("Content-Type", "text/html")
		w.WriteHeader(200)
		html := `
	<form enctype="multipart/form-data" action="/v2/yonghui/LoadExcelByYongHui" method="POST">
	    Send this file: <input name="userfiles" multiple accept="*" type="file" />
		<input type="submit" value="Send File" />
	</form>
	`
		io.WriteString(w, html)
	} else if c.Ctx.Input.IsPost() {
		c.callLoadExcelByYongHui(func(params *tYonghuiLoadExcelByYongHuiParams) (retVal interface{}, errCode string, err error) {
			r := c.Ctx.Request
			files := r.MultipartForm.File["userfiles"]
			retVal, err = yonghui.LoadExcelByYongHui(params.Ctx, files, params.IsAsync, params.IsContinueWhenError)
			return retVal, "", err
		})
	}
}

// @Title 根据微盟商品更新京西价格
// @Description 根据微盟商品更新京西价格
// @Param	token				header		string	true	"认证token"
// @Param	storeIDs			formData		string	true	"门店ID列表"
// @Param	isAsync				formData		bool	true	"是否异步，缺省是同步"
// @Param	isContinueWhenError	formData		bool	true	"单个同步失败是否继续，缺省false"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /UpdateJxPriceByWeimob [post]
func (c *YongHuiController) UpdateJxPriceByWeimob() {
	c.callUpdateJxPriceByWeimob(func(params *tYonghuiUpdateJxPriceByWeimobParams) (retVal interface{}, errCode string, err error) {
		var storeIDList []int
		if err = jxutils.Strings2Objs(params.StoreIDs, &storeIDList); err == nil {
			retVal, err = yonghui.UpdateJxPriceByWeimob(params.Ctx, storeIDList, params.IsAsync, params.IsContinueWhenError)
		}
		return retVal, "", err
	})
}

// @Title 查询微盟订单
// @Description 查询微盟订单
// @Param	token				header		string	true	"认证token"
// @Param	keyword				query		string	false	"查询关键字"
// @Param	fromTime			formData	string	false	"订单起始时间 (yyyy-mm-dd 00:00:00)"
// @Param	toTime				formData	string	false	"订单结束时间 (yyyy-mm-dd 00:00:00)"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetWeimobOrders [post]
func (c *YongHuiController) GetWeimobOrders() {
	c.callGetWeimobOrders(func(params *tYonghuiGetWeimobOrdersParams) (retVal interface{}, errCode string, err error) {
		retVal, err = yonghui.GetWeimobOrders(params.Ctx, params.FromTime, params.ToTime, params.MapData)
		return retVal, "", err
	})
}

// @Title 根据微盟订单号生成Excel
// @Description 根据微盟订单号生成Excel
// @Param	token				header		string	true	"认证token"
// @Param	orderNo				formData	string	true	"订单号"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetWeimobOrdersExcel [post]
func (c *YongHuiController) GetWeimobOrdersExcel() {
	c.callGetWeimobOrdersExcel(func(params *tYonghuiGetWeimobOrdersExcelParams) (retVal interface{}, errCode string, err error) {
		retVal, err = yonghui.GetWeimobOrdersExcel(params.Ctx, params.OrderNo)
		return retVal, "", err
	})
}
