123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- package service
- import (
- "cold-logistics/common/actions"
- cDto "cold-logistics/common/dto"
- "cold-logistics/common/global"
- "cold-logistics/common/nats/nats_server"
- "errors"
- "gogs.baozhida.cn/zoie/OAuth-core/service"
- "gorm.io/gorm"
- "gorm.io/gorm/clause"
- "cold-logistics/app/admin/model"
- "cold-logistics/app/admin/service/dto"
- "gogs.baozhida.cn/zoie/OAuth-core/pkg"
- )
- type Company struct {
- service.Service
- }
- // GetPage 获取Address列表
- func (e *Company) GetPage(c *dto.CompanyGetPageReq, list *[]model.SysDept, count *int64) error {
- var err error
- var data model.SysDept
- err = e.Orm.Model(&data).
- Scopes(
- cDto.MakeCondition(c.GetNeedSearch()),
- cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
- ).
- Find(list).Limit(-1).Offset(-1).
- Count(count).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.GetFailedErr
- }
- return nil
- }
- func CompanyIdScopes(id int) func(db *gorm.DB) *gorm.DB {
- return func(db *gorm.DB) *gorm.DB {
- if id == 0 {
- return db
- }
- return db.Where("id = ? ", id)
- }
- }
- func (e *Company) GetAll(c *dto.CompanyGetAllReq, list *[]model.SysDept, count *int64, p *actions.DataPermission) error {
- var err error
- var data model.SysDept
- err = e.Orm.Model(&data).
- Scopes(
- CompanyIdScopes(p.DeptId),
- cDto.MakeCondition(c.GetNeedSearch()),
- ).
- Find(list).Limit(-1).Offset(-1).
- Count(count).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.GetFailedErr
- }
- return nil
- }
- // Get 获取SysDept对象
- func (e *Company) Get(d *dto.CompanyGetReq, deptModel *model.SysDept) error {
- err := e.Orm.
- First(deptModel, d.GetId()).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return global.GetNotFoundOrNoPermissionErr
- }
- return global.GetFailedErr
- }
- return nil
- }
- // Insert 创建SysDept对象
- func (e *Company) Insert(c *dto.CompanyInsertReq) error {
- var err error
- var data model.SysDept
- tx := e.Orm.Debug().Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- var i int64
- err = e.Orm.Model(&data).Where("name = ?", c.Name).Count(&i).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- if i > 0 {
- err = errors.New("公司名称已存在!")
- e.Log.Errorf("db error: %s", err)
- return err
- }
- coldCompany, err := nats_server.Cold_ReadCompanyByT_key(c.ColdKey)
- if err != nil {
- err = errors.New("查询公司秘钥信息失败,请检查秘钥是否正确!")
- e.Log.Errorf("db error: %s", err)
- return err
- }
- data.ColdPid = coldCompany.Id
- c.Generate(&data)
- err = tx.Create(&data).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- c.Id = data.Id
- deptPath := pkg.IntToString(data.Id) + "/"
- if data.ParentId != 0 {
- var deptP model.SysDept
- tx.First(&deptP, data.ParentId)
- deptPath = deptP.Path + deptPath
- } else {
- deptPath = "/0/" + deptPath
- }
- var mp = map[string]string{}
- mp["path"] = deptPath
- err = tx.Model(&data).Update("path", deptPath).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- return nil
- }
- // Update 修改SysDept对象
- func (e *Company) Update(c *dto.CompanyUpdateReq) error {
- var err error
- var deptModel = model.SysDept{}
- tx := e.Orm.Debug().Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- err = tx.First(&deptModel, c.GetId()).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return global.UpdateNotFoundOrNoPermissionErr
- }
- return global.UpdateFailedErr
- }
- if c.ColdKey != deptModel.ColdKey {
- var coldCompany nats_server.Company
- coldCompany, err = nats_server.Cold_ReadCompanyByT_key(c.ColdKey)
- if err != nil {
- err = errors.New("查询公司秘钥信息失败,请检查秘钥是否正确!")
- e.Log.Errorf("db error: %s", err)
- return err
- }
- deptModel.ColdPid = coldCompany.Id
- }
- c.Generate(&deptModel)
- DeptPath := pkg.IntToString(deptModel.Id) + "/"
- if deptModel.ParentId != 0 {
- var DeptP model.SysDept
- tx.First(&DeptP, deptModel.ParentId)
- DeptPath = DeptP.Path + DeptPath
- } else {
- DeptPath = "/0/" + DeptPath
- }
- deptModel.Path = DeptPath
- db := tx.Save(&deptModel)
- if err = db.Error; err != nil {
- e.Log.Errorf("UpdateSysDept error: %s", err)
- return err
- }
- return nil
- }
- // Update 修改SysDept对象
- func (e *Company) UpdateMyCompany(c *dto.MyCompanyUpdateReq) error {
- var err error
- var deptModel = model.SysDept{}
- tx := e.Orm.Debug().Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- err = tx.First(&deptModel, c.GetId()).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return global.UpdateNotFoundOrNoPermissionErr
- }
- return global.UpdateFailedErr
- }
- c.Generate(&deptModel)
- db := tx.Save(&deptModel)
- if err = db.Error; err != nil {
- e.Log.Errorf("UpdateSysDept error: %s", err)
- return err
- }
- return nil
- }
- // Remove 删除SysDept
- // 删除部门 同时删除其子部门、部门下的用户
- func (e *Company) Remove(d *dto.CompanyDeleteReq, p *actions.DataPermission) error {
- var err error
- tx := e.Orm.Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- var dept model.SysDept
- err = e.Orm.
- First(&dept, d.GetId()).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return global.GetNotFoundOrNoPermissionErr
- }
- return global.DeleteFailedErr
- }
- var deptList = make([]model.SysDept, 0)
- var deptIds = make([]int, 0)
- tx.Where("path like ?", dept.Path+"%").Find(&deptList)
- // 获取部门及其子部门下ID
- for _, v := range deptList {
- deptIds = append(deptIds, v.Id)
- }
- db := tx.Select(clause.Associations).Delete(&deptList)
- if err = db.Error; err != nil {
- e.Log.Errorf("Delete error: %s", err)
- return global.DeleteFailedErr
- }
- if db.RowsAffected == 0 {
- return global.DeleteNotFoundOrNoPermissionErr
- }
- // 删除部门下的用户
- dbUser := tx.Where("dept_id in (?)", deptIds).Delete(&model.SysUser{})
- if err = dbUser.Error; err != nil {
- e.Log.Errorf("Delete User error: %s", err)
- return global.DeleteFailedErr
- }
- return nil
- }
|