272 lines
8.6 KiB
Go
272 lines
8.6 KiB
Go
// ==========================================================================
|
||
// GFast自动生成logic操作代码。
|
||
// 生成日期:2025-08-11 09:34:44
|
||
// 生成路径: internal/app/businesses/logic/host_botany.go
|
||
// 生成人:gfast
|
||
// desc:寄主生物
|
||
// company:云南奇讯科技有限公司
|
||
// ==========================================================================
|
||
|
||
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.RegisterHostBotany(NewIHostBotany())
|
||
}
|
||
|
||
func NewIHostBotany() service.IHostBotany {
|
||
return &sHostBotany{}
|
||
}
|
||
|
||
type sHostBotany struct{}
|
||
|
||
func (s *sHostBotany) List(ctx context.Context, req *model.HostBotanySearchReq) (listRes *model.HostBotanySearchRes, err error) {
|
||
listRes = new(model.HostBotanySearchRes)
|
||
err = g.Try(ctx, func(ctx context.Context) {
|
||
m := dao.HostBotany.Ctx(ctx).WithAll()
|
||
if req.Id != "" {
|
||
m = m.Where(dao.HostBotany.Columns().Id+" = ?", req.Id)
|
||
}
|
||
if req.SpeciesCode != "" {
|
||
m = m.Where(dao.HostBotany.Columns().SpeciesCode+" like ?", "%"+req.SpeciesCode+"%")
|
||
}
|
||
if req.ZhName != "" {
|
||
m = m.Where(dao.HostBotany.Columns().ZhName+" like ?", "%"+req.ZhName+"%")
|
||
}
|
||
if req.ZhSynonym != "" {
|
||
m = m.Where(dao.HostBotany.Columns().ZhSynonym+" like ?", "%"+req.ZhSynonym+"%")
|
||
}
|
||
if req.EnName != "" {
|
||
m = m.Where(dao.HostBotany.Columns().EnName+" like ?", "%"+req.EnName+"%")
|
||
}
|
||
if req.LatinName != "" {
|
||
m = m.Where(dao.HostBotany.Columns().LatinName+" like ?", "%"+req.LatinName+"%")
|
||
}
|
||
if req.LatinSynonym != "" {
|
||
m = m.Where(dao.HostBotany.Columns().LatinSynonym+" like ?", "%"+req.LatinSynonym+"%")
|
||
}
|
||
if req.Mu != "" {
|
||
m = m.Where(dao.HostBotany.Columns().Mu+" like ?", "%"+req.Mu+"%")
|
||
}
|
||
if req.OrderTitle != "" {
|
||
m = m.Where(dao.HostBotany.Columns().OrderTitle+" like ?", "%"+req.OrderTitle+"%")
|
||
}
|
||
if req.Ke != "" {
|
||
m = m.Where(dao.HostBotany.Columns().Ke+" like ?", "%"+req.Ke+"%")
|
||
}
|
||
if req.Family != "" {
|
||
m = m.Where(dao.HostBotany.Columns().Family+" like ?", "%"+req.Family+"%")
|
||
}
|
||
if req.Shu != "" {
|
||
m = m.Where(dao.HostBotany.Columns().Shu+" like ?", "%"+req.Shu+"%")
|
||
}
|
||
if req.Genus != "" {
|
||
m = m.Where(dao.HostBotany.Columns().Genus+" like ?", "%"+req.Genus+"%")
|
||
}
|
||
if req.Zhong != "" {
|
||
m = m.Where(dao.HostBotany.Columns().Zhong+" like ?", "%"+req.Zhong+"%")
|
||
}
|
||
if req.Species != "" {
|
||
m = m.Where(dao.HostBotany.Columns().Species+" like ?", "%"+req.Species+"%")
|
||
}
|
||
if req.HostType != "" {
|
||
m = m.Where(dao.HostBotany.Columns().HostType+" = ?", req.HostType)
|
||
}
|
||
if req.SourcesData != "" {
|
||
m = m.Where(dao.HostBotany.Columns().SourcesData+" like ?", "%"+req.SourcesData+"%")
|
||
}
|
||
if req.CreateUser != "" {
|
||
m = m.Where(dao.HostBotany.Columns().CreateUser+" = ?", gconv.Int(req.CreateUser))
|
||
}
|
||
if req.AuditStatus != "" {
|
||
m = m.Where(dao.HostBotany.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.HostBotanyListRes
|
||
err = m.Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
|
||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||
listRes.List = make([]*model.HostBotanyListRes, len(res))
|
||
for k, v := range res {
|
||
listRes.List[k] = &model.HostBotanyListRes{
|
||
Id: v.Id,
|
||
SpeciesCode: v.SpeciesCode,
|
||
ZhName: v.ZhName,
|
||
ZhSynonym: v.ZhSynonym,
|
||
EnName: v.EnName,
|
||
LatinName: v.LatinName,
|
||
LatinSynonym: v.LatinSynonym,
|
||
Mu: v.Mu,
|
||
OrderTitle: v.OrderTitle,
|
||
Ke: v.Ke,
|
||
Family: v.Family,
|
||
Shu: v.Shu,
|
||
Genus: v.Genus,
|
||
Zhong: v.Zhong,
|
||
Species: v.Species,
|
||
HostType: v.HostType,
|
||
SourcesData: v.SourcesData,
|
||
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 *sHostBotany) GetById(ctx context.Context, id int) (res *model.HostBotanyInfoRes, err error) {
|
||
err = g.Try(ctx, func(ctx context.Context) {
|
||
err = dao.HostBotany.Ctx(ctx).WithAll().Where(dao.HostBotany.Columns().Id, id).Scan(&res)
|
||
liberr.ErrIsNil(ctx, err, "获取信息失败")
|
||
})
|
||
return
|
||
}
|
||
|
||
func (s *sHostBotany) Add(ctx context.Context, req *model.HostBotanyAddReq) (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.HostBotany.Ctx(ctx).Where(dao.HostBotany.Columns().SpeciesCode+" = ?", req.SpeciesCode).Count()
|
||
if err != nil {
|
||
liberr.ErrIsNil(ctx, err, "获取物种编码失败")
|
||
return
|
||
|
||
}
|
||
if count > 0 {
|
||
liberr.ErrIsNil(ctx, errors.New("请勿重复添加"), "请勿重复添加")
|
||
return
|
||
}
|
||
_, err = dao.HostBotany.Ctx(ctx).Insert(do.HostBotany{
|
||
Id: req.Id,
|
||
SpeciesCode: req.SpeciesCode,
|
||
ZhName: req.ZhName,
|
||
ZhSynonym: req.ZhSynonym,
|
||
EnName: req.EnName,
|
||
LatinName: req.LatinName,
|
||
LatinSynonym: req.LatinSynonym,
|
||
Mu: req.Mu,
|
||
OrderTitle: req.OrderTitle,
|
||
Ke: req.Ke,
|
||
Family: req.Family,
|
||
Shu: req.Shu,
|
||
Genus: req.Genus,
|
||
Zhong: req.Zhong,
|
||
Species: req.Species,
|
||
HostType: req.HostType,
|
||
SourcesData: req.SourcesData,
|
||
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 *sHostBotany) Edit(ctx context.Context, req *model.HostBotanyEditReq) (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.HostBotany.Ctx(ctx).
|
||
Where(dao.HostBotany.Columns().SpeciesCode+" = ?", req.SpeciesCode).
|
||
WhereNot(dao.HostBotany.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.HostBotany.Ctx(ctx).WherePri(req.Id).Update(do.HostBotany{
|
||
SpeciesCode: req.SpeciesCode,
|
||
ZhName: req.ZhName,
|
||
ZhSynonym: req.ZhSynonym,
|
||
EnName: req.EnName,
|
||
LatinName: req.LatinName,
|
||
LatinSynonym: req.LatinSynonym,
|
||
Mu: req.Mu,
|
||
OrderTitle: req.OrderTitle,
|
||
Ke: req.Ke,
|
||
Family: req.Family,
|
||
Shu: req.Shu,
|
||
Genus: req.Genus,
|
||
Zhong: req.Zhong,
|
||
Species: req.Species,
|
||
HostType: req.HostType,
|
||
SourcesData: req.SourcesData,
|
||
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 *sHostBotany) Delete(ctx context.Context, ids []int) (err error) {
|
||
err = g.Try(ctx, func(ctx context.Context) {
|
||
_, err = dao.HostBotany.Ctx(ctx).Delete(dao.HostBotany.Columns().Id+" in (?)", ids)
|
||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||
})
|
||
return
|
||
}
|