作者 陈志颖

Merge branch 'feature-exchange_list' into dev

... ... @@ -35,5 +35,9 @@ func main() {
}
}
}()
// excel文件路径映射
beego.SetStaticPath("/public", "public")
beego.Run()
}
... ...
package command
import (
"fmt"
"github.com/astaxie/beego/validation"
"time"
)
type EmployeesRankingListStatisticsCommand struct {
CompanyId int `json:"companyId" valid:"Required"` // 公司id
StartTime time.Time `json:"startTime"` // 年榜开始时间
EndTime time.Time `json:"endTime"` // 年榜结束时间
Offset int `json:"offset,omitempty"` // 查询偏离量
Limit int `json:"limit,omitempty"` // 查询限制
}
func (employeesRankingListStatisticsCommand *EmployeesRankingListStatisticsCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(employeesRankingListStatisticsCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
... ... @@ -361,6 +361,46 @@ func (statisticsService *StatisticsService) EmployeesContributionsStatistics(emp
}
}
// 员工排行榜统计
func (statisticsService *StatisticsService) EmployeesRankingListStatistics(employeesRankingListStatisticsCommand *command.EmployeesRankingListStatisticsCommand) (interface{}, error) {
if err := employeesRankingListStatisticsCommand.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 employeeDao *dao.EmployeeDao
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else {
employeeDao = value
}
if employeesRankingListStatisticsCommand.StartTime.IsZero() && employeesRankingListStatisticsCommand.EndTime.IsZero() {
employeesRankingListStatisticsCommand.StartTime = time.Date(1971, time.Month(1), 1, 0, 0, 0, 0, time.Now().Location())
employeesRankingListStatisticsCommand.EndTime = time.Now().Local()
}
if employeesRankingListStatistics, err := employeeDao.CalculateEmployeesRankingList(employeesRankingListStatisticsCommand.CompanyId, employeesRankingListStatisticsCommand.StartTime, employeesRankingListStatisticsCommand.EndTime, employeesRankingListStatisticsCommand.Offset, employeesRankingListStatisticsCommand.Limit); 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 employeesRankingListStatistics, nil
}
}
func NewStatisticsService(options map[string]interface{}) *StatisticsService {
newStatisticsService := &StatisticsService{}
return newStatisticsService
... ...
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
}
... ...
... ... @@ -7,6 +7,7 @@ import (
// 获取兑换活动兑换清单
type ListExchangeCashPersonQuery struct {
CompanyId int64 `json:"companyId"` // 公司id
ExchangeCashActivityId int64 `json:"exchangeCashActivityId"` // 兑换现金活动id
ExchangeCashPersonNameMatch string `json:"exchangeCashPersonNameMatch,omitempty"` // 兑换活动名称匹配
Offset int `json:"offset,omitempty"` // 查询偏离量
... ...
... ... @@ -57,7 +57,6 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司id")
}
// 获取平台素币兑换情况
var employeeDao *dao.EmployeeDao
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -66,7 +65,7 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co
} else {
employeeDao = value
}
// 获取平台素币兑换情况
systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(createCashPoolCommand.CompanyId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -93,7 +92,6 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// 新建现金池
newCashPool := &domain.CashPool{
CompanyId: createCashPoolCommand.CompanyId,
Cash: createCashPoolCommand.Cash,
... ... @@ -998,6 +996,11 @@ func (cashPoolService *CashPoolService) GetExchangeCashPerson(getExchangeCashPer
}
}
// 返回素币兑换现金活动总清单
//func (cashPoolService *CashPoolService) ListSystemExchangeCashPerson(listSystemExchangeCashPersonQuery *query.ListSystemExchangeCashPersonQuery) (interface{}, error) {
//
//}
// 返回兑换素币清单列表
func (cashPoolService *CashPoolService) ListExchangeCashPerson(listExchangeCashPersonQuery *query.ListExchangeCashPersonQuery) (interface{}, error) {
if err := listExchangeCashPersonQuery.ValidateQuery(); err != nil {
... ... @@ -1023,7 +1026,46 @@ func (cashPoolService *CashPoolService) ListExchangeCashPerson(listExchangeCashP
exchangeCashPersonListRepository = value
}
// TODO 增加总榜单查询
// 返回兑换现金活动总榜
if listExchangeCashPersonQuery.ExchangeCashActivityId == 0 && listExchangeCashPersonQuery.CompanyId != 0 {
// 找到该公司下的所有活动id
var exchangeActivityRepository domain.ExchangeActivityRepository
if value, err := factory.CreateExchangeCashActivityRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
exchangeActivityRepository = value
}
if _, activities, err := exchangeActivityRepository.FindAll(map[string]interface{}{
"companyId": listExchangeCashPersonQuery.CompanyId,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
var activityIds []int64
for _, activity := range activities {
activityIds = append(activityIds, activity.ActivityId)
}
if count, people, err := exchangeCashPersonListRepository.FindAll(map[string]interface{}{
"exchangeCashActivityIds": activityIds,
"offset": listExchangeCashPersonQuery.Offset,
"limit": listExchangeCashPersonQuery.Limit,
}); 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
}
}
}
if count, people, err := exchangeCashPersonListRepository.Find(tool_funs.SimpleStructToMap(listExchangeCashPersonQuery)); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
... ... @@ -1462,6 +1504,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
... ...
... ... @@ -14,6 +14,8 @@ type ExchangeCashPersonListRepository interface {
Remove(exchangeCashPersonList *ExchangeCashPersonList) (*ExchangeCashPersonList, error)
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{} {
... ...
... ... @@ -156,7 +156,7 @@ func (dao *EmployeeDao) CalculateSystemCash(companyId int64) (map[string] interf
// 系统未兑换现金
cashPool := new(models.CashPool)
if err := tx.Model(cashPool).
ColumnExpr("exchanged_cash").
Column("exchanged_cash").
Where("cash_pool.company_id = ?", companyId).
Order("id DESC").
Limit(1).
... ... @@ -164,7 +164,7 @@ func (dao *EmployeeDao) CalculateSystemCash(companyId int64) (map[string] interf
return nil, err
}
if err := tx.Model(cashPool).
ColumnExpr("un_exchange_cash").
Column("un_exchange_cash").
Where("cash_pool.company_id = ?", companyId).
Order("id DESC").
Limit(1).
... ... @@ -283,6 +283,95 @@ func (dao *EmployeeDao) CalculateEmployeesSuMoney(companyId int, startTime time.
// }, nil
//}
// TODO 排行榜统计,增加分页
func (dao *EmployeeDao) CalculateEmployeesRankingList(companyId int, startTime time.Time, endTime time.Time, offset int, limit int) (map[string]interface{}, error) {
var retWealth []struct {
Uid int
EmployeeName string
EmployeeSuMoney float64
}
var retContributions []struct { // 员工贡献值
Uid int
EmployeeName string
EmployeesContributions float64
}
var retContributionDecrease []struct { // 员工减少的贡献值
Uid int
EmployeeName string
EmployeesContributions float64
}
tx := dao.transactionContext.PgTx
suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
if limit < -1 {
limit = 20
}
if offset < -1 {
offset = 0
}
// 员工财富值
if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint").
ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid").
ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name").
ColumnExpr("sum(su_money_transaction_record.su_money) AS employee_su_money").
Where(`e.company_id = ?`, companyId).
Where(`e.status = ?`, 1).
Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{2, 3})). // 增加,任务奖励的
Where(`su_money_transaction_record.create_time > ?`, startTime).
Where(`su_money_transaction_record.create_time < ?`, endTime).
Group("su_money_transaction_record.employee").
Order("employee_su_money DESC").
Limit(limit).
Offset(offset).
Select(&retWealth); err != nil {
return nil, err
}
// 增加的贡献值
if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint").
ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid").
ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name").
ColumnExpr("sum(su_money_transaction_record.su_money) AS employees_contributions").
Where(`e.company_id = ?`, companyId).
Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{2, 3})).
Where(`e.status = ?`, 1).
Where(`su_money_transaction_record.create_time > ?`, startTime).
Where(`su_money_transaction_record.create_time < ?`, endTime).
Group("su_money_transaction_record.employee").
Order("employees_contributions DESC").
Limit(limit).
Offset(offset).
Select(&retContributions); err != nil {
return nil, err
}
// 减少的贡献值
if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint").
ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid").
ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name").
ColumnExpr("sum(su_money_transaction_record.su_money) AS employees_contributions").
Where(`e.company_id = ?`, companyId).
Where(`su_money_transaction_record.record_type = ?`, 4).
Where(`e.status = ?`, 1).
Where(`su_money_transaction_record.create_time > ?`, startTime).
Where(`su_money_transaction_record.create_time < ?`, endTime).
Group("su_money_transaction_record.employee").
Order("employees_contributions DESC").
Limit(limit).
Offset(offset).
Select(&retContributionDecrease); err != nil {
return nil, err
}
for i := 0; i < len(retContributions); i++ {
for j := 0; j < len(retContributionDecrease); j++ {
if retContributions[i].Uid == retContributionDecrease[j].Uid {
retContributions[i].EmployeesContributions -= retContributionDecrease[j].EmployeesContributions
}
}
}
return map[string]interface{}{
"employeesContributions": retContributions,
"employeesWealth": retWealth,
}, nil
}
// 员工贡献值统计
func (dao *EmployeeDao) CalculateEmployeesContributions(companyId int, startTime time.Time, endTime time.Time) (map[string]interface{}, error) {
var ret []struct { // 员工贡献值
... ...
... ... @@ -54,6 +54,68 @@ 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
var exchangeCashListModels []*models.ExchangeCashPersonList
exchangeCashPeople := make([]*domain.ExchangeCashPersonList, 0)
query := tx.Model(&exchangeCashListModels)
if exchangeCashActivityIds, ok := queryOptions["exchangeCashActivityIds"]; ok && len(exchangeCashActivityIds.([]int64)) != 0 {
query = query.Where("exchange_cash_person_list.activity_id IN (?)", pg.In(exchangeCashActivityIds.([]int64)) )
}
if offset, ok := queryOptions["offset"]; ok {
offset := offset.(int)
if offset > -1 {
query = query.Offset(offset)
}
} else {
query = query.Offset(0)
}
if limit, ok := queryOptions["limit"]; ok {
limit := limit.(int)
if limit > -1 {
query = query.Limit(limit)
}
} else {
query = query.Limit(20)
}
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) Find(queryOptions map[string]interface{}) (int64, []*domain.ExchangeCashPersonList, error) {
tx := repository.transactionContext.PgTx
var exchangeCashListModels []*models.ExchangeCashPersonList
... ...
... ... @@ -126,7 +126,6 @@ func (controller *StatisticsController) EmployeesContributionsStatistics() {
statisticsService := service.NewStatisticsService(nil)
employeesContributionsStatisticsCommand := &command.EmployeesContributionsStatisticsCommand{}
json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), employeesContributionsStatisticsCommand)
fmt.Print(employeesContributionsStatisticsCommand, "\n")
data, err := statisticsService.EmployeesContributionsStatistics(employeesContributionsStatisticsCommand)
var response utils.JsonResponse
if err != nil {
... ... @@ -136,4 +135,20 @@ func (controller *StatisticsController) EmployeesContributionsStatistics() {
}
controller.Data["json"] = response
controller.ServeJSON()
}
// TODO 合并员工财富值、贡献值排行榜
func (controller *StatisticsController) RankingListStatistics() {
statisticsService := service.NewStatisticsService(nil)
employeesRankingListStatisticsCommand := &command.EmployeesRankingListStatisticsCommand{}
json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), employeesRankingListStatisticsCommand)
data, err := statisticsService.EmployeesRankingListStatistics(employeesRankingListStatisticsCommand)
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()
}
\ No newline at end of file
... ...
... ... @@ -231,6 +231,8 @@ func (controller *SuMoneyController) ListExchangeList () {
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")
... ... @@ -396,9 +398,73 @@ func (controller *SuMoneyController) ListDeadline() {
controller.ServeJSON()
}
// TODO 导出素币兑换清单,选择导出(ids),增加导出失败信息
// 导出素币兑换清单,选择导出(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),增加导出失败信息
... ...
... ... @@ -14,4 +14,5 @@ func init() {
beego.Router("/statistics/system-cash", &controllers.StatisticsController{}, "Post:SystemCashStatistics") // 系统现金统计
beego.Router("/statistics/employees-su-money", &controllers.StatisticsController{}, "Post:EmployeesSuMoneyStatistics") // 员工财富值统计
beego.Router("/statistics/employees-contributions", &controllers.StatisticsController{}, "Post:EmployeesContributionsStatistics") // 员工贡献值统计
beego.Router("/statistics/employees-ranking-list", &controllers.StatisticsController{}, "Post:RankingListStatistics") // 员工财富值、贡献值排行榜
}
... ...
#mmm-worth 1
\ No newline at end of file
... ...
不能预览此文件类型
不能预览此文件类型