package api

import (
	"box-cost/db/model"
	"box-cost/db/repo"
	"box-cost/log"
	"errors"
	"fmt"
	"time"

	"github.com/gin-gonic/gin"
)

// 更新生产计划
func DiffUpdatePlanTest(c *gin.Context, apictx *ApiSession) (interface{}, error) {
	var plan model.ProductPlan
	err := c.ShouldBindJSON(&plan)
	if err != nil {
		fmt.Println(err)
		log.Error(err)
		return nil, errors.New("参数错误")
	}
	if plan.Id.Hex() == "" {
		return nil, errors.New("id的为空")
	}
	plan.UpdateTime = time.Now()
	// repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
	// 	CollectName: "product-plan_copy1",
	// 	Query:       repo.Map{"_id": plan.Id.Hex()},
	// }, &plan)
	// return plan, nil

	// ========================记录更新日志 查询更新前数据============================
	// planModel1 := &model.ProductPlan{}
	// oldPlan := getDataById(apictx, plan.Id.Hex(), planModel1, "product-plan_copy1")
	// // ============================================================================

	result, err1 := repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), "product-plan_copy1", plan.Id.Hex(), &plan, &repo.RecordLogReq{
		Path:     c.Request.URL.Path,
		TargetId: plan.Id.Hex(),
	})

	// ========================记录更新日志 查询更新后数据============================
	// planModel2 := &model.ProductPlan{}
	// newPlan := getDataById(apictx, plan.Id.Hex(), planModel2, "product-plan_copy1")
	// // ============================================================================

	// // ============================记录更新前后差异================================
	// diffStr, err := diffUpdateData(oldPlan, newPlan)
	// if err != nil {
	// 	fmt.Println(err)
	// }

	// // 记录到数据库中
	// changeLogs := &model.Logs{
	// 	Path:       c.Request.URL.Path,
	// 	UserId:     apictx.User.ID,
	// 	TargetId:   plan.Id.Hex(),
	// 	Diff:       diffStr,
	// 	CreateTime: time.Now(),
	// 	UpdateTime: time.Now(),
	// }
	// repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionLogs, changeLogs)
	// ==========================================================================
	return result, err1
}

// 更新生产计划
func DiffUpdateProduceTest(c *gin.Context, apictx *ApiSession) (interface{}, error) {
	var produce model.ProduceBill
	err := c.ShouldBindJSON(&produce)
	if err != nil {
		fmt.Println(err)
		log.Error(err)
		return nil, errors.New("参数错误")
	}
	if produce.Id.Hex() == "" {
		return nil, errors.New("id的为空")
	}
	produce.UpdateTime = time.Now()

	result, err1 := repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), "bill-produce_copy1", produce.Id.Hex(), &produce, &repo.RecordLogReq{
		Path:     c.Request.URL.Path,
		TargetId: produce.Id.Hex(),
	})

	// ========================记录更新日志 查询更新后数据============================
	// planModel2 := &model.ProductPlan{}
	// newPlan := getDataById(apictx, plan.Id.Hex(), planModel2, "product-plan_copy1")
	// // ============================================================================

	// // ============================记录更新前后差异================================
	// diffStr, err := diffUpdateData(oldPlan, newPlan)
	// if err != nil {
	// 	fmt.Println(err)
	// }

	// // 记录到数据库中
	// changeLogs := &model.Logs{
	// 	Path:       c.Request.URL.Path,
	// 	UserId:     apictx.User.ID,
	// 	TargetId:   plan.Id.Hex(),
	// 	Diff:       diffStr,
	// 	CreateTime: time.Now(),
	// 	UpdateTime: time.Now(),
	// }
	// repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionLogs, changeLogs)
	// ==========================================================================
	return result, err1
}