su_money_controller.go 14.1 KB
package controllers

import (
	"encoding/json"
	"fmt"
	"github.com/360EntSecGroup-Skylar/excelize/v2"
	"github.com/astaxie/beego"
	"github.com/linmadan/egglib-go/web/beego/utils"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/suMoney/command"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/suMoney/query"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/suMoney/service"
	"path"
	"strconv"
)

type SuMoneyController struct {
	beego.Controller
}

func (controller *SuMoneyController) OperationSuMoney() {
	suMoneyService := service.NewSuMoneyService(nil)
	operationSuMoneyCommand := &command.OperationSuMoneyCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), operationSuMoneyCommand)
	data, err := suMoneyService.OperationSuMoney(operationSuMoneyCommand)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

func (controller *SuMoneyController) GetSuMoneyTransactionRecord() {
	suMoneyService := service.NewSuMoneyService(nil)
	getSuMoneyTransactionRecordQuery := &query.GetSuMoneyTransactionRecordQuery{}
	suMoneyTransactionRecordId, _ := controller.GetInt64(":suMoneyTransactionRecordId")
	getSuMoneyTransactionRecordQuery.SuMoneyTransactionRecordId = suMoneyTransactionRecordId
	data, err := suMoneyService.GetSuMoneyTransactionRecord(getSuMoneyTransactionRecordQuery)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

func (controller *SuMoneyController) ExchangeSuMoney() {
	suMoneyService := service.NewSuMoneyService(nil)
	exchangeSuMoneyCommand := &command.ExchangeSuMoneyCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), exchangeSuMoneyCommand)
	data, err := suMoneyService.ExchangeSuMoney(exchangeSuMoneyCommand)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

func (controller *SuMoneyController) SearchSuMoneyTransactionRecord() {
	suMoneyService := service.NewSuMoneyService(nil)
	searchSuMoneyTransactionRecordCommand := &command.SearchSuMoneyTransactionRecordCommand{}
	employeeMatchName := controller.GetString("employeeMatchName")
	searchSuMoneyTransactionRecordCommand.EmployeeMatchName =  employeeMatchName
	offset, _ := controller.GetInt("offset")
	searchSuMoneyTransactionRecordCommand.Offset = offset
	limit, _ := controller.GetInt("limit")
	searchSuMoneyTransactionRecordCommand.Limit = limit
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), searchSuMoneyTransactionRecordCommand)
	data, err := suMoneyService.SearchSuMoneyTransactionRecord(searchSuMoneyTransactionRecordCommand)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

func (controller *SuMoneyController) SuMoneyTransactionRecordStatistics() {
	suMoneyService := service.NewSuMoneyService(nil)
	suMoneyTransactionRecordStatisticsCommand := &command.SuMoneyTransactionRecordStatisticsCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), suMoneyTransactionRecordStatisticsCommand)
	data, err := suMoneyService.SuMoneyTransactionRecordStatistics(suMoneyTransactionRecordStatisticsCommand)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

// 投入现金池
func (controller *SuMoneyController) CashInput() {
	cashPoolService := service.NewCashPoolService(nil)
	createCashPoolCommand := &command.CreateCashPoolCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), createCashPoolCommand)
	data, err := cashPoolService.CreateCashPool(createCashPoolCommand)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

// 返回现金池
func (controller *SuMoneyController) GetCashPool() {
	cashPoolService := service.NewCashPoolService(nil)
	getCashPoolQuery := &query.GetCashPoolQuery{}
	//json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), getCashPoolQuery)
	companyId, _ := controller.GetInt64("companyId")
	getCashPoolQuery.CompanyId = companyId
	data, err := cashPoolService.GetCashPool(getCashPoolQuery)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

// 返回兑换活动列表
func (controller *SuMoneyController) ListExchangeActivities () {
	cashPoolService := service.NewCashPoolService(nil)
	listExchangeCashActivityQuery := &query.ListExchangeCashActivityQuery{}
	companyId, _ := controller.GetInt64("companyId")
	listExchangeCashActivityQuery.CompanyId = companyId
	exchangeCashActivityNameMatch := controller.GetString("activityNameMatch")
	listExchangeCashActivityQuery.ExchangeCashActivityNameMatch = exchangeCashActivityNameMatch
	offset, _ := controller.GetInt("offset")
	listExchangeCashActivityQuery.Offset = offset
	limit, _ := controller.GetInt("limit")
	listExchangeCashActivityQuery.Limit = limit
	data, err := cashPoolService.ListExchangeCashActivity(listExchangeCashActivityQuery)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

// 更新兑换活动信息
func (controller *SuMoneyController) UpdateExchangeActivities () {
	cashPoolService := service.NewCashPoolService(nil)
	updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), updateExchangeCashActivityCommand)
	activityId, _ := controller.GetInt64(":activityId")
	updateExchangeCashActivityCommand.ExchangeCashActivityId = activityId
	data, err := cashPoolService.UpdateExchangeCashActivity(updateExchangeCashActivityCommand)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

// 新增兑换活动
func (controller *SuMoneyController) CreateExchangeActivities ()  {
	cashPoolService := service.NewCashPoolService(nil)
	createExchangeCashActivityCommand := &command.CreateExchangeCashActivityCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), createExchangeCashActivityCommand)
	data, err := cashPoolService.CreateExchangeCashActivity(createExchangeCashActivityCommand)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

// 移除兑换活动
func (controller *SuMoneyController) RemoveExchangeActivities () {
	cashPoolService := service.NewCashPoolService(nil)
	removeExchangeCashActivityCommand := &command.RemoveExchangeCashActivityCommand{}
	activityId, _ := controller.GetInt64(":activityId")
	beego.Info(activityId)
	removeExchangeCashActivityCommand.ActivityId = activityId
	data, err := cashPoolService.RemoveExchangeCashActivity(removeExchangeCashActivityCommand)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

// 返回兑换现金活动
func (controller *SuMoneyController) GetExchangeCashActivity () {
	cashPoolService := service.NewCashPoolService(nil)
	getExchangeCashActivityQuery := &query.GetExchangeCashActivityQuery{}
	activityId, _ := controller.GetInt64(":activityId")
	getExchangeCashActivityQuery.ExchangeCashActivityId = activityId
	data, err := cashPoolService.GetExchangeCashActivity(getExchangeCashActivityQuery)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

// 返回兑换素币清单列表
func (controller *SuMoneyController) ListExchangeList () {
	cashPoolService := service.NewCashPoolService(nil)
	listExchangeCashListQuery := &query.ListExchangeCashPersonQuery{}
	activityId, _ := controller.GetInt64("activityId")
	listExchangeCashListQuery.ExchangeCashActivityId = activityId
	exchangeCashPersonNameMatch := controller.GetString("personNameMatch")
	listExchangeCashListQuery.ExchangeCashPersonNameMatch = exchangeCashPersonNameMatch
	offset, _ := controller.GetInt("offset")
	listExchangeCashListQuery.Offset = offset
	limit, _ := controller.GetInt("limit")
	listExchangeCashListQuery.Limit = limit
	data, err := cashPoolService.ListExchangeCashPerson(listExchangeCashListQuery)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

// 返回兑换素币清单
func (controller *SuMoneyController) CreateExchangeList () {
	cashPoolService := service.NewCashPoolService(nil)
	createExchangeCashPersonCommand := &command.CreateExchangeCashPersonCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), createExchangeCashPersonCommand)
	data, err := cashPoolService.CreateExchangeCashPerson(createExchangeCashPersonCommand)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

// 更新兑换素币清单
func (controller *SuMoneyController) UpdateExchangeList ()  {
	cashPoolService := service.NewCashPoolService(nil)
	updateExchangeCashPersonCommand := &command.UpdateExchangeCashPersonCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), updateExchangeCashPersonCommand)
	personId, _ := controller.GetInt64(":listId")
	updateExchangeCashPersonCommand.ListId = personId
	data, err := cashPoolService.UpdateExchangeCashPerson(updateExchangeCashPersonCommand)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

// 移除兑换素币清单
func (controller *SuMoneyController) RemoveExchangeCashPerson() {
	cashPoolService := service.NewCashPoolService(nil)
	removeExchangeCashPersonCommand := &command.RemoveExchangeCashPersonCommand{}
	personId, _ := controller.GetInt64(":listId")
	removeExchangeCashPersonCommand.ListId = personId
	data, err := cashPoolService.RemoveExchangeCashPerson(removeExchangeCashPersonCommand)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

// 返回兑换素币清单
func (controller *SuMoneyController) GetExchangeCashPerson () {
	cashPoolService := service.NewCashPoolService(nil)
	getExchangeCashPersonQuery := &query.GetExchangeCashPersonQuery{}
	listId, _ := controller.GetInt64(":listId")
	getExchangeCashPersonQuery.ExchangeCashPersonId = listId
	data, err := cashPoolService.GetExchangeCashPerson(getExchangeCashPersonQuery)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

// 导入兑换素币清单
func (controller *SuMoneyController) ImportExchangeList () {
	cashPoolService := service.NewCashPoolService(nil)
	createExchangeCashPersonCommand := &command.CreateExchangeCashPersonCommand{}
	activityId, _ := controller.GetInt64("activityId")
	file, h, _ := controller.GetFile("file") //获取上传的文件
	ext := path.Ext(h.Filename)
	AllowExtMap := map[string]bool{
		".xlsx":true,
	}
	if _,ok:=AllowExtMap[ext];!ok{
		controller.Ctx.WriteString( "后缀名不符合上传要求" )
		return
	}
	xlsx, err := excelize.OpenReader(file)
	if err != nil {
		fmt.Println(err)
		return
	}
	var response utils.JsonResponse
	dataList := make([]interface{}, 0)
	rows, _ := xlsx.GetRows("Sheet1")
	for i, row := range rows {
		if i > 3 {
			for _, _ = range row {
				createExchangeCashPersonCommand.ExchangeCashActivityId = activityId
				createExchangeCashPersonCommand.PersonName = row[0]
				createExchangeCashPersonCommand.PersonAccount = row[1]
				createExchangeCashPersonCommand.ExchangedSuMoney, _ = strconv.ParseFloat(row[2], 64)
			}
			fmt.Print(createExchangeCashPersonCommand)
			data, err := cashPoolService.CreateExchangeCashPerson(createExchangeCashPersonCommand)
			if err != nil {
				response = utils.ResponseError(controller.Ctx, err)
			} else {
				dataList = append(dataList, data)
			}
		}
	}
	ret := map[string]interface{}{
		"count": len(dataList),
		"people": dataList,
	}
	response = utils.ResponseData(controller.Ctx, ret)
	controller.Data["json"] = response
	controller.ServeJSON()
}

// TODO 导出素币兑换清单,选择导出(ids),增加导出失败信息
func (controller *SuMoneyController) ExportExchangeList() {

}

// TODO 导出素币流水记录,选择导出(ids),增加导出失败信息
func (controller *SuMoneyController) ExportSuMoneyTransactionRecord() {

}