123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- package services
- import (
- "ERP_storage/dto"
- db "ERP_storage/initialize"
- "ERP_storage/logs"
- models "ERP_storage/models/ContractReview"
- "errors"
- "gogs.baozhida.cn/zoie/ERP_libs/lib"
- "gorm.io/gorm"
- )
- type ServiceType struct {
- }
- func (e *ServiceType) GetPage(c *dto.ServiceTypePageReq) (list []models.ServiceType, cnt int64) {
- var err error
- err = db.DB.Model(&models.ServiceType{}).
- Scopes(
- dto.MakeCondition(c.GetNeedSearch()),
- dto.Paginate(c.GetPageSize(), c.GetPageIndex()),
- dto.WithNormalState(),
- ).
- Find(&list).Limit(-1).Offset(-1).
- Count(&cnt).Error
- if err != nil {
- logs.Error("db error: %s ", err)
- return
- }
- return
- }
- func (e *ServiceType) Get(Id int) (r models.ServiceType, err error) {
- err = db.DB.Scopes(dto.WithNormalState()).First(&r, Id).Error
- if err != nil {
- logs.Error("db error: %s", err)
- return
- }
- return
- }
- // 添加
- func (e *ServiceType) Insert(c *dto.ServiceTypeInsertReq) (id int, err error) {
- var data models.ServiceType
- err = db.DB.Scopes(dto.WithNormalState()).Where("t_name = ?", c.T_name).First(&data).Error
- if err != nil {
- // 没有则创建
- if errors.Is(err, gorm.ErrRecordNotFound) {
- c.Generate(&data)
- err = db.DB.Create(&data).Error
- if err != nil {
- logs.Error("db error: %s", err)
- return
- }
- id = data.Id
- return
- }
- logs.Error("db error: %s", err)
- return
- }
- id = data.Id
- return
- }
- // 修改
- func (e *ServiceType) Update(c *dto.ServiceTypeUpdateReq) error {
- var serviceType = models.ServiceType{}
- err := db.DB.Scopes(dto.WithNormalState()).First(&serviceType, c.GetId()).Error
- if err != nil {
- logs.Error("db error: %s", err)
- return dto.GetNotFoundErr
- }
- c.Generate(&serviceType)
- err = db.DB.Save(&serviceType).Error
- if err != nil {
- logs.Error("db error: %s", err)
- return dto.UpdateFailedErr
- }
- return nil
- }
- // 删除
- func (e *ServiceType) Delete(c *dto.ServiceTypeDeleteReq) error {
- var serviceType = models.ServiceType{}
- err := db.DB.Scopes(dto.WithNormalState()).First(&serviceType, c.GetId()).Error
- if err != nil {
- logs.Error("db error: %s", err)
- return dto.GetNotFoundErr
- }
- var ServiceItemCount int64
- err = db.DB.Model(&models.ServiceItem{}).
- Scopes(dto.WithNormalState()).
- Where("t_service_type_id = ?", serviceType.Id).
- Count(&ServiceItemCount).Error
- if err != nil {
- logs.Error("db error: %s", err)
- return dto.GetFailedErr
- }
- if ServiceItemCount > 0 {
- return errors.New("存在已关联的服务内容,无法删除!")
- }
- serviceType.T_State = 0
- err = db.DB.Save(&serviceType).Error
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return dto.DeleteFailedErr
- }
- return nil
- }
|