package data_manage

import (
	"eta/eta_api/utils"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"html"
	"regexp"
	"strings"
	"time"
)

// CCFStockExcel CCF化纤装置表格
type CCFStockExcel struct {
	CcfStockExcelId int       `orm:"column(ccf_stock_excel_id);pk"`
	ClassifyId      int       `description:"分类ID"`
	ExcelDate       time.Time `description:"表格日期"`
	ExcelContent    string    `description:"表格HTML"`
	FromPage        string    `description:"表格来源"`
	CreateTime      time.Time `description:"创建时间"`
	ModifyTime      time.Time `description:"修改时间"`
}

func (m *CCFStockExcel) TableName() string {
	return "ccf_stock_excel"
}

type CCFStockExcelCols struct {
	CcfStockExcelId string
	ClassifyId      string
	ExcelDate       string
	ExcelContent    string
	FromPage        string
	CreateTime      string
	ModifyTime      string
}

func (m *CCFStockExcel) Cols() CCFStockExcelCols {
	return CCFStockExcelCols{
		CcfStockExcelId: "ccf_stock_excel_id",
		ClassifyId:      "classify_id",
		ExcelDate:       "excel_date",
		ExcelContent:    "excel_content",
		FromPage:        "from_page",
		CreateTime:      "create_time",
		ModifyTime:      "modify_time",
	}
}

func (m *CCFStockExcel) PrimaryId() string {
	return m.Cols().CcfStockExcelId
}

func (m *CCFStockExcel) Create() (err error) {
	o := orm.NewOrmUsingDB("data")
	id, err := o.Insert(m)
	if err != nil {
		return
	}
	m.CcfStockExcelId = int(id)
	return
}

func (m *CCFStockExcel) CreateMulti(items []*CCFStockExcel) (err error) {
	if len(items) == 0 {
		return
	}
	o := orm.NewOrmUsingDB("data")
	_, err = o.InsertMulti(len(items), items)
	return
}

func (m *CCFStockExcel) Update(cols []string) (err error) {
	o := orm.NewOrmUsingDB("data")
	_, err = o.Update(m, cols...)
	return
}

func (m *CCFStockExcel) Del() (err error) {
	o := orm.NewOrmUsingDB("data")
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
	_, err = o.Raw(sql, m.CcfStockExcelId).Exec()
	return
}

func (m *CCFStockExcel) MultiDel(menuIds []int) (err error) {
	if len(menuIds) == 0 {
		return
	}
	o := orm.NewOrmUsingDB("data")
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.PrimaryId(), utils.GetOrmInReplace(len(menuIds)))
	_, err = o.Raw(sql, menuIds).Exec()
	return
}

func (m *CCFStockExcel) GetItemById(id int) (item *CCFStockExcel, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
	err = o.Raw(sql, id).QueryRow(&item)
	return
}

func (m *CCFStockExcel) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *CCFStockExcel, err error) {
	o := orm.NewOrmUsingDB("data")
	order := ``
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
	err = o.Raw(sql, pars).QueryRow(&item)
	return
}

func (m *CCFStockExcel) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func (m *CCFStockExcel) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*CCFStockExcel, err error) {
	o := orm.NewOrmUsingDB("data")
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

func (m *CCFStockExcel) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*CCFStockExcel, err error) {
	o := orm.NewOrmUsingDB("data")
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

type CCFStockExcelItem struct {
	ExcelId      int    `description:"表格ID"`
	ClassifyId   int    `description:"分类ID"`
	ExcelDate    string `description:"表格日期"`
	ExcelContent string `description:"表格HTML"`
	CreateTime   string `description:"创建时间"`
	ModifyTime   string `description:"修改时间"`
}

func (m *CCFStockExcel) Format2Item() (item *CCFStockExcelItem) {
	item = new(CCFStockExcelItem)
	item.ExcelId = m.CcfStockExcelId
	item.ClassifyId = m.ClassifyId
	item.ExcelDate = utils.TimeTransferString(utils.FormatDate, m.ExcelDate)
	content := html.UnescapeString(m.ExcelContent)
	content = regexp.MustCompile(`\n`).ReplaceAllString(content, "")
	item.ExcelContent = html.UnescapeString(content)
	item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, m.CreateTime)
	item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, m.ModifyTime)
	return
}
