su_money_controller.go 17.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
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"
	"os"
	"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
	companyId, _ := controller.GetInt64("companyId")
	listExchangeCashListQuery.CompanyId = companyId
	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")
	operatorId, _ := controller.GetInt64("operator")
	removeExchangeCashPersonCommand.ListId = personId
	removeExchangeCashPersonCommand.Operator = operatorId
	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")
	operator, _ := controller.GetInt64("operator")
	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
	}
	// TODO 增加非空校验,导入失败原因返回
	var response utils.JsonResponse
	dataList := make([]interface{}, 0)
	rows, _ := xlsx.GetRows("Sheet1")
	if len(rows) > 1000 {
		controller.Ctx.WriteString( "导入行数超过1000行" )
		return
	}
	for i, row := range rows {
		if i > 2 {
			for _, _ = range row {
				createExchangeCashPersonCommand.ExchangeCashActivityId = activityId
				r2, _ := strconv.ParseFloat(row[2], 64)
				if row[0] == "" || row[1] == "" || r2 == 0 {
					controller.Ctx.WriteString("单元格包含空字段")
					return
				}
				createExchangeCashPersonCommand.Operator = operator
				createExchangeCashPersonCommand.PersonName = row[0]
				createExchangeCashPersonCommand.PersonAccount = row[1]
				createExchangeCashPersonCommand.ExchangedSuMoney, _ = strconv.ParseFloat(row[2], 64)
			}
			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()
}

// 返回兑换活动截止时间列表
func (controller *SuMoneyController) ListDeadline() {
	cashPoolService := service.NewCashPoolService(nil)
	listExchangeCashActivityDeadlineQuery := &query.ListExchangeCashActivityDeadlineQuery{}
	companyId, _ := controller.GetInt64("companyId")
	listExchangeCashActivityDeadlineQuery.CompanyId = companyId
	data, err := cashPoolService.ListExchangeCashActivityDeadline(listExchangeCashActivityDeadlineQuery)
	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()
}

// 导出素币兑换清单,选择导出(ids),增加导出失败信息
func (controller *SuMoneyController) ExportExchangeList() {
	cashPoolService := service.NewCashPoolService(nil)
	exportExchangeCashListCommand := &command.ExportExchangeCashListCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), exportExchangeCashListCommand)
	// 列标题
	titles := []string{
		"姓名","手机账号","已兑换现金","已兑换素币",
	}
	// 列单元
	cells := []string{
		"A","B","C","D",
	}
	// 数据源
	var data []map[string]interface{}
	people, err := cashPoolService.ListExchangeCashPersonById(exportExchangeCashListCommand)
	for _, person := range people {
		p := map[string]interface{} {
			"name": person.EmployeeInfo.EmployeeName,
			"account": person.EmployeeInfo.EmployeeAccount,
			"exchanged_cash": person.ExchangedCash,
			"exchanged_su_money": person.ExchangedSuMoney,
		}
		data = append(data, p)
	}
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
		controller.Data["json"] = response
		controller.ServeJSON()
	} else {
		// 新建文件
		f := excelize.NewFile()
		// 新建工作簿
		index := f.NewSheet("Sheet1")
		//列标题赋值
		for column, v := range titles {
			f.SetCellValue("Sheet1", cells[column]+"1", v)
		}
		for lineNum, v := range data {   //行数 lineNum  行内容 v
			columnNum := 0 //列数
			for _, vv := range v {
				sheetPosition := cells[columnNum] + strconv.Itoa(lineNum+2)
				switch vv.(type) {
				case string:
					f.SetCellValue("Sheet1", sheetPosition, vv.(string))
					break
				case int:
					f.SetCellValue("Sheet1", sheetPosition, vv.(int))
					break
				case float64:
					f.SetCellValue("Sheet1", sheetPosition, vv.(float64))
					break
				}
				columnNum++
			}
		}
		f.SetActiveSheet(index)
		// 创建下载文件夹
		mkErr :=os.Mkdir("download", os.ModePerm)
		if err!=nil{
			fmt.Println(mkErr)
		}
		//保存为文件
		f.Path="download/素币兑换清单.xlsx"
		if err := f.Save(); err != nil {
			fmt.Println(err)
		}
		controller.Ctx.Output.Download(f.Path,"素币兑换清单.xlsx")
		// 下载完成删除文件
		removeErr := os.Remove(f.Path)
		if err != nil {
			fmt.Println(removeErr)
		}
	}
}

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

}