package cygx

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

type CygxActivitySpecialPermissionPoints struct {
	Id                  int       `orm:"column(id);pk"`
	CompanyId           int       `description:"公司ID"`
	Points              int       `description:"剩余点数"`
	CompanyName         string    `description:"公司名称"`
	ChartPermissionId   int       `description:"行业id"`
	ChartPermissionName string    `description:"行业名称"`
	CreateTime          time.Time `description:"创建时间"`
	ModifyTime          time.Time `description:"更新时间"`
}

// 添加
func AddCygxActivitySpecialPermissionPoints(item *CygxActivitySpecialPermissionPoints) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	_, err = o.Insert(item)
	return
}

// MultiAddCygxActivitySpecialPermissionPoints 批量添加
func MultiAddCygxActivitySpecialPermissionPoints(items []*CygxActivitySpecialPermissionPoints) (err error) {
	if len(items) == 0 {
		return
	}

	var chartPermissionIds []int
	var companyId int
	for _, v := range items {
		companyId = v.CompanyId
		if v.ChartPermissionId > 0 {
			chartPermissionIds = append(chartPermissionIds, v.ChartPermissionId)
		}
	}
	lenchartPermissionIds := len(chartPermissionIds)
	o, err := orm.NewOrmUsingDB("hz_cygx").Begin()
	if err != nil {
		return
	}
	defer func() {
		if err == nil {
			o.Commit()
		} else {
			o.Rollback()
		}
	}()

	var condition string
	var pars []interface{}
	pars = make([]interface{}, 0)
	condition = " AND  company_id  = ?  "
	pars = append(pars, companyId)
	if lenchartPermissionIds > 0 {
		condition += " AND  chart_permission_id IN (" + utils.GetOrmInReplace(lenchartPermissionIds) + ")   "
		pars = append(pars, chartPermissionIds)
	}
	//删除历史记录
	sql := ""
	if condition != "" {
		sql = " DELETE FROM cygx_activity_special_permission_points  WHERE 1= 1 " + condition
		_, err = o.Raw(sql, pars).Exec()
		if err != nil {
			return
		}
	}
	//批量插入
	_, err = o.InsertMulti(len(items), items)
	if err != nil {
		return
	}
	return
}

// 获取公司每个行业剩余研选点数列表
func GetCygxActivitySpecialPermissionPointsList(condition string, pars []interface{}) (items []*CygxActivitySpecialPermissionPoints, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_activity_special_permission_points WHERE 1 = 1  `
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

// 更新公司每个行业剩余研选点数
func UpdateCygxActivitySpecialPermissionPoints(points, companyId, chartPermissionId int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `UPDATE cygx_activity_special_permission_points SET points = ?  WHERE company_id=?  AND chart_permission_id=? `
	_, err = o.Raw(sql, points, companyId, chartPermissionId).Exec()
	return
}

// 获取客户是否有研选扣点点数
func GetCygxActivitySpecialPermissionPointsCount(companyId int) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sqlCount := `SELECT COUNT(1) AS count FROM cygx_activity_special_permission_points  WHERE company_id=?  `
	err = o.Raw(sqlCount, companyId).QueryRow(&count)
	return
}
