package company

//合同未续约说明
import (
	"github.com/beego/beego/v2/client/orm"
	"time"
)

type CompanyContractNoRenewedAscribe 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"`
	CompanyContractId  int       `description:"合同id"`
	ProductId          int       `description:"产品id"`
	CreateTime         time.Time `description:"创建时间"`
	ModifyTime         time.Time `description:"更新时间"`
}

type CompanyContractNoRenewedAscribeLog 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"`
	CompanyContractId  int       `description:"合同id"`
	ProductId          int       `description:"产品id"`
	CreateTime         time.Time `description:"创建时间"`
	ModifyTime         time.Time `description:"更新时间"`
}

type CompanyContractNoRenewedAscribeResp 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"`
	CompanyContractId  int    `description:"合同id"`
	CreateTime         string `description:"创建时间"`
	ModifyTime         string `description:"更新时间"`
}
type CompanyContractNoRenewedAscribeListResp struct {
	List []*CompanyContractNoRenewedAscribeResp
}

type CompanyContractNoRenewedAscribeDetailResp struct {
	Detail *CompanyContractNoRenewedAscribeResp
}

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

// 添加
func AddCompanyContractNoRenewedAscribe(item *CompanyContractNoRenewedAscribe, itemLog *CompanyContractNoRenewedAscribeLog) (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)

	//修改未续约归因
	sql := `UPDATE company_contract SET company_ascribe_id = ?  WHERE company_contract_id=? `
	_, err = to.Raw(sql, item.CompanyAscribeId, item.CompanyContractId).Exec()
	return
}

// 修改
func UpdateCompanyContractNoRenewedAscribe(item *CompanyContractNoRenewedAscribe, itemLog *CompanyContractNoRenewedAscribeLog) (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_contract_no_renewed_ascribe"
	whereParam := map[string]interface{}{"company_id": item.CompanyId, "company_contract_id": item.CompanyContractId}
	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)

	//修改未续约归因
	sql := `UPDATE company_contract SET company_ascribe_id = ?  WHERE company_contract_id=? `
	_, err = to.Raw(sql, item.CompanyAscribeId, item.CompanyContractId).Exec()
	return
}

// 获取数量
func GetCompanyContractNoRenewedAscribeCount(condition string, pars []interface{}) (count int, err error) {
	sqlCount := ` SELECT COUNT(1) AS count  FROM company_contract_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 GetCompanyContractNoRenewedAscribeDetail(companyContractId int) (item *CompanyContractNoRenewedAscribeResp, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM company_contract_no_renewed_ascribe   WHERE company_contract_id=?  `
	err = o.Raw(sql, companyContractId).QueryRow(&item)
	return
}

// 列表
func GetCompanyContractNoRenewedAscribeList(condition string, pars []interface{}, startSize, pageSize int) (items []*CompanyContractNoRenewedAscribeResp, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM company_contract_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
}
