作者 陈志颖

feat:增加素币兑换清单导出

... ... @@ -35,5 +35,9 @@ func main() {
}
}
}()
// excel文件路径映射
beego.SetStaticPath("/public", "public")
beego.Run()
}
... ...
package command
import (
"fmt"
"github.com/astaxie/beego/validation"
)
type ExportExchangeCashListCommand struct {
IDs []int `json:"iDs"`
}
func (exportExchangeCashListCommand *ExportExchangeCashListCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(exportExchangeCashListCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
... ... @@ -1502,6 +1502,45 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
}
}
// 根据id获取兑换清单
func (cashPoolService *CashPoolService) ListExchangeCashPersonById(exportExchangeCashListCommand *command.ExportExchangeCashListCommand) ([]*domain.ExchangeCashPersonList, error) {
if err := exportExchangeCashListCommand.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 exchangeCashPersonListRepository domain.ExchangeCashPersonListRepository
if value, err := factory.CreateExchangeCashPersonListRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
exchangeCashPersonListRepository = value
}
if _, people, err := exchangeCashPersonListRepository.FindById(tool_funs.SimpleStructToMap(exportExchangeCashListCommand)); 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 map[string]interface{}{
// "count": count,
// "people": people,
//}, nil
return people, nil
}
}
func NewCashPoolService(options map[string]interface{}) *CashPoolService {
newCashPoolService := &CashPoolService{}
return newCashPoolService
... ...
... ... @@ -15,6 +15,7 @@ type ExchangeCashPersonListRepository interface {
FindOne(queryOptions map[string]interface{}) (*ExchangeCashPersonList, error)
Find(queryOptions map[string]interface{}) (int64, []*ExchangeCashPersonList, error)
FindAll(queryOptions map[string]interface{}) (int64, []*ExchangeCashPersonList, error)
FindById(queryOptions map[string]interface{}) (int64, []*ExchangeCashPersonList, error)
}
func (exchangeCashPersonList *ExchangeCashPersonList) Identity() interface{} {
... ...
... ... @@ -54,6 +54,29 @@ 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
exchangeCashPeople := make([]*domain.ExchangeCashPersonList, 0)
query := tx.Model(&exchangeCashListModels)
if iDs, ok := queryOptions["iDs"]; ok && len(iDs.([]int)) != 0 {
query = query.Where("exchange_cash_person_list.id IN (?)", pg.In(iDs.([]int)) )
}
if count, err := query.Order("id DESC").SelectAndCount(); err != nil {
return 0, exchangeCashPeople, err
} else {
for _, exchangeCashListModel := range exchangeCashListModels {
if taskNature, err := repository.transformPgModelToDomainModel(exchangeCashListModel); err != nil {
return 0, exchangeCashPeople, err
} else {
exchangeCashPeople = append(exchangeCashPeople, taskNature)
}
}
return int64(count), exchangeCashPeople, nil
}
}
// 获取平台所有兑换活动清单(兑换现金活动总清单)
func (repository *ExchangeCashPersonListRepository) FindAll(queryOptions map[string]interface{}) (int64, []*domain.ExchangeCashPersonList, error) {
tx := repository.transactionContext.PgTx
... ...
... ... @@ -399,7 +399,71 @@ func (controller *SuMoneyController) ListDeadline() {
// TODO 导出素币兑换清单,选择导出(ids),增加导出失败信息
func (controller *SuMoneyController) ExportExchangeList() {
cashPoolService := service.NewCashPoolService(nil)
exportExchangeCashListCommand := &command.ExportExchangeCashListCommand{}
json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), exportExchangeCashListCommand)
fmt.Print(exportExchangeCashListCommand, "\n")
// 列标题
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)
}
fmt.Print(data, "\n")
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)
//保存为文件
f.Path="public/file/素币兑换清单.xlsx"
if err := f.Save(); err != nil {
fmt.Println(err)
}
controller.Ctx.Output.Download(f.Path,"素币兑换清单.xlsx")
}
}
// TODO 导出素币流水记录,选择导出(ids),增加导出失败信息
... ...
#mmm-worth 1
\ No newline at end of file
... ...
不能预览此文件类型
不能预览此文件类型