forked from free-lancers/electricity_bill_calc_service
refactor(deps):将一些工具函数用工具库替换。
This commit is contained in:
@@ -5,9 +5,10 @@ import (
|
||||
"electricity_bill_calc/exceptions"
|
||||
"electricity_bill_calc/global"
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/utils"
|
||||
"time"
|
||||
|
||||
"github.com/fufuok/utils"
|
||||
"github.com/samber/lo"
|
||||
"xorm.io/builder"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
@@ -176,7 +177,7 @@ func (_ChargeService) ListPagedChargeRecord(keyword, beginDate, endDate string,
|
||||
}
|
||||
if len(beginDate) != 0 {
|
||||
beginTime, err := time.ParseInLocation("2006-01-02", beginDate, time.Local)
|
||||
beginTime = utils.VeryBeginOfDate(beginTime)
|
||||
beginTime = utils.BeginOfDay(beginTime)
|
||||
if err != nil {
|
||||
return make([]model.ChargeWithName, 0), 0, err
|
||||
}
|
||||
@@ -184,7 +185,7 @@ func (_ChargeService) ListPagedChargeRecord(keyword, beginDate, endDate string,
|
||||
}
|
||||
if len(endDate) != 0 {
|
||||
endTime, err := time.ParseInLocation("2006-01-02", endDate, time.Local)
|
||||
endTime = utils.VeryEndOfDate(endTime)
|
||||
endTime = utils.EndOfDay(endTime)
|
||||
if err != nil {
|
||||
return make([]model.ChargeWithName, 0), 0, err
|
||||
}
|
||||
@@ -222,16 +223,16 @@ func (_ChargeService) lastValidChargeTo(uid string) (time.Time, error) {
|
||||
if err != nil {
|
||||
return veryBlankTime, nil
|
||||
}
|
||||
mappedRecords := utils.Map(records, func(elem string) time.Time {
|
||||
mappedRecords := lo.Map(records, func(elem string, index int) time.Time {
|
||||
t, _ := time.Parse(time.RFC3339, elem)
|
||||
return utils.VeryBeginOfDate(t)
|
||||
return utils.BeginOfDay(t)
|
||||
})
|
||||
lastValid := utils.Reduce(mappedRecords, veryBlankTime, func(acc, elem time.Time) time.Time {
|
||||
lastValid := lo.Reduce(mappedRecords, func(acc, elem time.Time, index int) time.Time {
|
||||
if elem.After(acc) {
|
||||
return elem
|
||||
} else {
|
||||
return acc
|
||||
}
|
||||
})
|
||||
}, veryBlankTime)
|
||||
return lastValid, nil
|
||||
}
|
||||
|
@@ -5,10 +5,10 @@ import (
|
||||
"electricity_bill_calc/excel"
|
||||
"electricity_bill_calc/global"
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/utils"
|
||||
"fmt"
|
||||
|
||||
mapset "github.com/deckarep/golang-set/v2"
|
||||
"github.com/samber/lo"
|
||||
"xorm.io/builder"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
@@ -123,10 +123,10 @@ func (_Meter04kVService) DuplicateMeterCodeValidate(meters []model.Meter04KV) []
|
||||
}
|
||||
|
||||
func (m _Meter04kVService) BatchCreateMeter(meters []model.Meter04KV) error {
|
||||
parkIds := utils.Reduce(meters, mapset.NewSet[string](), func(acc mapset.Set[string], elem model.Meter04KV) mapset.Set[string] {
|
||||
parkIds := lo.Reduce(meters, func(acc mapset.Set[string], elem model.Meter04KV, index int) mapset.Set[string] {
|
||||
acc.Add(elem.ParkId)
|
||||
return acc
|
||||
})
|
||||
}, mapset.NewSet[string]())
|
||||
if parkIds.Cardinality() > 1 {
|
||||
return fmt.Errorf("一次只能向同一个园区中添加0.4kV表计。")
|
||||
}
|
||||
|
@@ -3,10 +3,11 @@ package service
|
||||
import (
|
||||
"electricity_bill_calc/global"
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/utils"
|
||||
"electricity_bill_calc/tools"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/samber/lo"
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
||||
@@ -24,10 +25,9 @@ func (_ReportService) FetchParksWithNewestReport(uid string) ([]model.ParkNewest
|
||||
if err != nil {
|
||||
return make([]model.ParkNewestReport, 0), err
|
||||
}
|
||||
reducedParks := utils.Reduce(
|
||||
reducedParks := lo.Reduce(
|
||||
parks,
|
||||
make(map[string]model.ParkNewestReport, 0),
|
||||
func(acc map[string]model.ParkNewestReport, elem model.ParkNewestReport) map[string]model.ParkNewestReport {
|
||||
func(acc map[string]model.ParkNewestReport, elem model.ParkNewestReport, index int) map[string]model.ParkNewestReport {
|
||||
if v, ok := acc[elem.Park.Id]; ok {
|
||||
if elem.Report != nil {
|
||||
if v.Report == nil || (elem.Report.Period.After(v.Report.Period)) {
|
||||
@@ -39,8 +39,9 @@ func (_ReportService) FetchParksWithNewestReport(uid string) ([]model.ParkNewest
|
||||
}
|
||||
return acc
|
||||
},
|
||||
make(map[string]model.ParkNewestReport, 0),
|
||||
)
|
||||
return utils.Values(reducedParks), nil
|
||||
return lo.Values(reducedParks), nil
|
||||
}
|
||||
|
||||
func (_ReportService) IsNewPeriodValid(uid string, period time.Time) (bool, error) {
|
||||
@@ -54,25 +55,24 @@ func (_ReportService) IsNewPeriodValid(uid string, period time.Time) (bool, erro
|
||||
return false, nil
|
||||
}
|
||||
// 检查给定的期数在目前的记录中是否已经存在
|
||||
exists := utils.Reduce(
|
||||
exists := lo.Reduce(
|
||||
reports,
|
||||
false,
|
||||
func(acc bool, elem model.Report) bool {
|
||||
func(acc bool, elem model.Report, index int) bool {
|
||||
if elem.Period.Equal(period) {
|
||||
return acc || true
|
||||
} else {
|
||||
return acc || false
|
||||
}
|
||||
},
|
||||
false,
|
||||
)
|
||||
if exists {
|
||||
return false, nil
|
||||
}
|
||||
// 检查给定的期数与目前已发布的最大期数的关系
|
||||
maxPublished := utils.Reduce(
|
||||
maxPublished := lo.Reduce(
|
||||
reports,
|
||||
nil,
|
||||
func(acc *time.Time, elem model.Report) *time.Time {
|
||||
func(acc *time.Time, elem model.Report, index int) *time.Time {
|
||||
if elem.Published {
|
||||
if acc == nil || (acc != nil && elem.Period.After(*acc)) {
|
||||
return &elem.Period
|
||||
@@ -80,24 +80,25 @@ func (_ReportService) IsNewPeriodValid(uid string, period time.Time) (bool, erro
|
||||
}
|
||||
return acc
|
||||
},
|
||||
nil,
|
||||
)
|
||||
// 检查给定的期数与目前未发布的最大期数的关系
|
||||
maxUnpublished := utils.Reduce(
|
||||
maxUnpublished := lo.Reduce(
|
||||
reports,
|
||||
nil,
|
||||
func(acc *time.Time, elem model.Report) *time.Time {
|
||||
func(acc *time.Time, elem model.Report, index int) *time.Time {
|
||||
if acc == nil || (acc != nil && elem.Period.After(*acc)) {
|
||||
return &elem.Period
|
||||
}
|
||||
return acc
|
||||
},
|
||||
nil,
|
||||
)
|
||||
if maxUnpublished == nil {
|
||||
return true, nil
|
||||
}
|
||||
if maxPublished != nil && maxUnpublished.Equal(*maxPublished) {
|
||||
// 此时不存在未发布的报表
|
||||
return utils.IsNextMonth(*maxPublished, period), nil
|
||||
return tools.IsNextMonth(*maxPublished, period), nil
|
||||
} else {
|
||||
// 存在未发布的报表
|
||||
return false, nil
|
||||
|
@@ -1,16 +1,16 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"crypto/sha512"
|
||||
"electricity_bill_calc/cache"
|
||||
"electricity_bill_calc/config"
|
||||
"electricity_bill_calc/exceptions"
|
||||
"electricity_bill_calc/global"
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/utils"
|
||||
"electricity_bill_calc/tools"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/fufuok/utils"
|
||||
"github.com/google/uuid"
|
||||
"xorm.io/builder"
|
||||
)
|
||||
@@ -34,9 +34,7 @@ func (u _UserService) ProcessEnterpriseUserLogin(username, password string) (*mo
|
||||
if !user.Enabled {
|
||||
return nil, exceptions.NewAuthenticationError(401, "用户已被禁用。")
|
||||
}
|
||||
hash := sha512.New512_256()
|
||||
hash.Write([]byte(password))
|
||||
hashedPassword := fmt.Sprintf("%x", hash.Sum(nil))
|
||||
hashedPassword := utils.Sha512Hex(password)
|
||||
if hashedPassword != user.Password {
|
||||
return nil, exceptions.NewAuthenticationError(401, "用户凭据不正确。")
|
||||
}
|
||||
@@ -78,9 +76,7 @@ func (u _UserService) ProcessManagementUserLogin(username, password string) (*mo
|
||||
if !user.Enabled {
|
||||
return nil, exceptions.NewAuthenticationError(401, "用户已被禁用。")
|
||||
}
|
||||
hash := sha512.New512_256()
|
||||
hash.Write([]byte(password))
|
||||
hashedPassword := fmt.Sprintf("%x", hash.Sum(nil))
|
||||
hashedPassword := utils.Sha512Hex(password)
|
||||
if hashedPassword != user.Password {
|
||||
return nil, exceptions.NewAuthenticationError(401, "用户凭据不正确。")
|
||||
}
|
||||
@@ -109,10 +105,8 @@ func (u _UserService) InvalidUserPassword(uid string) (string, error) {
|
||||
if user == nil && err != nil {
|
||||
return "", exceptions.NewNotFoundError("指定的用户不存在。")
|
||||
}
|
||||
verifyCode := utils.RandStr(10)
|
||||
hash := sha512.New512_256()
|
||||
hash.Write([]byte(verifyCode))
|
||||
user.Password = fmt.Sprintf("%x", hash.Sum(nil))
|
||||
verifyCode := tools.RandStr(10)
|
||||
user.Password = utils.Sha512Hex(verifyCode)
|
||||
user.ResetNeeded = true
|
||||
affected, err := global.DBConn.ID(uid).Cols("password", "reset_needed").Update(user)
|
||||
if err != nil {
|
||||
@@ -133,9 +127,7 @@ func (u _UserService) VerifyUserPassword(username, verifyCode string) (bool, err
|
||||
if user == nil || err != nil {
|
||||
return false, exceptions.NewNotFoundError("指定的用户不存在。")
|
||||
}
|
||||
hash := sha512.New512_256()
|
||||
hash.Write([]byte(verifyCode))
|
||||
hashedVerifyCode := fmt.Sprintf("%x", hash.Sum(nil))
|
||||
hashedVerifyCode := utils.Sha512Hex(verifyCode)
|
||||
if hashedVerifyCode != user.Password {
|
||||
return false, nil
|
||||
} else {
|
||||
@@ -148,9 +140,7 @@ func (u _UserService) ResetUserPassword(username, password string) (bool, error)
|
||||
if user == nil || err != nil {
|
||||
return false, exceptions.NewNotFoundError("指定的用户不存在。")
|
||||
}
|
||||
hash := sha512.New512_256()
|
||||
hash.Write([]byte(password))
|
||||
user.Password = fmt.Sprintf("%x", hash.Sum(nil))
|
||||
user.Password = utils.Sha512Hex(password)
|
||||
user.ResetNeeded = false
|
||||
affected, err := global.DBConn.ID(user.Id).Cols("password", "reset_needed").Update(user)
|
||||
if err != nil {
|
||||
@@ -186,14 +176,12 @@ func (u _UserService) CreateUser(user *model.User, detail *model.UserDetail) (st
|
||||
}
|
||||
detail.Id = user.Id
|
||||
|
||||
verifyCode := utils.RandStr(10)
|
||||
hash := sha512.New512_256()
|
||||
hash.Write([]byte(verifyCode))
|
||||
user.Password = fmt.Sprintf("%x", hash.Sum(nil))
|
||||
verifyCode := tools.RandStr(10)
|
||||
user.Password = utils.Sha512Hex(verifyCode)
|
||||
user.ResetNeeded = true
|
||||
|
||||
if detail.Name != nil {
|
||||
finalAbbr := utils.PinyinAbbr(*detail.Name)
|
||||
finalAbbr := tools.PinyinAbbr(*detail.Name)
|
||||
detail.Abbr = &finalAbbr
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user