package router

import (
	agentHdl "applet/app/agent/hdl"
	"applet/app/cfg"
	"applet/app/hdl"
	"applet/app/mw"
	storeHdl "applet/app/store/hdl"
	_ "applet/docs"
	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

// 初始化路由
// 1
func Init() *gin.Engine {
	// debug, release, test 项目阶段
	mode := "release"
	if cfg.Debug {
		mode = "debug"
	}
	gin.SetMode(mode)
	//创建一个新的启动器
	r := gin.New()
	r.GET("/api/swagger/*any", func(c *gin.Context) {
		//r.Use(mw.SwagAuth())
		ginSwagger.DisablingWrapHandler(swaggerFiles.Handler, "SWAGGER")(c)
	})
	r.Use(mw.ChangeHeader)

	// 是否打印访问日志, 在非正式环境都打印
	if mode != "release" {
		r.Use(gin.Logger())
	}
	r.Use(gin.Recovery())

	r.GET("/favicon.ico", func(c *gin.Context) {
		c.Status(204)
	})
	r.NoRoute(func(c *gin.Context) {
		c.JSON(404, gin.H{"code": 404, "msg": "page not found", "data": []struct{}{}})
	})
	r.NoMethod(func(c *gin.Context) {
		c.JSON(405, gin.H{"code": 405, "msg": "method not allowed", "data": []struct{}{}})
	})
	r.Use(mw.Cors)
	routeCommunityTeamAgent(r.Group("/api/v1/communityTeam/agent"))
	routeCommunityTeamOwnStore(r.Group("/api/v1/communityTeam/ownStore"))
	routeCommunityTeam(r.Group("/api/v1/communityTeam"))
	return r
}
func routeCommunityTeamAgent(r *gin.RouterGroup) {
	// 文件上传确认回调, 不校验设备等头部信息
	r.POST("/file/img/callback", agentHdl.FileImgCallback)
	r.Use(mw.DB) // 下面接口再根据mid 获取数据库名
	r.POST("/login", agentHdl.Login)
	r.Use(mw.AuthJWT)
	// 文件上传
	r.POST("/img/upload", agentHdl.ImgReqUpload)
	r.GET("/user", agentHdl.User)
	r.POST("/service/file/upload", agentHdl.ServiceFileUpload)
	r.GET("/wechat/base", agentHdl.WechatBase)
	r.POST("/wechat/base/set", agentHdl.WechatBaseSet)
	r.GET("/alipay/base", agentHdl.AlipayBase)
	r.POST("/alipay/base/set", agentHdl.AlipayBaseSet)
	r.POST("/store/list", agentHdl.UserStoreList)
	r.POST("/store/save", agentHdl.UserStoreSave)
	r.POST("/store/order", agentHdl.UserStoreOrder)
	r.POST("/store/order/pay", agentHdl.UserStoreOrderPay)
	r.POST("/store/order/detail", agentHdl.UserStoreOrderDetail)
	r.POST("/store/total", agentHdl.UserStoreTotal)

	r.POST("/goods/list", agentHdl.Goods)
	r.POST("/goods/save", agentHdl.GoodsSave)
	r.POST("/goods/detail", agentHdl.GoodsDetail)
	r.POST("/goods/del", agentHdl.GoodsDel)
	r.POST("/goods/cate/list", agentHdl.GoodsCate)
	r.POST("/goods/cate/save", agentHdl.GoodsCateSave)
	r.POST("/goods/cate/show", agentHdl.GoodsCateShow)
	r.POST("/goods/cate/del", agentHdl.GoodsCateDel)
	r.POST("/store/withdraw/flow", agentHdl.StoreWithdrawFlow)
	r.POST("/store/withdraw/total", agentHdl.StoreWithdrawTotal)
	r.POST("/store/withdraw/audit", agentHdl.StoreWithdrawAudit)
	r.POST("/store/withdraw/all/audit", agentHdl.StoreWithdrawAuditAll)
	r.POST("/store/withdraw/output", agentHdl.StoreWithdrawOutPut)

}
func routeCommunityTeamOwnStore(r *gin.RouterGroup) {
	r.POST("/file/img/callback", storeHdl.FileImgCallback)
	r.Use(mw.DB) // 下面接口再根据mid 获取数据库名
	r.POST("/login", storeHdl.Login)
	r.POST("/login/send_sms", storeHdl.LoginSendSms)
	r.POST("/login/fast_in", storeHdl.LoginFastIn)
	r.GET("/gd/key", storeHdl.GdKey)

	r.Use(mw.AuthJWT)
	r.GET("/sub_region_list", storeHdl.GetRegionChildNode)
	r.POST("/sms", storeHdl.Sms)
	r.POST("/img/upload", storeHdl.ImgReqUpload)
	r.GET("/user", storeHdl.User)
	r.GET("/base", storeHdl.Base)
	r.POST("/base/set", storeHdl.BaseSet)
	r.GET("/info", storeHdl.UserStoreInfo)
	r.POST("/info/set", storeHdl.UserStoreInfoSet)
	r.POST("/banner/save", storeHdl.UserStoreBannerSave)
	r.POST("/banner/del", storeHdl.UserStoreBannerDel)
	r.POST("/banner/detail", storeHdl.UserStoreBannerDetail)
	r.POST("/banner", storeHdl.UserStoreBanner)
	r.GET("/audit/info", storeHdl.AuditInfo)
	r.POST("/audit/doing", storeHdl.AuditDoing)
	r.POST("/store/order", storeHdl.UserStoreOrder)
	r.POST("/store/order/pay", storeHdl.UserStoreOrderPay)
	r.POST("/store/order/detail", storeHdl.UserStoreOrderDetail)
	r.POST("/store/order/confirm", storeHdl.StoreOrderConfirm)
	r.GET("/store/amount/base", storeHdl.StoreAmountBase)
	r.POST("/store/amount/flow", storeHdl.StoreAmountFlow)
	r.POST("/store/withdraw/flow", storeHdl.StoreWithdrawFlow)
	r.POST("/store/withdraw/doing", storeHdl.StoreWithdrawDoing)
	r.POST("/store/withdraw/bind/alipay", storeHdl.StoreWithdrawBindAlipay)
	r.POST("/goods/list", storeHdl.Goods)
	r.POST("/goods/save", storeHdl.GoodsSave)
	r.POST("/goods/detail", storeHdl.GoodsDetail)
	r.POST("/goods/del", storeHdl.GoodsDel)
	r.GET("/qrcode/base", storeHdl.QrcodeBase)
	r.GET("/qrcode/pay/style", storeHdl.QrcodePayStyle)
	r.POST("/qrcode/list", storeHdl.Qrcode)
	r.POST("/qrcode/save", storeHdl.QrcodeSave)
	r.POST("/qrcode/del", storeHdl.QrcodeDel)
	r.POST("/goods/cate/list", storeHdl.GoodsCate)
	r.POST("/goods/cate/save", storeHdl.GoodsCateSave)
	r.POST("/goods/cate/show", storeHdl.GoodsCateShow)
	r.POST("/goods/cate/del", storeHdl.GoodsCateDel)
	r.POST("/agent/goods/list", storeHdl.AgentGoods)
	r.POST("/agent/goods/detail", storeHdl.AgentGoodsDetail)
	r.POST("/agent/goods/copy", storeHdl.AgentGoodsCopy)
	r.POST("/notice/list", storeHdl.Notice)
	r.POST("/notice/save", storeHdl.NoticeSave)
	r.POST("/notice/del", storeHdl.NoticeDel)
}
func routeCommunityTeam(r *gin.RouterGroup) {
	r.Any("/pay/callback", hdl.PayCallback)
	r.Use(mw.DB)        // 下面接口再根据mid 获取数据库名
	r.Use(mw.CheckBody) //body参数转换
	r.Use(mw.CheckSign) //签名校验
	r.Use(mw.Checker)
	r.GET("/style/store/scan", hdl.StyleStoreScan)
	r.GET("/store/scan", hdl.StoreScan)
	r.GET("/store/pay/scan", hdl.StorePayScan)
	r.POST("/store/pay/info", hdl.StorePayInfo)
	r.POST("/city", hdl.City)
	r.GET("/bank/store/cate", hdl.BankStoreCate)
	r.GET("/bank/store/new/cate", hdl.BankStoreNewCate)
	r.POST("/bank/store/list", hdl.BankStore)
	r.POST("/bank/store/activity", hdl.BankActivity)
	r.POST("/bank/store/notice", hdl.BankNotice)
	r.POST("/bank/card", hdl.BankCard)
	r.POST("/bank/card/detail", hdl.BankCardDetail)

	r.GET("/new/store/cate", hdl.NewStoreCate)
	r.POST("/new/store/list", hdl.NewStore)
	r.POST("/store", hdl.Store)
	r.POST("/store/like/list", hdl.StoreLike)
	r.GET("/goods/cate", hdl.Cate)
	r.POST("/goods", hdl.Goods)
	r.POST("/goods/sku", hdl.GoodsSku)
	r.POST("/coupon", hdl.Coupon)
	r.GET("/user", hdl.User)
	// 用户授权后调用的接口
	r.Use(mw.AuthJWT)
	r.GET("/bank/user/info", hdl.BankUserInfo)
	r.POST("/coupon/receive", hdl.CouponReceive)
	r.POST("/store/addLike", hdl.StoreAddLike)
	r.POST("/store/cancelLike", hdl.StoreCancelLike)
	r.POST("/goods/coupon", hdl.GoodsCoupon)
	r.POST("/order/total", hdl.OrderTotal)
	r.POST("/order/create", hdl.OrderCreate)
	r.POST("/order/cancel", hdl.OrderCancel)
	r.POST("/order/confirm", hdl.OrderConfirm)
	r.POST("/order/coupon", hdl.OrderCoupon)
	r.POST("/order/list", hdl.OrderList)
	r.POST("/order/detail", hdl.OrderDetail)
	r.GET("/order/cate", hdl.OrderCate)
	r.POST("/pay/:payMethod/:orderType", hdl.Pay)
	r.POST("/pay/order/create", hdl.PayOrderCreate)
	r.POST("/pay/order/list", hdl.PayOrderList)
	r.POST("/store/order/list", hdl.StoreOrderList)
	r.POST("/store/order/detail", hdl.StoreOrderDetail)
	r.POST("/store/order/confirm", hdl.StoreOrderConfirm)
	r.GET("/store/order/cate", hdl.StoreOrderCate)
	r.GET("/store/order/notice", hdl.GetNewOrderNoticeList)
	r.POST("/store/index/total", hdl.StoreIndexTotal)
	r.GET("/store/withdraw/base", hdl.StoreWithdrawBase)
	r.GET("/store/withdraw/flow/cate", hdl.StoreWithdrawFlowCate)
	r.POST("/store/withdraw/flow", hdl.StoreWithdrawFlow)
	r.POST("/store/withdraw/doing", hdl.StoreWithdrawDoing)
	r.GET("/printer/cate/list", hdl.PrinterCateList)              //打印机类型列表
	r.GET("/printer/location/list", hdl.PrinterLocationList)      //打印机位置列表
	r.POST("/printer/list", hdl.PrinterIndex)                     //打印机列表
	r.POST("/printer/save", hdl.PrinterIndexSave)                 //打印机添加编辑
	r.POST("/printer/del", hdl.PrinterIndexDel)                   //打印机删除
	r.POST("/printer/module/list", hdl.PrinterModule)             //打印机模板
	r.POST("/printer/module/save", hdl.PrinterIndexModuleSave)    //打印机模板选择
	r.POST("/printer/use/save", hdl.PrinterIndexStateSave)        //打印机使用状态调整
	r.POST("/printer/record/list", hdl.PrinterIndexRecord)        //打印机明细
	r.GET("/printer/record/screen", hdl.PrinterIndexRecordScreen) //打印机筛选
	r.POST("/printer/send", hdl.PrinterIndexToSend)               //打印机测试
	r.POST("/printer/check", hdl.PrinterIndexCheck)               //订
	r.POST("/printer/doing", hdl.ReplacePrinterContent)           //订
}
