package talent_service

import (
	"context"
	"github.com/gogf/gf/database/gdb"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/os/gtime"
	"youngmini_server/app/dao"
	"youngmini_server/app/model"
	"youngmini_server/app/model/talent_model"
	"youngmini_server/app/utils"
)

type orderStage int

const (
	orderStageWaitForSelect orderStage = iota + 1
	orderStageSendSamples
	orderStageDraft
	orderStageArticle
	orderStageLinkExamine
	orderStageDataExamine
	orderStageSamplesReturn
	orderStageComplete
)

func OnGetExpressInfo(r *ghttp.Request) *TalentHttpResult {
	tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
	if err != nil {
		return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
	}

	oid := r.GetQueryInt("order_id", 0)
	if oid == 0 {
		return &TalentHttpResult{Code: -2, Msg: "请输入订单id"}
	}

	rec, err := g.DB().Model(dao.OrderInfo.Table).Fields(dao.OrderInfo.Columns.TalentId).One(dao.OrderInfo.Columns.OrderId, oid)
	if err != nil || rec == nil {
		return &TalentHttpResult{Code: -3, Msg: "没有此订单"}
	}

	if rec[dao.OrderInfo.Columns.TalentId].Int() != tid {
		return &TalentHttpResult{Code: -4, Msg: "达人未接此任务"}
	}

	rec, err = g.DB().Model(dao.OrderDeliveryInfo.Table).Fields(
		dao.OrderDeliveryInfo.Columns.OrderAddressee,   // 收货人
		dao.OrderDeliveryInfo.Columns.OrderPhone,       // 联系电话
		dao.OrderDeliveryInfo.Columns.OrderAddress,     // 详细地址
		dao.OrderDeliveryInfo.Columns.RegionCode,       // 区域码
		dao.OrderDeliveryInfo.Columns.DeliveryTime,     // 发货时间
		dao.OrderDeliveryInfo.Columns.LogisticsCompany, // 快递公司
		dao.OrderDeliveryInfo.Columns.TrackingNum,      // 订单号
		dao.OrderDeliveryInfo.Columns.DeliveryStatus).One(dao.OrderDeliveryInfo.Columns.OrderId, oid)
	if err != nil {
		return &TalentHttpResult{Code: -5, Msg: "获取运单信息失败"}
	}

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

// OnConfirmReceiveExpress 达人确认收到样品
func OnConfirmReceiveExpress(r *ghttp.Request) *TalentHttpResult {
	tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
	if err != nil {
		return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
	}

	var receiveSamplesData talent_model.ConfirmReceiveSampleData
	if err = r.ParseForm(&receiveSamplesData); err != nil {
		return &TalentHttpResult{Code: -2, Msg: "upload data error"}
	}

	// 查询订单及订单的执行阶段信息
	var orderInfo *model.OrderInfo
	err = g.DB().Model(dao.OrderInfo.Table).
		Where("order_id = ?", receiveSamplesData.OrderId).Scan(&orderInfo)
	if err != nil {
		return &TalentHttpResult{Code: -3, Msg: "order or procedure info not found"}
	}

	// 判定达人是否拥有此订单
	if orderInfo.TalentId != tid {
		return &TalentHttpResult{Code: -4, Msg: "talent have no this order"}
	}

	// 判断订单是否已完结
	if orderInfo.CompleteStatus > int(utils.OrderCompleteTypeNoComp) {
		return &TalentHttpResult{Code: -5, Msg: "order has complete"}
	}

	// 获取达人昵称
	var talentNickname string
	rec, err := g.DB().Model(dao.TalentInfo.Table).One("id", tid)
	if err != nil || rec == nil {
		talentNickname = ""
	} else {
		talentNickname = rec[dao.TalentInfo.Columns.TalentWxNickname].String()
	}

	var procedureInfo *model.WorkflowNodeContainer
	err = g.DB().Model(dao.WorkflowNodeContainer.Table).
		Where("order_id = ? and sort_id = ?", orderInfo.OrderId, orderInfo.OrderStatus).Scan(&procedureInfo)
	if err != nil {
		return &TalentHttpResult{Code: -6, Msg: "order procedure info not  found"}
	}

	// 如果订单当前执行步骤不处于等待上传初稿状态则不处理
	if procedureInfo.ProcedureStage != int(orderStageSendSamples) ||
		procedureInfo.StepInStage != 2 {
		return &TalentHttpResult{Code: -7, Msg: "order procedure is not wait for upload draft"}
	}

	// 记录初稿信息，并将订单执行步骤记录为初稿上传后的步骤
	err = g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
		_, err1 := tx.Ctx(ctx).Model(dao.OrderDeliveryInfo.Table).Update(g.Map{
			"confirm_time":      gtime.Now(),
			"delivery_status":   3,
		}, "order_id", receiveSamplesData.OrderId)
		if err1 != nil {
			return err
		}

		return utils.OrderProcedureManager.OnOperateOrder(receiveSamplesData.OrderId, true, tid, talentNickname)
	})


	if err != nil {
		return &TalentHttpResult{Code: -8, Msg: "update data failed"}
	}

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

func OnGetDraftInfo(r *ghttp.Request) *TalentHttpResult {
	tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
	if err != nil {
		return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
	}

	oid := r.GetQueryInt("order_id", 0)
	if oid == 0 {
		return &TalentHttpResult{Code: -2, Msg: "请输入订单id"}
	}

	rec, err := g.DB().Model(dao.OrderInfo.Table).Fields(dao.OrderInfo.Columns.TalentId).One(dao.OrderInfo.Columns.OrderId, oid)
	if err != nil || rec == nil {
		return &TalentHttpResult{Code: -3, Msg: "没有此订单"}
	}

	if rec[dao.OrderInfo.Columns.TalentId].Int() != tid {
		return &TalentHttpResult{Code: -4, Msg: "达人未接此任务"}
	}

	rec, err = g.DB().Model(dao.OrderDraftReview.Table).Fields(
		dao.OrderDraftReview.Columns.OnlineLink,
		dao.OrderDraftReview.Columns.AuditStatus,
		dao.OrderDraftReview.Columns.AuditOpinion,
		dao.OrderDraftReview.Columns.ConfirmTime).One(dao.OrderDraftReview.Columns.OrderId, oid)
	if err != nil {
		return &TalentHttpResult{Code: -2, Msg: "获取初稿信息失败"}
	}

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

// OnUploadDraft 上传初稿
func OnUploadDraft(r *ghttp.Request) *TalentHttpResult {
	tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
	if err != nil {
		return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
	}

	var draftData talent_model.UploadDraftData
	if err = r.ParseForm(&draftData); err != nil {
		return &TalentHttpResult{Code: -2, Msg: "upload data error"}
	}

	// 获取达人昵称
	var talentNickname string
	rec, err := g.DB().Model(dao.TalentInfo.Table).One("id", tid)
	if err != nil || rec == nil {
		talentNickname = ""
	} else {
		talentNickname = rec[dao.TalentInfo.Columns.TalentWxNickname].String()
	}

	// 查询订单及订单的执行阶段信息
	var orderInfo *model.OrderInfo
	err = g.DB().Model(dao.OrderInfo.Table).
		Where("order_id = ?", draftData.OrderId).Scan(&orderInfo)
	if err != nil {
		return &TalentHttpResult{Code: -3, Msg: "order or procedure info not found"}
	}

	// 判定达人是否拥有此订单
	if orderInfo.TalentId != tid {
		return &TalentHttpResult{Code: -4, Msg: "talent have no this order"}
	}

	// 判断订单是否已完结
	if orderInfo.CompleteStatus > int(utils.OrderCompleteTypeNoComp) {
		return &TalentHttpResult{Code: -5, Msg: "order has complete"}
	}

	var procedureInfo *model.WorkflowNodeContainer
	err = g.DB().Model(dao.WorkflowNodeContainer.Table).
		Where("order_id = ? and sort_id = ?", orderInfo.OrderId, orderInfo.OrderStatus).Scan(&procedureInfo)
	if err != nil {
		return &TalentHttpResult{Code: -6, Msg: "order procedure info not  found"}
	}

	// 如果订单当前执行步骤不处于等待上传初稿状态则不处理
	if procedureInfo.ProcedureStage != int(orderStageDraft) ||
		procedureInfo.StepInStage != 1 {
		return &TalentHttpResult{Code: -7, Msg: "order procedure is not wait for upload draft"}
	}

	// 记录初稿信息，并将订单执行步骤记录为初稿上传后的步骤
	err = g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
		_, err1 := tx.Ctx(ctx).Model(dao.OrderDraftReview.Table).Save(model.OrderDraftReview{
			OrderId:      draftData.OrderId,
			OnlineLink:   draftData.OnlineLink,
			AuditStatus: 1,
			AuditOpinion: "",
			CreatedAt:    gtime.Now(),
			ConfirmTime:  nil,
		})
		if err1 != nil {
			return err
		}

		return utils.OrderProcedureManager.OnOperateOrder(draftData.OrderId, true, tid, talentNickname)
	})


	if err != nil {
		return &TalentHttpResult{Code: -8, Msg: "update data failed"}
	}

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

func OnGetArticleInfo(r *ghttp.Request) *TalentHttpResult {
	tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
	if err != nil {
		return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
	}

	oid := r.GetQueryInt("order_id", 0)
	if oid == 0 {
		return &TalentHttpResult{Code: -2, Msg: "请输入订单id"}
	}

	rec, err := g.DB().Model(dao.OrderInfo.Table).Fields(dao.OrderInfo.Columns.TalentId).One(dao.OrderInfo.Columns.OrderId, oid)
	if err != nil || rec == nil {
		return &TalentHttpResult{Code: -3, Msg: "没有此订单"}
	}

	if rec[dao.OrderInfo.Columns.TalentId].Int() != tid {
		return &TalentHttpResult{Code: -4, Msg: "达人未接此任务"}
	}

	rec, err = g.DB().Model(dao.OrderArticleReview.Table).Fields(
		dao.OrderArticleReview.Columns.ArticleLink,
		dao.OrderArticleReview.Columns.AuditStatus,
		dao.OrderArticleReview.Columns.AuditOpinion,
		dao.OrderArticleReview.Columns.ConfirmTime).One(dao.OrderArticleReview.Columns.OrderId, oid)
	if err != nil {
		return &TalentHttpResult{Code: -2, Msg: "获取作品信息失败"}
	}

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

// OnUploadArticle 上传作品
func OnUploadArticle(r *ghttp.Request) *TalentHttpResult {
	tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
	if err != nil {
		return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
	}

	var articleData talent_model.UploadArticleData
	if err = r.ParseForm(&articleData); err != nil {
		return &TalentHttpResult{Code: -2, Msg: "upload data error"}
	}

	// 查询订单及订单的执行阶段信息
	var orderInfo *model.OrderInfo
	err = g.DB().Model(dao.OrderInfo.Table).
		Where("order_id = ?", articleData.OrderId).Scan(&orderInfo)
	if err != nil {
		return &TalentHttpResult{Code: -3, Msg: "order or procedure info not found"}
	}

	// 判定达人是否拥有此订单
	if orderInfo.TalentId != tid {
		return &TalentHttpResult{Code: -4, Msg: "talent have no this order"}
	}

	// 判断订单是否已完结
	if orderInfo.CompleteStatus > int(utils.OrderCompleteTypeNoComp) {
		return &TalentHttpResult{Code: -5, Msg: "order has complete"}
	}

	var procedureInfo *model.WorkflowNodeContainer
	err = g.DB().Model(dao.WorkflowNodeContainer.Table).
		Where("order_id = ? and sort_id = ?", orderInfo.OrderId, orderInfo.OrderStatus).Scan(&procedureInfo)
	if err != nil {
		return &TalentHttpResult{Code: -6, Msg: "order procedure info not  found"}
	}

	// 如果订单当前执行步骤不处于等待上传作品状态则不处理
	if procedureInfo.ProcedureStage != int(orderStageArticle) ||
		procedureInfo.StepInStage != 1 {
		return &TalentHttpResult{Code: -7, Msg: "order procedure is not wait for upload draft"}
	}

	// 获取达人昵称
	var talentNickname string
	rec, err := g.DB().Model(dao.TalentInfo.Table).One("id", tid)
	if err != nil || rec == nil {
		talentNickname = ""
	} else {
		talentNickname = rec[dao.TalentInfo.Columns.TalentWxNickname].String()
	}

	// 记录作品信息，并将订单执行步骤记录为作品上传后的步骤
	err = g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
		// 将作品信息记录再order_audit_review表中
		_, err1 := tx.Ctx(ctx).Model(dao.OrderArticleReview.Table).Save(model.OrderArticleReview{
			OrderId:      articleData.OrderId,
			ArticleLink:  articleData.ArticleUrls,
			AuditStatus:  1,
			AuditOpinion:   "",
			CreatedAt:    gtime.Now(),
			ConfirmTime:  nil,
		})
		if err1 != nil {
			return err
		}

		return utils.OrderProcedureManager.OnOperateOrder(int(articleData.OrderId), true, tid, talentNickname)
	})


	if err != nil {
		return &TalentHttpResult{Code: -8, Msg: "update data failed"}
	}

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

// OnGetPublishLinkInfo 获取发布链接信息
func OnGetPublishLinkInfo(r *ghttp.Request) *TalentHttpResult {
	tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
	if err != nil {
		return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
	}

	oid := r.GetQueryInt("order_id", 0)
	if oid == 0 {
		return &TalentHttpResult{Code: -2, Msg: "请输入订单id"}
	}

	rec, err := g.DB().Model(dao.OrderInfo.Table).Fields(dao.OrderInfo.Columns.TalentId).One(dao.OrderInfo.Columns.OrderId, oid)
	if err != nil || rec == nil {
		return &TalentHttpResult{Code: -3, Msg: "没有此订单"}
	}

	if rec[dao.OrderInfo.Columns.TalentId].Int() != tid {
		return &TalentHttpResult{Code: -4, Msg: "达人未接此任务"}
	}

	rec, err = g.DB().Model(dao.OrderLinkQuality.Table).Fields(
		dao.OrderLinkQuality.Columns.PostLink,
		dao.OrderLinkQuality.Columns.PhotoLink,
		dao.OrderLinkQuality.Columns.AuditStatus,
		dao.OrderLinkQuality.Columns.AuditOpinion,
		dao.OrderLinkQuality.Columns.ConfirmTime).One(dao.OrderLinkQuality.Columns.OrderId, oid)
	if err != nil {
		return &TalentHttpResult{Code: -2, Msg: "获取质检链接信息失败"}
	}

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

// OnUploadLink 上传发布链接
func OnUploadLink(r *ghttp.Request) *TalentHttpResult {
	tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
	if err != nil {
		return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
	}

	var linkData talent_model.UploadLinkData
	if err = r.ParseForm(&linkData); err != nil {
		return &TalentHttpResult{Code: -2, Msg: "upload data error"}
	}

	// 查询订单及订单的执行阶段信息
	var orderInfo *model.OrderInfo
	err = g.DB().Model(dao.OrderInfo.Table).
		Where("order_id = ?", linkData.OrderId).Scan(&orderInfo)
	if err != nil {
		return &TalentHttpResult{Code: -3, Msg: "order or procedure info not found"}
	}

	// 判定达人是否拥有此订单
	if orderInfo.TalentId != tid {
		return &TalentHttpResult{Code: -4, Msg: "talent have no this order"}
	}

	// 判断订单是否已完结
	if orderInfo.CompleteStatus > int(utils.OrderCompleteTypeNoComp) {
		return &TalentHttpResult{Code: -5, Msg: "order has complete"}
	}

	var procedureInfo *model.WorkflowNodeContainer
	err = g.DB().Model(dao.WorkflowNodeContainer.Table).
		Where("order_id = ? and sort_id = ?", orderInfo.OrderId, orderInfo.OrderStatus).Scan(&procedureInfo)
	if err != nil {
		return &TalentHttpResult{Code: -6, Msg: "order procedure info not  found"}
	}

	// 如果订单当前执行步骤不处于等待上传发布链接状态则不处理
	if procedureInfo.ProcedureStage != int(orderStageLinkExamine) ||
		procedureInfo.StepInStage != 1 {
		return &TalentHttpResult{Code: -7, Msg: "order procedure is not wait for upload draft"}
	}

	// 获取达人昵称
	var talentNickname string
	rec, err := g.DB().Model(dao.TalentInfo.Table).One("id", tid)
	if err != nil || rec == nil {
		talentNickname = ""
	} else {
		talentNickname = rec[dao.TalentInfo.Columns.TalentWxNickname].String()
	}

	// 记录发布链接信息，并将订单执行步骤记录为发布链接上传后的步骤
	err = g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
		// 将作品信息记录再order_audit_review表中
		_, err1 := tx.Ctx(ctx).Model(dao.OrderLinkQuality.Table).Save(model.OrderLinkQuality{
			PostLink:      linkData.LinkUrl,
			PhotoLink:     linkData.PhotoLink,
			AuditStatus: 1,
			AuditOpinion:    "",
			CreatedAt:     gtime.Now(),
			ConfirmTime:   nil,
			OrderId:       linkData.OrderId,
		})
		if err1 != nil {
			return err
		}

		return utils.OrderProcedureManager.OnOperateOrder(int(linkData.OrderId), true, tid, talentNickname)
	})


	if err != nil {
		return &TalentHttpResult{Code: -8, Msg: "update data failed"}
	}

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

// OnGetDataLinkInfo 获取数据质检信息
func OnGetDataLinkInfo(r *ghttp.Request) *TalentHttpResult {
	tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
	if err != nil {
		return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
	}

	oid := r.GetQueryInt("order_id", 0)
	if oid == 0 {
		return &TalentHttpResult{Code: -2, Msg: "请输入订单id"}
	}

	rec, err := g.DB().Model(dao.OrderInfo.Table).Fields(dao.OrderInfo.Columns.TalentId).One(dao.OrderInfo.Columns.OrderId, oid)
	if err != nil || rec == nil {
		return &TalentHttpResult{Code: -3, Msg: "没有此订单"}
	}

	if rec[dao.OrderInfo.Columns.TalentId].Int() != tid {
		return &TalentHttpResult{Code: -4, Msg: "达人未接此任务"}
	}

	combineData := talent_model.OrderDataAuditCombineData{}

	rec, err = g.DB().Model(dao.OrderLinkQuality.Table).
		Fields(dao.OrderLinkQuality.Columns.PostLink, dao.OrderLinkQuality.Columns.PhotoLink).
		One(dao.OrderLinkQuality.Columns.OrderId, oid)
	if err != nil || rec == nil {
		return &TalentHttpResult{Code: -5, Msg: "查询发布链接信息失败"}
	}

	combineData.OrderLinkInfoData = &talent_model.OrderLinkInfoData{
		PostLink:  rec[dao.OrderLinkQuality.Columns.PostLink].String(),
		PhotoLink: rec[dao.OrderLinkQuality.Columns.PhotoLink].String(),
	}

	rec, err = g.DB().Model(dao.OrderDataReview.Table).Fields(
		dao.OrderDataReview.Columns.AuditStatus,
		dao.OrderDataReview.Columns.AuditOpinion,
		dao.OrderDataReview.Columns.LikeNumber,
		dao.OrderDataReview.Columns.LittleEye,
		dao.OrderDataReview.Columns.CommitNum,
		dao.OrderDataReview.Columns.CollectNum,
		dao.OrderDataReview.Columns.DataLink,
		dao.OrderDataReview.Columns.ConfirmTime).One(dao.OrderDataReview.Columns.OrderId, oid)
	if err != nil {
		return &TalentHttpResult{Code: -2, Msg: "获取数据链接信息失败"}
	}

	if rec != nil {
		combineData.OrderDataAuditData = &talent_model.OrderDataAuditData{
			LikeNumber:   rec[dao.OrderDataReview.Columns.LikeNumber].Int(),
			LittleEye:    rec[dao.OrderDataReview.Columns.LittleEye].Int(),
			CommitNum:    rec[dao.OrderDataReview.Columns.CommitNum].Int(),
			CollectNum:   rec[dao.OrderDataReview.Columns.CollectNum].Int(),
			DataLink:     rec[dao.OrderDataReview.Columns.DataLink].String(),
			AuditStatus:  rec[dao.OrderDataReview.Columns.AuditStatus].Int(),
			AuditOpinion: rec[dao.OrderDataReview.Columns.AuditOpinion].String(),
			ConfirmTime:  rec[dao.OrderDataReview.Columns.ConfirmTime].GTime(),
		}
	}
	return &TalentHttpResult{Code: 0, Msg: "success", Data: combineData}
}

// OnUploadData 上传质检数据
func OnUploadData(r *ghttp.Request) *TalentHttpResult {
	tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
	if err != nil {
		return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
	}

	var dataData talent_model.UploadDataData
	if err = r.ParseForm(&dataData); err != nil {
		return &TalentHttpResult{Code: -2, Msg: "upload data error"}
	}

	// 查询订单及订单的执行阶段信息
	var orderInfo *model.OrderInfo
	err = g.DB().Model(dao.OrderInfo.Table).
		Where("order_id = ?", dataData.OrderId).Scan(&orderInfo)
	if err != nil {
		return &TalentHttpResult{Code: -3, Msg: "order or procedure info not found"}
	}

	// 判定达人是否拥有此订单
	if orderInfo.TalentId != tid {
		return &TalentHttpResult{Code: -4, Msg: "talent have no this order"}
	}

	// 判断订单是否已完结
	if orderInfo.CompleteStatus > int(utils.OrderCompleteTypeNoComp) {
		return &TalentHttpResult{Code: -5, Msg: "order has complete"}
	}

	var procedureInfo *model.WorkflowNodeContainer
	err = g.DB().Model(dao.WorkflowNodeContainer.Table).
		Where("order_id = ? and sort_id = ?", orderInfo.OrderId, orderInfo.OrderStatus).Scan(&procedureInfo)
	if err != nil {
		return &TalentHttpResult{Code: -6, Msg: "order procedure info not  found"}
	}

	// 如果订单当前执行步骤不处于等待上传数据状态则不处理
	if procedureInfo.ProcedureStage != int(orderStageDataExamine) ||
		procedureInfo.StepInStage != 1 {
		return &TalentHttpResult{Code: -7, Msg: "order procedure is not wait for upload draft"}
	}

	// 获取达人昵称
	var talentNickname string
	rec, err := g.DB().Model(dao.TalentInfo.Table).One("id", tid)
	if err != nil || rec == nil {
		talentNickname = ""
	} else {
		talentNickname = rec[dao.TalentInfo.Columns.TalentWxNickname].String()
	}

	// 记录数据信息，并将订单执行步骤记录为数据上传后的步骤
	err = g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
		// 将作品信息记录再order_audit_review表中
		_, err1 := tx.Ctx(ctx).Model(dao.OrderDataReview.Table).Save(model.OrderDataReview{
			OrderId:       dataData.OrderId,
			LikeNumber:    dataData.LikeNumber,
			LittleEye:     dataData.LittleEye,
			CommitNum:     dataData.CommitNum,
			CollectNum:    dataData.CollectNum,
			DataLink:      dataData.DataLink,
			AuditStatus:   1,
			AuditOpinion:  "",
			CreatedAt:     gtime.Now(),
			ConfirmTime:   nil,
		})
		if err1 != nil {
			return err
		}

		return utils.OrderProcedureManager.OnOperateOrder(int(dataData.OrderId), true, tid, talentNickname)
	})

	if err != nil {
		return &TalentHttpResult{Code: -8, Msg: "update data failed"}
	}

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

// OnPostSampleReturnPostInfo 上传返还样品快递信息
func OnPostSampleReturnPostInfo(r *ghttp.Request) *TalentHttpResult {
	tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
	if err != nil {
		return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
	}

	var expressInfo *talent_model.SamplesReturnPostInfo
	err = r.ParseForm(&expressInfo)
	if err != nil {
		return &TalentHttpResult{Code: -2, Msg: "请正确填写快递信息"}
	}

	// 查询订单及订单的执行阶段信息
	var orderInfo *model.OrderInfo
	err = g.DB().Model(dao.OrderInfo.Table).
		Where("order_id = ?", expressInfo.OrderId).Scan(&orderInfo)
	if err != nil {
		return &TalentHttpResult{Code: -3, Msg: "order or procedure info not found"}
	}

	// 判定达人是否拥有此订单
	if orderInfo.TalentId != tid {
		return &TalentHttpResult{Code: -4, Msg: "talent have no this order"}
	}

	// 判断订单是否已完结
	if orderInfo.CompleteStatus > int(utils.OrderCompleteTypeNoComp) {
		return &TalentHttpResult{Code: -5, Msg: "order has complete"}
	}

	var procedureInfo *model.WorkflowNodeContainer
	err = g.DB().Model(dao.WorkflowNodeContainer.Table).
		Where("order_id = ? and sort_id = ?", orderInfo.OrderId, orderInfo.OrderStatus).Scan(&procedureInfo)
	if err != nil {
		return &TalentHttpResult{Code: -5, Msg: "order procedure info not  found"}
	}

	// 如果订单当前执行步骤不处于拍单寄回状态则不处理
	if procedureInfo.ProcedureStage != int(orderStageSamplesReturn) ||
		procedureInfo.StepInStage != 1 {
		return &TalentHttpResult{Code: -6, Msg: "order procedure is not wait for upload draft"}
	}

	//// 记录作品信息，并将订单执行步骤记录为初稿上传后的步骤
	//err = g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
	//	// 将作品信息记录再order_audit_review表中
	//	_, err1 := tx.Ctx(ctx).Model(dao.OrderAuditReview.Table).Insert(model.OrderDataReview{
	//		OrderId:       dataData.OrderId,
	//		PostLink:      dataData.PostLink,
	//		PhotoLink:     dataData.PhotoLink,
	//		LikeNumber:    dataData.LikeNumber,
	//		LittleEye:     dataData.LittleEye,
	//		CommitNum:     dataData.CommitNum,
	//		CollectNum:    dataData.CollectNum,
	//		DataLink:      dataData.DataLink,
	//		AuditStatus:   1,
	//		AuditOpinion:  "",
	//		CreatedAt:     gtime.Now(),
	//		ConfirmTime:   nil,
	//	})
	//	if err1 != nil {
	//		return err
	//	}
	//
	//	return utils.OrderProcedureManager.OnOperateOrder(int(dataData.OrderId), true, tid, talentNickname, ctx, tx)
	//})

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

// OnGetOrderCompleteData 获取任务完成数据
func OnGetOrderCompleteData(r *ghttp.Request) *TalentHttpResult {
	tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
	if err != nil {
		return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
	}

	oid := r.GetQueryInt("order_id", 0)
	if oid == 0 {
		return &TalentHttpResult{Code: -2, Msg: "请输入订单id"}
	}

	rec, err := g.DB().Model(dao.OrderInfo.Table).Fields(dao.OrderInfo.Columns.TalentId).One(dao.OrderInfo.Columns.OrderId, oid)
	if err != nil || rec == nil {
		return &TalentHttpResult{Code: -3, Msg: "没有此订单"}
	}

	if rec[dao.OrderInfo.Columns.TalentId].Int() != tid {
		return &TalentHttpResult{Code: -4, Msg: "达人未接此任务"}
	}

	incomeRec, err := g.DB().Model(dao.BobocoinIncomeRecord.Table).One(dao.BobocoinIncomeRecord.Columns.OrderId, oid)
	if err != nil || incomeRec == nil {
		return &TalentHttpResult{Code: -5, Msg: "query income value failed"}
	}

	deductRecs, err := g.DB().Model(dao.BobocoinDeductRecord.Table).All(dao.BobocoinDeductRecord.Columns.OrderId, oid)
	if err != nil {
		return &TalentHttpResult{Code: -6, Msg: "query deduct records failed"}
	}

	incomeValue := incomeRec[dao.BobocoinIncomeRecord.Columns.BobocoinValue].Int()

	var deductValue int
	if deductRecs != nil {
		for _, v := range deductRecs {
			deductValue += v[dao.BobocoinDeductRecord.Columns.BobocoinValue].Int()
		}
	}

	incomeDetail := talent_model.OrderIncomeDetailInfo{
		OrderIncome: incomeValue,
		DeductList:  deductRecs,
		RealIncome:  incomeValue - deductValue,
		OrderCompleteDate: incomeRec[dao.BobocoinIncomeRecord.Columns.CompleteDate].GTime(),
	}

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

// CancelOrder 达人取消订单
func CancelOrder(r *ghttp.Request) *TalentHttpResult {
	tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
	if err != nil {
		return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
	}

	oid := r.GetRequestInt("order_id", 0)
	if oid == 0 {
		return &TalentHttpResult{Code: -2, Msg: "请输入订单id"}
	}

	var orderInfo *model.OrderInfo
	err = g.DB().Model(dao.OrderInfo.Table).Where(dao.OrderInfo.Columns.OrderId, oid).Scan(&orderInfo)
	if err != nil {
		return &TalentHttpResult{Code: -3, Msg: "没有此订单"}
	}

	if orderInfo.TalentId != tid {
		return &TalentHttpResult{Code: -4, Msg: "达人未接此任务"}
	}

	// 判断订单是否已完结
	if orderInfo.CompleteStatus > int(utils.OrderCompleteTypeNoComp) {
		return &TalentHttpResult{Code: -5, Msg: "order has complete"}
	}

	_, err = g.DB().Model(dao.OrderInfo.Table).Update(g.Map{
		dao.OrderInfo.Columns.CompleteStatus: utils.OrderCompleteTypeCancel,
		dao.OrderInfo.Columns.CompleteDate: gtime.Now(),
	}, dao.OrderInfo.Columns.OrderId, oid)
	if err != nil {
		return &TalentHttpResult{Code: -5, Msg: "cancel order failed"}
	}

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