package dao

import (
	"youngee_b_api/app/entity"
	"youngee_b_api/app/vo"
)

type ProjectTaskInfoDao struct{}

// 获取指定违约类型的种草子任务数量
func (d ProjectTaskInfoDao) CountByDefaultType(projectId string, defaultType int64) int64 {
	var total int64
	Db.Model(&entity.ProjectTaskInfo{}).Where("project_id = ? AND cur_default_type = ?", projectId, defaultType).Count(&total)
	return total
}

// 获取指定任务阶段的种草子任务数量
func (d ProjectTaskInfoDao) CountByTaskStage(projectId string, taskStage int64) int64 {
	var total int64
	Db.Model(&entity.ProjectTaskInfo{}).Where("project_id = ? AND task_stage = ?", projectId, taskStage).Count(&total)
	return total
}

// 获取指定任务阶段的种草子任务
func (d ProjectTaskInfoDao) GetListByTaskStage(projectId string, taskStage int64, time string, page int, pageSize int, talentNickname string) ([]*entity.ProjectTaskInfo, int64, error) {
	var taskInfos []*entity.ProjectTaskInfo
	var total int64
	query := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id = ? AND task_stage = ?", projectId, taskStage)
	if talentNickname != "" {
		var talentInfos []entity.YoungeeTalentInfo
		err1 := Db.Model(&entity.YoungeeTalentInfo{}).Where("talent_wx_nickname = ?", talentNickname).Find(&talentInfos).Error
		if err1 != nil {
			return nil, 0, err1
		}
		var talentIds []string
		for _, talentInfo := range talentInfos {
			talentIds = append(talentIds, talentInfo.ID)
		}
		query = query.Where("talent_id in ?", talentIds)
	}
	// 计算偏移量
	offset := (page - 1) * pageSize
	var err error
	if taskStage == 4 {
		query.Count(&total)
		err = query.Order("create_date desc").Offset(offset).Limit(pageSize).Find(&taskInfos).Error
	} else if taskStage == 5 {
		if time != "" {
			query = query.Where("DATE(delivery_date) = ?", time)
		}
		query.Count(&total)
		err = query.Order("delivery_date desc").Offset(offset).Limit(pageSize).Find(&taskInfos).Error
	} else if taskStage == 6 {
		query.Count(&total)
		err = query.Order("signed_time desc").Offset(offset).Limit(pageSize).Find(&taskInfos).Error
	}
	if err != nil {
		return nil, 0, err
	}
	return taskInfos, total, nil
}

// 获取未传初稿的种草子任务数据
func (d ProjectTaskInfoDao) GetListBySketchDefault(param *vo.DefaultSearchParam) ([]entity.ProjectTaskInfo, int64, error) {
	projectTaskInfos := []entity.ProjectTaskInfo{}
	var total int64
	query := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id = ? AND cur_default_type = ?", param.TaskId, 4)
	query.Count(&total)
	query = query.Select("task_id, talent_id, settle_amount, draft_fee, sketch_missing_time")
	offset := (param.Page - 1) * param.PageSize
	if err := query.Order("sketch_missing_time desc").Offset(offset).Limit(param.PageSize).Find(&projectTaskInfos).Error; err != nil {
		return nil, 0, err
	}
	return projectTaskInfos, total, nil
}

// 获取未发作品的种草子任务数据
func (d ProjectTaskInfoDao) GetListByLinkDefault(param *vo.DefaultSearchParam) ([]entity.ProjectTaskInfo, int64, error) {
	projectTaskInfos := []entity.ProjectTaskInfo{}
	var total int64
	query := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id = ? AND cur_default_type = ?", param.TaskId, 6)
	query.Count(&total)
	query = query.Select("task_id, talent_id, settle_amount, draft_fee, link_missing_time")
	offset := (param.Page - 1) * param.PageSize
	if err := query.Order("link_missing_time desc").Offset(offset).Limit(param.PageSize).Find(&projectTaskInfos).Error; err != nil {
		return nil, 0, err
	}
	return projectTaskInfos, total, nil
}

// 获取未传数据的种草子任务数据
func (d ProjectTaskInfoDao) GetListByDataDefault(param *vo.DefaultSearchParam) ([]entity.ProjectTaskInfo, int64, error) {
	projectTaskInfos := []entity.ProjectTaskInfo{}
	var total int64
	query := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id = ? AND cur_default_type = ?", param.TaskId, 8)
	query.Count(&total)
	query = query.Select("task_id, talent_id, settle_amount, draft_fee, data_missing_time")
	offset := (param.Page - 1) * param.PageSize
	if err := query.Order("data_missing_time desc").Offset(offset).Limit(param.PageSize).Find(&projectTaskInfos).Error; err != nil {
		return nil, 0, err
	}
	return projectTaskInfos, total, nil
}

// 获取终止合作的种草子任务数据
func (d ProjectTaskInfoDao) GetListByTerminateDefault(param *vo.DefaultSearchParam) ([]entity.ProjectTaskInfo, int64, error) {
	projectTaskInfos := []entity.ProjectTaskInfo{}
	var total int64
	query := Db.Debug().Model(&entity.ProjectTaskInfo{}).Where("project_id = ? AND task_stage = ?", param.TaskId, 17)
	query.Count(&total)
	query = query.Select("task_id, talent_id, settle_amount, draft_fee, terminate_time, terminate_reason, terminate_operator_type, terminate_operator")
	offset := (param.Page - 1) * param.PageSize
	if err := query.Order("terminate_time desc").Offset(offset).Limit(param.PageSize).Find(&projectTaskInfos).Error; err != nil {
		return nil, 0, err
	}
	return projectTaskInfos, total, nil
}

// 获取已解约的种草子任务数据
func (d ProjectTaskInfoDao) GetListByCancelDefault(param *vo.DefaultSearchParam) ([]entity.ProjectTaskInfo, int64, error) {
	projectTaskInfos := []entity.ProjectTaskInfo{}
	var total int64
	query := Db.Debug().Model(&entity.ProjectTaskInfo{}).Where("project_id = ? AND task_stage = ?", param.TaskId, 16)
	query.Count(&total)
	query = query.Select("task_id, talent_id, settle_amount, draft_fee, cancel_time, cancel_reason, cancel_operator_type, cancel_operator")
	offset := (param.Page - 1) * param.PageSize
	if err := query.Order("cancel_time desc").Offset(offset).Limit(param.PageSize).Find(&projectTaskInfos).Error; err != nil {
		return nil, 0, err
	}
	return projectTaskInfos, total, nil
}

// 更新字段
func (d ProjectTaskInfoDao) UpdateField(taskId string, updateData map[string]interface{}) error {
	err := Db.Model(&entity.ProjectTaskInfo{}).Where("task_id = ?", taskId).Updates(updateData).Error
	if err != nil {
		return err
	}
	return nil
}

// 批量更新字段
func (d ProjectTaskInfoDao) UpdateFieldBatch(taskIds []string, updateData map[string]interface{}) error {
	err := Db.Model(&entity.ProjectTaskInfo{}).Where("task_id IN ?", taskIds).Updates(updateData).Error
	if err != nil {
		return err
	}
	return nil
}
