package Task

import (
	"ColdVerify_server/lib"
	"ColdVerify_server/logs"
	"ColdVerify_server/models/Account"
	"github.com/beego/beego/v2/adapter/orm"
	orm2 "github.com/beego/beego/v2/client/orm"
	_ "github.com/go-sql-driver/mysql"
	"time"
)

// 模版
type TaskTime struct {
	Id int `orm:"column(ID);size(11);auto;pk"`

	T_task_id    string `orm:"size(256);"`     // 任务ID
	T_task_type  int    `orm:"size(256);"`     // 任务类型 方案0 实施1 报告2
	T_uuid       string `orm:"size(256);null"` // 用户 UUID
	T_start_time string `orm:"size(256);null"` // 开始时间
	T_end_time   string `orm:"size(256);null"` // 结束时间
	T_remarks    string `orm:"type(text)"`     // 备注

	CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
	UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"`     //auto_now 每次 model 保存时都会对时间自动更新
}

func (t *TaskTime) TableName() string {
	return "tasktime" // 数据库名称   // ************** 替换 FormulaList **************
}

func init() {
	//注册模型
	orm.RegisterModel(new(TaskTime))
}

// -------------------------------------------------------------
type TaskTime_R struct {
	Id           int
	T_task_id    string // 任务ID
	T_task_type  int    // 任务类型 方案0 实施1 报告2
	T_uuid       string // 用户 UUID
	T_uuid_name  string // 用户 UUID
	T_start_time string // 开始时间
	T_end_time   string // 结束时间
	T_remarks    string // 验证方案客户签字确认图片

}

// -------------------------------------------------------------
func TaskTimeToTaskTime_R(T TaskTime) (T_r TaskTime_R) {
	T_r.Id = T.Id
	T_r.T_task_id = T.T_task_id
	T_r.T_task_type = T.T_task_type
	T_r.T_uuid = T.T_uuid
	if err, r := Account.Read_Admin_ByT_uuid(T.T_uuid); err == nil {
		T_r.T_uuid_name = r.T_name
	}
	T_r.T_start_time = T.T_start_time
	T_r.T_end_time = T.T_end_time
	T_r.T_remarks = T.T_remarks
	return T_r
}

// ---------------- 特殊方法 -------------------

// 获取 ById
func Read_TaskTime_ById(id int) (r TaskTime, is bool) {
	o := orm.NewOrm()
	r = TaskTime{Id: id}
	err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return r, false
	}
	return r, true
}

// 获取 By
func Read_TaskTime(T_TaskTime_id string) (r TaskTime, is bool) {

	o := orm.NewOrm()
	qs := o.QueryTable(new(TaskTime))
	err := qs.Filter("T_TaskTime_id", T_TaskTime_id).One(&r)
	if err != nil {
		return r, false
	}

	return r, true
}

func Read_TaskTime_T_name(T_name string) (r TaskTime, is bool) {

	o := orm.NewOrm()
	qs := o.QueryTable(new(TaskTime))
	err := qs.Filter("T_name", T_name).One(&r)
	if err != nil {
		return r, false
	}

	return r, true
}

// 添加
func Add_TaskTime(r TaskTime) (is bool) {
	o := orm.NewOrm()

	_, err := o.Insert(&r)
	if err != nil {
		logs.Error(lib.FuncName(), err)
		return false
	}

	return true
}

// 删除
func Delete_TaskTime(v TaskTime) bool {
	o := orm.NewOrm()
	if num, err := o.Delete(&v); err == nil {
		logs.Println("Number of records deleted in database:", num)
	} else {
		return false
	}

	return true
}

// 修改
func Update_TaskTime(m TaskTime, cols ...string) bool {
	o := orm.NewOrm()
	if num, err := o.Update(&m, cols...); err == nil {
		logs.Println("Number of records updated in database:", num)
		return true
	}
	return false
}

// 获取列表
func Read_TaskTime_List(T_task_id string, T_task_type int) ([]TaskTime_R, int64) {

	o := orm.NewOrm()
	// 也可以直接使用 Model 结构体作为表名
	var r []TaskTime
	qs := o.QueryTable(new(TaskTime))

	cond := orm.NewCondition()
	cond1 := cond.And("T_task_id", T_task_id).And("T_task_type", T_task_type)

	qs.SetCond((*orm2.Condition)(cond1)).OrderBy("Id").All(&r)
	cnt, _ := qs.SetCond((*orm2.Condition)(cond1)).Count()
	// 转换
	var TaskTime_r []TaskTime_R
	for _, v := range r {
		TaskTime_r = append(TaskTime_r, TaskTimeToTaskTime_R(v))
	}

	return TaskTime_r, cnt
}

// 获取项目延时
func Read_TaskTime_Compute(T_task_id string, T_task_type int) int64 {
	List, _ := Read_TaskTime_List(T_task_id, T_task_type)
	var time_interval_Compute float64
	for _, v := range List {
		if len(v.T_end_time) == 0 {
			continue
		}

		_, err := time.ParseInLocation("2006-01-02 15:04:05", v.T_end_time, time.Local)
		if err != nil {
			continue
		}
		_, err = time.ParseInLocation("2006-01-02 15:04:05", v.T_start_time, time.Local)
		if err != nil {
			continue
		}
		time_interval, err := lib.MinutesDifference(v.T_start_time, v.T_end_time)
		if err != nil {
			continue
		}

		time_interval_Compute += time_interval
	}

	logs.Println("Read_TaskTime_Compute", T_task_id, T_task_type, time_interval_Compute)
	return int64(time_interval_Compute)
}
