228 lines
7.5 KiB
Go
228 lines
7.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.RegisterBiocontrolResource(NewIBiocontrolResource())
|
|
}
|
|
|
|
func NewIBiocontrolResource() service.IBiocontrolResource {
|
|
return &sBiocontrolResource{}
|
|
}
|
|
|
|
type sBiocontrolResource struct{}
|
|
|
|
func (s *sBiocontrolResource) List(ctx context.Context, req *model.BiocontrolResourceSearchReq) (listRes *model.BiocontrolResourceSearchRes, err error) {
|
|
listRes = new(model.BiocontrolResourceSearchRes)
|
|
err = g.Try(ctx, func(ctx context.Context) {
|
|
m := dao.BiocontrolResource.Ctx(ctx).WithAll()
|
|
if req.Id != "" {
|
|
m = m.Where(dao.BiocontrolResource.Columns().Id+" = ?", req.Id)
|
|
}
|
|
if req.SpeciesCode != "" {
|
|
m = m.Where(dao.BiocontrolResource.Columns().SpeciesCode+" like ?", "%"+req.SpeciesCode+"%")
|
|
}
|
|
if req.ZhName != "" {
|
|
m = m.Where(dao.BiocontrolResource.Columns().ZhName+" like ?", "%"+req.ZhName+"%")
|
|
}
|
|
if req.ZhSynonym != "" {
|
|
m = m.Where(dao.BiocontrolResource.Columns().ZhSynonym+" like ?", "%"+req.ZhSynonym+"%")
|
|
}
|
|
if req.EnName != "" {
|
|
m = m.Where(dao.BiocontrolResource.Columns().EnName+" like ?", "%"+req.EnName+"%")
|
|
}
|
|
if req.LatinName != "" {
|
|
m = m.Where(dao.BiocontrolResource.Columns().LatinName+" like ?", "%"+req.LatinName+"%")
|
|
}
|
|
if req.LatinSynonym != "" {
|
|
m = m.Where(dao.BiocontrolResource.Columns().LatinSynonym+" like ?", "%"+req.LatinSynonym+"%")
|
|
}
|
|
if req.BiocontrolType != "" {
|
|
m = m.Where(dao.BiocontrolResource.Columns().BiocontrolType+" = ?", gconv.Int(req.BiocontrolType))
|
|
}
|
|
if req.SourcesData != "" {
|
|
m = m.Where(dao.BiocontrolResource.Columns().SourcesData+" like ?", "%"+req.SourcesData+"%")
|
|
}
|
|
if req.CreateUser != "" {
|
|
m = m.Where(dao.BiocontrolResource.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.BiocontrolResource.Columns().CreateDate+" >= ? AND "+
|
|
dao.BiocontrolResource.Columns().CreateDate+" <= ?",
|
|
start,
|
|
end,
|
|
)
|
|
}
|
|
}
|
|
if req.AuditStatus != "" {
|
|
m = m.Where(dao.BiocontrolResource.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.BiocontrolResourceListRes
|
|
err = m.Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
|
|
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
|
listRes.List = make([]*model.BiocontrolResourceListRes, len(res))
|
|
for k, v := range res {
|
|
listRes.List[k] = &model.BiocontrolResourceListRes{
|
|
Id: v.Id,
|
|
SpeciesCode: v.SpeciesCode,
|
|
ZhName: v.ZhName,
|
|
ZhSynonym: v.ZhSynonym,
|
|
EnName: v.EnName,
|
|
LatinName: v.LatinName,
|
|
LatinSynonym: v.LatinSynonym,
|
|
BiocontrolType: v.BiocontrolType,
|
|
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 *sBiocontrolResource) GetById(ctx context.Context, id int) (res *model.BiocontrolResourceInfoRes, err error) {
|
|
err = g.Try(ctx, func(ctx context.Context) {
|
|
err = dao.BiocontrolResource.Ctx(ctx).WithAll().Where(dao.BiocontrolResource.Columns().Id, id).Scan(&res)
|
|
liberr.ErrIsNil(ctx, err, "获取信息失败")
|
|
})
|
|
return
|
|
}
|
|
|
|
func (s *sBiocontrolResource) Add(ctx context.Context, req *model.BiocontrolResourceAddReq) (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.BiocontrolResource.Ctx(ctx).Where(dao.BiocontrolResource.Columns().SpeciesCode+" = ?", req.SpeciesCode).Count()
|
|
if err != nil {
|
|
liberr.ErrIsNil(ctx, err, "获取物种编码失败")
|
|
return
|
|
|
|
}
|
|
if count > 0 {
|
|
liberr.ErrIsNil(ctx, errors.New("请勿重复添加"), "请勿重复添加")
|
|
return
|
|
}
|
|
_, err = dao.BiocontrolResource.Ctx(ctx).Insert(do.BiocontrolResource{
|
|
Id: req.Id,
|
|
SpeciesCode: req.SpeciesCode,
|
|
ZhName: req.ZhName,
|
|
ZhSynonym: req.ZhSynonym,
|
|
EnName: req.EnName,
|
|
LatinName: req.LatinName,
|
|
LatinSynonym: req.LatinSynonym,
|
|
BiocontrolType: req.BiocontrolType,
|
|
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 *sBiocontrolResource) Edit(ctx context.Context, req *model.BiocontrolResourceEditReq) (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.BiocontrolResource.Ctx(ctx).
|
|
Where(dao.BiocontrolResource.Columns().SpeciesCode+" = ?", req.SpeciesCode).
|
|
WhereNot(dao.BiocontrolResource.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.BiocontrolResource.Ctx(ctx).WherePri(req.Id).Update(do.BiocontrolResource{
|
|
SpeciesCode: req.SpeciesCode,
|
|
ZhName: req.ZhName,
|
|
ZhSynonym: req.ZhSynonym,
|
|
EnName: req.EnName,
|
|
LatinName: req.LatinName,
|
|
LatinSynonym: req.LatinSynonym,
|
|
BiocontrolType: req.BiocontrolType,
|
|
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 *sBiocontrolResource) Delete(ctx context.Context, ids []int) (err error) {
|
|
err = g.Try(ctx, func(ctx context.Context) {
|
|
_, err = dao.BiocontrolResource.Ctx(ctx).Delete(dao.BiocontrolResource.Columns().Id+" in (?)", ids)
|
|
liberr.ErrIsNil(ctx, err, "删除失败")
|
|
})
|
|
return
|
|
}
|