package rai_serve

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

type CygxRaiServeWeekBill struct {
	ServeWeekBillId int       `orm:"column(serve_week_bill_id);pk" description:"服务周账单ID"`
	CompanyId       int       `comment:"公司ID"`
	CompanyName     string    `comment:"公司名称"`
	UserTotal       int       `comment:"用户数量"`
	WeekServeCount  float64   `comment:"周度服务量总计"`
	CoverageRate    float64   `comment:"覆盖率"`
	WeekStartDate   string    `comment:"周一开始日期"`
	WeekEndDate     string    `comment:"周日结束日期"`
	MonthStartDate  string    `comment:"月份开始日期"`
	MonthEndDate    string    `comment:"月份结束日期"`
	CreateTime      time.Time `comment:"创建时间"`
	ModifyTime      time.Time `comment:"修改时间"`
}

// AddCygxRaiServeWeekBillMulti 批量添加
func AddCygxRaiServeWeekBillMulti(items []*CygxRaiServeWeekBill) (err error) {
	if len(items) == 0 {
		return
	}
	o, err := orm.NewOrm().Begin()
	if err != nil {
		return
	}
	defer func() {
		if err == nil {
			o.Commit()
		} else {
			o.Rollback()
		}
	}()
	if len(items) > 0 {
		//批量添加流水信息
		_, err = o.InsertMulti(len(items), items)
	}
	return
}

// UpdateCygxRaiServeWeekBillMulti 批量修改客户周度信息
func UpdateCygxRaiServeWeekBillMulti(items []*CygxRaiServeWeekBill) (err error) {
	o := orm.NewOrm()
	p, err := o.Raw("UPDATE cygx_rai_serve_week_bill SET user_total = ? , week_serve_count =? , coverage_rate =? ,modify_time = ?   WHERE company_id = ?  AND week_start_date = ?  ").Prepare()
	if err != nil {
		return
	}
	defer func() {
		_ = p.Close() // 别忘记关闭 statement
	}()
	for _, v := range items {
		_, err = p.Exec(v.UserTotal, v.WeekServeCount, v.CoverageRate, v.ModifyTime, v.CompanyId, v.WeekStartDate)
		if err != nil {
			return
		}
	}
	return
}

// 列表
func GetCygxRaiServeWeekBillListAll(condition string, pars []interface{}) (items []*CygxRaiServeWeekBill, err error) {
	if condition == "" {
		return
	}
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_rai_serve_week_bill   WHERE 1= 1 `
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}
