package merchant_deploy_info

import (
	"context"
	"kami/internal/dao"
	"kami/internal/model"
	"kami/internal/model/do"
	"kami/internal/model/entity"
	"kami/internal/service"
	"kami/utility/config"

	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/os/glog"
)

func init() {
	service.RegisterMerchantDeployInfo(New())
}

func New() *sMerchantDeployInfo {
	return &sMerchantDeployInfo{}
}

type sMerchantDeployInfo struct{}

func (i *sMerchantDeployInfo) GetListByUid(ctx context.Context, uid string) (dataList []*model.MerchantDeployRecord, err error) {
	dataList = make([]*model.MerchantDeployRecord, 0)
	err = dao.V1MerchantDeployInfo.Ctx(ctx).
		DB(config.GetDatabaseV1()).
		Where(dao.V1MerchantDeployInfo.Columns().MerchantUid, uid).
		Scan(&dataList)
	if err != nil {
		glog.Error(ctx, "获取商户部署信息失败", err)
		err = gerror.New("获取商户部署信息失败")
	}
	return
}

func (i *sMerchantDeployInfo) Delete(ctx context.Context, uid, roadUid string) (err error) {
	m := dao.V1MerchantDeployInfo.Ctx(ctx).DB(config.GetDatabaseV1())
	_, err = m.Where(dao.V1MerchantDeployInfo.Columns().MerchantUid, uid).
		Where(dao.V1MerchantDeployInfo.Columns().SingleRoadUid, roadUid).
		Delete()
	return
}

func (i *sMerchantDeployInfo) Add(ctx context.Context, info *do.V1MerchantDeployInfo) (err error) {
	m := dao.V1MerchantDeployInfo.Ctx(ctx).DB(config.GetDatabaseV1())
	_, err = m.Save(info)
	return
}

func (i *sMerchantDeployInfo) Update(ctx context.Context, info *do.V1MerchantDeployInfo) (err error) {
	_, err = dao.V1MerchantDeployInfo.Ctx(ctx).
		DB(config.GetDatabaseV1()).
		Where(dao.V1MerchantDeployInfo.Columns().MerchantUid, info.MerchantUid).
		Where(dao.V1MerchantDeployInfo.Columns().Id, info.Id).Update(info)
	return
}

// AddOrUpdate 如果当前配置存在，就更新，否则就添加一个信配置
func (i *sMerchantDeployInfo) AddOrUpdate(ctx context.Context, info *do.V1MerchantDeployInfo) (err error) {
	exist, err := i.Exist(ctx, info.MerchantUid.(string), info.PayType.(string), info.SingleRoadUid.(string))
	if err != nil {
		return
	}
	if exist {
		err = i.Update(ctx, info)
	} else {
		err = i.Add(ctx, info)
	}
	return
}

func (i *sMerchantDeployInfo) GetDetailByUidAndPayType(ctx context.Context, uid, payType, roadUid string) (info *entity.V1MerchantDeployInfo, err error) {
	info = &entity.V1MerchantDeployInfo{}
	err = dao.V1MerchantDeployInfo.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1MerchantDeployInfo.Columns().MerchantUid, uid).
		Where(dao.V1MerchantDeployInfo.Columns().PayType, payType).
		Where(dao.V1MerchantDeployInfo.Columns().SingleRoadUid, roadUid).
		Scan(info)
	return
}

// Exist 判断当前数据是否存在
func (i *sMerchantDeployInfo) Exist(ctx context.Context, uid, payType, roadUid string) (exist bool, err error) {
	m := dao.V1MerchantDeployInfo.Ctx(ctx).DB(config.GetDatabaseV1())
	count, err := m.Where(dao.V1MerchantDeployInfo.Columns().MerchantUid, uid).
		Where(dao.V1MerchantDeployInfo.Columns().SingleRoadUid, roadUid).
		Where(dao.V1MerchantDeployInfo.Columns().PayType, payType).Count()
	return count != 0, err
}

func (i *sMerchantDeployInfo) GetDetailByID(ctx context.Context, id int) (data *model.MerchantDeployRecord, err error) {
	m := dao.V1MerchantDeployInfo.Ctx(ctx).DB(config.GetDatabaseV1())
	err = m.Where(dao.V1MerchantDeployInfo.Columns().Id, id).Scan(&data)
	return
}

// GetRestrictStrategyById 获取当前限制的限制策略
func (i *sMerchantDeployInfo) GetRestrictStrategyById(ctx context.Context, id uint) (output model.MerchantDeployInfoStrategyOutput, err error) {
	err = dao.V1MerchantDeployInfo.Ctx(ctx).DB(config.GetDatabaseV1()).WherePri(id).Scan(&output)
	return output, err
}
