package db

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

	"github.com/issue9/conv"
	"github.com/sirupsen/logrus"
)

func GetWorkspaceNums(ctx context.Context, enterpriseID string) (*http_model.WorkspaceNums, error) {
	var workspaceNums http_model.WorkspaceNums
	var PaymentPending, DraftNum, RecruitingFullNum, ExecutionSpecNum, ExecutionFullNum int64
	db := GetReadDB(ctx)
	db = db.Model(gorm_model.ProjectInfo{}).Where("enterprise_id = ?", enterpriseID)
	db.Where("project_status = 1").Count(&DraftNum)

	db1 := GetReadDB(ctx)
	db1 = db1.Model(gorm_model.ProjectInfo{}).Where("enterprise_id = ?", enterpriseID)
	db1.Where("project_status = 4 AND project_type = 1").Count(&RecruitingFullNum)

	db2 := GetReadDB(ctx)
	db2 = db2.Model(gorm_model.ProjectInfo{}).Where("enterprise_id = ?", enterpriseID)
	db2.Where("project_status = 6").Count(&PaymentPending)
	db3 := GetReadDB(ctx)
	db3 = db3.Model(gorm_model.ProjectInfo{}).Where("enterprise_id = ?", enterpriseID)
	db3.Where("project_status = 9 AND project_type = 2").Count(&ExecutionSpecNum)

	db4 := GetReadDB(ctx)
	db4 = db4.Model(gorm_model.ProjectInfo{}).Where("enterprise_id = ?", enterpriseID)
	db4.Where("project_status = 9 AND project_type = 1").Count(&ExecutionFullNum)

	var ProjectFunds float64
	db5 := GetReadDB(ctx)
	db5 = db5.Model(gorm_model.Enterprise{}).Select("frozen_balance").Where("enterprise_id = ?", enterpriseID).First(&ProjectFunds)

	workspaceNums.PaymentPending = PaymentPending
	workspaceNums.DraftNum = DraftNum
	workspaceNums.RecruitingFullNum = RecruitingFullNum
	workspaceNums.ExecutionSpecNum = ExecutionSpecNum
	workspaceNums.ExecutionFullNum = ExecutionFullNum
	workspaceNums.ProjectFunds = ProjectFunds
	return &workspaceNums, nil
}

func GetWorkspaceDDLproject(ctx context.Context, enterpriseID string, pageSize, pageNum int64) (*http_model.WorkspaceDDLprojectdata, error) {
	var projectInfos []gorm_model.ProjectInfo
	//DDLproject := http_model.WorkspaceDDLprojectdata{}
	db := GetReadDB(ctx)
	ddl := conv.MustString(time.Now())[0:10] + "%"
	db = db.Model(gorm_model.ProjectInfo{}).Where("enterprise_id = ?", enterpriseID)
	db = db.Where("project_status = 4 AND recruit_ddl like ?", ddl).Or("project_status = 6 AND auto_fail_at like ?", ddl).Or("project_status = 3 AND pass_at like ?", ddl).Or("project_status = 10 AND finish_at like ?", ddl)
	// 查询总数
	var total int64
	if err := db.Count(&total).Error; err != nil {
		logrus.WithContext(ctx).Errorf("[GetWorkspaceDDLproject db] error query mysql total, err:%+v", err)
		return nil, err
	}
	// 查询该页数据
	limit := pageSize
	offset := pageSize * pageNum // assert pageNum start with 0
	wsDDLprojectData := http_model.WorkspaceDDLprojectdata{}
	err := db.Order("project_id").Limit(int(limit)).Offset(int(offset)).Find(&projectInfos).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetWorkspaceDDLproject db] Find Pagesize DDLProjectInfo error,err:%+v", err)
		return nil, err
	}
	wsDDLprojectData.Total = conv.MustString(total)
	for _, projectInfo := range projectInfos {
		DDLProjectPreview := new(http_model.DDLProjectPreview)
		DDLProjectPreview.ProjectId = projectInfo.ProjectID
		DDLProjectPreview.ProjectForm = conv.MustString(projectInfo.ProjectForm)
		DDLProjectPreview.ProjectName = projectInfo.ProjectName
		DDLProjectPreview.ProjectStatus = conv.MustString(projectInfo.ProjectStatus)
		DDLProjectPreview.ProjectPlatform = conv.MustString(projectInfo.ProjectPlatform)
		DDLProjectPreview.ProjectContentType = conv.MustString(projectInfo.ContentType)
		DDLProjectPreview.RecruitDDL = conv.MustString(projectInfo.RecruitDdl)[0:19]
		if projectInfo.AutoFailAt != nil {
			DDLProjectPreview.AutoFailAt = conv.MustString(projectInfo.AutoFailAt)[0:19]
		}
		if projectInfo.PassAt != nil {
			DDLProjectPreview.PassAt = conv.MustString(projectInfo.PassAt)[0:19]
		}
		if projectInfo.FinishAt != nil {
			DDLProjectPreview.FinishAt = conv.MustString(projectInfo.FinishAt)[0:19]
		}
		DDLProjectPreview.RecruitNum = conv.MustString(projectInfo.RecruitNum)
		wsDDLprojectData.DDLProjectPreview = append(wsDDLprojectData.DDLProjectPreview, DDLProjectPreview)
	}
	return &wsDDLprojectData, nil
}

func GetWorkspaceBarNums(ctx context.Context, enterpriseID string, projectType int64) (*http_model.WorkspaceBarNums, error) {
	var workspaceNums http_model.WorkspaceBarNums
	var DataPendingNum, LinkPendingNum, SketchPendingNum, ScriptPendingNum, AccSelecting, ShipmentPendingNum, ShippedNum int64
	//fmt.Printf("TypeShow %+v", projectType)
	//先查企业所属的全部项目
	var RecruitingProjectIDs []string
	db := GetReadDB(ctx)
	fmt.Println("测试", projectType == 1)
	if projectType == 1 {
		fmt.Println("全流程", projectType)
		db = db.Debug().Model(gorm_model.ProjectInfo{}).Select("project_id").Where("enterprise_id = ? AND project_type = ?  AND project_status = 4", enterpriseID, projectType).Find(&RecruitingProjectIDs)
	} else {
		fmt.Println("专项", projectType)
		db = db.Debug().Model(gorm_model.ProjectInfo{}).Select("project_id").Where("enterprise_id = ? AND project_type = ?  AND project_status = 9", enterpriseID, projectType).Find(&RecruitingProjectIDs)
	}
	db1 := GetReadDB(ctx)
	db1 = db1.Debug().Model(gorm_model.YoungeeTaskInfo{}).Where("project_id IN ?", RecruitingProjectIDs)
	//在一个db上持续操作，会默认一直AND连接
	fmt.Println("招募ID", RecruitingProjectIDs)
	//1 招募中-待选
	err := db1.Where("task_status = 1").Count(&AccSelecting).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetWorkspaceBarNums db] Find AccSelecting error,err:%+v", err)
		return nil, err
	}

	//执行中都是状态9 再次过滤
	var ExecutingProjectIDs []string

	db = GetReadDB(ctx)
	db = db.Debug().Model(gorm_model.ProjectInfo{}).Select("project_id").Where("enterprise_id = ? AND project_type = ?  AND project_status = 9", enterpriseID, projectType).Find(&ExecutingProjectIDs)
	db1 = GetReadDB(ctx)
	db1 = db1.Debug().Model(gorm_model.YoungeeTaskInfo{}).Where("project_id IN ?", ExecutingProjectIDs)

	//2 数据待审
	db1 = GetReadDB(ctx)
	err = db1.Debug().Model(gorm_model.YoungeeTaskInfo{}).Where("project_id IN ? AND task_stage = 14", ExecutingProjectIDs).Count(&DataPendingNum).Error
	//err = db1.Where("task_stage = 14").Count(&DataPendingNum).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetWorkspaceBarNums db] Find DataPendingNum error,err:%+v", err)
		return nil, err
	}

	//3 链接待审
	db1 = GetReadDB(ctx)
	err = db1.Debug().Model(gorm_model.YoungeeTaskInfo{}).Where("project_id IN ? AND task_stage = 12", ExecutingProjectIDs).Count(&LinkPendingNum).Error
	//err = db1.Where("task_stage = 12").Count(&LinkPendingNum).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetWorkspaceBarNums db] Find LinkPendingNum error,err:%+v", err)
		return nil, err
	}

	//4 初稿待审
	db1 = GetReadDB(ctx)
	err = db1.Debug().Model(gorm_model.YoungeeTaskInfo{}).Where("project_id IN ? AND task_stage = 10", ExecutingProjectIDs).Count(&SketchPendingNum).Error
	//err = db1.Where("task_stage = 10").Count(&SketchPendingNum).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetWorkspaceBarNums db] Find SketchPendingNum error,err:%+v", err)
		return nil, err
	}

	//5 脚本待审
	db1 = GetReadDB(ctx)
	err = db1.Debug().Model(gorm_model.YoungeeTaskInfo{}).Where("project_id IN ? AND task_stage = 8", ExecutingProjectIDs).Count(&ScriptPendingNum).Error
	//err = db1.Where("task_stage = 8").Count(&ScriptPendingNum).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetWorkspaceBarNums db] Find ScriptPendingNum error,err:%+v", err)
		return nil, err
	}

	//6 待签收
	db1 = GetReadDB(ctx)
	err = db1.Debug().Model(gorm_model.YoungeeTaskInfo{}).Where("project_id IN ? AND task_status = 2  AND logistics_status = 2", ExecutingProjectIDs).Count(&ShippedNum).Error
	//err = db1.Where("task_stage = 5").Count(&ShippedNum).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetWorkspaceBarNums db] Find ShippedNum error,err:%+v", err)
		return nil, err
	}

	//7 待发货
	db1 = GetReadDB(ctx)
	err = db1.Debug().Model(gorm_model.YoungeeTaskInfo{}).Where("project_id IN ? AND task_status = 2 AND logistics_status = 1", ExecutingProjectIDs).Count(&ShipmentPendingNum).Error
	//err = db1.Where("task_stage = 4").Count(&ShipmentPendingNum).Error
	if err != nil {
		logrus.WithContext(ctx).Errorf("[GetWorkspaceBarNums db] Find ShipmentPendingNum error,err:%+v", err)
		return nil, err
	}

	workspaceNums.DataPendingNum = DataPendingNum
	workspaceNums.LinkPendingNum = LinkPendingNum
	workspaceNums.SketchPendingNum = SketchPendingNum
	workspaceNums.ScriptPendingNum = ScriptPendingNum
	workspaceNums.AccSelecting = AccSelecting
	workspaceNums.ShippedNum = ShippedNum
	workspaceNums.ShipmentPendingNum = ShipmentPendingNum

	fmt.Printf("work %+v", workspaceNums)
	return &workspaceNums, nil
}
