package supply_analysis

import (
	"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:"添加时间"`
}

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

// AddVarietyPlant 添加future_good_chart分类
func AddVarietyPlant(item *VarietyPlant) (err error) {
	o := orm.NewOrm()
	lastId, err := o.Insert(item)
	if err != nil {
		return
	}
	item.VarietyPlantId = int(lastId)

	return
}

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

// GetVarietyPlantById 根据品种装置id获取品种装置详情
func GetVarietyPlantById(id int) (item *VarietyPlant, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM variety_plant WHERE variety_plant_id = ?`
	err = o.Raw(sql, id).QueryRow(&item)
	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:"日均产量减量"`
	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:"删除权限"`
}

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

// GetCountVarietyPlantByVarietyId 根据品种id获取所有的装置数量
func GetCountVarietyPlantByVarietyId(varietyId int) (total int, err error) {
	o := orm.NewOrm()
	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.NewOrm()
	sql := `DELETE FROM variety_plant WHERE variety_plant_id = ?`
	_, err = o.Raw(sql, id).Exec()
	return
}
