2026-03-18 10:18:03 +08:00
|
|
|
|
// 单位换算DAO层
|
|
|
|
|
|
// 职责:换算规则CRUD、按单位类型和源目标单位查询
|
|
|
|
|
|
// 紧密耦合:service.UnitConversion、service.Capacity(容量计算换算)
|
|
|
|
|
|
// 注意:GetByUnits用于容量计算时获取换算系数
|
|
|
|
|
|
package dao
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"assets/consts/public"
|
|
|
|
|
|
"assets/consts/stock"
|
|
|
|
|
|
entity "assets/model/entity/stock"
|
|
|
|
|
|
"context"
|
|
|
|
|
|
|
2026-06-10 15:40:17 +08:00
|
|
|
|
"gitea.redpowerfuture.com/red-future/common/db/mongo"
|
2026-03-18 10:18:03 +08:00
|
|
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var UnitConversion = new(unitConversion)
|
|
|
|
|
|
|
|
|
|
|
|
type unitConversion struct{}
|
|
|
|
|
|
|
|
|
|
|
|
// GetByUnits 根据单位类型和源目标单位查询换算规则
|
|
|
|
|
|
func (d *unitConversion) GetByUnits(ctx context.Context, unitType stock.CapacityUnitType, fromUnit, toUnit string) (res *entity.UnitConversion, err error) {
|
|
|
|
|
|
filter := bson.M{
|
|
|
|
|
|
"unitType": unitType,
|
|
|
|
|
|
"fromUnit": fromUnit,
|
|
|
|
|
|
"toUnit": toUnit,
|
|
|
|
|
|
}
|
|
|
|
|
|
err = mongo.DB().FindOne(ctx, filter, &res, public.UnitConversionCollection)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Insert 插入换算规则
|
|
|
|
|
|
func (d *unitConversion) Insert(ctx context.Context, conversion *entity.UnitConversion) (ids []interface{}, err error) {
|
|
|
|
|
|
ids, err = mongo.DB().Insert(ctx, []interface{}{conversion}, public.UnitConversionCollection)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Update 更新换算规则
|
|
|
|
|
|
func (d *unitConversion) Update(ctx context.Context, id *bson.ObjectID, update bson.M) (err error) {
|
|
|
|
|
|
filter := bson.M{"_id": id}
|
|
|
|
|
|
updateDoc := bson.M{"$set": update}
|
|
|
|
|
|
_, err = mongo.DB().Update(ctx, filter, updateDoc, public.UnitConversionCollection)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// DeleteFake 软删除换算规则
|
|
|
|
|
|
func (d *unitConversion) DeleteFake(ctx context.Context, id *bson.ObjectID) (err error) {
|
|
|
|
|
|
filter := bson.M{"_id": id}
|
|
|
|
|
|
_, err = mongo.DB().DeleteSoft(ctx, filter, public.UnitConversionCollection)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// List 查询换算规则列表
|
|
|
|
|
|
func (d *unitConversion) List(ctx context.Context, unitType *stock.CapacityUnitType, fromUnit, toUnit string) (res []entity.UnitConversion, err error) {
|
|
|
|
|
|
filter := bson.M{}
|
|
|
|
|
|
|
|
|
|
|
|
if unitType != nil {
|
|
|
|
|
|
filter["unitType"] = *unitType
|
|
|
|
|
|
}
|
|
|
|
|
|
if fromUnit != "" {
|
|
|
|
|
|
filter["fromUnit"] = fromUnit
|
|
|
|
|
|
}
|
|
|
|
|
|
if toUnit != "" {
|
|
|
|
|
|
filter["toUnit"] = toUnit
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_, err = mongo.DB().Find(ctx, filter, &res, public.UnitConversionCollection, nil, nil)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|