作者 陈志颖

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

package command
import (
"fmt"
"github.com/astaxie/beego/validation"
)
type ExportTransactionRecordCommand struct {
IDs []int `json:"iDs"`
}
func (exportTransactionRecordCommand *ExportTransactionRecordCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(exportTransactionRecordCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
... ... @@ -129,6 +129,7 @@ func (suMoneyService *SuMoneyService) SearchSuMoneyTransactionRecord(searchSuMon
defer func() {
transactionContext.RollbackTransaction()
}()
var suMoneyTransactionRecordRepository domain.SuMoneyTransactionRecordRepository
if value, err := factory.CreateSuMoneyTransactionRecordRepository(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -137,6 +138,7 @@ func (suMoneyService *SuMoneyService) SearchSuMoneyTransactionRecord(searchSuMon
} else {
suMoneyTransactionRecordRepository = value
}
if count, suMoneyTransactionRecords, err := suMoneyTransactionRecordRepository.Find(tool_funs.SimpleStructToMap(searchSuMoneyTransactionRecordCommand)); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
... ... @@ -220,6 +222,41 @@ func (suMoneyService *SuMoneyService) ContributionsTransactionRecordStatistics(c
}
}
// 根据id获取事务记录
func (suMoneyService *SuMoneyService) ListSuMoneyTransactionRecordById(exportSuMoneyTransactionRecordCommand *command.ExportTransactionRecordCommand) ([]*domain.SuMoneyTransactionRecord, error) {
if err := exportSuMoneyTransactionRecordCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var suMoneyTransactionRecordRepository domain.SuMoneyTransactionRecordRepository
if value, err := factory.CreateSuMoneyTransactionRecordRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
suMoneyTransactionRecordRepository = value
}
if _, suMoneyTransactionRecords, err := suMoneyTransactionRecordRepository.FindById(tool_funs.SimpleStructToMap(exportSuMoneyTransactionRecordCommand)); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return suMoneyTransactionRecords, nil
}
}
func NewSuMoneyService(options map[string]interface{}) *SuMoneyService {
newSuMoneyService := &SuMoneyService{}
... ...
... ... @@ -31,6 +31,7 @@ type SuMoneyTransactionRecordRepository interface {
Remove(suMoneyTransactionRecord *SuMoneyTransactionRecord) (*SuMoneyTransactionRecord, error)
FindOne(queryOptions map[string]interface{}) (*SuMoneyTransactionRecord, error)
Find(queryOptions map[string]interface{}) (int64, []*SuMoneyTransactionRecord, error)
FindById(queryOptions map[string]interface{}) (int64, []*SuMoneyTransactionRecord, error)
}
func (suMoneyTransactionRecord *SuMoneyTransactionRecord) Identify() interface{} {
... ...
... ... @@ -54,7 +54,6 @@ func (repository *ExchangeCashPersonListRepository) FindOne(queryOptions map[str
}
}
// 根据id获取兑换活动清单
func (repository *ExchangeCashPersonListRepository) FindById(queryOptions map[string]interface{}) (int64, []*domain.ExchangeCashPersonList, error) {
tx := repository.transactionContext.PgTx
var exchangeCashListModels []*models.ExchangeCashPersonList
... ...
... ... @@ -65,6 +65,28 @@ func (repository *SuMoneyTransactionRecordRepository) FindOne(queryOptions map[s
}
}
func (repository *SuMoneyTransactionRecordRepository) FindById(queryOptions map[string]interface{}) (int64, []*domain.SuMoneyTransactionRecord, error) {
tx := repository.transactionContext.PgTx
var suMoneyTransactionRecordModels []*models.SuMoneyTransactionRecord
suMoneyTransactionRecords := make([]*domain.SuMoneyTransactionRecord, 0)
query := tx.Model(&suMoneyTransactionRecordModels)
if iDs, ok := queryOptions["iDs"]; ok && len(iDs.([]int)) != 0 {
query = query.Where("su_money_transaction_record.id IN (?)", pg.In(iDs.([]int)))
}
if count, err := query.Order("id DESC").SelectAndCount(); err != nil {
return 0, suMoneyTransactionRecords, err
} else {
for _, suMoneyTransactionRecordModel := range suMoneyTransactionRecordModels {
if suMoneyTransactionRecord, err := repository.transformPgModelToDomainModel(suMoneyTransactionRecordModel); err != nil {
return 0, suMoneyTransactionRecords, err
} else {
suMoneyTransactionRecords = append(suMoneyTransactionRecords, suMoneyTransactionRecord)
}
}
return int64(count), suMoneyTransactionRecords, nil
}
}
func (repository *SuMoneyTransactionRecordRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.SuMoneyTransactionRecord, error) {
tx := repository.transactionContext.PgTx
var suMoneyTransactionRecordModels []*models.SuMoneyTransactionRecord
... ...
... ... @@ -12,6 +12,7 @@ import (
"os"
"path"
"strconv"
"time"
)
type SuMoneyController struct {
... ... @@ -510,13 +511,149 @@ func (controller *SuMoneyController) ExportExchangeList() {
}
}
// TODO 导出素币流水记录,选择导出(ids),增加导出失败信息
func (controller *SuMoneyController) ExportSuMoneyTransactionRecord() {
}
// TODO 导出员工素币情况列表,选择导出(ids),增加导出失败信息
// 导出素币流水
func (controller *SuMoneyController) ExportSuMoney() {
suMoneyService := service.NewSuMoneyService(nil)
exportTransactionRecordCommand := &command.ExportTransactionRecordCommand{}
json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), exportTransactionRecordCommand)
// 列标题
titles := []string{
"员工姓名","手机账号","当前素币",
}
// 列单元
cells := []string{
"A","B","C",
}
var response utils.JsonResponse
var data []map[string]interface{}
records, err := suMoneyService.ListSuMoneyTransactionRecordById(exportTransactionRecordCommand)
for _, record := range records {
p := map[string]interface{} {
"name": record.Employee.EmployeeName,
"account": record.Employee.EmployeeAccount,
"current_su_money": record.SuMoney,
}
data = append(data, p)
}
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)
f.SetCellValue("Sheet1", sheetPosition, vv) // 所有字段保存为字符串类型
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)
}
}
}
// 导出素币流水记录
func (controller *SuMoneyController) ExportSuMoneyTransactionRecord() {
suMoneyService := service.NewSuMoneyService(nil)
exportTransactionRecordCommand := &command.ExportTransactionRecordCommand{}
json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), exportTransactionRecordCommand)
// 列标题
titles := []string{
"员工姓名","当前素币","素币流水","操作时间","操作人",
}
// 列单元
cells := []string{
"A","B","C","D","E",
}
var response utils.JsonResponse
var data []map[string]interface{}
records, err := suMoneyService.ListSuMoneyTransactionRecordById(exportTransactionRecordCommand)
for _, record := range records {
p := map[string]interface{} {
"name": record.Employee.EmployeeName,
"current_su_money": record.SuMoney,
"record": record.SuMoney,
"create_time": record.CreateTime,
"operator": record.Operator,
}
data = append(data, p)
}
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)
//f.SetCellValue("Sheet1", sheetPosition, vv) // 所有字段保存为字符串类型
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
case time.Time:
f.SetCellValue("Sheet1", sheetPosition, vv.(time.Time))
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)
}
}
}
... ...