package overseas_custom

import (
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"hongze/hz_crm_api/models/company"
	"hongze/hz_crm_api/utils"
	"time"
)

// 海外客户销售
type OverseasCustomerSale struct {
	OverseasCustomSellerId int       `orm:"column(overseas_custom_seller_id);pk"`
	SellerId               int       `description:"销售id"`
	CreateTime             time.Time `description:"创建时间"`
}

// 海外客户销售
type OverseasCustomerSaleView struct {
	SellerId int    `description:"销售id"`
	RealName string `description:"销售姓名"`
}

func (m *OverseasCustomerSale) Create() (err error) {
	o := orm.NewOrm()
	id, err := o.Insert(m)
	if err != nil {
		return
	}
	m.OverseasCustomSellerId = int(id)
	return
}

func (m *OverseasCustomerSale) Delete() (err error) {
	o := orm.NewOrm()
	sql := ` DELETE FROM overseas_custom_seller WHERE seller_id=? `
	_, err = o.Raw(sql, m.SellerId).Exec()
	return
}

func (m *OverseasCustomerSale) TableName() string {
	return "overseas_custom_seller"
}

// OverseasCustomerSaleSaveReq 保存销售
type OverseasCustomerSaleSaveReq struct {
	AddSellerIdArr []int `description:"新增销售id,数组"`
	DelSellerIdArr []int `description:"删除销售id,数组"`
}

func (m *OverseasCustomerSale) GetOverseasCustomerSaleAll() (list []*OverseasCustomerSaleView, err error) {
	o := orm.NewOrm()
	sql := ` SELECT a.*,b.real_name FROM overseas_custom_seller AS a
			INNER JOIN admin AS b ON a.seller_id=b.admin_id `
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

type AdminItem struct {
	AdminId   int    `description:"系统用户id"`
	AdminName string `description:"系统用户名称"`
	RealName  string `description:"系统用户姓名"`
	Mobile    string `description:"手机号"`
}

func (m *OverseasCustomerSale) HandelOverseasCustomerSale(sellerId, isOverseas int) (err error) {
	o := orm.NewOrm()
	sellerItem := new(AdminItem)
	sql := ` SELECT * FROM admin WHERE admin_id=? `
	err = o.Raw(sql, sellerId).QueryRow(&sellerItem)
	if err != nil {
		return err
	}
	if sellerItem.Mobile == "13065778668" {
		sql = ` UPDATE company_product SET is_overseas = ? WHERE seller_id=? AND company_id IN (SELECT company_id FROM company WHERE region_type='国内') `
		_, err = o.Raw(sql, isOverseas, sellerId).Exec()
		return err
	}
	return err
}

// EnglishCompany 英文客户
type EnglishCompany struct {
	CompanyId      int       `orm:"column(company_id);pk" description:"英文客户ID"`
	CompanyName    string    `description:"客户名称"`
	CountryCode    string    `description:"国家Code"`
	Country        string    `description:"国家"`
	SellerId       int       `description:"销售ID"`
	SellerName     string    `description:"销售姓名"`
	ViewTotal      int       `description:"累计点击量/阅读量"`
	IsDeleted      int       `description:"删除状态：0-正常；1-已删除"`
	CreateTime     time.Time `description:"创建时间"`
	ModifyTime     time.Time `description:"更新时间"`
	Enabled        int       `description:"0-禁用; 1-启用; 2-部分禁用"`
	Status         int       `description:"1:正式,2:临时,3:终止"`
	Nation         string    `description:"所属国家"`
	IsHide         int       `description:"是否隐藏:0:不隐藏,1:隐藏"`
	OverseasStatus string    `description:"海外客户状态:'正式','试用','关闭'"`
	OverseasLabel  int       `description:"海外客户试用子标签：1未分类、2  推进、3 跟踪、4 预备、"`
}

func (m *OverseasCustomerSale) HandelCompanyStatus(sellerId int) (err error) {
	var databaseName string
	if utils.RunMode == "debug" {
		databaseName = "test_v2_hongze_rddp"
	} else {
		databaseName = "hongze_rddp"
	}

	o := orm.NewOrm()
	enCompanyList := make([]*EnglishCompany, 0)
	sql := ` SELECT * FROM %s.english_company WHERE seller_id=? AND is_deleted=0 `
	sql = fmt.Sprintf(sql, databaseName)
	_, err = o.Raw(sql, sellerId).QueryRows(&enCompanyList)
	if err != nil {
		return err
	}

	if len(enCompanyList) > 0 {
		for _, v := range enCompanyList {
			var overseasStatus string
			var resetBtn int
			if v.Enabled == 0 {
				overseasStatus = "关闭"
				resetBtn = 0
			} else {
				overseasStatus = "试用"
				resetBtn = 1
			}
			sql = ` UPDATE %s.english_company SET overseas_status=?,reset_btn=? WHERE company_id=? `
			sql = fmt.Sprintf(sql, databaseName)
			_, err = o.Raw(sql, overseasStatus, resetBtn, v.CompanyId).Exec()
			if err != nil {
				return err
			}
		}
	}

	companyList := make([]*company.CompanyProduct, 0)
	sql = ` SELECT * FROM company_product WHERE seller_id=? `
	_, err = o.Raw(sql, sellerId).QueryRows(&companyList)
	if err != nil {
		return err
	}

	if len(companyList) > 0 {
		for _, v := range companyList {
			var overseasStatus string
			var resetBtn int
			if v.Status == "正式" || v.Status == "永续" {
				overseasStatus = "正式"
				resetBtn = 0
			} else if v.Status == "试用" {
				overseasStatus = "试用"
				resetBtn = 1
			} else {
				overseasStatus = "关闭"
				resetBtn = 0
			}
			sql = ` UPDATE company SET overseas_status=?,reset_btn=? WHERE company_id=? `
			_, err = o.Raw(sql, overseasStatus, resetBtn, v.CompanyId).Exec()
			if err != nil {
				return err
			}
		}
	}
	return err
}
