package cygx

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

// 查研观向报告匹配类型
type CygxReportMappingCygx struct {
	Id                  int       `orm:"column(id);pk" description:"id"`
	ChartPermissionId   int       `description:"行业ID"`
	ChartPermissionName string    `description:"行业名称"`
	MatchTypeName       string    `description:"分类名称"`
	ReportType          int       `description:"报告类型，2产业报告，1行业报告"`
	Sort                int       `description:"排序"`
	IsCustom            int       `description:"是否属于自定义的匹配类型 ，1是，0否"`
	IsSummary           int       `description:"是否是纪要库，1是，0否"`
	IsReport            int       `description:"是否是报告，1是，0否"`
	PermissionType      int       `description:"1主观，2客观"`
	CreateTime          time.Time `description:"创建时间"`
	ModifyTime          time.Time `description:"更新时间"`
}

type CygxCygxReportMappingCygxResp struct {
	List []*CygxReportMappingCygx
}

// 报告归类入参
type CygxReportMappingCygxAdd struct {
	ReportType          int    `description:"报告类型，2产业报告，1行业报告"`
	ChartPermissionId   int    `description:"行业ID"`
	ChartPermissionName string `description:"行业名称"`
	MatchTypeName       string `description:"分类名称"`
}

// 列表
func GetCygxReportMappingCygxList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxReportMappingCygx, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_report_mapping_cygx as art WHERE 1= 1 `
	if condition != "" {
		sql += condition
	}
	if pageSize > 0 {
		sql += ` LIMIT ?,?  `
		_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	} else {
		_, err = o.Raw(sql, pars).QueryRows(&items)
	}
	return
}

// 列表
func GetCygxReportMappingCygxByCon(condition string, pars []interface{}) (items []*CygxReportMappingCygx, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_report_mapping_cygx as art WHERE 1= 1 `
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

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

// 添加
func AddCygxReportMappingCygx(itemCygx *CygxReportMappingCygx, itemCelue *CygxReportMappingCelue) (itemCygxId int64, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()
	itemCygxId, err = to.Insert(itemCygx)
	if err != nil {
		return
	}

	_, err = to.Insert(itemCelue)
	if err != nil {
		return
	}

	item := new(CygxReportMappingGroup)
	item.IdCygx = int(itemCygxId)
	item.CategoryIdCelue = itemCelue.CategoryId
	item.ModifyTime = time.Now()
	item.CreateTime = time.Now()
	_, err = to.Insert(item)
	return
}

// 通过ID获取详情
func GetCygxReportMappingCygxDetail(Id int) (item *CygxReportMappingCygx, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_report_mapping_cygx  WHERE id=? `
	err = o.Raw(sql, Id).QueryRow(&item)
	return
}

// 通过ID获取详情
func GetCygxReportMappingCygxDetailByName(name string) (item *CygxReportMappingCygx, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_report_mapping_cygx  WHERE match_type_name=?  LIMIT 1 `
	err = o.Raw(sql, name).QueryRow(&item)
	return
}

// 通过ID获取详情
func GetCygxReportMappingCygxDetailByNameAndChartPermissionId(name string, chartPermissionId int) (item *CygxReportMappingCygx, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_report_mapping_cygx  WHERE match_type_name=?  AND chart_permission_id = ?  LIMIT 1 `
	err = o.Raw(sql, name, chartPermissionId).QueryRow(&item)
	return
}
