package company

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

type CompanyNoRenewedAscribe struct {
	NoRenewedAscribeId int       `orm:"column(no_renewed_ascribe_id);pk" description:"主键ID"`
	CompanyAscribeId   int       `description:"归因ID"`
	AscribeContent     string    `description:"归因说明"`
	Content            string    `description:"内容说明"`
	AdminId            int       `description:"管理员ID"`
	CompanyId          int       `description:"公司ID"`
	ProductId          int       `description:"产品id"`
	CreateTime         time.Time `description:"创建时间"`
	ModifyTime         time.Time `description:"更新时间"`
}

type CompanyNoRenewedAscribeLog struct {
	NoRenewedAscribeId int       `orm:"column(no_renewed_ascribe_id);pk" description:"主键ID"`
	CompanyAscribeId   int       `description:"归因ID"`
	AscribeContent     string    `description:"归因说明"`
	Content            string    `description:"内容说明"`
	AdminId            int       `description:"管理员ID"`
	CompanyId          int       `description:"公司ID"`
	ProductId          int       `description:"产品id"`
	CreateTime         time.Time `description:"创建时间"`
	ModifyTime         time.Time `description:"更新时间"`
}

type CompanyNoRenewedAscribeResp struct {
	NoRenewedAscribeId int    `description:"主键ID"`
	CompanyAscribeId   int    `description:"归因ID"`
	AscribeContent     string `description:"归因说明"`
	ProductId          int    `description:"产品id"`
	Content            string `description:"内容说明"`
	AdminId            int    `description:"管理员ID"`
	CompanyId          int    `description:"公司ID"`
	CreateTime         string `description:"创建时间"`
	ModifyTime         string `description:"更新时间"`
}
type CompanyNoRenewedAscribeListResp struct {
	List []*CompanyNoRenewedAscribeResp
}

type CompanyNoRenewedAscribeDetailResp struct {
	Detail *CompanyNoRenewedAscribeResp
}

type CompanyNoRenewedAscribeReq struct {
	CompanyAscribeId int    `description:"归因ID"`
	ProductId        int    `description:"产品id"`
	Content          string `description:"内容说明"`
	CompanyId        int    `description:"公司ID"`
}

// 添加
func AddCompanyNoRenewedAscribe(item *CompanyNoRenewedAscribe, itemLog *CompanyNoRenewedAscribeLog) (err error) {
	o := orm.NewOrm()
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()
	_, err = to.Insert(item)
	if err != nil {
		return
	}

	_, err = to.Insert(itemLog)
	return
}

// 修改
func UpdateCompanyNoRenewedAscribe(item *CompanyNoRenewedAscribe, itemLog *CompanyNoRenewedAscribeLog) (err error) {
	o := orm.NewOrm()
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()
	updateParams := make(map[string]interface{})
	updateParams["CompanyAscribeId"] = item.CompanyAscribeId
	updateParams["ModifyTime"] = item.ModifyTime
	updateParams["Content"] = item.Content
	updateParams["AscribeContent"] = item.AscribeContent
	ptrStructOrTableName := "company_no_renewed_ascribe"
	whereParam := map[string]interface{}{"company_id": item.CompanyId, "product_id": item.ProductId}
	qs := to.QueryTable(ptrStructOrTableName)
	for expr, exprV := range whereParam {
		qs = qs.Filter(expr, exprV)
	}
	_, err = qs.Update(updateParams)
	if err != nil {
		return
	}
	_, err = to.Insert(itemLog)
	return
}

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

// 通过ID获取详情
func GetCompanyNoRenewedAscribeDetail(companyId, productId int) (item *CompanyNoRenewedAscribeResp, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM company_no_renewed_ascribe   WHERE company_id=?  AND product_id = ? `
	err = o.Raw(sql, companyId, productId).QueryRow(&item)
	return
}

// 列表
func GetCompanyNoRenewedAscribeList(condition string, pars []interface{}, startSize, pageSize int) (items []*CompanyNoRenewedAscribeResp, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM company_no_renewed_ascribe as a  WHERE 1= 1 `
	if condition != "" {
		sql += condition
	}
	if startSize+pageSize > 0 {
		sql += ` LIMIT ?,?  `
		_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	}
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}
