package cygx

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

type IndustrialAnalystAdd struct {
	AnalystName            string `description:"分析师名称"`
	IndustrialManagementId int    `description:"产业id"`
}
type IndustrialAnalystDelete struct {
	IndustrialAnalystId int `orm:"column(industrial_analyst_id);pk" description:"分析师id"`
}

type CygxIndustrialAnalyst struct {
	IndustrialAnalystId    int       `orm:"column(industrial_analyst_id);pk" description:"分析师id"`
	IndustrialManagementId int       `description:"产业id"`
	AnalystName            string    `description:"分析师名称"`
	CreateTime             time.Time `description:"创建时间"`
}

type GetIndustrialAnalystList struct {
	List []*CygxIndustrialAnalyst
}

// 新增
func AddIndustrialAnalyst(item *CygxIndustrialAnalyst) (err error) {
	o := orm.NewOrm()
	_, err = o.Insert(item)
	return
}

// 列表
func GetIndustrialAnalystAll(IndustrialManagementId int) (items []*CygxIndustrialAnalyst, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_industrial_analyst where industrial_management_id = ? `
	_, err = o.Raw(sql, IndustrialManagementId).QueryRows(&items)
	return
}

// 获取数量
func GetIndustrialAnalystCount(condition string, pars []interface{}) (count int, err error) {
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_industrial_analyst WHERE 1=1 `
	if condition != "" {
		sqlCount += condition
	}
	o := orm.NewOrmUsingDB("hz_cygx")
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 修改
func EditIndustrialAnalyst(item *CygxIndustrialAnalyst) (err error) {
	sql := `UPDATE cygx_industrial_analyst SET  analyst_name=? WHERE industrial_analyst_id=? `
	o := orm.NewOrmUsingDB("hz_cygx")
	_, err = o.Raw(sql, item.AnalystName, item.IndustrialAnalystId).Exec()
	return
}

// 删除数据
func DeleteIndustrialAnalyst(industrialAnalystId int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` DELETE FROM cygx_industrial_analyst WHERE industrial_analyst_id = ?`
	_, err = o.Raw(sql, industrialAnalystId).Exec()
	return
}
