package supply_analysis

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

// VarietyPlant variety_plant 品种装置表
type VarietyPlant struct {
	VarietyPlantId                int       `orm:"column(variety_plant_id);pk"`
	VarietyId                     int       `description:"品种id"`
	Province                      string    `description:"所属省份"`
	City                          string    `description:"所属城市"`
	FactoryName                   string    `description:"工厂名称"`
	PlantName                     string    `description:"装置/产线名称"`
	MaintenanceDate               time.Time `description:"检修日期"`
	ResumptionDate                time.Time `description:"复产日期"`
	AnnualCapacity                float64   `description:"年产能"`
	Coefficient                   float64   `description:"降负系数"`
	AverageDailyCapacityReduction float64   `description:"日均产量减量"`
	IsStop                        int       `description:"是否停产，0：未停产；1：停产；默认未停产"`
	Sort                          int       `description:"排序字段，越小越靠前"`
	SysUserId                     int       `description:"添加人id"`
	SysUserRealName               string    `description:"添加人真实姓名"`
	ModifyTime                    time.Time `description:"最近一次更新时间"`
	CreateTime                    time.Time `description:"添加时间"`
}

// AddVarietyPlant 添加variety_plant装置
func AddVarietyPlant(item *VarietyPlant) (err error) {
	o := orm.NewOrmUsingDB("data")
	lastId, err := o.Insert(item)
	if err != nil {
		return
	}
	item.VarietyPlantId = int(lastId)

	return
}

// MultiAddVarietyPlant 批量添加variety_plant装置
func MultiAddVarietyPlant(items []*VarietyPlant) (err error) {
	o := orm.NewOrmUsingDB("data")
	_, err = o.InsertMulti(len(items), items)
	if err != nil {
		return
	}

	return
}

// Update 更新基础信息
func (varietyPlant *VarietyPlant) Update(cols []string) (err error) {
	o := orm.NewOrmUsingDB("data")
	_, err = o.Update(varietyPlant, cols...)
	return
}

// GetVarietyPlantById 根据品种装置id获取品种装置详情
func GetVarietyPlantById(id int) (item *VarietyPlant, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM variety_plant WHERE variety_plant_id = ?`
	err = o.Raw(sql, id).QueryRow(&item)
	return
}

// GetVarietyPlantByIdList 根据品种装置id列表获取品种装置详情列表
func GetVarietyPlantByIdList(idList []int) (items []*VarietyPlant, err error) {
	num := len(idList)
	if num <= 0 {
		return
	}
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM variety_plant WHERE variety_plant_id in (` + utils.GetOrmInReplace(num) + `)`
	_, err = o.Raw(sql, idList).QueryRows(&items)
	return
}

// VarietyPlantItem 品种装置数据（展示使用）
type VarietyPlantItem struct {
	VarietyPlantId                int                `orm:"column(variety_plant_id);pk"`
	VarietyId                     int                `description:"品种id"`
	Province                      string             `description:"所属省份"`
	City                          string             `description:"所属城市"`
	FactoryName                   string             `description:"工厂名称"`
	PlantName                     string             `description:"装置/产线名称"`
	MaintenanceDate               string             `description:"检修日期"`
	ResumptionDate                string             `description:"复产日期"`
	AnnualCapacity                float64            `description:"年产能"`
	Coefficient                   float64            `description:"降负系数"`
	AverageDailyCapacityReduction float64            `description:"日均产量减量"`
	Day                           int                `description:"时长"`
	CapacityReduction             float64            `description:"期间减量"`
	IsStop                        int                `description:"是否停产，0：未停产；1：停产；默认未停产"`
	Sort                          int                `description:"排序字段，越小越靠前"`
	SysUserId                     int                `description:"添加人id"`
	SysUserRealName               string             `description:"添加人真实姓名"`
	ModifyTime                    string             `description:"最近一次更新时间"`
	CreateTime                    string             `description:"添加时间"`
	Button                        VarietyPlantButton `description:"操作按钮权限"`
}

type VarietyPlantButton struct {
	Edit   bool `description:"操作权限"`
	Delete bool `description:"删除权限"`
}

// GetAllVarietyPlantByVarietyId 根据品种id获取所有的装置
func GetAllVarietyPlantByVarietyId(varietyId int) (items []*VarietyPlantItem, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM variety_plant a  WHERE variety_id = ? ORDER BY sort ASC, variety_plant_id ASC `
	_, err = o.Raw(sql, varietyId).QueryRows(&items)
	return
}

// GetCountVarietyPlantByVarietyId 根据品种id获取所有的装置数量
func GetCountVarietyPlantByVarietyId(varietyId int) (total int, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT count(1) total FROM variety_plant a  WHERE variety_id = ? `
	err = o.Raw(sql, varietyId).QueryRow(&total)
	return
}

// DeleteVarietyPlantById 根据装置id删除
func DeleteVarietyPlantById(id int) (err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `DELETE FROM variety_plant WHERE variety_plant_id = ?`
	_, err = o.Raw(sql, id).Exec()
	return
}

// UpdateEdbInfoSortByPrevId 根据上一层级id更新排序
func UpdateEdbInfoSortByPrevId(varietyId, prevVarietyId, nowSort int, updateSort string) (err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `UPDATE variety_plant set sort = ` + updateSort + ` WHERE variety_id=? AND `
	if prevVarietyId > 0 {
		sql += ` ( sort > ? or ( sort=? AND variety_plant_id > ?)) `
	}
	_, err = o.Raw(sql, varietyId, nowSort, nowSort, prevVarietyId).Exec()
	return
}

// UpdateEdbInfoSortByNextId 根据下一层级id更新排序
func UpdateEdbInfoSortByNextId(varietyId, prevVarietyId, nowSort int, updateSort string) (err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `UPDATE variety_plant set sort = ` + updateSort + ` WHERE variety_id=? AND `
	if prevVarietyId > 0 {
		sql += ` ( sort < ? or (  sort=? AND variety_plant_id < ? )) `
	}
	_, err = o.Raw(sql, varietyId, nowSort, nowSort, prevVarietyId).Exec()
	return
}
