package rai_serve

import (
	"github.com/beego/beego/v2/client/orm"
	"hongze/hongze_cygx/utils"
	"time"
)

type CygxRaiServeCompany struct {
	ServeCompanyId    int       `comment:"ServeCompanyId 主键ID"`
	CompanyId         int       `comment:"公司ID"`
	CompanyName       string    `comment:"公司名称"`
	CreateTime        time.Time `comment:"创建时间"`
	Money             float64   `comment:"合同金额"`
	ServeCoverageRate string    `comment:"近四周服务覆盖率"`
	SellerId          int       `comment:"所属销售id"`
	SellerName        string    `comment:"所属销售名称"`
	StartDate         string    `comment:"开始日期"`
	EndDate           string    `comment:"结束日期"`
	ShareSeller       string    `comment:"共享销售"`
	ShareSellerId     int       `comment:"共享销售员id"`
	GroupId           int       `description:"销售分组id"`
	ShareGroupId      int       `description:"共享销售分组id"`
	Status            string    `comment:"客户状态"`
	PermissionName    string    `comment:"权限名"`
	IsUserMaker       int       `comment:"近四周之内是否包含决策人 ，1是、0否"`
}

func AddCygxRaiServeCompanyMulti(items, itemsUpdate []*CygxRaiServeCompany, delCompanyIds []int) (err error) {
	o, err := orm.NewOrm().Begin()
	if err != nil {
		return
	}
	defer func() {
		if err == nil {
			o.Commit()
		} else {
			o.Rollback()
		}
	}()

	if len(delCompanyIds) > 0 {
		//删除不属于权益共享的公司
		sql := ` DELETE FROM cygx_rai_serve_company WHERE company_id IN (` + utils.GetOrmInReplace(len(delCompanyIds)) + `)  `
		_, err = o.Raw(sql, delCompanyIds).Exec()
		if err != nil {
			return
		}
	}

	//批量插入新的关联数据
	if len(items) > 0 {
		//批量添加流水信息
		_, err = o.InsertMulti(len(items), items)
		if err != nil {
			return
		}
	}

	if len(itemsUpdate) > 0 {
		p, errUpdate := o.Raw("UPDATE cygx_rai_serve_company SET company_name = ? ,seller_id =? ,seller_name = ?  ,start_date = ? ,end_date = ? ,share_seller = ? ,share_seller_id = ? ,group_id = ? ,share_group_id = ?,status = ?  WHERE company_id = ?").Prepare()
		if errUpdate != nil {
			err = errUpdate
			return
		}
		defer func() {
			_ = p.Close() // 别忘记关闭 statement
		}()

		for _, v := range itemsUpdate {
			_, err = p.Exec(v.CompanyName, v.SellerId, v.SellerName, v.StartDate, v.EndDate, v.ShareSeller, v.ShareSellerId, v.GroupId, v.ShareGroupId, v.Status, v.CompanyId)
			if err != nil {
				return
			}
		}
	}

	return
}

// UpdateCygxRaiServeCompanyMulti 批量修改正式客户信息
func UpdateCygxRaiServeCompanyMulti(items []*CygxRaiServeCompany) (err error) {
	o := orm.NewOrm()
	p, err := o.Raw("UPDATE cygx_rai_serve_company SET money = ? ,serve_coverage_rate =? ,permission_name = ?  ,is_user_maker = ?   WHERE company_id = ?").Prepare()
	if err != nil {
		return
	}
	defer func() {
		_ = p.Close() // 别忘记关闭 statement
	}()
	for _, v := range items {
		_, err = p.Exec(v.Money, v.ServeCoverageRate, v.PermissionName, v.IsUserMaker, v.CompanyId)
		if err != nil {
			return
		}
	}
	return
}

// UpdateCygxRaiServeCompanyFormalNoMulti 批量修改非正式客户信息
func UpdateCygxRaiServeCompanyFormalNoMulti(items []*CygxRaiServeCompany) (err error) {
	o := orm.NewOrm()
	p, err := o.Raw("UPDATE cygx_rai_serve_company SET money = ? , serve_coverage_rate =? ,permission_name = ? , start_date = ? , end_date = ? ,is_user_maker = ?    WHERE company_id = ?").Prepare()
	if err != nil {
		return
	}
	defer func() {
		_ = p.Close() // 别忘记关闭 statement
	}()
	for _, v := range items {
		_, err = p.Exec(v.Money, v.ServeCoverageRate, v.PermissionName, v.StartDate, v.EndDate, v.IsUserMaker, v.CompanyId)
		if err != nil {
			return
		}
	}
	return
}

// UpdateCygxRaiServeCompanyFormalNoContractMulti 批量修改非正式，且没有签约过合同的客户信息
func UpdateCygxRaiServeCompanyFormalNoContractMulti(items []*CygxRaiServeCompany) (err error) {
	o := orm.NewOrm()
	p, err := o.Raw("UPDATE cygx_rai_serve_company SET serve_coverage_rate =?  ,is_user_maker = ?    WHERE company_id = ?").Prepare()
	if err != nil {
		return
	}
	defer func() {
		_ = p.Close() // 别忘记关闭 statement
	}()
	for _, v := range items {
		_, err = p.Exec(v.ServeCoverageRate, v.IsUserMaker, v.CompanyId)
		if err != nil {
			return
		}
	}
	return
}

// 获取当前所属权益服务公司表所有ID
func GetCygxRaiServeCompanyAllList() (items []*CygxRaiServeCompany, err error) {
	o := orm.NewOrm()
	sql := ` SELECT *  FROM	cygx_rai_serve_company LIMIT  1000  `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}
