package models

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

type Stdeftab struct {
	Finr      int    `xorm:"pk comment('工厂号') Int" json:"stdeftab-finr"`
	Stdeftyp  string `xorm:"pk comment('种类') VARCHAR(15)" json:"stdeftab-stdeftyp"`
	Statid    string `xorm:"pk comment('标识') VARCHAR(30)" json:"stdeftab-statid"`
	Lang      string `xorm:"pk comment('语言') VARCHAR(3)" json:"stdeftab-lang"`
	Bez       string `xorm:"comment('名称') VARCHAR(30)" json:"stdeftab-bez"`
	Lastmodif string `xorm:"comment('最近一次更改时间') VARCHAR(14)" json:"stdeftab-lastmodif"`
	Lastuser  string `xorm:"comment('最近一次更改人') VARCHAR(20)" json:"stdeftab-lastuser"`
	Credatuz  string `xorm:"comment('创建时间') VARCHAR(14)" json:"stdeftab-credatuz"`
}

type StdeftabTemp struct {
	*Stdeftab
	Stdeftyp int `json:"stdeftab-stdeftyp"`
}

func (t *Stdeftab) TableName() string {
	return "stdeftab"
}

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

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

	if err != nil {
		return err
	}
	return nil
}

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

//改
func (t *Stdeftab) Update() bool {
	e := db.Eloquent.Master()
	_, err := e.ID(core.PK{t.Finr, t.Stdeftyp, t.Statid, t.Lang}).Update(t)
	if err != nil {
		return false
	}
	return true
}

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

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

//查询当前Stdeftabs集合
//TODO 添加一张表 做类型判断
func (t *Stdeftab) SelectArr() ([]interface{}, error) {
	e := db.Eloquent.Master()
	var data []Stdeftab
	err := e.Where("finr = ? and statid =?", t.Finr, t.Statid).Find(&data)
	if err != nil {
		return nil, err
	}
	result := make([]interface{}, 0)
	//TODO 该处为临时处理
	for k, _ := range data {
		data[k].Clipped()
		if t.Statid == "log-vendor-status" {
			var stdeftabTemp StdeftabTemp
			stdeftabTemp.Stdeftab = &data[k]
			v, _ := strconv.Atoi(data[k].Stdeftyp)
			stdeftabTemp.Stdeftyp = v
			result = append(result, stdeftabTemp)
		} else {
			result = append(result, data[k])
		}
	}
	return result, nil
}

//查询当前Stdeftabs集合
func (t *Stdeftab) SelectAll(arr []string) (map[string][]Stdeftab, error) {
	e := db.Eloquent.Master()
	dataMap := map[string][]Stdeftab{}
	var data []Stdeftab
	err := e.In("statid", arr).Where("finr = ?", t.Finr).Find(&data)
	if err != nil {
		return dataMap, err
	}
	for _, v := range data {
		v.Clipped()
		key := v.Statid
		dataMap[key] = append(dataMap[key], v)
	}
	return dataMap, nil
}
