package youngee_talent_service

import (
	"fmt"
	"reflect"
	"youngmini_server/app/dao"
	"youngmini_server/app/model/youngee_talent_model"

	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
)

type projectStatus int

const (
	projectStatusCreating = iota + 1
	projectStatusReviewing
	projectStatusReviewed
	projectStatusRecruiting
	projectStatusRecruited
	projectStatusPaying
	projectStatusPaid
	projectStatusInvalid
	projectStatusInProgress
	projectStatusClosed
)

// 获取项目信息列表service
func GetProjectInfoList(r *ghttp.Request) *TalentHttpResult {

	pageIndex := r.GetQueryInt("idx", -1)
	cntPerPage := r.GetQueryInt("cnt", -1)
	platform := r.Get("platform")
	projectForm := r.Get("projectform")
	feeForm := r.Get("feeform")
	if pageIndex == -1 || cntPerPage == -1 || cntPerPage == 0 {
		return &TalentHttpResult{Code: -1, Msg: "参数错误"}
	}

	// 如果有平台的过滤条件，则将平台列表保存于platformList
	var platformList []interface{}
	if platform != nil {
		if reflect.TypeOf(platform).Kind() != reflect.Slice {
			return &TalentHttpResult{Code: -2, Msg: "搜索条件平台类型错误"}
		}

		platformList = make([]interface{}, 0)
		platformList = platform.([]interface{})
	}

	// 如果有任务形式的过滤条件，则将过滤条件保存于taskModeList
	var projectFormList []interface{}
	if projectForm != nil {
		if reflect.TypeOf(projectForm).Kind() != reflect.Slice {
			return &TalentHttpResult{Code: -3, Msg: "搜索条件任务形式错误"}
		}

		projectFormList = make([]interface{}, 0)
		projectFormList = projectForm.([]interface{})
	}

	// 如果有稿费形式的过滤条件，则将过滤条件保存于taskModeList
	var feeFormList []interface{}
	if feeForm != nil {
		if reflect.TypeOf(feeForm).Kind() != reflect.Slice {
			return &TalentHttpResult{Code: -3, Msg: "搜索条件稿费形式错误"}
		}

		feeFormList = make([]interface{}, 0)
		feeFormList = feeForm.([]interface{})
	}

	// 构造查询的条件
	startId := pageIndex * cntPerPage
	whereStr := fmt.Sprintf("project_status = %d or project_status = %d", projectStatusRecruiting, projectStatusRecruited)
	if platformList != nil {
		whereStr = whereStr + " and project_platform in ("
		for _, v := range platformList {
			whereStr += v.(string) + ", "
		}

		whereStr = whereStr[0 : len(whereStr)-2]
		whereStr += ")"
	}

	if projectFormList != nil {
		whereStr += " and project_form in ("
		for _, v := range projectFormList {
			whereStr += v.(string) + ", "
		}

		whereStr = whereStr[0 : len(whereStr)-2]
		whereStr += ")"
	}

	if feeFormList != nil {
		whereStr += " and fee_form in ("
		for _, v := range feeFormList {
			whereStr += v.(string) + ", "
		}

		whereStr = whereStr[0 : len(whereStr)-2]
		whereStr += ")"
	}

	// 判断请求页面是否超过最大页面
	c, err := g.DB().Model(dao.ProjectInfo.Table).Fields("project_id").
		Count()
	// c, err := g.DB().Model("project_info").WithAll().Where(whereStr).Count()

	if c <= 0 {
		return &TalentHttpResult{Code: -4, Msg: "has no project", Data: nil}
	}

	maxPage := c / cntPerPage
	if c%cntPerPage > 0 {
		maxPage += 1
	}

	if pageIndex+1 > maxPage {
		return &TalentHttpResult{Code: -5, Msg: "over max page"}
	}

	var projectInfoList = youngee_talent_model.ProjectInfoList{}
	err = g.DB().Model("project_info").WithAll().
		Where(whereStr).
		Order("recruit_ddl DESC, project_id").Limit(startId, cntPerPage).Scan(&projectInfoList.ProjectInfos)

	if err != nil {
		return &TalentHttpResult{Code: -6, Msg: "查询数据库失败"}
	}

	projectInfoList.MaxPage = maxPage

	return &TalentHttpResult{Code: 0, Msg: "success", Data: projectInfoList}
}

// 获取单个项目详情service
func GetProjectDetail(r *ghttp.Request) *TalentHttpResult {
	pid := r.GetQueryInt("projectid", 0)
	if pid == 0 {
		return &TalentHttpResult{Code: -2, Msg: "parse param error"}
	}

	var ProjectDetail *youngee_talent_model.ProjectDetail
	err := g.DB().Model(youngee_talent_model.ProjectDetail{}).WithAll().Where("project_id", pid).Scan(&ProjectDetail)

	if err != nil {
		return &TalentHttpResult{Code: -3, Msg: "data query failed"}
	}
	return &TalentHttpResult{Code: 0, Msg: "success", Data: ProjectDetail}
}
