作者 陈志颖

feat:增加流水导出和流水记录导出

  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/astaxie/beego/validation"
  6 +)
  7 +
  8 +type ExportTransactionRecordCommand struct {
  9 + IDs []int `json:"iDs"`
  10 +}
  11 +
  12 +func (exportTransactionRecordCommand *ExportTransactionRecordCommand) ValidateCommand() error {
  13 + valid := validation.Validation{}
  14 + b, err := valid.Valid(exportTransactionRecordCommand)
  15 + if err != nil {
  16 + return err
  17 + }
  18 + if !b {
  19 + for _, validErr := range valid.Errors {
  20 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  21 + }
  22 + }
  23 + return nil
  24 +}
@@ -129,6 +129,7 @@ func (suMoneyService *SuMoneyService) SearchSuMoneyTransactionRecord(searchSuMon @@ -129,6 +129,7 @@ func (suMoneyService *SuMoneyService) SearchSuMoneyTransactionRecord(searchSuMon
129 defer func() { 129 defer func() {
130 transactionContext.RollbackTransaction() 130 transactionContext.RollbackTransaction()
131 }() 131 }()
  132 +
132 var suMoneyTransactionRecordRepository domain.SuMoneyTransactionRecordRepository 133 var suMoneyTransactionRecordRepository domain.SuMoneyTransactionRecordRepository
133 if value, err := factory.CreateSuMoneyTransactionRecordRepository(map[string]interface{}{ 134 if value, err := factory.CreateSuMoneyTransactionRecordRepository(map[string]interface{}{
134 "transactionContext": transactionContext, 135 "transactionContext": transactionContext,
@@ -137,6 +138,7 @@ func (suMoneyService *SuMoneyService) SearchSuMoneyTransactionRecord(searchSuMon @@ -137,6 +138,7 @@ func (suMoneyService *SuMoneyService) SearchSuMoneyTransactionRecord(searchSuMon
137 } else { 138 } else {
138 suMoneyTransactionRecordRepository = value 139 suMoneyTransactionRecordRepository = value
139 } 140 }
  141 +
140 if count, suMoneyTransactionRecords, err := suMoneyTransactionRecordRepository.Find(tool_funs.SimpleStructToMap(searchSuMoneyTransactionRecordCommand)); err != nil { 142 if count, suMoneyTransactionRecords, err := suMoneyTransactionRecordRepository.Find(tool_funs.SimpleStructToMap(searchSuMoneyTransactionRecordCommand)); err != nil {
141 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 143 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
142 } else { 144 } else {
@@ -220,6 +222,41 @@ func (suMoneyService *SuMoneyService) ContributionsTransactionRecordStatistics(c @@ -220,6 +222,41 @@ func (suMoneyService *SuMoneyService) ContributionsTransactionRecordStatistics(c
220 } 222 }
221 } 223 }
222 224
  225 +// 根据id获取事务记录
  226 +func (suMoneyService *SuMoneyService) ListSuMoneyTransactionRecordById(exportSuMoneyTransactionRecordCommand *command.ExportTransactionRecordCommand) ([]*domain.SuMoneyTransactionRecord, error) {
  227 + if err := exportSuMoneyTransactionRecordCommand.ValidateCommand(); err != nil {
  228 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  229 + }
  230 + transactionContext, err := factory.CreateTransactionContext(nil)
  231 + if err != nil {
  232 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  233 + }
  234 + if err := transactionContext.StartTransaction(); err != nil {
  235 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  236 + }
  237 + defer func() {
  238 + transactionContext.RollbackTransaction()
  239 + }()
  240 +
  241 + var suMoneyTransactionRecordRepository domain.SuMoneyTransactionRecordRepository
  242 + if value, err := factory.CreateSuMoneyTransactionRecordRepository(map[string]interface{}{
  243 + "transactionContext": transactionContext,
  244 + }); err != nil {
  245 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  246 + } else {
  247 + suMoneyTransactionRecordRepository = value
  248 + }
  249 +
  250 + if _, suMoneyTransactionRecords, err := suMoneyTransactionRecordRepository.FindById(tool_funs.SimpleStructToMap(exportSuMoneyTransactionRecordCommand)); err != nil {
  251 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  252 + } else {
  253 + if err := transactionContext.CommitTransaction(); err != nil {
  254 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  255 + }
  256 + return suMoneyTransactionRecords, nil
  257 + }
  258 +}
  259 +
223 260
224 func NewSuMoneyService(options map[string]interface{}) *SuMoneyService { 261 func NewSuMoneyService(options map[string]interface{}) *SuMoneyService {
225 newSuMoneyService := &SuMoneyService{} 262 newSuMoneyService := &SuMoneyService{}
@@ -31,6 +31,7 @@ type SuMoneyTransactionRecordRepository interface { @@ -31,6 +31,7 @@ type SuMoneyTransactionRecordRepository interface {
31 Remove(suMoneyTransactionRecord *SuMoneyTransactionRecord) (*SuMoneyTransactionRecord, error) 31 Remove(suMoneyTransactionRecord *SuMoneyTransactionRecord) (*SuMoneyTransactionRecord, error)
32 FindOne(queryOptions map[string]interface{}) (*SuMoneyTransactionRecord, error) 32 FindOne(queryOptions map[string]interface{}) (*SuMoneyTransactionRecord, error)
33 Find(queryOptions map[string]interface{}) (int64, []*SuMoneyTransactionRecord, error) 33 Find(queryOptions map[string]interface{}) (int64, []*SuMoneyTransactionRecord, error)
  34 + FindById(queryOptions map[string]interface{}) (int64, []*SuMoneyTransactionRecord, error)
34 } 35 }
35 36
36 func (suMoneyTransactionRecord *SuMoneyTransactionRecord) Identify() interface{} { 37 func (suMoneyTransactionRecord *SuMoneyTransactionRecord) Identify() interface{} {
@@ -54,7 +54,6 @@ func (repository *ExchangeCashPersonListRepository) FindOne(queryOptions map[str @@ -54,7 +54,6 @@ func (repository *ExchangeCashPersonListRepository) FindOne(queryOptions map[str
54 } 54 }
55 } 55 }
56 56
57 -// 根据id获取兑换活动清单  
58 func (repository *ExchangeCashPersonListRepository) FindById(queryOptions map[string]interface{}) (int64, []*domain.ExchangeCashPersonList, error) { 57 func (repository *ExchangeCashPersonListRepository) FindById(queryOptions map[string]interface{}) (int64, []*domain.ExchangeCashPersonList, error) {
59 tx := repository.transactionContext.PgTx 58 tx := repository.transactionContext.PgTx
60 var exchangeCashListModels []*models.ExchangeCashPersonList 59 var exchangeCashListModels []*models.ExchangeCashPersonList
@@ -65,6 +65,28 @@ func (repository *SuMoneyTransactionRecordRepository) FindOne(queryOptions map[s @@ -65,6 +65,28 @@ func (repository *SuMoneyTransactionRecordRepository) FindOne(queryOptions map[s
65 } 65 }
66 } 66 }
67 67
  68 +func (repository *SuMoneyTransactionRecordRepository) FindById(queryOptions map[string]interface{}) (int64, []*domain.SuMoneyTransactionRecord, error) {
  69 + tx := repository.transactionContext.PgTx
  70 + var suMoneyTransactionRecordModels []*models.SuMoneyTransactionRecord
  71 + suMoneyTransactionRecords := make([]*domain.SuMoneyTransactionRecord, 0)
  72 + query := tx.Model(&suMoneyTransactionRecordModels)
  73 + if iDs, ok := queryOptions["iDs"]; ok && len(iDs.([]int)) != 0 {
  74 + query = query.Where("su_money_transaction_record.id IN (?)", pg.In(iDs.([]int)))
  75 + }
  76 + if count, err := query.Order("id DESC").SelectAndCount(); err != nil {
  77 + return 0, suMoneyTransactionRecords, err
  78 + } else {
  79 + for _, suMoneyTransactionRecordModel := range suMoneyTransactionRecordModels {
  80 + if suMoneyTransactionRecord, err := repository.transformPgModelToDomainModel(suMoneyTransactionRecordModel); err != nil {
  81 + return 0, suMoneyTransactionRecords, err
  82 + } else {
  83 + suMoneyTransactionRecords = append(suMoneyTransactionRecords, suMoneyTransactionRecord)
  84 + }
  85 + }
  86 + return int64(count), suMoneyTransactionRecords, nil
  87 + }
  88 +}
  89 +
68 func (repository *SuMoneyTransactionRecordRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.SuMoneyTransactionRecord, error) { 90 func (repository *SuMoneyTransactionRecordRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.SuMoneyTransactionRecord, error) {
69 tx := repository.transactionContext.PgTx 91 tx := repository.transactionContext.PgTx
70 var suMoneyTransactionRecordModels []*models.SuMoneyTransactionRecord 92 var suMoneyTransactionRecordModels []*models.SuMoneyTransactionRecord
@@ -12,6 +12,7 @@ import ( @@ -12,6 +12,7 @@ import (
12 "os" 12 "os"
13 "path" 13 "path"
14 "strconv" 14 "strconv"
  15 + "time"
15 ) 16 )
16 17
17 type SuMoneyController struct { 18 type SuMoneyController struct {
@@ -510,13 +511,149 @@ func (controller *SuMoneyController) ExportExchangeList() { @@ -510,13 +511,149 @@ func (controller *SuMoneyController) ExportExchangeList() {
510 } 511 }
511 } 512 }
512 513
513 -// TODO 导出素币流水记录,选择导出(ids),增加导出失败信息  
514 -func (controller *SuMoneyController) ExportSuMoneyTransactionRecord() {  
515 -  
516 -}  
517 -  
518 -// TODO 导出员工素币情况列表,选择导出(ids),增加导出失败信息 514 +// 导出素币流水
519 func (controller *SuMoneyController) ExportSuMoney() { 515 func (controller *SuMoneyController) ExportSuMoney() {
  516 + suMoneyService := service.NewSuMoneyService(nil)
  517 + exportTransactionRecordCommand := &command.ExportTransactionRecordCommand{}
  518 + json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), exportTransactionRecordCommand)
  519 + // 列标题
  520 + titles := []string{
  521 + "员工姓名","手机账号","当前素币",
  522 + }
  523 + // 列单元
  524 + cells := []string{
  525 + "A","B","C",
  526 + }
  527 + var response utils.JsonResponse
  528 + var data []map[string]interface{}
  529 + records, err := suMoneyService.ListSuMoneyTransactionRecordById(exportTransactionRecordCommand)
  530 + for _, record := range records {
  531 + p := map[string]interface{} {
  532 + "name": record.Employee.EmployeeName,
  533 + "account": record.Employee.EmployeeAccount,
  534 + "current_su_money": record.SuMoney,
  535 + }
  536 + data = append(data, p)
  537 + }
  538 + if err != nil {
  539 + response = utils.ResponseError(controller.Ctx, err)
  540 + controller.Data["json"] = response
  541 + controller.ServeJSON()
  542 + } else {
  543 + // 新建文件
  544 + f := excelize.NewFile()
  545 + // 新建工作簿
  546 + index := f.NewSheet("Sheet1")
  547 + //列标题赋值
  548 + for column, v := range titles {
  549 + f.SetCellValue("Sheet1", cells[column]+"1", v)
  550 + }
  551 + for lineNum, v := range data { //行数 lineNum 行内容 v
  552 + columnNum := 0 //列数
  553 + for _, vv := range v {
  554 + sheetPosition := cells[columnNum] + strconv.Itoa(lineNum+2)
  555 + f.SetCellValue("Sheet1", sheetPosition, vv) // 所有字段保存为字符串类型
  556 + columnNum++
  557 + }
  558 + }
  559 + f.SetActiveSheet(index)
  560 + // 创建下载文件夹
  561 + mkErr :=os.Mkdir("download", os.ModePerm)
  562 + if err!=nil{
  563 + fmt.Println(mkErr)
  564 + }
  565 + //保存为文件
  566 + f.Path="download/素币流水.xlsx"
  567 + if err := f.Save(); err != nil {
  568 + fmt.Println(err)
  569 + }
  570 + controller.Ctx.Output.Download(f.Path,"素币流水.xlsx")
  571 + // 下载完成删除文件
  572 + removeErr := os.Remove(f.Path)
  573 + if err != nil {
  574 + fmt.Println(removeErr)
  575 + }
  576 + }
  577 +}
520 578
  579 +// 导出素币流水记录
  580 +func (controller *SuMoneyController) ExportSuMoneyTransactionRecord() {
  581 + suMoneyService := service.NewSuMoneyService(nil)
  582 + exportTransactionRecordCommand := &command.ExportTransactionRecordCommand{}
  583 + json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), exportTransactionRecordCommand)
  584 + // 列标题
  585 + titles := []string{
  586 + "员工姓名","当前素币","素币流水","操作时间","操作人",
  587 + }
  588 + // 列单元
  589 + cells := []string{
  590 + "A","B","C","D","E",
  591 + }
  592 + var response utils.JsonResponse
  593 + var data []map[string]interface{}
  594 + records, err := suMoneyService.ListSuMoneyTransactionRecordById(exportTransactionRecordCommand)
  595 + for _, record := range records {
  596 + p := map[string]interface{} {
  597 + "name": record.Employee.EmployeeName,
  598 + "current_su_money": record.SuMoney,
  599 + "record": record.SuMoney,
  600 + "create_time": record.CreateTime,
  601 + "operator": record.Operator,
  602 + }
  603 + data = append(data, p)
  604 + }
  605 + if err != nil {
  606 + response = utils.ResponseError(controller.Ctx, err)
  607 + controller.Data["json"] = response
  608 + controller.ServeJSON()
  609 + } else {
  610 + // 新建文件
  611 + f := excelize.NewFile()
  612 + // 新建工作簿
  613 + index := f.NewSheet("Sheet1")
  614 + //列标题赋值
  615 + for column, v := range titles {
  616 + f.SetCellValue("Sheet1", cells[column]+"1", v)
  617 + }
  618 + for lineNum, v := range data { //行数 lineNum 行内容 v
  619 + columnNum := 0 //列数
  620 + for _, vv := range v {
  621 + sheetPosition := cells[columnNum] + strconv.Itoa(lineNum+2)
  622 + //f.SetCellValue("Sheet1", sheetPosition, vv) // 所有字段保存为字符串类型
  623 + switch vv.(type) {
  624 + case string:
  625 + f.SetCellValue("Sheet1", sheetPosition, vv.(string))
  626 + break
  627 + case int:
  628 + f.SetCellValue("Sheet1", sheetPosition, vv.(int))
  629 + break
  630 + case float64:
  631 + f.SetCellValue("Sheet1", sheetPosition, vv.(float64))
  632 + break
  633 + case time.Time:
  634 + f.SetCellValue("Sheet1", sheetPosition, vv.(time.Time))
  635 + break
  636 + }
  637 + columnNum++
  638 + }
  639 + }
  640 + f.SetActiveSheet(index)
  641 + // 创建下载文件夹
  642 + mkErr :=os.Mkdir("download", os.ModePerm)
  643 + if err!=nil{
  644 + fmt.Println(mkErr)
  645 + }
  646 + //保存为文件
  647 + f.Path="download/素币流水记录.xlsx"
  648 + if err := f.Save(); err != nil {
  649 + fmt.Println(err)
  650 + }
  651 + controller.Ctx.Output.Download(f.Path,"素币流水记录.xlsx")
  652 + // 下载完成删除文件
  653 + removeErr := os.Remove(f.Path)
  654 + if err != nil {
  655 + fmt.Println(removeErr)
  656 + }
  657 + }
521 } 658 }
522 659