package dao

import (
	"fmt"
	"github.com/civet148/log"
	"github.com/civet148/sqlca/v2"
	"intent-system/pkg/dal/models"
)

const (
	CustomerState_Enabled  = 1
	CustomerState_Disabled = 2
)

type CustomerDAO struct {
	db *sqlca.Engine
}

func NewCustomerDAO(db *sqlca.Engine) *CustomerDAO {
	return &CustomerDAO{
		db: db,
	}
}



// insert into table by data model
func (dao *CustomerDAO) Insert(do *models.CustomerDO) (lastInsertId int64, err error) {
	return dao.db.Model(&do).Table(models.TableNameCustomer).Insert()
}

// insert if not exist or update columns on duplicate key...
func (dao *CustomerDAO) Upsert(do *models.CustomerDO, columns ...string) (lastInsertId int64, err error) {
	if len(columns) == 0 {
		return 0, fmt.Errorf("no columns to update")
	}
	return dao.db.Model(&do).Table(models.TableNameCustomer).Select(columns...).Upsert()
}

// update table set columns where id=xxx
func (dao *CustomerDAO) Update(do *models.CustomerDO, columns ...string) (rows int64, err error) {
	if len(columns) == 0 {
		return 0, fmt.Errorf("no columns to update")
	}
	return dao.db.Model(&do).Table(models.TableNameCustomer).Select(columns...).Update()
}

func (dao *CustomerDAO) UpdateByEmail(do *models.CustomerDO, columns ...string) (rows int64, err error) {
	if len(columns) == 0 {
		return 0, fmt.Errorf("no columns to update")
	}
	return dao.db.Model(&do).
		Table(models.TableNameCustomer).
		Eq(models.CUSTOMER_COLUMN_EMAIL, do.Email).
		Eq(models.CUSTOMER_COLUMN_DELETED, 0).
		Select(columns...).
		Update()
}

// query records by id
func (dao *CustomerDAO) QueryById(id interface{}, columns ...string) (do *models.CustomerDO, err error) {
	if _, err = dao.db.Model(&do).Table(models.TableNameCustomer).Id(id).Select(columns...).Query(); err != nil {
		return nil, err
	}
	return
}

// query records by conditions
func (dao *CustomerDAO) QueryByCondition(conditions map[string]interface{}, columns ...string) (dos []*models.CustomerDO, err error) {
	if len(conditions) == 0 {
		return nil, fmt.Errorf("condition must not be empty")
	}
	e := dao.db.Model(&dos).Table(models.TableNameCustomer).Select(columns...)
	for k, v := range conditions {
		e.Eq(k, v)
	}
	if _, err = e.Query(); err != nil {
		return nil, err
	}
	return
}

func (dao *CustomerDAO) QueryList(pageNo, pageSize int, id int32, email string, columns ...string) (dos []*models.CustomerDO, total int64, err error) {
	e := dao.db.Model(&dos).Table(models.TableNameCustomer).Select(columns...).Page(pageNo, pageSize).Desc(models.CUSTOMER_COLUMN_ID)
	if id != 0 {
		e.Eq(models.CUSTOMER_COLUMN_ID, id)
	}
	if email != "" {
		e.Like(models.CUSTOMER_COLUMN_EMAIL, email)
	}
	if _, total, err = e.QueryEx(); err != nil {
		return nil, 0, log.Errorf(err.Error())
	}
	return
}

func (dao *CustomerDAO) UpdateByName(do *models.CustomerDO, columns ...string) (err error) {
	if _, err = dao.db.Model(do).
		Table(models.TableNameCustomer).
		Select(columns...).
		Exclude(models.CUSTOMER_COLUMN_USER_NAME, models.CUSTOMER_COLUMN_IS_ADMIN, models.CUSTOMER_COLUMN_EMAIL).
		Eq(models.CUSTOMER_COLUMN_USER_NAME, do.UserName).
		Update(); err != nil {
		log.Errorf(err.Error())
		return
	}
	return
}

// user name
func (dao *CustomerDAO) SelectCustomerByName(strName string) (do *models.CustomerDO, err error) {
	if _, err = dao.db.Model(&do).
		Table(models.TableNameCustomer).
		Select(
			models.CUSTOMER_COLUMN_ID,
			models.CUSTOMER_COLUMN_USER_NAME,
			models.CUSTOMER_COLUMN_PASSWORD,
			models.CUSTOMER_COLUMN_REFERRER,
			models.CUSTOMER_COLUMN_REFERRAL_CODE,
			models.CUSTOMER_COLUMN_FIRST_NAME,
			models.CUSTOMER_COLUMN_LAST_NAME,
			models.CUSTOMER_COLUMN_TITLE,
			models.CUSTOMER_COLUMN_COMPANY,
			models.CUSTOMER_COLUMN_SALT,
			models.CUSTOMER_COLUMN_USER_ALIAS,
			models.CUSTOMER_COLUMN_PHONE_NUMBER,
			models.CUSTOMER_COLUMN_IS_ADMIN,
			models.CUSTOMER_COLUMN_EMAIL,
			models.CUSTOMER_COLUMN_ADDRESS,
			models.CUSTOMER_COLUMN_REMARK,
			models.CUSTOMER_COLUMN_STATE,
			models.CUSTOMER_COLUMN_LOGIN_IP,
			models.CUSTOMER_COLUMN_LOGIN_TIME,
			models.CUSTOMER_COLUMN_CREATED_TIME,
			models.CUSTOMER_COLUMN_UPDATED_TIME,
		).
		Eq(models.CUSTOMER_COLUMN_USER_NAME, strName).
		Eq(models.CUSTOMER_COLUMN_DELETED, 0).
		Query(); err != nil {

		log.Errorf(err.Error())
		return
	}
	return
}

// user email
func (dao *CustomerDAO) SelectCustomerByEmail(strEmail string) (do *models.CustomerDO, err error) {
	if _, err = dao.db.Model(&do).
		Table(models.TableNameCustomer).
		Eq(models.CUSTOMER_COLUMN_EMAIL, strEmail).
		Eq(models.CUSTOMER_COLUMN_DELETED, 0).
		Query(); err != nil {

		log.Errorf(err.Error())
		return
	}
	return
}
