package hdl

import (
	"applet/app/cfg"
	"applet/app/db"
	"applet/app/db/model"
	"applet/app/e"
	"applet/app/md"
	"applet/app/svc"
	"applet/app/utils"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"github.com/tidwall/gjson"
	"io/ioutil"
)

// WechatBase 微信支付配置获取
// @Summary      基本配置-微信支付配置获取
// @Tags         基本配置
// @Description  基本配置-微信支付配置获取
// @Accept       json
// @Produce      json
// @Success      200   {object} md.WechatPay   				""
// @Failure      400   {object}   md.Response              	"具体错误"
// @Router       /api/v1/communityTeam/agent/wechat/base [GET]
func WechatBase(c *gin.Context) {
	user := svc.GetUser(c)
	info, _ := db.UserThirdPartyFindByID(svc.MasterDb(c), user.Info.Uid)
	var res = md.WechatPay{}
	if info != nil {
		res.PayWxApiKey = gjson.Get(info.WechatPayInfo, "pay_wx_api_key").String()
		res.PayWxMchId = gjson.Get(info.WechatPayInfo, "pay_wx_mch_id").String()
	}
	e.OutSuc(c, res, nil)
	return
}

// WechatBaseSet 微信支付配置保存
// @Summary      基本配置-微信支付配置保存
// @Tags         基本配置
// @Description  基本配置-微信支付配置保存
// @Accept       json
// @Produce      json
// @Param        req 	body      md.WechatPay          true   "请求参数"
// @Success      200   {string} 				""
// @Failure      400   {object}   md.Response              	"具体错误"
// @Router       /api/v1/communityTeam/agent/wechat/base/set [POST]
func WechatBaseSet(c *gin.Context) {
	var arg md.WechatPay
	if err := c.ShouldBindJSON(&arg); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	user := svc.GetUser(c)
	info, _ := db.UserThirdPartyFindByID(svc.MasterDb(c), user.Info.Uid)
	if info == nil {
		info = &model.UserThirdParty{
			Uid: user.Info.Uid,
		}
		svc.MasterDb(c).Insert(info)
	}
	var data md.WechatPaySave
	copier.Copy(&data, &arg)
	data.WechatP12ApiclientCert = c.GetString("mid") + "_" + utils.IntToStr(user.Info.Uid) + "_wechat_apiclient_cert.p12"
	info.WechatPayInfo = utils.SerializeStr(data)
	svc.MasterDb(c).Where("uid=?", info.Uid).Cols("wechat_pay_info").Update(info)
	e.OutSuc(c, "success", nil)
	return
}

// AlipayBase 支付宝支付配置获取
// @Summary      基本配置-支付宝支付配置获取
// @Tags         基本配置
// @Description  基本配置-支付宝支付配置获取
// @Accept       json
// @Produce      json
// @Success      200   {object} md.AlipayPay   				""
// @Failure      400   {object}   md.Response              	"具体错误"
// @Router       /api/v1/communityTeam/agent/alipay/base [GET]
func AlipayBase(c *gin.Context) {
	user := svc.GetUser(c)
	info, _ := db.UserThirdPartyFindByID(svc.MasterDb(c), user.Info.Uid)
	var res = md.AlipayPay{}
	if info != nil {
		res.PayAliNewAppId = gjson.Get(info.AlipayPayInfo, "pay_ali_new_app_id").String()
		res.PayAliNewPrivateKey = gjson.Get(info.AlipayPayInfo, "pay_ali_new_private_key").String()
	}
	e.OutSuc(c, res, nil)
	return
}

// AlipayBaseSet 微信支付配置保存
// @Summary      基本配置-微信支付配置保存
// @Tags         基本配置
// @Description  基本配置-微信支付配置保存
// @Accept       json
// @Produce      json
// @Param        req 	body      md.AlipayPay          true   "请求参数"
// @Success      200   {string} 				""
// @Failure      400   {object}   md.Response              	"具体错误"
// @Router       /api/v1/communityTeam/agent/alipay/base/set [POST]
func AlipayBaseSet(c *gin.Context) {
	var arg md.AlipayPay
	if err := c.ShouldBindJSON(&arg); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	user := svc.GetUser(c)
	info, _ := db.UserThirdPartyFindByID(svc.MasterDb(c), user.Info.Uid)
	if info == nil {
		info = &model.UserThirdParty{
			Uid: user.Info.Uid,
		}
		svc.MasterDb(c).Insert(info)
	}
	var data md.AlipayPaySet
	copier.Copy(&data, &arg)
	data.PayAliUseType = "1"
	data.PayAppCertSn = c.GetString("mid") + "_" + utils.IntToStr(user.Info.Uid) + "_payAliNewAppCertSn.crt"
	data.PayAlipayRootCertSn = c.GetString("mid") + "_" + utils.IntToStr(user.Info.Uid) + "_payAliNewAlipayRootCertSn.crt"
	data.PayAlipayrsaPublicKey = c.GetString("mid") + "_" + utils.IntToStr(user.Info.Uid) + "_payAliNewAlipayrsaPublicKey.crt"

	path := "./static/"
	if cfg.Prd {
		path = "/etc/zyos-admin/wx_check_file/"
	}
	PayAliNewAppCertSn, _ := ioutil.ReadFile(path + data.PayAppCertSn) // 读取文件内容
	if string(PayAliNewAppCertSn) != "" {
		data.PayAliNewAppCertSn = string(PayAliNewAppCertSn)
	}
	PayAlipayRootCertSn, _ := ioutil.ReadFile(path + data.PayAlipayRootCertSn) // 读取文件内容
	if string(PayAlipayRootCertSn) != "" {
		data.PayAliNewAlipayRootCertSn = string(PayAlipayRootCertSn)
	}
	PayAlipayrsaPublicKey, _ := ioutil.ReadFile(path + data.PayAlipayrsaPublicKey) // 读取文件内容
	if string(PayAlipayrsaPublicKey) != "" {
		data.PayAliNewAlipayrsaPublicKey = string(PayAlipayrsaPublicKey)
	}
	info.AlipayPayInfo = utils.SerializeStr(data)
	svc.MasterDb(c).Where("uid=?", info.Uid).Cols("alipay_pay_info").Update(info)
	e.OutSuc(c, "success", nil)
	return
}
