// ========================================================================== // GFast自动生成logic操作代码。 // 生成日期:2025-08-11 10:15:42 // 生成路径: internal/app/businesses/logic/prevention.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.RegisterPrevention(NewIPrevention()) } func NewIPrevention() service.IPrevention { return &sPrevention{} } type sPrevention struct{} func (s *sPrevention) List(ctx context.Context, req *model.PreventionSearchReq) (listRes *model.PreventionSearchRes, err error) { listRes = new(model.PreventionSearchRes) err = g.Try(ctx, func(ctx context.Context) { m := dao.Prevention.Ctx(ctx).WithAll() if req.Id != "" { m = m.Where(dao.Prevention.Columns().Id+" = ?", req.Id) } if req.SpeciesCode != "" { m = m.Where(dao.Prevention.Columns().SpeciesCode+" like ?", "%"+req.SpeciesCode+"%") } if req.BotanyQuarantine != "" { m = m.Where(dao.Prevention.Columns().BotanyQuarantine+" like ?", "%"+req.BotanyQuarantine+"%") } if req.Physics != "" { m = m.Where(dao.Prevention.Columns().Physics+" like ?", "%"+req.Physics+"%") } if req.Chemistry != "" { m = m.Where(dao.Prevention.Columns().Chemistry+" like ?", "%"+req.Chemistry+"%") } if req.Biology != "" { m = m.Where(dao.Prevention.Columns().Biology+" like ?", "%"+req.Biology+"%") } if req.Other != "" { m = m.Where(dao.Prevention.Columns().Other+" like ?", "%"+req.Other+"%") } if req.SourcesData != "" { m = m.Where(dao.Prevention.Columns().SourcesData+" like ?", "%"+req.SourcesData+"%") } if req.CreateUser != "" { m = m.Where(dao.Prevention.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.Prevention.Columns().CreateDate+" >= ? AND "+ dao.Prevention.Columns().CreateDate+" <= ?", start, end, ) } } if req.AuditStatus != "" { m = m.Where(dao.Prevention.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.PreventionListRes err = m.Page(req.PageNum, req.PageSize).Order(order).Scan(&res) liberr.ErrIsNil(ctx, err, "获取数据失败") listRes.List = make([]*model.PreventionListRes, len(res)) for k, v := range res { listRes.List[k] = &model.PreventionListRes{ Id: v.Id, SpeciesCode: v.SpeciesCode, BotanyQuarantine: v.BotanyQuarantine, Physics: v.Physics, Chemistry: v.Chemistry, Biology: v.Biology, Other: v.Other, 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 *sPrevention) GetById(ctx context.Context, id int) (res *model.PreventionInfoRes, err error) { err = g.Try(ctx, func(ctx context.Context) { err = dao.Prevention.Ctx(ctx).WithAll().Where(dao.Prevention.Columns().Id, id).Scan(&res) liberr.ErrIsNil(ctx, err, "获取信息失败") }) return } func (s *sPrevention) Add(ctx context.Context, req *model.PreventionAddReq) (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.Prevention.Ctx(ctx).Where(dao.Prevention.Columns().SpeciesCode+" = ?", req.SpeciesCode).Count() if err != nil { liberr.ErrIsNil(ctx, err, "获取物种编码失败") return } if count > 0 { liberr.ErrIsNil(ctx, errors.New("请勿重复添加"), "请勿重复添加") return } _, err = dao.Prevention.Ctx(ctx).Insert(do.Prevention{ Id: req.Id, SpeciesCode: req.SpeciesCode, BotanyQuarantine: req.BotanyQuarantine, Physics: req.Physics, Chemistry: req.Chemistry, Biology: req.Biology, Other: req.Other, 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 *sPrevention) Edit(ctx context.Context, req *model.PreventionEditReq) (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.Prevention.Ctx(ctx). Where(dao.Prevention.Columns().SpeciesCode+" = ?", req.SpeciesCode). WhereNot(dao.Prevention.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.Prevention.Ctx(ctx).WherePri(req.Id).Update(do.Prevention{ SpeciesCode: req.SpeciesCode, BotanyQuarantine: req.BotanyQuarantine, Physics: req.Physics, Chemistry: req.Chemistry, Biology: req.Biology, Other: req.Other, 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 *sPrevention) Delete(ctx context.Context, ids []int) (err error) { err = g.Try(ctx, func(ctx context.Context) { _, err = dao.Prevention.Ctx(ctx).Delete(dao.Prevention.Columns().Id+" in (?)", ids) liberr.ErrIsNil(ctx, err, "删除失败") }) return }