123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- package service
- import (
- "cold-logistics/app/admin/model"
- "cold-logistics/app/admin/service/dto"
- "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"
- )
- type Car struct {
- service.Service
- }
- func CarIsBindUserScopes(isBind bool) func(db *gorm.DB) *gorm.DB {
- return func(db *gorm.DB) *gorm.DB {
- // 未发货
- if isBind {
- return db.Where("user_id > 0")
- }
- return db
- }
- }
- // GetPage 获取Car列表
- func (e *Car) GetPage(c *dto.CarGetPageReq, list *[]model.Car, count *int64, p *actions.DataPermission) error {
- var err error
- var data model.Car
- if c.PageSize == 9999 {
- err = e.Orm.Model(&data).
- Scopes(
- actions.Permission(data.TableName(), p),
- ).
- Find(list).Limit(-1).Offset(-1).
- Count(count).Error
- } else {
- err = e.Orm.Model(&data).
- Scopes(
- cDto.MakeCondition(c.GetNeedSearch()),
- cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
- actions.Permission(data.TableName(), p),
- CarIsBindUserScopes(c.IsBind),
- ).
- Joins("User").
- Where("car_no like ? or User.nick_name like ?", "%"+c.Name+"%", "%"+c.Name+"%").
- 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 获取Car对象
- func (e *Car) Get(d *dto.CarGetReq, carModel *model.Car, p *actions.DataPermission) error {
- err := e.Orm.
- Scopes(actions.Permission(carModel.TableName(), p)).
- Preload("User").
- First(carModel, 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 创建Car对象
- func (e *Car) Insert(c *dto.CarInsertReq, p *actions.DataPermission) error {
- var err error
- var data model.Car
- tx := e.Orm.Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- var i int64
- err = tx.Model(&data).Where("car_no = ?", c.CarNo).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
- }
- var j int64
- err = tx.Model(&data).Where("user_id = ?", c.UserId).Count(&j).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- if j > 0 {
- err = errors.New("该用户已绑定其他车辆!")
- e.Log.Errorf("db error: %s", err)
- return err
- }
- var k int64
- err = tx.Model(&data).Where("sn = ?", c.Sn).Count(&k).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- if k > 0 {
- err = errors.New("该SN已绑定其他车辆!")
- e.Log.Errorf("db error: %s", err)
- return err
- }
- var device nats_server.Device
- var company model.SysDept
- device, err = nats_server.Cold_ReadDeviceByT_sn(c.Sn)
- if err != nil {
- err = errors.New("获取SN信息失败,请检查SN是否正确!")
- e.Log.Errorf("db error: %s", err)
- return err
- }
- company, err = model.GetCompanyById(p.DeptId)
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return err
- }
- if device.T_pid != company.Id {
- err = errors.New("获取SN信息失败,请检查SN是否正确!")
- e.Log.Errorf("获取SN信息失败,device.T_pid != company.Id")
- return err
- }
- // 添加车辆
- c.Generate(&data)
- data.HistorySn = []string{data.Sn}
- err = tx.Create(&data).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- c.Id = data.Id
- return nil
- }
- // Update 修改Car对象
- func (e *Car) Update(c *dto.CarUpdateReq, p *actions.DataPermission) error {
- var err error
- tx := e.Orm.Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- var carModel = model.Car{}
- // 查询车辆是否存在
- err = e.Orm.Scopes(actions.Permission(carModel.TableName(), p)).
- First(&carModel, 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 carModel.Sn != c.Sn {
- var k int64
- var data = model.Car{}
- err = tx.Model(&data).Where("sn = ?", c.Sn).Count(&k).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- if k > 0 {
- err = errors.New("该Sn已绑定其他车辆!")
- e.Log.Errorf("db error: %s", err)
- return err
- }
- var device nats_server.Device
- var company model.SysDept
- device, err = nats_server.Cold_ReadDeviceByT_sn(c.Sn)
- if err != nil {
- err = errors.New("获取SN信息失败,请检查SN是否正确!")
- e.Log.Errorf("db error: %s", err)
- return err
- }
- company, err = model.GetCompanyById(p.DeptId)
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return err
- }
- if device.T_pid != company.Id {
- err = errors.New("获取SN信息失败,请检查SN是否正确!")
- e.Log.Errorf("获取SN信息失败,device.T_pid != company.Id")
- return err
- }
- carModel.HistorySn = append(carModel.HistorySn, c.Sn)
- }
- if carModel.UserId != c.UserId {
- var k int64
- var data = model.Car{}
- err = tx.Model(&data).Where("user_id = ?", c.UserId).Count(&k).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- if k > 0 {
- err = errors.New("该用户已绑定其他车辆!")
- e.Log.Errorf("db error: %s", err)
- return err
- }
- }
- c.Generate(&carModel)
- err = tx.Save(&carModel).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.UpdateFailedErr
- }
- c.Id = carModel.Id
- return nil
- }
- // Remove 删除Car
- func (e *Car) Remove(c *dto.CarDeleteReq, p *actions.DataPermission) error {
- var err error
- tx := e.Orm.Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- var carModel model.Car
- // 查询车辆是否存在
- err = e.Orm.Scopes(actions.Permission(carModel.TableName(), p)).
- First(&carModel, c.GetId()).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return global.DeleteNotFoundOrNoPermissionErr
- }
- return global.DeleteFailedErr
- }
- db := tx.Delete(&carModel)
- if err = db.Error; err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.DeleteFailedErr
- }
- if db.RowsAffected == 0 {
- return global.DeleteNotFoundOrNoPermissionErr
- }
- return nil
- }
- // GetUserList 获取绑定车辆的用户
- func (e *Car) GetUserList(c *dto.SysUserGetPageReq, p *actions.DataPermission, list *[]model.SysUserForBind, count *int64) error {
- var err error
- var userList []model.SysUserForBind
- var carModel model.Car
- var userModel model.SysUserForBind
- var userIds []int
- // 查询车辆是否存在
- err = e.Orm.Model(&carModel).Select("user_id").Scopes(actions.Permission(carModel.TableName(), p)).
- Find(&userIds).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.GetFailedErr
- }
- err = e.Orm.
- Scopes(
- cDto.MakeCondition(c.GetNeedSearch()),
- cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
- actions.UserPermission(userModel.TableName(), p),
- ).
- Where("user_type = ? ", model.UserTypeSys).
- Find(&userList).Limit(-1).Offset(-1).
- Count(count).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.GetFailedErr
- }
- userIdMap := make(map[int]bool)
- for _, id := range userIds {
- userIdMap[id] = true
- }
- for i := 0; i < len(userList); i++ {
- if _, ok := userIdMap[userList[i].Id]; ok {
- userList[i].IsBind = true
- }
- }
- *list = userList
- return nil
- }
- func (e *Car) BatchInsert(c *dto.CarBatchInsertReq) error {
- var err error
- tx := e.Orm.Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- for _, car := range c.List {
- var data model.Car
- var k int64
- err = tx.Model(&data).Where("sn = ?", car.Sn).Count(&k).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- if k > 0 {
- continue
- }
- // 添加车辆
- car.CreateBy = c.CreateBy
- car.DeptId = c.DeptId
- car.Status = c.Status
- car.UserId = c.UserId
- car.Generate(&data)
- data.HistorySn = []string{data.Sn}
- err = tx.Create(&data).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- }
- return nil
- }
|