package db

import (
	"context"
	"youngee_b_api/model/gorm_model"
	"youngee_b_api/model/http_model"

	"github.com/sirupsen/logrus"
)

func GetLogisticsNumberInfo(ctx context.Context, projectId string, strategyIds []int64) (*http_model.GetLogisticsNumberInfoData, error) {
	var LogisticsNumberInfoDataList http_model.GetLogisticsNumberInfoData

	for _, strategyId := range strategyIds {
		var LogisticsNumberInfoData http_model.LogisticsNumberInfo
		LogisticsNumberInfoData.StrategyId = strategyId
		db := GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and strategy_id = ? and task_status = 2", projectId, strategyId)
		err := db.Where("logistics_status = 1").Count(&LogisticsNumberInfoData.UndeliveredNumber).Error
		if err != nil {
			logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
			return nil, err
		}
		db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and strategy_id = ? and task_status = 2", projectId, strategyId)
		err = db.Where("logistics_status = 2").Count(&LogisticsNumberInfoData.DeliveredNumber).Error
		if err != nil {
			logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
			return nil, err
		}
		db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and strategy_id = ? and task_status = 2", projectId, strategyId)
		err = db.Where("logistics_status = 3").Count(&LogisticsNumberInfoData.SignedNumber).Error
		if err != nil {
			logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
			return nil, err
		}
		db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and strategy_id = ? and task_status = 2", projectId, strategyId)
		err = db.Count(&LogisticsNumberInfoData.DeliverNumber).Error
		if err != nil {
			logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
			return nil, err
		}

		LogisticsNumberInfoDataList.LogisticsNumberInfoList = append(LogisticsNumberInfoDataList.LogisticsNumberInfoList, &LogisticsNumberInfoData)
	}
	return &LogisticsNumberInfoDataList, nil
}

func GetReviewNumberInfo(ctx context.Context, projectId string, strategyIds []int64) (*http_model.GetReviewNumberInfoData, error) {
	var ReviewNumberInfoDataList http_model.GetReviewNumberInfoData

	for _, strategyId := range strategyIds {
		var ReviewNumberInfoData http_model.ReviewNumberInfo
		ReviewNumberInfoData.StrategyId = strategyId
		db := GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and strategy_id = ? and task_status = 2", projectId, strategyId)
		err := db.Where("task_stage = 8").Count(&ReviewNumberInfoData.ScriptUnreviewNumber).Error
		if err != nil {
			logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
			return nil, err
		}
		db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and strategy_id = ? and task_status = 2", projectId, strategyId)
		err = db.Where("task_stage > 8").Count(&ReviewNumberInfoData.ScriptPassedNumber).Error
		if err != nil {
			logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
			return nil, err
		}
		db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and strategy_id = ? and task_status = 2", projectId, strategyId)
		err = db.Where("task_stage = 10").Count(&ReviewNumberInfoData.SketchUnreviewNumber).Error
		if err != nil {
			logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
			return nil, err
		}
		db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and strategy_id = ? and task_status = 2", projectId, strategyId)
		err = db.Where("task_stage > 10").Count(&ReviewNumberInfoData.SketchPassedNumber).Error
		if err != nil {
			logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
			return nil, err
		}
		db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and strategy_id = ? and task_status = 2", projectId, strategyId)
		err = db.Count(&ReviewNumberInfoData.ReviewNumber).Error
		if err != nil {
			logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
			return nil, err
		}

		ReviewNumberInfoDataList.ReviewNumberInfoList = append(ReviewNumberInfoDataList.ReviewNumberInfoList, &ReviewNumberInfoData)
	}
	return &ReviewNumberInfoDataList, nil
}

func GetLinkNumberInfo(ctx context.Context, projectId string, strategyIds []int64) (*http_model.GetLinkNumberInfoData, error) {
	var LinkNumberInfoDataList http_model.GetLinkNumberInfoData

	for _, strategyId := range strategyIds {
		var LinkNumberInfoData http_model.LinkNumberInfo
		LinkNumberInfoData.StrategyId = strategyId
		db := GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and strategy_id = ? and task_status = 2", projectId, strategyId)
		err := db.Where("task_stage = 12").Count(&LinkNumberInfoData.LinkUnreviewNumber).Error
		if err != nil {
			logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
			return nil, err
		}
		db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and strategy_id = ? and task_status = 2", projectId, strategyId)
		err = db.Where("task_stage > 12").Count(&LinkNumberInfoData.LinkPassedNumber).Error
		if err != nil {
			logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
			return nil, err
		}
		db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and strategy_id = ? and task_status = 2", projectId, strategyId)
		err = db.Count(&LinkNumberInfoData.LinkNumber).Error
		if err != nil {
			logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
			return nil, err
		}

		LinkNumberInfoDataList.LinkNumberInfoList = append(LinkNumberInfoDataList.LinkNumberInfoList, &LinkNumberInfoData)
	}
	return &LinkNumberInfoDataList, nil
}

func GetDataNumberInfo(ctx context.Context, projectId string, strategyIds []int64) (*http_model.GetDataNumberInfoData, error) {
	var DataNumberInfoDataList http_model.GetDataNumberInfoData

	for _, strategyId := range strategyIds {
		var DataNumberInfoData http_model.DataNumberInfo
		DataNumberInfoData.StrategyId = strategyId
		db := GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and strategy_id = ? and task_status = 2", projectId, strategyId)
		err := db.Where("task_stage = 14").Count(&DataNumberInfoData.DataUnreviewNumber).Error
		if err != nil {
			logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
			return nil, err
		}
		db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and strategy_id = ? and task_status = 2", projectId, strategyId)
		err = db.Where("task_stage = 15").Count(&DataNumberInfoData.DataPassedNumber).Error
		if err != nil {
			logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
			return nil, err
		}
		db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and strategy_id = ? and task_status = 2 and task_stage < 15", projectId, strategyId)
		err = db.Count(&DataNumberInfoData.DataNumber).Error
		if err != nil {
			logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
			return nil, err
		}

		DataNumberInfoDataList.DataNumberInfoList = append(DataNumberInfoDataList.DataNumberInfoList, &DataNumberInfoData)
	}
	return &DataNumberInfoDataList, nil
}

func GetDefaultNumberInfo(ctx context.Context, projectId string, strategyIds []int64) (*http_model.GetDefaultNumberInfoData, error) {
	var DefaultNumberInfoDataList http_model.GetDefaultNumberInfoData

	for _, strategyId := range strategyIds {
		var DefaultNumberInfoData http_model.DefaultNumberInfo
		DefaultNumberInfoData.StrategyId = strategyId
		db := GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and strategy_id = ? and task_status = 2", projectId, strategyId)
		err := db.Where("complete_status <> 4").Count(&DefaultNumberInfoData.ShouldFinishNumber).Error
		if err != nil {
			logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
			return nil, err
		}
		db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and strategy_id = ? and task_status = 2", projectId, strategyId)
		err = db.Where("cur_default_type = 1").Count(&DefaultNumberInfoData.UnuploadScriptNumber).Error
		if err != nil {
			logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
			return nil, err
		}
		db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and strategy_id = ? and task_status = 2", projectId, strategyId)
		err = db.Where("cur_default_type = 3").Count(&DefaultNumberInfoData.UnuploadSketchNumber).Error
		if err != nil {
			logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
			return nil, err
		}
		db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and strategy_id = ? and task_status = 2", projectId, strategyId)
		err = db.Where("cur_default_type = 5").Count(&DefaultNumberInfoData.UnuploadLinkNumber).Error
		if err != nil {
			logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
			return nil, err
		}
		db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and strategy_id = ? and task_status = 2", projectId, strategyId)
		err = db.Where("cur_default_type = 7").Count(&DefaultNumberInfoData.UnuploadDataNumber).Error
		if err != nil {
			logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
			return nil, err
		}
		db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and strategy_id = ? and task_status = 2", projectId, strategyId)
		err = db.Where("complete_status = 4").Count(&DefaultNumberInfoData.TerminateNumber).Error
		if err != nil {
			logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
			return nil, err
		}

		DefaultNumberInfoDataList.DefaultNumberInfoList = append(DefaultNumberInfoDataList.DefaultNumberInfoList, &DefaultNumberInfoData)
	}
	return &DefaultNumberInfoDataList, nil
}

func GetFinishNumberInfo(ctx context.Context, projectId string, strategyIds []int64) (*http_model.GetFinishNumberInfoData, error) {
	var FinishNumberInfoDataList http_model.GetFinishNumberInfoData

	for _, strategyId := range strategyIds {
		var FinishNumberInfoData http_model.FinishNumberInfo
		FinishNumberInfoData.StrategyId = strategyId
		db := GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and strategy_id = ? and task_status = 2", projectId, strategyId)
		err := db.Count(&FinishNumberInfoData.ShouldFinishNumber).Error
		if err != nil {
			logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
			return nil, err
		}
		db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and strategy_id = ? and task_status = 2", projectId, strategyId)
		err = db.Where("task_stage = 15").Count(&FinishNumberInfoData.FinishedNumber).Error
		if err != nil {
			logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
			return nil, err
		}
		FinishNumberInfoDataList.FinishNumberInfoList = append(FinishNumberInfoDataList.FinishNumberInfoList, &FinishNumberInfoData)
	}
	return &FinishNumberInfoDataList, nil
}

func GetSpecialInviteNumber(ctx context.Context, projectId string) (*http_model.GetSpecialInviteNumberData, error) {
	var specialInviteNumberData http_model.GetSpecialInviteNumberData
	db := GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? AND task_stage != 3", projectId)
	err := db.Where("task_status <> 2").Count(&specialInviteNumberData.UnpassNumber).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
		return nil, err
	}
	db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ?", projectId)
	err = db.Where("task_status = 2").Count(&specialInviteNumberData.PassNumber).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
		return nil, err
	}
	return &specialInviteNumberData, nil
}

func GetSpecialReviewNumber(ctx context.Context, projectId string) (*http_model.GetSpecialReviewNumberData, error) {
	var specialReviewNumber http_model.GetSpecialReviewNumberData
	db := GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and task_status = 2", projectId)
	err := db.Where("").Count(&specialReviewNumber.ReviewNumber).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
		return nil, err
	}
	db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and task_status = 2", projectId)
	err = db.Where("task_stage = 8").Count(&specialReviewNumber.ScriptUnreviewNumber).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
		return nil, err
	}
	db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and task_status = 2", projectId)
	err = db.Where("task_stage > 8").Count(&specialReviewNumber.ScriptPassedNumber).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
		return nil, err
	}
	db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and task_status = 2", projectId)
	err = db.Where("task_stage = 10").Count(&specialReviewNumber.SketchUnreviewNumber).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
		return nil, err
	}
	db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and task_status = 2", projectId)
	err = db.Where("task_stage > 10").Count(&specialReviewNumber.SketchPassedNumber).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
		return nil, err
	}
	return &specialReviewNumber, nil
}

func GetSpecialLinkNumber(ctx context.Context, projectId string) (*http_model.GetSpecialLinkNumberData, error) {
	var specialLinkNumber http_model.GetSpecialLinkNumberData
	db := GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and task_status = 2", projectId)
	err := db.Where("").Count(&specialLinkNumber.LinkNumber).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
		return nil, err
	}
	db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and task_status = 2", projectId)
	err = db.Where("task_stage = 12").Count(&specialLinkNumber.LinkUnreviewNumber).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
		return nil, err
	}
	db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and task_status = 2", projectId)
	err = db.Where("task_stage > 12").Count(&specialLinkNumber.LinkPassedNumber).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
		return nil, err
	}
	return &specialLinkNumber, nil
}

func GetSpecialDataNumber(ctx context.Context, projectId string) (*http_model.GetSpecialDataNumberData, error) {
	var specialDataNumber http_model.GetSpecialDataNumberData
	db := GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and task_status = 2", projectId)
	err := db.Where("").Count(&specialDataNumber.DataNumber).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
		return nil, err
	}
	db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and task_status = 2", projectId)
	err = db.Where("task_stage = 14").Count(&specialDataNumber.DataUnreviewNumber).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
		return nil, err
	}
	db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and task_status = 2", projectId)
	err = db.Where("task_stage > 14").Count(&specialDataNumber.DataPassedNumber).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
		return nil, err
	}
	return &specialDataNumber, nil
}

func GetSpecialLogisticNumber(ctx context.Context, projectId string) (*http_model.GetSpecialLogisticNumberData, error) {
	var specialLogisticNumberData http_model.GetSpecialLogisticNumberData
	db := GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? AND task_stage != 3", projectId)
	err := db.Where("task_status = 2").Count(&specialLogisticNumberData.DeliverNumber).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetSpecialLogisticNumber] error query mysql total, err:%+v", err)
		return nil, err
	}
	db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and task_status = 2", projectId)
	err = db.Where("logistics_status = 3").Count(&specialLogisticNumberData.SignedNumber).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetSpecialLogisticNumber] error query mysql total, err:%+v", err)
		return nil, err
	}
	db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and task_status = 2", projectId)
	err = db.Where("logistics_status = 1").Count(&specialLogisticNumberData.UndeliveredNumber).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetSpecialLogisticNumber] error query mysql total, err:%+v", err)
		return nil, err
	}
	db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and task_status = 2", projectId)
	err = db.Where("logistics_status = 2").Count(&specialLogisticNumberData.DeliveredNumber).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetSpecialLogisticNumber] error query mysql total, err:%+v", err)
		return nil, err
	}
	return &specialLogisticNumberData, nil
}

func GetSpecialSettleNumber(ctx context.Context, projectId string) (*http_model.GetSpecialSettleNumberData, error) {
	var specialSettleNumber http_model.GetSpecialSettleNumberData
	db := GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and task_status = 2 and task_stage = 15", projectId)
	err := db.Where("").Count(&specialSettleNumber.SettleNumber).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetSpecialSettleNumber] error query mysql total, err:%+v", err)
		return nil, err
	}
	db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and task_status = 2 and task_stage = 15 and settle_status = 1", projectId)
	err = db.Where("task_stage = 14").Count(&specialSettleNumber.UnsettleNumber).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetSpecialSettleNumber] error query mysql total, err:%+v", err)
		return nil, err
	}
	db = GetReadDB(ctx).Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ? and task_status = 2 and task_stage = 15 and settle_status = 2", projectId)
	err = db.Where("task_stage > 14").Count(&specialSettleNumber.SettledNumber).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetSpecialSettleNumber] error query mysql total, err:%+v", err)
		return nil, err
	}
	return &specialSettleNumber, nil
}
