package company

import (
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"github.com/rdlucklib/rdluck_tools/paging"
	"strings"
	"time"
)

// CompanyServiceRecord 客户-服务记录表
type CompanyServiceRecord struct {
	CompanyServiceRecordId int       `orm:"column(company_service_record_id);pk" description:"服务记录ID"`
	CompanyType            int       `description:"客户类型，1：英文客户，2：中文客户"`
	CompanyId              int       `description:"客户ID"`
	Content                string    `description:"内容"`
	SysAdminId             int       `description:"创建人ID"`
	SysAdminName           string    `description:"创建人姓名"`
	CreateTime             time.Time `description:"创建时间"`
	ModifyTime             time.Time `description:"修改时间"`
	Mark                   int       `description:"是否标记 0不是 1是"`
}

var CompanyServiceRecordColumns = struct {
	CompanyServiceRecordId string
	CompanyType            string
	CompanyId              string
	Content                string
	SysAdminId             string
	SysAdminName           string
	CreateTime             string
	ModifyTime             string
}{
	CompanyServiceRecordId: "company_service_record_id",
	CompanyType:            "company_type",
	CompanyId:              "company_id",
	Content:                "content",
	SysAdminId:             "sys_admin_id",
	SysAdminName:           "sys_admin_name",
	CreateTime:             "create_time",
	ModifyTime:             "modify_time",
}

func (m *CompanyServiceRecord) TableName() string {
	return "company_service_record"
}

func (m *CompanyServiceRecord) Create() (newId int, err error) {
	o := orm.NewOrm()
	id, err := o.Insert(m)
	if err != nil {
		return
	}
	newId = int(id)
	return
}

func (m *CompanyServiceRecord) Update(cols []string) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(m, cols...)
	return
}

func (m *CompanyServiceRecord) Del() (err error) {
	o := orm.NewOrm()
	sql := `DELETE FROM company_service_record WHERE company_service_record_id = ? LIMIT 1`
	_, err = o.Raw(sql, m.CompanyServiceRecordId).Exec()
	return
}

func (m *CompanyServiceRecord) GetItemById(id int) (err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM company_service_record WHERE company_service_record_id = ? LIMIT 1`
	err = o.Raw(sql, id).QueryRow(&m)
	return
}

func (m *CompanyServiceRecord) GetItemByCondition(condition string, pars []interface{}) (err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM company_service_record WHERE 1=1 `
	sql += condition
	sql += ` LIMIT 1`
	err = o.Raw(sql, pars).QueryRow(&m)
	return
}

func (m *CompanyServiceRecord) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	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 *CompanyServiceRecord) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*CompanyServiceRecord, err error) {
	o := orm.NewOrm()
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := `ORDER BY create_time DESC`
	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 *CompanyServiceRecord) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*CompanyServiceRecord, err error) {
	o := orm.NewOrm()
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := `ORDER BY create_time DESC`
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
	totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z`
	if err = o.Raw(totalSql, pars).QueryRow(&total); err != nil {
		return
	}
	sql += ` LIMIT ?,?`
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

type CompanyServiceRecordAddReq struct {
	CompanyType int    `description:"客户类型，0：中文客户，1：英文客户"`
	CompanyId   int    `description:"客户ID"`
	Content     string `description:"内容"`
}

type CompanyServiceRecordDelReq struct {
	CompanyServiceRecordId int `description:"服务记录ID"`
}

type CompanyServiceRecordPageListResp struct {
	List   []*CompanyServiceRecordItem
	Paging *paging.PagingItem `description:"分页数据"`
}

type CompanyServiceRecordItem struct {
	CompanyServiceRecordId int    `description:"文档ID"`
	Content                string `description:"服务描述"`
	SysAdminId             int    `description:"创建人ID"`
	SysAdminName           string `description:"创建人姓名"`
	CreateTime             string `description:"创建时间"`
	Mark                   int    `description:"是否标记 0不是 1是"`
}

// 更新备注标记
func UpdateCompanyServiceRecordMark(remarkId, mark int) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE company_service_record SET mark=? WHERE company_service_record_id=? `
	_, err = o.Raw(sql, mark, remarkId).Exec()
	return
}
