package merchant

import (
	"context"
	"errors"

	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/os/glog"
	"github.com/gogf/gf/v2/os/gtime"

	"kami/internal/dao"
	"kami/internal/model"
	"kami/internal/model/do"
	"kami/internal/model/entity"
	"kami/internal/service"
	"kami/utility/config"
	"kami/utility/utils"
)

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

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

type sMerchant struct{}

func (s *sMerchant) List(ctx context.Context, req model.MerchantListParamsInput) (total int, data []model.MerchantInfoRecord, err error) {
	m := dao.V1MerchantInfo.Ctx(ctx).DB(config.GetDatabaseV1())
	total, err = m.Count()
	if err != nil {
		glog.Errorf(ctx, "获取商户失败:%s", err.Error())
		err = errors.New("获取商户失败")
	}
	if req.MerchantUid != "" {
		m = m.Where(dao.V1MerchantInfo.Columns().MerchantUid, req.MerchantUid)
	}
	if req.MerchantName != "" {
		m = m.WhereLike(dao.V1MerchantInfo.Columns().MerchantName, utils.OrmLike(req.MerchantName))
	}
	m.Where(dao.V1MerchantInfo.Columns().MerchantUid, req.MerchantUid).
		Where(dao.V1MerchantInfo.Columns().MerchantName)
	err2 := m.Page(req.Current, req.PageSize).Scan(&data)

	for i := range data {
		data[i].ClearTextPassword = ""
	}

	if err2 != nil {
		total = 0
		glog.Errorf(ctx, "获取商户失败:%s", err2.Error())
		err = errors.New("获取商户失败")
	}
	return
}

func (s *sMerchant) Add(ctx context.Context, input do.V1MerchantInfo, tx gdb.TX) (err error) {
	m := dao.V1MerchantInfo.Ctx(ctx).DB(config.GetDatabaseV1())
	if tx != nil {
		m = m.TX(tx)
	}
	data := entity.V1MerchantInfo{}
	_ = m.Where(dao.V1MerchantInfo.Columns().MerchantName, input.MerchantName).Scan(&data)
	if data.Id != 0 {
		return errors.New("商户名称已存在")
	}
	_ = m.Where(dao.V1MerchantInfo.Columns().LoginAccount, input.LoginAccount).Scan(&data)
	if data.Id != 0 {
		return errors.New("商户名称已存在")
	}
	input.CreateTime = gtime.Now()
	input.UpdateTime = gtime.Now()
	_, err = m.Insert(input)
	return
}

func (s *sMerchant) HandleStatus(ctx context.Context, data entity.V1MerchantInfo, tx gdb.TX) (err error) {
	m := dao.V1MerchantInfo.Ctx(ctx).DB(config.GetDatabaseV1())
	if tx != nil {
		m = m.TX(tx)
	}
	_, err = m.Where(dao.V1MerchantInfo.Columns().MerchantUid, data.MerchantUid).
		Update(do.V1MerchantInfo{Status: data.Status})
	if err != nil {
		glog.Error(ctx, "更新商户状态失败", err)
	}
	return err
}

func (s *sMerchant) GetMerchantByUId(ctx context.Context, Uid string) (data entity.V1MerchantInfo, err error) {
	data = *new(entity.V1MerchantInfo)
	err = dao.V1MerchantInfo.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1MerchantInfo.Columns().MerchantUid, Uid).Scan(&data)
	if err != nil {
		glog.Error(ctx, "获取商户失败", err)
	}
	return
}

func (s *sMerchant) GetDetail(ctx context.Context, uid string) (res model.MerchantSimpleInfoRecord, err error) {
	res = *new(model.MerchantSimpleInfoRecord)
	m := dao.V1MerchantInfo.Ctx(ctx).DB(config.GetDatabaseV1())
	err = m.Where(dao.V1MerchantInfo.Columns().MerchantUid, uid).Scan(&res)
	return
}

func (s *sMerchant) UpdateDetail(ctx context.Context, data model.MerchantSimpleInfoRecord) (err error) {
	m := dao.V1MerchantInfo.Ctx(ctx).DB(config.GetDatabaseV1())
	_, err = m.Where(dao.V1MerchantInfo.Columns().MerchantUid, data.MerchantUid).Update(do.V1MerchantInfo{
		MerchantName: data.MerchantName,
		WhiteIps:     data.WhiteIps,
		Remark:       data.Remark,
	})
	return
}
