213 lines
6.5 KiB
Go
213 lines
6.5 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
"github.com/tiger1103/gfast/v3/internal/app/businesses/dao"
|
|
"github.com/tiger1103/gfast/v3/internal/app/businesses/model"
|
|
"github.com/tiger1103/gfast/v3/internal/app/businesses/model/do"
|
|
"github.com/tiger1103/gfast/v3/internal/app/businesses/service"
|
|
"github.com/tiger1103/gfast/v3/internal/app/system/consts"
|
|
"github.com/tiger1103/gfast/v3/library/liberr"
|
|
)
|
|
|
|
func init() {
|
|
service.RegisterGeography(NewIGeography())
|
|
}
|
|
|
|
func NewIGeography() service.IGeography {
|
|
return &sGeography{}
|
|
}
|
|
|
|
type sGeography struct{}
|
|
|
|
func (s *sGeography) List(ctx context.Context, req *model.GeographySearchReq) (listRes *model.GeographySearchRes, err error) {
|
|
listRes = new(model.GeographySearchRes)
|
|
err = g.Try(ctx, func(ctx context.Context) {
|
|
m := dao.Geography.Ctx(ctx).WithAll()
|
|
if req.Id != "" {
|
|
m = m.Where(dao.Geography.Columns().Id+" = ?", req.Id)
|
|
}
|
|
if req.SpeciesCode != "" {
|
|
m = m.Where(dao.Geography.Columns().SpeciesCode+" like ?", "%"+req.SpeciesCode+"%")
|
|
}
|
|
if req.CreateUser != "" {
|
|
m = m.Where(dao.Geography.Columns().CreateUser+" = ?", gconv.Int(req.CreateUser))
|
|
}
|
|
if req.CreateDate != nil {
|
|
start := req.CreateDate[0]
|
|
end := req.CreateDate[1]
|
|
if start != nil && end != nil {
|
|
m = m.Where(
|
|
dao.Geography.Columns().CreateDate+" >= ? AND "+
|
|
dao.Geography.Columns().CreateDate+" <= ?",
|
|
start,
|
|
end,
|
|
)
|
|
}
|
|
}
|
|
if req.AuditUser != "" {
|
|
m = m.Where(dao.Geography.Columns().AuditUser+" = ?", gconv.Int(req.AuditUser))
|
|
}
|
|
if req.AuditDate != nil {
|
|
start := req.AuditDate[0]
|
|
end := req.AuditDate[1]
|
|
if start != nil && end != nil {
|
|
m = m.Where(
|
|
dao.Geography.Columns().AuditDate+" >= ? AND "+
|
|
dao.Geography.Columns().AuditDate+" <= ?",
|
|
start,
|
|
end,
|
|
)
|
|
}
|
|
}
|
|
if req.AuditStatus != "" {
|
|
m = m.Where(dao.Geography.Columns().AuditStatus+" = ?", gconv.Int(req.AuditStatus))
|
|
}
|
|
listRes.Total, err = m.Count()
|
|
liberr.ErrIsNil(ctx, err, "获取总行数失败")
|
|
if req.PageNum == 0 {
|
|
req.PageNum = 1
|
|
}
|
|
listRes.CurrentPage = req.PageNum
|
|
if req.PageSize == 0 {
|
|
req.PageSize = consts.PageSize
|
|
}
|
|
order := "id asc"
|
|
if req.OrderBy != "" {
|
|
order = req.OrderBy
|
|
}
|
|
var res []*model.GeographyListRes
|
|
err = m.Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
|
|
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
|
listRes.List = make([]*model.GeographyListRes, len(res))
|
|
for k, v := range res {
|
|
listRes.List[k] = &model.GeographyListRes{
|
|
Id: v.Id,
|
|
SpeciesCode: v.SpeciesCode,
|
|
DistributionInfo: v.DistributionInfo,
|
|
OriginInfo: v.OriginInfo,
|
|
AbroadInfo: v.AbroadInfo,
|
|
DomesticInfo: v.DomesticInfo,
|
|
CreateUser: v.CreateUser,
|
|
CreateDate: v.CreateDate,
|
|
AuditUser: v.AuditUser,
|
|
AuditDate: v.AuditDate,
|
|
AuditStatus: v.AuditStatus,
|
|
AuditView: v.AuditView,
|
|
Remark: v.Remark,
|
|
Version: v.Version,
|
|
CreatedAt: v.CreatedAt,
|
|
}
|
|
}
|
|
})
|
|
return
|
|
}
|
|
|
|
func (s *sGeography) GetById(ctx context.Context, id int) (res *model.GeographyInfoRes, err error) {
|
|
err = g.Try(ctx, func(ctx context.Context) {
|
|
err = dao.Geography.Ctx(ctx).WithAll().Where(dao.Geography.Columns().Id, id).Scan(&res)
|
|
liberr.ErrIsNil(ctx, err, "获取信息失败")
|
|
})
|
|
return
|
|
}
|
|
|
|
func (s *sGeography) Add(ctx context.Context, req *model.GeographyAddReq) (err error) {
|
|
err = g.Try(ctx, func(ctx context.Context) {
|
|
//查询是否存在
|
|
speciesNameCount, err := dao.SpeciesName.Ctx(ctx).Where(dao.SpeciesName.Columns().SpeciesCode+" = ?", req.SpeciesCode).Count()
|
|
if err != nil {
|
|
liberr.ErrIsNil(ctx, err, "获取物种编码失败")
|
|
return
|
|
}
|
|
if speciesNameCount == 0 {
|
|
liberr.ErrIsNil(ctx, errors.New("物种编码不存在"), "物种编码不存在")
|
|
return
|
|
}
|
|
//查重
|
|
count, err := dao.Geography.Ctx(ctx).Where(dao.Geography.Columns().SpeciesCode+" = ?", req.SpeciesCode).Count()
|
|
if err != nil {
|
|
liberr.ErrIsNil(ctx, err, "获取物种编码失败")
|
|
return
|
|
|
|
}
|
|
if count > 0 {
|
|
liberr.ErrIsNil(ctx, errors.New("请勿重复添加"), "请勿重复添加")
|
|
return
|
|
}
|
|
_, err = dao.Geography.Ctx(ctx).Insert(do.Geography{
|
|
Id: req.Id,
|
|
SpeciesCode: req.SpeciesCode,
|
|
DistributionInfo: req.DistributionInfo,
|
|
OriginInfo: req.OriginInfo,
|
|
AbroadInfo: req.AbroadInfo,
|
|
DomesticInfo: req.DomesticInfo,
|
|
CreateUser: req.CreateUser,
|
|
CreateDate: req.CreateDate,
|
|
//AuditUser: req.AuditUser,
|
|
//AuditDate: req.AuditDate,
|
|
AuditStatus: req.AuditStatus,
|
|
//AuditView: req.AuditView,
|
|
Remark: req.Remark,
|
|
})
|
|
liberr.ErrIsNil(ctx, err, "添加失败")
|
|
})
|
|
return
|
|
}
|
|
|
|
func (s *sGeography) Edit(ctx context.Context, req *model.GeographyEditReq) (err error) {
|
|
err = g.Try(ctx, func(ctx context.Context) {
|
|
// 查询物种编码是否存在
|
|
speciesNameCount, err := dao.SpeciesName.Ctx(ctx).Where(dao.SpeciesName.Columns().SpeciesCode+" = ?", req.SpeciesCode).Count()
|
|
if err != nil {
|
|
liberr.ErrIsNil(ctx, err, "物种编码不存在")
|
|
return
|
|
}
|
|
if speciesNameCount == 0 {
|
|
liberr.ErrIsNil(ctx, errors.New("物种编码不存在"), "物种编码不存在")
|
|
return
|
|
}
|
|
count, err := dao.Geography.Ctx(ctx).
|
|
Where(dao.Geography.Columns().SpeciesCode+" = ?", req.SpeciesCode).
|
|
WhereNot(dao.Geography.Columns().Id, req.Id). // 排除当前记录
|
|
Count()
|
|
if err != nil {
|
|
liberr.ErrIsNil(ctx, err, "获取物种编码失败")
|
|
return
|
|
}
|
|
if count > 0 {
|
|
// 使用非nil错误触发错误处理
|
|
liberr.ErrIsNil(ctx, errors.New("物种编码已存在"), "请勿重复添加")
|
|
return
|
|
}
|
|
_, err = dao.Geography.Ctx(ctx).Where(dao.Geography.Columns().Id+" = ?", req.Id).Update(do.Geography{
|
|
SpeciesCode: req.SpeciesCode,
|
|
DistributionInfo: req.DistributionInfo,
|
|
OriginInfo: req.OriginInfo,
|
|
AbroadInfo: req.AbroadInfo,
|
|
DomesticInfo: req.DomesticInfo,
|
|
CreateUser: req.CreateUser,
|
|
CreateDate: req.CreateDate,
|
|
//AuditUser: req.AuditUser,
|
|
//AuditDate: req.AuditDate,
|
|
AuditStatus: req.AuditStatus,
|
|
//AuditView: req.AuditView,
|
|
Remark: req.Remark,
|
|
Version: req.Version,
|
|
})
|
|
liberr.ErrIsNil(ctx, err, "修改失败")
|
|
})
|
|
return
|
|
}
|
|
|
|
func (s *sGeography) Delete(ctx context.Context, ids []int) (err error) {
|
|
err = g.Try(ctx, func(ctx context.Context) {
|
|
_, err = dao.Geography.Ctx(ctx).Delete(dao.Geography.Columns().Id+" in (?)", ids)
|
|
liberr.ErrIsNil(ctx, err, "删除失败")
|
|
})
|
|
return
|
|
}
|