package controller

import (
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	"youngee_b_api/app/service"
	"youngee_b_api/app/vo"
)

type WorkspaceController struct{}

type GetTakegoodsInfoParam struct {
	EnterpriseId string `json:"enterprise_id"`
	SubAccountId int64  `json:"sub_account_id"`
	DateRange    string `json:"days"`
}

func (w WorkspaceController) GetTakegoodsInfo(c *gin.Context) {
	search := &GetTakegoodsInfoParam{}
	err := c.BindJSON(&search)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	result := service.EnterpriseService{}.GetEnterpriseTakegoodsInfo(search.EnterpriseId, search.DateRange)

	returnSuccess(c, 20000, result)
}

// 任务待办-公开任务
func (t WorkspaceController) GetPublicTask(c *gin.Context) {
	param := &vo.CommonParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	selectionToDo, err1 := service.SelectionInfoService{}.GetTaskToDo(param.EnterpriseId, param.SubAccountId)
	if err1 != nil {
		logrus.Errorf("[SelectionInfoService] call Show err:%+v\n", err1)
		returnError(c, 40000, err1.Error())
		return
	}
	projectToDo, err2 := service.ProjectService{}.GetTaskToDo(param.EnterpriseId, param.SubAccountId, 1)
	if err2 != nil {
		logrus.Errorf("[ProjectService] call Show err:%+v\n", err2)
		returnError(c, 40000, err2.Error())
		return
	}
	localLifeToDo, err3 := service.LocalLifeService{}.GetTaskToDo(param.EnterpriseId, param.SubAccountId, 1)
	if err3 != nil {
		logrus.Errorf("[LocalLifeService] call Show err:%+v\n", err3)
		returnError(c, 40000, err3.Error())
		return
	}
	resultMap := make(map[string]*map[string]map[string]int64)
	resultMap["selectionToDo"] = &selectionToDo
	resultMap["projectToDo"] = &projectToDo
	resultMap["localLifeToDo"] = &localLifeToDo
	returnSuccess(c, 20000, resultMap)
}

// 任务待办-定向任务
func (t WorkspaceController) GetTargetTask(c *gin.Context) {
	param := &vo.CommonParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	selectionToDo, err1 := service.SelectionInfoService{}.GetTaskToDo(param.EnterpriseId, param.SubAccountId)
	if err1 != nil {
		logrus.Errorf("[SelectionInfoService] call Show err:%+v\n", err1)
		returnError(c, 40000, err1.Error())
		return
	}
	projectToDo, err2 := service.ProjectService{}.GetTaskToDo(param.EnterpriseId, param.SubAccountId, 2)
	if err2 != nil {
		logrus.Errorf("[ProjectService] call Show err:%+v\n", err2)
		returnError(c, 40000, err2.Error())
		return
	}
	localLifeToDo, err3 := service.LocalLifeService{}.GetTaskToDo(param.EnterpriseId, param.SubAccountId, 2)
	if err3 != nil {
		logrus.Errorf("[LocalLifeService] call Show err:%+v\n", err3)
		returnError(c, 40000, err3.Error())
		return
	}
	resultMap := make(map[string]*map[string]map[string]int64)
	resultMap["selectionToDo"] = &selectionToDo
	resultMap["projectToDo"] = &projectToDo
	resultMap["localLifeToDo"] = &localLifeToDo
	returnSuccess(c, 20000, resultMap)
}

// 任务待办-寄样物流
func (t WorkspaceController) GetLogistics(c *gin.Context) {
	param := &vo.CommonParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	selectionToDo, err1 := service.SelectionInfoService{}.GetLogisticsToDo(param.EnterpriseId, param.SubAccountId)
	if err1 != nil {
		logrus.Errorf("[SelectionInfoService] call Show err:%+v\n", err1)
		returnError(c, 40000, err1.Error())
		return
	}
	projectToDo, err2 := service.ProjectService{}.GetLogisticsToDo(param.EnterpriseId, param.SubAccountId)
	if err2 != nil {
		logrus.Errorf("[ProjectService] call Show err:%+v\n", err2)
		returnError(c, 40000, err2.Error())
		return
	}
	resultMap := make(map[string]*map[string]map[string]int64)
	resultMap["selectionToDo"] = &selectionToDo
	resultMap["projectToDo"] = &projectToDo
	returnSuccess(c, 20000, resultMap)
}

// 任务待办-探店邀约
func (t WorkspaceController) GetExplore(c *gin.Context) {
	param := &vo.CommonParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	localLifeToDo, err1 := service.LocalLifeService{}.GetExploreToDo(param.EnterpriseId, param.SubAccountId)
	if err1 != nil {
		logrus.Errorf("[LocalLifeService] call Show err:%+v\n", err1)
		returnError(c, 40000, err1.Error())
		return
	}
	resultMap := make(map[string]*map[string]map[string]int64)
	resultMap["localLifeToDo"] = &localLifeToDo
	returnSuccess(c, 20000, resultMap)
}

// 任务待办-违约管理
func (t WorkspaceController) GetDefault(c *gin.Context) {
	param := &vo.CommonParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	resultMap := make(map[string]*map[string]map[string]int64)
	// 公开
	projectToDoPublic, err1 := service.ProjectService{}.GetDefaultToDo(param.EnterpriseId, param.SubAccountId, 1)
	if err1 != nil {
		logrus.Errorf("[ProjectService] call Show err:%+v\n", err1)
		returnError(c, 40000, err1.Error())
		return
	}
	localLifeToDoPublic, err2 := service.LocalLifeService{}.GetDefaultToDo(param.EnterpriseId, param.SubAccountId, 1)
	if err2 != nil {
		logrus.Errorf("[LocalLifeService] call Show err:%+v\n", err2)
		returnError(c, 40000, err2.Error())
		return
	}
	resPublic := make(map[string]map[string]int64)

	redbook1 := make(map[string]int64)
	redbook1["noSketch"] = projectToDoPublic["redbook"]["noSketch"] + localLifeToDoPublic["redbook"]["noSketch"]
	redbook1["noWork"] = projectToDoPublic["redbook"]["noWork"] + localLifeToDoPublic["redbook"]["noWork"]
	redbook1["noData"] = projectToDoPublic["redbook"]["noData"] + localLifeToDoPublic["redbook"]["noData"]
	redbook1["cooperateOver"] = projectToDoPublic["redbook"]["cooperateOver"] + localLifeToDoPublic["redbook"]["cooperateOver"]
	resPublic["redbook"] = redbook1

	douyin1 := make(map[string]int64)
	douyin1["noSketch"] = projectToDoPublic["douyin"]["noSketch"] + localLifeToDoPublic["douyin"]["noSketch"]
	douyin1["noWork"] = projectToDoPublic["douyin"]["noWork"] + localLifeToDoPublic["douyin"]["noWork"]
	douyin1["noData"] = projectToDoPublic["douyin"]["noData"] + localLifeToDoPublic["douyin"]["noData"]
	douyin1["cooperateOver"] = projectToDoPublic["douyin"]["cooperateOver"] + localLifeToDoPublic["douyin"]["cooperateOver"]
	resPublic["douyin"] = douyin1

	kuaishou1 := make(map[string]int64)
	kuaishou1["noSketch"] = projectToDoPublic["kuaishou"]["noSketch"] + localLifeToDoPublic["kuaishou"]["noSketch"]
	kuaishou1["noWork"] = projectToDoPublic["kuaishou"]["noWork"] + localLifeToDoPublic["kuaishou"]["noWork"]
	kuaishou1["noData"] = projectToDoPublic["kuaishou"]["noData"] + localLifeToDoPublic["kuaishou"]["noData"]
	kuaishou1["cooperateOver"] = projectToDoPublic["kuaishou"]["cooperateOver"] + localLifeToDoPublic["kuaishou"]["cooperateOver"]
	resPublic["kuaishou"] = kuaishou1

	weibo1 := make(map[string]int64)
	weibo1["noSketch"] = projectToDoPublic["weibo"]["noSketch"] + localLifeToDoPublic["weibo"]["noSketch"]
	weibo1["noWork"] = projectToDoPublic["weibo"]["noWork"] + localLifeToDoPublic["weibo"]["noWork"]
	weibo1["noData"] = projectToDoPublic["weibo"]["noData"] + localLifeToDoPublic["weibo"]["noData"]
	weibo1["cooperateOver"] = projectToDoPublic["weibo"]["cooperateOver"] + localLifeToDoPublic["weibo"]["cooperateOver"]
	resPublic["weibo"] = weibo1

	bilibili1 := make(map[string]int64)
	bilibili1["noSketch"] = projectToDoPublic["bilibili"]["noSketch"] + localLifeToDoPublic["bilibili"]["noSketch"]
	bilibili1["noWork"] = projectToDoPublic["bilibili"]["noWork"] + localLifeToDoPublic["bilibili"]["noWork"]
	bilibili1["noData"] = projectToDoPublic["bilibili"]["noData"] + localLifeToDoPublic["bilibili"]["noData"]
	bilibili1["cooperateOver"] = projectToDoPublic["bilibili"]["cooperateOver"] + localLifeToDoPublic["bilibili"]["cooperateOver"]
	resPublic["bilibili"] = bilibili1

	all1 := make(map[string]int64)
	all1["noSketch"] = resPublic["redbook"]["noSketch"] + resPublic["douyin"]["noSketch"] + resPublic["kuaishou"]["noSketch"] + resPublic["weibo"]["noSketch"] + resPublic["bilibili"]["noSketch"]
	all1["noWork"] = resPublic["redbook"]["noWork"] + resPublic["douyin"]["noWork"] + resPublic["kuaishou"]["noWork"] + resPublic["weibo"]["noWork"] + resPublic["bilibili"]["noWork"]
	all1["noData"] = resPublic["redbook"]["noData"] + resPublic["douyin"]["noData"] + resPublic["kuaishou"]["noData"] + resPublic["weibo"]["noData"] + resPublic["bilibili"]["noData"]
	all1["cooperateOver"] = resPublic["redbook"]["cooperateOver"] + resPublic["douyin"]["cooperateOver"] + resPublic["kuaishou"]["cooperateOver"] + resPublic["weibo"]["cooperateOver"] + resPublic["bilibili"]["cooperateOver"]
	resPublic["all"] = all1

	resultMap["resPublic"] = &resPublic
	// 定向
	projectToDoTarget, err3 := service.ProjectService{}.GetDefaultToDo(param.EnterpriseId, param.SubAccountId, 2)
	if err3 != nil {
		logrus.Errorf("[ProjectService] call Show err:%+v\n", err3)
		returnError(c, 40000, err3.Error())
		return
	}
	localLifeToDoTarget, err4 := service.LocalLifeService{}.GetDefaultToDo(param.EnterpriseId, param.SubAccountId, 2)
	if err4 != nil {
		logrus.Errorf("[LocalLifeService] call Show err:%+v\n", err4)
		returnError(c, 40000, err4.Error())
		return
	}
	resTarget := make(map[string]map[string]int64)

	redbook2 := make(map[string]int64)
	redbook2["noSketch"] = projectToDoTarget["redbook"]["noSketch"] + localLifeToDoTarget["redbook"]["noSketch"]
	redbook2["noWork"] = projectToDoTarget["redbook"]["noWork"] + localLifeToDoTarget["redbook"]["noWork"]
	redbook2["noData"] = projectToDoTarget["redbook"]["noData"] + localLifeToDoTarget["redbook"]["noData"]
	redbook2["cooperateOver"] = projectToDoTarget["redbook"]["cooperateOver"] + localLifeToDoTarget["redbook"]["cooperateOver"]
	resTarget["redbook"] = redbook2

	douyin2 := make(map[string]int64)
	douyin2["noSketch"] = projectToDoTarget["douyin"]["noSketch"] + localLifeToDoTarget["douyin"]["noSketch"]
	douyin2["noWork"] = projectToDoTarget["douyin"]["noWork"] + localLifeToDoTarget["douyin"]["noWork"]
	douyin2["noData"] = projectToDoTarget["douyin"]["noData"] + localLifeToDoTarget["douyin"]["noData"]
	douyin2["cooperateOver"] = projectToDoTarget["douyin"]["cooperateOver"] + localLifeToDoTarget["douyin"]["cooperateOver"]
	resTarget["douyin"] = douyin2

	kuaishou2 := make(map[string]int64)
	kuaishou2["noSketch"] = projectToDoTarget["kuaishou"]["noSketch"] + localLifeToDoTarget["kuaishou"]["noSketch"]
	kuaishou2["noWork"] = projectToDoTarget["kuaishou"]["noWork"] + localLifeToDoTarget["kuaishou"]["noWork"]
	kuaishou2["noData"] = projectToDoTarget["kuaishou"]["noData"] + localLifeToDoTarget["kuaishou"]["noData"]
	kuaishou2["cooperateOver"] = projectToDoTarget["kuaishou"]["cooperateOver"] + localLifeToDoTarget["kuaishou"]["cooperateOver"]
	resTarget["kuaishou"] = kuaishou2

	weibo2 := make(map[string]int64)
	weibo2["noSketch"] = projectToDoTarget["weibo"]["noSketch"] + localLifeToDoTarget["weibo"]["noSketch"]
	weibo2["noWork"] = projectToDoTarget["weibo"]["noWork"] + localLifeToDoTarget["weibo"]["noWork"]
	weibo2["noData"] = projectToDoTarget["weibo"]["noData"] + localLifeToDoTarget["weibo"]["noData"]
	weibo2["cooperateOver"] = projectToDoTarget["weibo"]["cooperateOver"] + localLifeToDoTarget["weibo"]["cooperateOver"]
	resTarget["weibo"] = weibo2

	bilibili2 := make(map[string]int64)
	bilibili2["noSketch"] = projectToDoTarget["bilibili"]["noSketch"] + localLifeToDoTarget["bilibili"]["noSketch"]
	bilibili2["noWork"] = projectToDoTarget["bilibili"]["noWork"] + localLifeToDoTarget["bilibili"]["noWork"]
	bilibili2["noData"] = projectToDoTarget["bilibili"]["noData"] + localLifeToDoTarget["bilibili"]["noData"]
	bilibili2["cooperateOver"] = projectToDoTarget["bilibili"]["cooperateOver"] + localLifeToDoTarget["bilibili"]["cooperateOver"]
	resTarget["bilibili"] = bilibili2

	all2 := make(map[string]int64)
	all2["noSketch"] = resTarget["redbook"]["noSketch"] + resTarget["douyin"]["noSketch"] + resTarget["kuaishou"]["noSketch"] + resTarget["weibo"]["noSketch"] + resTarget["bilibili"]["noSketch"]
	all2["noWork"] = resTarget["redbook"]["noWork"] + resTarget["douyin"]["noWork"] + resTarget["kuaishou"]["noWork"] + resTarget["weibo"]["noWork"] + resTarget["bilibili"]["noWork"]
	all2["noData"] = resTarget["redbook"]["noData"] + resTarget["douyin"]["noData"] + resTarget["kuaishou"]["noData"] + resTarget["weibo"]["noData"] + resTarget["bilibili"]["noData"]
	all2["cooperateOver"] = resTarget["redbook"]["cooperateOver"] + resTarget["douyin"]["cooperateOver"] + resTarget["kuaishou"]["cooperateOver"] + resTarget["weibo"]["cooperateOver"] + resTarget["bilibili"]["cooperateOver"]
	resTarget["all"] = all2

	resultMap["resTarget"] = &resTarget

	returnSuccess(c, 20000, resultMap)
}

// 合作待办-任务邀约
func (t WorkspaceController) GetTaskInvite(c *gin.Context) {
	param := &vo.CommonParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	projectToDo, err1 := service.ProjectService{}.GetTaskInviteToDo(param.EnterpriseId, param.SubAccountId)
	if err1 != nil {
		logrus.Errorf("[ProjectService] call Show err:%+v\n", err1)
		returnError(c, 40000, err1.Error())
		return
	}
	localLifeToDo, err2 := service.LocalLifeService{}.GetTaskInviteToDo(param.EnterpriseId, param.SubAccountId)
	if err2 != nil {
		logrus.Errorf("[ProjectService] call Show err:%+v\n", err2)
		returnError(c, 40000, err2.Error())
		return
	}
	resultMap := make(map[string]*map[string]map[string]int64)
	resultMap["projectToDo"] = &projectToDo
	resultMap["localLifeToDo"] = &localLifeToDo
	returnSuccess(c, 20000, resultMap)
}

// 合作待办-入库邀约
func (t WorkspaceController) GetWarehouseInvite(c *gin.Context) {
	param := &vo.CommonParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	resultMap := make(map[string]int64)
	resultMap["inviteConfirming"] = 0
	returnSuccess(c, 20000, resultMap)
}

// 财务待办-充值与发票
func (t WorkspaceController) GetFinance(c *gin.Context) {
	param := &vo.CommonParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	// 充值确认中
	confirmingRecharge, err1 := service.RechargeService{}.GetFinance(param)
	if err1 != nil {
		logrus.Errorf("[RechargeService] call Show err:%+v\n", err1)
		returnError(c, 40000, err1.Error())
		return
	}
	// 开票中
	invoicingAmount, err2 := service.InvoiceService{}.GetFinance(param)
	if err2 != nil {
		logrus.Errorf("[InvoiceService] call Show err:%+v\n", err2)
		returnError(c, 40000, err2.Error())
		return
	}
	res := make(map[string]float64)
	res["confirmingRecharge"] = confirmingRecharge
	res["invoicingAmount"] = invoicingAmount

	returnSuccess(c, 20000, res)
}
