package dao

import (
	"errors"
	"fmt"
	"gorm.io/gorm"
	"time"
	"youngee_b_api/app/entity"
	"youngee_b_api/app/vo"
)

type LocalLifeDao struct{}

// 根据localId获取localLife信息
func (d LocalLifeDao) GetLocalById(localId string) (*entity.LocalLifeInfo, error) {
	var localLife entity.LocalLifeInfo
	err := Db.Model(&entity.LocalLifeInfo{}).Where("local_id = ?", localId).First(&localLife).Error
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, nil
		} else {
			return nil, err
		}
	}
	return &localLife, err
}

// 根据localId获取违约状态id
func (d LocalLifeDao) GetAutoDefaultId(localId string) (*int64, error) {
	var autoDefaultId int64
	err := Db.Model(&entity.LocalLifeInfo{}).Where("local_id = ?", localId).Select("auto_default_id").Find(&autoDefaultId).Error
	if err != nil {
		return nil, nil
	}
	return &autoDefaultId, nil
}

// 根据enterpriseId查询指定某天的所有本地生活数据
func (d LocalLifeDao) GetLocalListOfDay(enterpriseId string, date time.Time) ([]entity.LocalLifeInfo, error) {
	var localLifes []entity.LocalLifeInfo
	// 构建查询
	query := Db.Model(&entity.LocalLifeInfo{})
	if enterpriseId != "" {
		query = query.Where("enterprise_id = ?", enterpriseId)
	}
	// 将日期部分提取出来进行匹配
	query = query.Where("DATE(created_at) = ?", date.Format("2006-01-02"))
	err := query.Find(&localLifes).Error
	return localLifes, err
}

// 创建本地生活任务
func (d LocalLifeDao) CreateLocalLife(localLife entity.LocalLifeInfo) error {
	err := Db.Omit("auto_fail_at", "pay_at", "submit_at", "pass_at", "finish_at").Create(&localLife).Error
	if err != nil {
		return err
	}
	return nil
}

// 更新本地生活任务
func (d LocalLifeDao) UpdateLocal(localLife entity.LocalLifeInfo) error {
	err := Db.Model(&entity.LocalLifeInfo{}).Where("local_id = ?", localLife.LocalID).Updates(localLife).Error
	if err != nil {
		return err
	}
	return nil
}

// 更新开票状态字段
func (d LocalLifeDao) UpdateInvoiceStatus(localIDs []string) error {
	err := Db.Debug().Model(&entity.LocalLifeInfo{}).Where("local_id IN ?", localIDs).Updates(entity.LocalLifeInfo{InvoiceStatus: 1}).Error
	if err != nil {
		return err
	}
	return nil
}

// 获取本地生活任务列表
func (d LocalLifeDao) GetLocalPreviews(param *vo.LocalSearchParam) ([]vo.ReLocalTaskPreview, int64, error) {
	var reLocalTaskPreviews []vo.ReLocalTaskPreview
	var localLifes []entity.LocalLifeInfo
	var total int64
	query := Db.Model(&entity.LocalLifeInfo{})
	// 动态添加查询条件
	if param.SubAccountId == 0 {
		if param.EnterpriseId == "" {
			return reLocalTaskPreviews, 0, errors.New("enterpriseId is empty")
		}
		query = query.Where("enterprise_id = ?", param.EnterpriseId)
	} else {
		query = query.Where("sub_account_id = ?", param.SubAccountId)
	}
	if param.LocalType != 0 {
		query = query.Where("local_type = ?", param.LocalType)
	}
	if param.LocalPlatform != 0 {
		query = query.Where("local_platform = ?", param.LocalPlatform)
	}
	if param.LocalStatus != 0 {
		query = query.Where("task_status = ?", param.LocalStatus)
	} else {
		query = query.Where("task_status not in ?", []int{1, 3})
	}
	if param.LocalForm != 0 {
		query = query.Where("task_form = ?", param.LocalForm)
	}
	if param.ContentType != 0 {
		query = query.Where("content_type = ?", param.ContentType)
	}
	if param.Others != "" {
		query = query.Where("enterprise_id = ? or local_id = ? or local_name LIKE ?", param.Others, param.Others, "%"+param.Others+"%")
	}
	query.Count(&total)
	query = query.Select("enterprise_id, sub_account_id, local_id, local_platform, task_status, estimated_cost, need_pay, task_form, content_type, need_review, need_quality, need_calculate, store_id, team_buying_id, tools")
	offset := (param.Page - 1) * param.PageSize
	if param.Order == 1 {
		if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&localLifes).Error; err != nil {
			return nil, 0, err
		}
	} else {
		if err := query.Order("created_at desc").Offset(offset).Limit(param.PageSize).Find(&localLifes).Error; err != nil {
			return nil, 0, err
		}
	}
	for _, localLife := range localLifes {
		reLocalTaskPreview := vo.ReLocalTaskPreview{
			EnterpriseId:  localLife.EnterpriseID,
			SubAccountId:  localLife.SubAccountID,
			LocalId:       localLife.LocalID,
			LocalPlatform: localLife.LocalPlatform,
			LocalStatus:   localLife.TaskStatus,
			LocalForm:     localLife.TaskForm,
			ContentType:   localLife.ContentType,
			NeedReview:    localLife.NeedReview,
			NeedQuality:   localLife.NeedQuality,
			NeedCalculate: localLife.NeedCalculate,
			StoreId:       localLife.StoreID,
			TeamBuyingId:  localLife.TeamBuyingId,
			Tools:         localLife.Tools,
		}
		if localLife.TaskStatus < 6 {
			reLocalTaskPreview.EstimatedCost = localLife.EstimatedCost
		} else {
			reLocalTaskPreview.EstimatedCost = localLife.NeedPay
		}
		reLocalTaskPreviews = append(reLocalTaskPreviews, reLocalTaskPreview)
	}

	return reLocalTaskPreviews, total, nil
}

// 删除本地生活任务
func (d LocalLifeDao) DeleteLocalLife(localId string) (*string, error) {
	if localId == "" {
		return &localId, nil
	}
	err := Db.Model(&entity.LocalLifeInfo{}).Where("local_id = ?", localId).Delete(&entity.LocalLifeInfo{}).Error
	if err != nil {
		return nil, err
	}
	return &localId, nil
}

// 获取草稿箱——本地生活任务列表
func (d LocalLifeDao) GetLocalDraftList(param *vo.LocalDraftParam) ([]vo.ReLocalTaskPreview, int64, error) {
	var reLocalTaskPreviews []vo.ReLocalTaskPreview
	var localLifeInfos []entity.LocalLifeInfo
	var total int64
	query := Db.Model(&entity.LocalLifeInfo{}).Where("task_status = ?", 1)
	// 动态添加查询条件
	if param.SubAccountId == 0 {
		if param.EnterpriseId == "" {
			return reLocalTaskPreviews, 0, errors.New("enterpriseId is empty")
		}
		query = query.Where("enterprise_id = ?", param.EnterpriseId)
	} else {
		query = query.Where("sub_account_id = ?", param.SubAccountId)
	}
	if param.LocalType != 0 {
		query = query.Where("local_type = ?", param.LocalType)
	}
	if param.LocalPlatform != 0 {
		query = query.Where("local_platform = ?", param.LocalPlatform)
	}
	if param.Others != "" {
		query = query.Where("enterprise_id = ? or local_id = ? or local_name LIKE ?", param.Others, param.Others, "%"+param.Others+"%")
	}
	query.Count(&total)
	query = query.Select("enterprise_id, sub_account_id, local_id, local_platform, local_type, created_at, store_id")
	offset := (param.Page - 1) * param.PageSize
	if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&localLifeInfos).Error; err != nil {
		return nil, 0, err
	}
	for _, localLifeInfo := range localLifeInfos {
		reLocalTaskPreview := vo.ReLocalTaskPreview{
			EnterpriseId:  localLifeInfo.EnterpriseID,
			SubAccountId:  localLifeInfo.SubAccountID,
			LocalId:       localLifeInfo.LocalID,
			LocalPlatform: localLifeInfo.LocalPlatform,
			LocalType:     localLifeInfo.LocalType,
			CreatedAt:     localLifeInfo.CreatedAt.Format("2006-01-02 15:04:05"),
			StoreId:       localLifeInfo.StoreID,
		}
		reLocalTaskPreviews = append(reLocalTaskPreviews, reLocalTaskPreview)
	}

	return reLocalTaskPreviews, total, nil
}

// 获取公开本地生活中全部指定状态值的项目
func (d LocalLifeDao) GetLocalLifeList(value int64, fieldName string) ([]*entity.LocalLifeInfo, error) {
	var localLifeInfos []*entity.LocalLifeInfo
	err := Db.Model(entity.LocalLifeInfo{}).Where(fmt.Sprintf("%s = ? ", fieldName), value).Find(&localLifeInfos).Error
	if err != nil {
		return nil, err
	}
	return localLifeInfos, nil
}

// 违约管理——违约公开本地生活任务列表
func (d LocalLifeDao) GetLocalPublicList(param *vo.DefaultSearchParam) ([]vo.ReTaskDefaultPublic, int64, error) {
	var reTaskDefaultPublics []vo.ReTaskDefaultPublic
	var localLifeInfos []entity.LocalLifeInfo
	var total int64
	query := Db.Model(&entity.LocalLifeInfo{}).Where("local_type = ?", 1)
	// 动态添加查询条件
	if param.SubAccountId == 0 {
		if param.EnterpriseId == "" {
			return reTaskDefaultPublics, 0, errors.New("enterpriseId is empty")
		}
		query = query.Where("enterprise_id = ?", param.EnterpriseId)
	} else {
		query = query.Where("sub_account_id = ?", param.SubAccountId)
	}
	if param.Platform != 0 {
		query = query.Where("local_platform = ?", param.Platform)
	}
	if param.Others != "" {
		query = query.Where("enterprise_id = ? or local_id = ? or local_name LIKE ?", param.Others, param.Others, "%"+param.Others+"%")
	}
	query.Count(&total)
	query = query.Select("enterprise_id, sub_account_id, local_id, local_platform, task_form, content_type, store_id")
	offset := (param.Page - 1) * param.PageSize
	if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&localLifeInfos).Error; err != nil {
		return nil, 0, err
	}
	for _, localLife := range localLifeInfos {
		reTaskDefaultPublic := vo.ReTaskDefaultPublic{
			EnterpriseId: localLife.EnterpriseID,
			SubAccountId: localLife.SubAccountID,
			TaskId:       localLife.LocalID,
			Platform:     localLife.LocalPlatform,
			TaskForm:     localLife.TaskForm,
			ContentType:  localLife.ContentType,
			TaskType:     1,
			StoreId:      localLife.StoreID,
		}
		reTaskDefaultPublics = append(reTaskDefaultPublics, reTaskDefaultPublic)
	}

	return reTaskDefaultPublics, total, nil
}

// 探店本地生活列表
func (d LocalLifeDao) GetLocalStoreExplorePreviews(param *vo.LocalSearchParam) ([]vo.ReLocalStoreExplorePreview, int64, error) {
	var reLocalStoreExplorePreviews []vo.ReLocalStoreExplorePreview
	var localLifes []entity.LocalLifeInfo
	var total int64
	query := Db.Model(&entity.LocalLifeInfo{})
	// 动态添加查询条件
	if param.SubAccountId == 0 {
		if param.EnterpriseId == "" {
			return reLocalStoreExplorePreviews, 0, errors.New("enterpriseId is empty")
		}
		query = query.Where("enterprise_id = ?", param.EnterpriseId)
	} else {
		query = query.Where("sub_account_id = ?", param.SubAccountId)
	}
	if param.LocalType != 0 {
		query = query.Where("local_type = ?", param.LocalType)
	}
	if param.LocalPlatform != 0 {
		query = query.Where("local_platform = ?", param.LocalPlatform)
	}
	if param.Others != "" {
		query = query.Where("enterprise_id = ? or local_id = ? or local_name LIKE ?", param.Others, param.Others, "%"+param.Others+"%")
	}
	query = query.Where("task_status = ? AND task_form = ?", 8, 1)
	query.Count(&total)
	query = query.Select("enterprise_id, sub_account_id, local_id, local_type, local_platform, need_reserve, need_confirm, need_explore, explored_num, store_id, team_buying_id")
	offset := (param.Page - 1) * param.PageSize
	if err := query.Order("pay_at asc").Offset(offset).Limit(param.PageSize).Find(&localLifes).Error; err != nil {
		return nil, 0, err
	}
	for _, localLife := range localLifes {
		reLocalStoreExplorePreview := vo.ReLocalStoreExplorePreview{
			EnterpriseId:  localLife.EnterpriseID,
			SubAccountId:  localLife.SubAccountID,
			LocalId:       localLife.LocalID,
			LocalPlatform: localLife.LocalPlatform,
			LocalType:     localLife.LocalType,
			NeedReserve:   localLife.NeedReserve,
			NeedConfirm:   localLife.NeedConfirm,
			NeedExplore:   localLife.NeedExplore,
			ExploredNum:   localLife.ExploredNum,
			StoreId:       localLife.StoreID,
			TeamBuyingId:  localLife.TeamBuyingId,
		}
		reLocalStoreExplorePreviews = append(reLocalStoreExplorePreviews, reLocalStoreExplorePreview)
	}

	return reLocalStoreExplorePreviews, total, nil
}

// 获取本地生活账单列表
func (d LocalLifeDao) GetBillLocalPreviews(param *vo.LocalSearchParam) ([]vo.ReBillLocalTaskPreview, int64, error) {
	var reBillLocalTaskPreviews []vo.ReBillLocalTaskPreview
	var localLifes []entity.LocalLifeInfo
	var total int64
	query := Db.Model(&entity.LocalLifeInfo{})
	// 动态添加查询条件
	if param.SubAccountId == 0 {
		if param.EnterpriseId == "" {
			return reBillLocalTaskPreviews, 0, errors.New("enterpriseId is empty")
		}
		query = query.Where("enterprise_id = ?", param.EnterpriseId)
	} else {
		query = query.Where("sub_account_id = ?", param.SubAccountId)
	}
	if param.LocalType != 0 {
		query = query.Where("local_type = ?", param.LocalType)
	}
	if param.LocalPlatform != 0 {
		query = query.Where("local_platform = ?", param.LocalPlatform)
	}
	if param.LocalStatus != 0 {
		query = query.Where("task_status = ?", param.LocalStatus)
	}
	if param.Others != "" {
		query = query.Where("enterprise_id = ? or local_id = ? or local_name LIKE ?", param.Others, param.Others, "%"+param.Others+"%")
	}
	query.Count(&total)
	query = query.Select("enterprise_id, sub_account_id, local_id, local_platform, task_status, estimated_cost, need_pay, task_form, content_type, store_id, team_buying_id, settlement_amount")
	offset := (param.Page - 1) * param.PageSize
	if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&localLifes).Error; err != nil {
		return nil, 0, err
	}
	for _, localLife := range localLifes {
		reBillLocalTaskPreview := vo.ReBillLocalTaskPreview{
			EnterpriseId:  localLife.EnterpriseID,
			SubAccountId:  localLife.SubAccountID,
			LocalId:       localLife.LocalID,
			LocalPlatform: localLife.LocalPlatform,
			LocalStatus:   localLife.TaskStatus,
			LocalForm:     localLife.TaskForm,
			ContentType:   localLife.ContentType,
			StoreId:       localLife.StoreID,
			TeamBuyingId:  localLife.TeamBuyingId,
			CashAmount:    localLife.SettlementAmount,
		}
		if localLife.TaskStatus < 6 {
			reBillLocalTaskPreview.EstimatedCost = localLife.EstimatedCost
		} else {
			reBillLocalTaskPreview.EstimatedCost = localLife.NeedPay
		}
		reBillLocalTaskPreviews = append(reBillLocalTaskPreviews, reBillLocalTaskPreview)
	}

	return reBillLocalTaskPreviews, total, nil
}

// 本地生活任务待办
func (d LocalLifeDao) GetLocalLifeToDo(enterpriseId string, subAccountId int64, platform int64, taskType int64) (map[string]int64, error) {
	resultMap := make(map[string]int64)
	var needReview int64 // 待审核
	var needPay int64
	var needProcess int64
	var needCheck int64     // 初稿待审
	var needQuality int64   // 链接待审
	var needCalculate int64 // 待结算
	var localInfos []entity.LocalLifeInfo
	//query := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
	if subAccountId == 0 {
		// 待审核、待支付、达人未处理、初稿待审、链接待审、待结算
		query1 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
		query1.Where("task_status = ?", 2).Count(&needReview)
		query2 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
		query2.Where("task_status = ?", 6).Count(&needPay)
		query3 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
		err := query3.Where("task_status = ?", 8).Select("local_id").Find(&localInfos).Error
		if err != nil {
			if errors.Is(err, gorm.ErrRecordNotFound) {
				needProcess = 0
			} else {
				return resultMap, err
			}
		} else if len(localInfos) > 0 {
			var localIDs []string
			for _, info := range localInfos {
				localIDs = append(localIDs, info.LocalID)
			}
			if len(localIDs) > 0 {
				err1 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and task_status = ?", localIDs, 1).Count(&needProcess).Error // task_status=1待选
				if err1 != nil {
					needProcess = 0
				}
			}
		}
		query4 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
		err1 := query4.Select("need_review, need_quality, need_calculate").Find(&localInfos).Error
		if err1 != nil {
			return resultMap, err1
		} else if len(localInfos) > 0 {
			for _, info := range localInfos {
				needCheck += info.NeedReview
				needQuality += info.NeedQuality
				needCalculate += info.NeedCalculate
			}
		}
	} else {
		// 待审核、待支付、达人未处理
		query1 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
		query1.Where("sub_account_id = ? and task_status = ?", subAccountId, 2).Count(&needReview)
		query2 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
		query2.Where("sub_account_id = ? and task_status = ?", subAccountId, 6).Count(&needPay)
		query3 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
		err := query3.Where("sub_account_id = ? and task_status = ?", subAccountId, 8).Select("local_id").Find(&localInfos).Error
		if err != nil {
			if errors.Is(err, gorm.ErrRecordNotFound) {
				needProcess = 0
			} else {
				return resultMap, err
			}
		} else {
			var localIDs []string
			for _, info := range localInfos {
				localIDs = append(localIDs, info.LocalID)
			}
			if len(localIDs) > 0 {
				err1 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and task_status = ?", localIDs, 1).Count(&needProcess).Error // task_status=1待选
				if err1 != nil {
					needProcess = 0
				}
			}
		}
		query4 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
		err1 := query4.Select("need_review, need_quality, need_calculate").Find(&localInfos).Error
		if err1 != nil {
			return resultMap, err1
		} else if len(localInfos) > 0 {
			for _, info := range localInfos {
				needCheck += info.NeedReview
				needQuality += info.NeedQuality
				needCalculate += info.NeedCalculate
			}
		}
	}
	resultMap["needReview"] = needReview
	resultMap["needPay"] = needPay
	resultMap["needProcess"] = needProcess
	resultMap["needCheck"] = needCheck
	resultMap["needQuality"] = needQuality
	resultMap["needCalculate"] = needCalculate
	return resultMap, nil
}

// 探店邀约任务待办
func (d LocalLifeDao) GetExploreToDo(enterpriseId string, subAccountId int64, platform int64) (map[string]int64, error) {
	resultMap := make(map[string]int64)
	var needBook int64    // 待预约探店时间
	var needConfirm int64 // 探店时间待确认
	var needExplore int64 // 达人待探店
	var localInfos []entity.LocalLifeInfo
	//query := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
	if subAccountId == 0 {
		// 待预约探店时间、探店时间待确认、达人待探店
		query1 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and task_form = ?", enterpriseId, platform, 1)
		err := query1.Where("task_status = ?", 8).Select("local_id").Find(&localInfos).Error
		if err != nil {
			if errors.Is(err, gorm.ErrRecordNotFound) {
				needBook = 0
				needConfirm = 0
				needExplore = 0
			} else {
				return resultMap, err
			}
		} else if len(localInfos) > 0 {
			var localIDs []string
			for _, info := range localInfos {
				localIDs = append(localIDs, info.LocalID)
			}
			if len(localIDs) > 0 {
				err1 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and task_stage = ?", localIDs, 4).Count(&needBook).Error // task_stage=4待预约探店
				if err1 != nil {
					needBook = 0
				}
				err2 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and task_stage = ?", localIDs, 5).Count(&needConfirm).Error // task_stage=4预约确认中
				if err2 != nil {
					needConfirm = 0
				}
				err3 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and book_status = ?", localIDs, 5).Count(&needExplore).Error // book_status=5达人待探店
				if err3 != nil {
					needExplore = 0
				}
			}
		}
	} else {
		// 待预约探店时间、探店时间待确认、达人待探店
		query1 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and task_form = ?", enterpriseId, platform, 1)
		err := query1.Where("sub_account_id = ? and task_status = ?", subAccountId, 8).Select("local_id").Find(&localInfos).Error
		if err != nil {
			if errors.Is(err, gorm.ErrRecordNotFound) {
				needBook = 0
				needConfirm = 0
				needExplore = 0
			} else {
				return resultMap, err
			}
		} else if len(localInfos) > 0 {
			var localIDs []string
			for _, info := range localInfos {
				localIDs = append(localIDs, info.LocalID)
			}
			if len(localIDs) > 0 {
				err1 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and task_stage = ?", localIDs, 4).Count(&needBook).Error // task_stage=4待预约探店
				if err1 != nil {
					needBook = 0
				}
				err2 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and task_stage = ?", localIDs, 5).Count(&needConfirm).Error // task_stage=4预约确认中
				if err2 != nil {
					needConfirm = 0
				}
				err3 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and book_status = ?", localIDs, 5).Count(&needExplore).Error // book_status=5达人待探店
				if err3 != nil {
					needExplore = 0
				}
			}
		}
	}
	resultMap["needBook"] = needBook
	resultMap["needConfirm"] = needConfirm
	resultMap["needExplore"] = needExplore

	return resultMap, nil
}

// 违约管理任务待办
func (d LocalLifeDao) GetDefaultToDo(enterpriseId string, subAccountId int64, platform int64, taskType int64) (map[string]int64, error) {
	resultMap := make(map[string]int64)
	var noSketch int64      // 未传初稿
	var noWork int64        // 未发作品
	var noData int64        // 未传数据
	var cooperateOver int64 // 终止合作
	var localInfos []entity.LocalLifeInfo
	//query := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
	if subAccountId == 0 {
		// 未传初稿、未发作品、未传数据、终止合作
		query1 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
		err := query1.Where("task_status = ?", 8).Select("local_id").Find(&localInfos).Error
		if err != nil {
			if errors.Is(err, gorm.ErrRecordNotFound) {
				noSketch = 0
				noWork = 0
				noData = 0
				cooperateOver = 0
			} else {
				return resultMap, err
			}
		} else if len(localInfos) > 0 {
			var localIDs []string
			for _, info := range localInfos {
				localIDs = append(localIDs, info.LocalID)
			}
			if len(localIDs) > 0 {
				err1 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and cur_default_type = ?", localIDs, 4).Count(&noSketch).Error // cur_default_type=4 初稿未上传违约
				if err1 != nil {
					noSketch = 0
				}
				err2 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and cur_default_type = ?", localIDs, 6).Count(&noWork).Error // cur_default_type=6 链接未上传违约
				if err2 != nil {
					noWork = 0
				}
				err3 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and cur_default_type = ?", localIDs, 8).Count(&noData).Error // cur_default_type=8 数据未上传违约
				if err3 != nil {
					noData = 0
				}
				err4 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and cur_default_type = ?", localIDs, 10).Count(&cooperateOver).Error // cur_default_type=10 解约
				if err4 != nil {
					cooperateOver = 0
				}
			}
		}
	} else {
		// 未传初稿、未发作品、未传数据、终止合作
		query1 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
		err := query1.Where("sub_account_id = ? and task_status = ?", subAccountId, 8).Select("local_id").Find(&localInfos).Error
		if err != nil {
			if errors.Is(err, gorm.ErrRecordNotFound) {
				noSketch = 0
				noWork = 0
				noData = 0
				cooperateOver = 0
			} else {
				return resultMap, err
			}
		} else if len(localInfos) > 0 {
			var localIDs []string
			for _, info := range localInfos {
				localIDs = append(localIDs, info.LocalID)
			}
			if len(localIDs) > 0 {
				err1 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and cur_default_type = ?", localIDs, 4).Count(&noSketch).Error // cur_default_type=4 初稿未上传违约
				if err1 != nil {
					noSketch = 0
				}
				err2 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and cur_default_type = ?", localIDs, 6).Count(&noWork).Error // cur_default_type=6 链接未上传违约
				if err2 != nil {
					noWork = 0
				}
				err3 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and cur_default_type = ?", localIDs, 8).Count(&noData).Error // cur_default_type=8 数据未上传违约
				if err3 != nil {
					noData = 0
				}
				err4 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and cur_default_type = ?", localIDs, 10).Count(&cooperateOver).Error // cur_default_type=10 解约
				if err4 != nil {
					cooperateOver = 0
				}
			}
		}
	}
	resultMap["noSketch"] = noSketch
	resultMap["noWork"] = noWork
	resultMap["noData"] = noData
	resultMap["cooperateOver"] = cooperateOver

	return resultMap, nil
}

// 获取指定商家已结案的指定开票状态数据
func (d LocalLifeDao) GetLocalLifeFinished(enterpriseId string, invoiceStatus int64) (float64, error) {
	var localLifeAmount float64
	err := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and task_status = ? and invoice_status = ?", enterpriseId, 10, invoiceStatus).Select("COALESCE(SUM(settlement_amount), 0)").Find(&localLifeAmount).Error
	if err != nil {
		return 0, err
	}
	return localLifeAmount, err
}

// 合作待办-任务邀约
func (d LocalLifeDao) GetTaskInviteToDo(enterpriseId string, subAccountId int64, platform int64) (map[string]int64, error) {
	resultMap := make(map[string]int64)
	var availInvitationNum int64 //
	var invitingNum int64        //
	var cooperatingNum int64     //
	//var localInfos []entity.LocalLifeInfo
	////query := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
	//if subAccountId == 0 {
	//	// 待预约探店时间、探店时间待确认、达人待探店
	//	query1 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and task_form = ?", enterpriseId, platform, 1)
	//	err := query1.Where("task_status = ?", 8).Select("local_id").Find(&localInfos).Error
	//	if err != nil {
	//		if errors.Is(err, gorm.ErrRecordNotFound) {
	//			needBook = 0
	//			needConfirm = 0
	//			needExplore = 0
	//		} else {
	//			return resultMap, err
	//		}
	//	} else if len(localInfos) > 0 {
	//		var localIDs []string
	//		for _, info := range localInfos {
	//			localIDs = append(localIDs, info.LocalID)
	//		}
	//		if len(localIDs) > 0 {
	//			err1 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and task_stage = ?", localIDs, 4).Count(&needBook).Error // task_stage=4待预约探店
	//			if err1 != nil {
	//				needBook = 0
	//			}
	//			err2 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and task_stage = ?", localIDs, 5).Count(&needConfirm).Error // task_stage=4预约确认中
	//			if err2 != nil {
	//				needConfirm = 0
	//			}
	//			err3 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and book_status = ?", localIDs, 5).Count(&needExplore).Error // book_status=5达人待探店
	//			if err3 != nil {
	//				needExplore = 0
	//			}
	//		}
	//	}
	//} else {
	//	// 待预约探店时间、探店时间待确认、达人待探店
	//	query1 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and task_form = ?", enterpriseId, platform, 1)
	//	err := query1.Where("sub_account_id = ? and task_status = ?", subAccountId, 8).Select("local_id").Find(&localInfos).Error
	//	if err != nil {
	//		if errors.Is(err, gorm.ErrRecordNotFound) {
	//			needBook = 0
	//			needConfirm = 0
	//			needExplore = 0
	//		} else {
	//			return resultMap, err
	//		}
	//	} else if len(localInfos) > 0 {
	//		var localIDs []string
	//		for _, info := range localInfos {
	//			localIDs = append(localIDs, info.LocalID)
	//		}
	//		if len(localIDs) > 0 {
	//			err1 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and task_stage = ?", localIDs, 4).Count(&needBook).Error // task_stage=4待预约探店
	//			if err1 != nil {
	//				needBook = 0
	//			}
	//			err2 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and task_stage = ?", localIDs, 5).Count(&needConfirm).Error // task_stage=4预约确认中
	//			if err2 != nil {
	//				needConfirm = 0
	//			}
	//			err3 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and book_status = ?", localIDs, 5).Count(&needExplore).Error // book_status=5达人待探店
	//			if err3 != nil {
	//				needExplore = 0
	//			}
	//		}
	//	}
	//}
	resultMap["availInvitationNum"] = availInvitationNum
	resultMap["invitingNum"] = invitingNum
	resultMap["cooperatingNum"] = cooperatingNum

	return resultMap, nil
}
