package autocode

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
	autoCodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"gorm.io/gorm"
	"time"
)

type UnitService struct {
}

// CreateUnit 创建Unit记录
// Author [piexlmax](https://github.com/piexlmax)
func (unitService *UnitService) CreateUnit(unit autocode.Unit) (err error) {
	err = global.GVA_DB.Create(&unit).Error
	return err
}

// DeleteUnit 删除Unit记录
// Author [piexlmax](https://github.com/piexlmax)
func (unitService *UnitService) DeleteUnit(unit autocode.Unit) (err error) {
	err = global.GVA_DB.Delete(&unit).Error
	return err
}

// DeleteUnitByIds 批量删除Unit记录
// Author [piexlmax](https://github.com/piexlmax)
func (unitService *UnitService) DeleteUnitByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]autocode.Unit{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateUnit 更新Unit记录
// Author [piexlmax](https://github.com/piexlmax)
func (unitService *UnitService) UpdateUnit(unit autocode.Unit) (err error) {
	//if unit {
	//
	//}
	//now := *unit.UnitIntegral
	//now = now * 100
	//unit.UnitIntegral = &now
	err = global.GVA_DB.Save(&unit).Error
	return err
}

func (unitService *UnitService) UpdateUnitMaxIntegral(id *int, maxIntergral int) (err error) {
	var unit autocode.Unit
	global.GVA_DB.Where("id = ?", id).First(&unit)
	unit.UnitMaxIntegral = &maxIntergral
	err = global.GVA_DB.Save(unit).Error
	return err
}

// UpdateUnit 开始下期
// Author [piexlmax](https://github.com/piexlmax)
func (unitService *UnitService) UpdateUnitNext(unit autocode.Unit) (err error) {
	//开始下一期
	now := *unit.UnitIntegral
	now = now * 10
	unit.UnitIntegral = &now
	global.GVA_DB.Session(&gorm.Session{
		AllowGlobalUpdate: true,
	}).Model(&unit).Updates(&autocode.Unit{UnitMaxIntegral: unit.UnitIntegral, UnitIntegral: unit.UnitIntegral, Period: time.Now().Format("20060102150405")})
	return err
}

func (unitService *UnitService) UpdateUnitA(unit autocode.Unit) (err error) {
	err = global.GVA_DB.Save(&unit).Error
	return err
}

// GetUnit 根据id获取Unit记录
// Author [piexlmax](https://github.com/piexlmax)
func (unitService *UnitService) GetUnit(id uint) (err error, unit autocode.Unit) {
	err = global.GVA_DB.Where("id = ?", id).First(&unit).Error
	return
}

func (unitService *UnitService) GetUnitTop() (err error, unit autocode.Unit) {
	top := autocode.Unit{}
	err = global.GVA_DB.First(&top).Error
	return err, top
}

// GetUnitInfoList 分页获取Unit记录
// Author [piexlmax](https://github.com/piexlmax)
func (unitService *UnitService) GetUnitInfoList(info autoCodeReq.UnitSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&autocode.Unit{})
	if info.UnitName != "" {
		db.Where("unit_name like ?", "%"+info.UnitName+"%")
	}
	var units []autocode.Unit
	// 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&units).Error
	return err, units, total
}

func (unitService *UnitService) GetUnitInfoListAll() (err error, list []autocode.Unit) {
	var units []autocode.Unit
	// 创建db
	err = global.GVA_DB.Model(&autocode.Unit{}).Find(&units).Error
	return err, units
}
