package models

import (
	"LAPP_LF_MOM_BACKEND/db"
	"LAPP_LF_MOM_BACKEND/utils"
	"errors"
	"xorm.io/core"
)

type PmAssetKpi struct {
	Finr            int    `json:"pm_asset_kpi-finr" xorm:"not null pk INT(4)"`
	Assetid         string `json:"pm_asset_kpi-assetid" xorm:"not null pk VARCHAR(20)"`
	Kpi             string `json:"pm_asset_kpi-kpi" xorm:"not null pk VARCHAR(20)"`
	Kpiname         string `json:"pm_asset_kpi-kpiname" xorm:"not null VARCHAR(40)"`
	Kpitype         int    `json:"pm_asset_kpi-kpitype" xorm:"not null INT(4)"`
	Triggerstatus   int    `json:"pm_asset_kpi-triggerstatus" xorm:"not null INT(4)"`
	Thresholdval    string `json:"pm_asset_kpi-thresholdval" xorm:"not null DECIMAL(16,6)"`
	Actualval       string `json:"pm_asset_kpi-actualval" xorm:"not null DECIMAL(16,6)"`
	Resetattrnr     int    `json:"pm_asset_kpi-resetattrnr" xorm:"not null INT(4)"`
	Counterval      int    `json:"pm_asset_kpi-counterval" xorm:"not null INT(4)"`
	Countercycle    int    `json:"pm_asset_kpi-countercycle" xorm:"not null INT(4)"`
	Countercycleval int    `json:"pm_asset_kpi-countercycleval" xorm:"not null INT(4)"`
	Mainserviceid   string `json:"pm_asset_kpi-mainserviceid" xorm:"not null VARCHAR(20)"`
	Lastmodif       string `json:"pm_asset_kpi-lastmodif" xorm:"not null VARCHAR(14)"`
	Lastuser        string `json:"pm_asset_kpi-lastuser" xorm:"not null VARCHAR(20)"`
	Credatuz        string `json:"pm_asset_kpi-credatuz" xorm:"not null VARCHAR(14)"`
}

func (t *PmAssetKpi) TableName() string {
	return "pm_asset_kpi"
}

// 清除string字段的右侧空格
func (t *PmAssetKpi) Clipped() {
	utils.TrimStruct(t, *t)
}

//增
func (t *PmAssetKpi) Add() error {
	e := db.Eloquent.Master()
	countrole := new(PmAssetKpi)
	affw, err := e.Table("pm_asset_kpi").ID(core.PK{t.Finr, t.Assetid, t.Kpi}).Count(countrole)
	if err != nil {
		return err
	}
	if affw > 0 {
		return errors.New("数据已经存在！")
	}
	_, err = e.Table("pm_asset_kpi").Insert(t)
	if err != nil {
		return err
	}
	return nil
}

//删
func (t *PmAssetKpi) Del() bool {
	e := db.Eloquent.Master()
	_, err := e.ID(core.PK{t.Finr, t.Assetid, t.Kpi}).Delete(&PmAssetKpi{})
	if err != nil {
		return false
	}
	return true
}

//改
func (t *PmAssetKpi) Update() error {
	e := db.Eloquent.Master()
	_, err := e.ID(core.PK{t.Finr, t.Assetid, t.Kpi}).Update(t)
	if err != nil {
		return err
	}
	return err
}

//查
func (t *PmAssetKpi) SelectOne() (PmAssetKpi, error) {
	e := db.Eloquent.Master()
	var data PmAssetKpi
	_, err := e.ID(core.PK{t.Finr, t.Assetid, t.Kpi}).Get(&data)
	if err != nil {
		return data, err
	}
	return data, nil
}

//分页
func (t *PmAssetKpi) GetPage(pageSize int, pageIndex int) ([]PmAssetKpi, int, error) {
	data := make([]PmAssetKpi, 0)
	e := db.Eloquent.Master()
	table := e.Table("pm_asset_kpi").Where("finr = ? ", t.Finr)
	where := "where finr = " + "'" + utils.ValueToString(t.Finr, "") + "'"
	if !utils.ValueIsEmpty(t.Assetid) {
		table = table.And("assetid = ?", t.Assetid)
		where += " and assetid = " + "'" + t.Assetid + "'"
	}
	if !utils.ValueIsEmpty(t.Kpi) {
		table = table.And("kpi = ?", t.Kpi)
		where += " and kpi = " + "'" + t.Kpi + "'"
	}
	Offset := (pageIndex - 1) * pageSize
	err := e.SQL("SELECT TOP " + utils.ValueToString(pageSize, "") + " pm_asset_kpi.* FROM pm_asset_kpi " + where + " AND (convert(varchar(10),finr)+convert(varchar(40),assetid) NOT IN (SELECT TOP " + utils.ValueToString(Offset, "") + " convert(varchar(10),finr)+convert(varchar(40),assetid) FROM pm_asset_kpi " + where + " ORDER BY credatuz DESC)) ORDER BY credatuz DESC").Find(&data)
	pcount := new(PmAssetKpi)
	count, err := table.Count(pcount)
	if err != nil {
		return data, 0, err
	}
	for k, _ := range data {
		data[k].Clipped()
	}
	return data, int(count), nil
}

//根据assetid查询出列表
func (t *PmAssetKpi) SelectAll() ([]PmAssetKpi, error) {
	e := db.Eloquent.Master()
	data := make([]PmAssetKpi, 0)
	err := e.Where("finr = ? and mainserviceid = ?", t.Finr, t.Mainserviceid).Find(&data)
	if err != nil {
		return data, err
	}
	return data, nil
}
