123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980 |
- package service
- import (
- "errors"
- "fmt"
- "gas-cylinder-api/app/admin/model"
- "gas-cylinder-api/app/admin/service/dto"
- "gas-cylinder-api/common/actions"
- cDto "gas-cylinder-api/common/dto"
- "gas-cylinder-api/common/global"
- cModel "gas-cylinder-api/common/model"
- log "gogs.baozhida.cn/zoie/OAuth-core/logger"
- "gogs.baozhida.cn/zoie/OAuth-core/service"
- "gorm.io/gorm/utils"
- "gorm.io/gorm"
- "time"
- )
- type OperationLog struct {
- service.Service
- }
- // GetPage 获取OperationLog列表
- func (e *OperationLog) GetPage(c *dto.OperationLogGetPageReq, list *[]model.OperationLog, count *int64, p *actions.DataPermission) error {
- var err error
- var data model.OperationLog
- err = e.Orm.Model(&data).
- Scopes(
- cDto.MakeCondition(c.GetNeedSearch()),
- cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
- actions.Permission(data.TableName(), p),
- ).
- Preload("OptUserObj").
- Preload("ObjectUserObj").
- Preload("ObjectCustomerObj").
- Preload("OptCustomerObj").
- Preload("CompanyObj").
- Preload("CurrentEnterpriseObj").
- Preload("CurrentStationObj").
- Preload("CurrentStoreObj").
- Preload("CurrentTruckObj").
- Preload("CurrentAddressObj").
- 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 获取OperationLog对象
- func (e *OperationLog) Get(d *dto.OperationLogGetReq, carInfoModel *model.OperationLog, p *actions.DataPermission) error {
- err := e.Orm.
- Scopes(actions.Permission(carInfoModel.TableName(), p)).
- First(carInfoModel, 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 创建OperationLog对象
- func (e *OperationLog) Insert(c *dto.OperationLogInsertReq, p *actions.DataPermission) error {
- var err error
- data := make([]model.OperationLog, 0)
- //company, err := model.GetProvCodeById(p.DeptId)
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- user, err := model.GetUserCode(p.UserId)
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- tx := e.Orm.Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- batchNumber := fmt.Sprintf("%03d%s", p.DeptId, time.Now().Format("20060102150405"))
- optTime := time.Now().Format("2006-01-02 15:04:05")
- switch c.OptType {
- case "25", "27":
- // 送气员领重瓶
- for _, chipUid := range c.ChipUidList {
- // 1、通过高频ID查询气瓶内编码
- //var gasCylinder model.GasCylinder
- //err = tx.Where("uid = ?", chipUid).First(&gasCylinder).Error
- //if err != nil {
- // tx.Rollback()
- // log.Errorf("db error: %s", err)
- // return global.CreateFailedErr
- //}
- operationLog := model.OperationLog{
- BatchNumber: batchNumber,
- ProvOperationLog: model.ProvOperationLog{
- InnerCode: chipUid,
- OptType: c.OptType,
- OptUser: user.ProvUserId,
- ObjectUser: user.ProvUserId,
- CompanyId: user.ProvUser.CmpCode,
- CurrentEnterprise: user.ProvUser.CmpCode,
- CurrentStore: user.Dept.CmpCode,
- Lng: utils.ToString(user.Dept.ProvStore.Lng),
- Lat: utils.ToString(user.Dept.ProvStore.Lat),
- OptTime: optTime,
- },
- ControlBy: cModel.ControlBy{
- CreateBy: p.UserId,
- },
- DeptBy: cModel.DeptBy{
- DeptId: p.DeptId,
- },
- }
- if c.OptType == "27" {
- // 修改用户关联的气瓶信息
- var cgc model.CustomerGasCylinder
- err = tx.Where("inner_code = ? and state = 1", chipUid).First(&cgc).Error
- if err == nil {
- cgc.ReturnTime = cModel.Time(time.Now())
- cgc.State = 2
- err = tx.Save(&cgc).Error
- if err != nil {
- log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- operationLog.ObjectCustomer = cgc.CustomerId
- operationLog.OptCustomer = cgc.CustomerId
- operationLog.CurrentAddress = cgc.CustomerId
- }
- }
- data = append(data, operationLog)
- }
- case "6", "10", "21", "31", "33", "34", "35", "37":
- // 门店端
- for _, chipUid := range c.ChipUidList {
- // 1、通过高频ID查询气瓶内编码
- //var gasCylinder model.GasCylinder
- //err = tx.Where("uid = ?", chipUid).First(&gasCylinder).Error
- //if err != nil {
- // log.Errorf("db error: %s", err)
- // return global.CreateFailedErr
- //}
- if c.OptType == "21" {
- err = tx.Model(&model.OperationLog{}).
- Where("inner_code = ? and state = 1", chipUid).
- Update("state", 2).Error
- if err != nil {
- log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- }
- operationLog := model.OperationLog{
- BatchNumber: batchNumber,
- ProvOperationLog: model.ProvOperationLog{
- InnerCode: chipUid,
- OptType: c.OptType,
- OptUser: user.ProvUserId,
- ObjectUser: user.ProvUserId,
- CompanyId: user.ProvUser.CmpCode,
- CurrentEnterprise: user.ProvUser.CmpCode,
- CurrentStore: user.ProvUser.CmpCode,
- Lng: utils.ToString(user.Dept.ProvStore.Lng),
- Lat: utils.ToString(user.Dept.ProvStore.Lat),
- OptTime: optTime,
- },
- ControlBy: cModel.ControlBy{
- CreateBy: p.UserId,
- },
- DeptBy: cModel.DeptBy{
- DeptId: p.DeptId,
- },
- }
- data = append(data, operationLog)
- }
- case "11", "12", "16", "17", "19":
- // 司机端
- for _, chipUid := range c.ChipUidList {
- // 1、通过高频ID查询气瓶内编码
- //var gasCylinder model.GasCylinder
- //err = tx.Where("uid = ?", chipUid).First(&gasCylinder).Error
- //if err != nil {
- // tx.Rollback()
- // e.Log.Errorf("获取气瓶信息失败: %s", err)
- // return errors.New("获取气瓶信息失败")
- //}
- var truckUserCarInfo model.TruckUserCarInfo
- err = tx.Where("prov_user_id = ?", user.ProvUserId).First(&truckUserCarInfo).Error
- if err != nil {
- tx.Rollback()
- e.Log.Errorf("获取车辆信息失败: %s", err)
- return errors.New("获取车辆信息失败")
- }
- operationLog := model.OperationLog{
- BatchNumber: batchNumber,
- ProvOperationLog: model.ProvOperationLog{
- InnerCode: chipUid,
- OptType: c.OptType,
- OptUser: user.ProvUserId,
- ObjectUser: user.ProvUserId,
- //CompanyId: user.ProvUser.CmpCode,
- //CurrentEnterprise: user.ProvUser.CmpCode,
- //CurrentStore: user.ProvUser.CmpCode,
- CurrentTruck: user.ProvUserId,
- CurrentMotor: truckUserCarInfo.CarNo,
- OptTime: optTime,
- },
- ControlBy: cModel.ControlBy{
- CreateBy: p.UserId,
- },
- DeptBy: cModel.DeptBy{
- DeptId: p.DeptId,
- },
- }
- if c.OptType == "12" || c.OptType == "16" || c.OptType == "17" {
- operationLog.CurrentStation = c.CurrentEnterprise
- operationLog.CompanyId = c.CurrentEnterprise
- operationLog.CurrentEnterprise = c.CurrentEnterprise
- }
- if c.OptType == "11" || c.OptType == "19" {
- operationLog.CurrentStore = c.CurrentEnterprise
- operationLog.CompanyId = c.CurrentEnterprise
- operationLog.CurrentEnterprise = c.CurrentEnterprise
- }
- data = append(data, operationLog)
- }
- case "13", "14", "15":
- // 气站端
- for _, chipUid := range c.ChipUidList {
- // 1、通过高频ID查询气瓶内编码
- //var gasCylinder model.GasCylinder
- //err = tx.Where("uid = ?", chipUid).First(&gasCylinder).Error
- //if err != nil {
- // tx.Rollback()
- // e.Log.Errorf("获取气瓶信息失败: %s", err)
- // return errors.New("获取气瓶信息失败")
- //}
- operationLog := model.OperationLog{
- BatchNumber: batchNumber,
- ProvOperationLog: model.ProvOperationLog{
- InnerCode: chipUid,
- OptType: c.OptType,
- ObjectUser: user.ProvUserId,
- OptUser: user.ProvUserId,
- CompanyId: user.ProvUser.CmpCode,
- CurrentEnterprise: user.ProvUser.CmpCode,
- //CurrentStore: user.ProvUser.CmpCode,
- CurrentStation: user.ProvUser.CmpCode,
- Lng: utils.ToString(user.Dept.ProvStore.Lng),
- Lat: utils.ToString(user.Dept.ProvStore.Lat),
- OptTime: optTime,
- },
- ControlBy: cModel.ControlBy{
- CreateBy: p.UserId,
- },
- DeptBy: cModel.DeptBy{
- DeptId: p.DeptId,
- },
- }
- data = append(data, operationLog)
- }
- }
- provList := make([]model.ProvOperationLog, 0)
- for _, v := range data {
- provList = append(provList, v.GenProvOperationLog())
- }
- err = e.UpdateGasCylinderStatus(tx, c, user, p)
- if err != nil {
- e.Log.Errorf("更新钢瓶状态失败: %s", err)
- return errors.New("更新气瓶状态失败")
- }
- // 更新钢瓶最新流转步骤时间
- err = tx.Model(&model.GasCylinder{}).Where("inner_code in (?) ", c.ChipUidList).
- Updates(map[string]interface{}{
- "last_operation_time": time.Now(),
- }).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return errors.New("更新钢瓶最新流转步骤时间失败")
- }
- // TODO 同步省平台 1.1.1.22 批量新增操作记录
- if len(data) > 0 {
- err = tx.Create(&data).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- }
- return nil
- }
- func (e *OperationLog) UpdateGasCylinderStatus(tx *gorm.DB, c *dto.OperationLogInsertReq, user model.SysUser, p *actions.DataPermission) (err error) {
- switch c.OptType {
- case "15":
- // 气站重瓶出库 删除气站重瓶
- err = tx.Where("inner_code in (?) and user_id = 0 and company_id = ? and status = ?",
- c.ChipUidList, user.Dept.Id, model.GasCylinderStatusWeighty).
- Delete(&model.GasCylinderStatus{}).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- case "17":
- // 司机确认重瓶从气站出库 添加司机重瓶
- for _, chipUid := range c.ChipUidList {
- gasCylinderStatus := &model.GasCylinderStatus{
- InnerCode: chipUid,
- UserId: user.Id,
- CompanyId: user.Dept.Id,
- Status: model.GasCylinderStatusWeighty,
- ControlBy: cModel.ControlBy{
- CreateBy: p.UserId,
- },
- DeptBy: cModel.DeptBy{
- DeptId: p.DeptId,
- },
- }
- // 司机添加重瓶
- err = tx.Where(model.GasCylinderStatus{InnerCode: chipUid}).
- Assign(map[string]interface{}{
- "status": model.GasCylinderStatusWeighty,
- "user_id": user.Id,
- "company_id": user.Dept.Id,
- }).FirstOrCreate(&gasCylinderStatus).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- }
- case "19":
- // 删除司机重瓶
- err = tx.Where("inner_code in (?) and user_id = ? and company_id = ? and status = ?",
- c.ChipUidList, user.Id, user.Dept.Id, model.GasCylinderStatusWeighty).
- Delete(&model.GasCylinderStatus{}).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- case "31":
- // 门店确认重瓶卸货入库 添加门店重瓶
- for _, chipUid := range c.ChipUidList {
- gasCylinderStatus := &model.GasCylinderStatus{
- InnerCode: chipUid,
- UserId: 0,
- CompanyId: user.Dept.Id,
- Status: model.GasCylinderStatusWeighty,
- ControlBy: cModel.ControlBy{
- CreateBy: p.UserId,
- },
- DeptBy: cModel.DeptBy{
- DeptId: p.DeptId,
- },
- }
- // 门店添加重瓶
- err = tx.Where(model.GasCylinderStatus{InnerCode: chipUid}).
- Assign(map[string]interface{}{
- "status": model.GasCylinderStatusWeighty,
- "user_id": 0,
- "company_id": user.Dept.Id,
- }).FirstOrCreate(&gasCylinderStatus).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- }
- case "37":
- // 门店重瓶出库 删除门店重瓶
- err = tx.Where("inner_code in (?) and user_id = 0 and company_id = ?", c.ChipUidList, user.Dept.Id).
- Delete(&model.GasCylinderStatus{}).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- case "25":
- // 送气员领重瓶出库 添加送气员重瓶
- for _, chipUid := range c.ChipUidList {
- // 送气员领重瓶出库
- gasCylinderStatus := &model.GasCylinderStatus{
- InnerCode: chipUid,
- UserId: user.Id,
- CompanyId: user.Dept.Id,
- Status: model.GasCylinderStatusWeighty,
- ControlBy: cModel.ControlBy{
- CreateBy: p.UserId,
- },
- DeptBy: cModel.DeptBy{
- DeptId: p.DeptId,
- },
- }
- // 添加送气员重瓶
- err = tx.Where(model.GasCylinderStatus{InnerCode: chipUid}).
- Assign(map[string]interface{}{
- "status": model.GasCylinderStatusWeighty,
- "user_id": user.Id,
- "company_id": user.Dept.Id,
- }).FirstOrCreate(&gasCylinderStatus).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- }
- case "27":
- // 送气员回收空瓶 添加送气员空瓶
- for _, chipUid := range c.ChipUidList {
- // 送气员回收空瓶
- gasCylinderStatus := &model.GasCylinderStatus{
- InnerCode: chipUid,
- UserId: user.Id,
- CompanyId: user.Dept.Id,
- Status: model.GasCylinderStatusEmpty,
- ControlBy: cModel.ControlBy{
- CreateBy: p.UserId,
- },
- DeptBy: cModel.DeptBy{
- DeptId: p.DeptId,
- },
- }
- // 添加送气员空瓶
- err = tx.Where(model.GasCylinderStatus{InnerCode: chipUid, UserId: user.Id, CompanyId: user.Dept.Id}).
- Assign(model.GasCylinderStatus{Status: model.GasCylinderStatusEmpty}).FirstOrCreate(&gasCylinderStatus).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- }
- case "21":
- //21门店回收空瓶
- for _, chipUid := range c.ChipUidList {
- gasCylinderStatus := &model.GasCylinderStatus{
- InnerCode: chipUid,
- UserId: 0,
- CompanyId: user.Dept.Id,
- Status: model.GasCylinderStatusEmpty,
- ControlBy: cModel.ControlBy{
- CreateBy: p.UserId,
- },
- DeptBy: cModel.DeptBy{
- DeptId: p.DeptId,
- },
- }
- // 添加门店空瓶
- err = tx.Where(model.GasCylinderStatus{InnerCode: chipUid}).
- Assign(map[string]interface{}{
- "status": model.GasCylinderStatusEmpty,
- "user_id": 0,
- "company_id": user.Dept.Id,
- }).FirstOrCreate(&gasCylinderStatus).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- }
- case "10":
- // 门店空瓶出库 删除门店空瓶
- err = tx.Where("inner_code in (?) and user_id = 0 and company_id = ? and status = ?", c.ChipUidList, user.Dept.Id, model.GasCylinderStatusEmpty).
- Delete(&model.GasCylinderStatus{}).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- case "11":
- // 司机确认空瓶装车 添加司机空瓶
- for _, chipUid := range c.ChipUidList {
- gasCylinderStatus := &model.GasCylinderStatus{
- InnerCode: chipUid,
- UserId: user.Id,
- CompanyId: user.Dept.Id,
- Status: model.GasCylinderStatusEmpty,
- ControlBy: cModel.ControlBy{
- CreateBy: p.UserId,
- },
- DeptBy: cModel.DeptBy{
- DeptId: p.DeptId,
- },
- }
- err = tx.Where(model.GasCylinderStatus{InnerCode: chipUid}).
- Assign(map[string]interface{}{
- "status": model.GasCylinderStatusEmpty,
- "user_id": user.Id,
- "company_id": user.Dept.Id,
- }).FirstOrCreate(&gasCylinderStatus).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- }
- case "13":
- // 气站确认空瓶到达气站 添加气站空瓶
- for _, chipUid := range c.ChipUidList {
- gasCylinderStatus := &model.GasCylinderStatus{
- InnerCode: chipUid,
- UserId: 0,
- CompanyId: user.Dept.Id,
- Status: model.GasCylinderStatusEmpty,
- ControlBy: cModel.ControlBy{
- CreateBy: p.UserId,
- },
- DeptBy: cModel.DeptBy{
- DeptId: p.DeptId,
- },
- }
- err = tx.Where(model.GasCylinderStatus{InnerCode: chipUid}).
- Assign(map[string]interface{}{
- "status": model.GasCylinderStatusEmpty,
- "user_id": 0,
- "company_id": user.Dept.Id,
- }).FirstOrCreate(&gasCylinderStatus).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- }
- case "12":
- // 司机运输空瓶到达气站 删除司机空瓶
- err = tx.Where("inner_code in (?) and user_id = ? and company_id = ? and status = ?", c.ChipUidList, user.Id, user.Dept.Id, model.GasCylinderStatusEmpty).
- Delete(&model.GasCylinderStatus{}).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- case "14":
- // 气站充装空瓶 气站空瓶转重瓶
- err = tx.Model(&model.GasCylinderStatus{}).Where("inner_code in (?) and user_id = 0 and company_id = ? and status = ?", c.ChipUidList, user.Dept.Id, model.GasCylinderStatusEmpty).
- Updates(map[string]interface{}{
- "status": model.GasCylinderStatusWeighty,
- }).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- // 更新钢瓶充装记录
- err = tx.Model(&model.GasCylinder{}).Where("inner_code in (?) ", c.ChipUidList).
- Updates(map[string]interface{}{
- "station_name": user.Dept.Name,
- "fill_time": time.Now().Format("2006-01-02 15:04:05"),
- }).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- case "16":
- // 司机讲订单(重瓶)退回气站 删除司机重瓶
- err = tx.Where("inner_code in (?) and user_id = ? and company_id = ? and status = ?",
- c.ChipUidList, user.Id, user.Dept.Id, model.GasCylinderStatusWeighty).
- Delete(&model.GasCylinderStatus{}).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- case "016":
- // 气站确认重瓶到达气站 添加气站重瓶
- for _, chipUid := range c.ChipUidList {
- gasCylinderStatus := &model.GasCylinderStatus{
- InnerCode: chipUid,
- UserId: 0,
- CompanyId: user.Dept.Id,
- Status: model.GasCylinderStatusWeighty,
- ControlBy: cModel.ControlBy{
- CreateBy: p.UserId,
- },
- DeptBy: cModel.DeptBy{
- DeptId: p.DeptId,
- },
- }
- // 气站添加重瓶
- err = tx.Where(model.GasCylinderStatus{InnerCode: chipUid}).
- Assign(map[string]interface{}{
- "status": model.GasCylinderStatusWeighty,
- "user_id": 0,
- "company_id": user.Dept.Id,
- }).FirstOrCreate(&gasCylinderStatus).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- }
- case "33":
- // 门店讲重瓶退回司机 删除门店重瓶
- err = tx.Where("inner_code in (?) and user_id = 0 and company_id = ? and status = ?",
- c.ChipUidList, user.Dept.Id, model.GasCylinderStatusWeighty).
- Delete(&model.GasCylinderStatus{}).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- case "033":
- // 司机确认重瓶从门店出库 添加司机重瓶
- for _, chipUid := range c.ChipUidList {
- gasCylinderStatus := &model.GasCylinderStatus{
- InnerCode: chipUid,
- UserId: user.Id,
- CompanyId: user.Dept.Id,
- Status: model.GasCylinderStatusWeighty,
- ControlBy: cModel.ControlBy{
- CreateBy: p.UserId,
- },
- DeptBy: cModel.DeptBy{
- DeptId: p.DeptId,
- },
- }
- // 司机添加重瓶
- err = tx.Where(model.GasCylinderStatus{InnerCode: chipUid}).
- Assign(map[string]interface{}{
- "status": model.GasCylinderStatusWeighty,
- "user_id": user.Id,
- "company_id": user.Dept.Id,
- }).FirstOrCreate(&gasCylinderStatus).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- }
- case "35":
- // 门店确认未配送重瓶返库 门店添加重瓶
- for _, chipUid := range c.ChipUidList {
- gasCylinderStatus := &model.GasCylinderStatus{
- InnerCode: chipUid,
- UserId: 0,
- CompanyId: user.Dept.Id,
- Status: model.GasCylinderStatusWeighty,
- ControlBy: cModel.ControlBy{
- CreateBy: p.UserId,
- },
- DeptBy: cModel.DeptBy{
- DeptId: p.DeptId,
- },
- }
- // 门店添加重瓶
- err = tx.Where(model.GasCylinderStatus{InnerCode: chipUid}).
- Assign(map[string]interface{}{
- "status": model.GasCylinderStatusWeighty,
- "user_id": 0,
- "company_id": user.Dept.Id,
- }).FirstOrCreate(&gasCylinderStatus).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- }
- }
- return nil
- }
- func (e *OperationLog) InsertForGasStation(c *dto.OperationLogInsertForGasStationReq) error {
- var err error
- tx := e.Orm.Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- // 通过sn查询设备信息
- var device model.Device
- err = tx.Where("sn = ?", c.Sn).Preload("User").First(&device).Error
- if err != nil {
- tx.Rollback()
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- // 1、通过高频ID查询气瓶内编码
- //var gasCylinder model.GasCylinder
- //err = tx.Where("uid = ?", c.ChipUid).First(&gasCylinder).Error
- operationLog := model.OperationLog{
- ProvOperationLog: model.ProvOperationLog{
- OptType: device.OptType,
- OptTime: time.Now().Format("2006-01-02 15:04:05"),
- OptUser: device.ProvUserId,
- InnerCode: c.ChipUid,
- CompanyId: device.ProvCmpCode,
- CurrentEnterprise: device.ProvCmpCode,
- CurrentStation: device.ProvCmpCode,
- },
- DeptBy: cModel.DeptBy{
- DeptId: device.DeptId,
- },
- }
- // TODO 同步省平台 1.1.1.20 新增操作记录
- //ipr := data.GenProvOperationLog()
- err = tx.Create(&operationLog).Error
- if err != nil {
- tx.Rollback()
- log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- return nil
- }
- // 送气员送达重瓶,送气订单取消
- func InsertOperationLogByOptType26Or36(tx *gorm.DB, OptType string, chipUids []string, order model.Order) error {
- var err error
- for _, chipUid := range chipUids {
- // 1、通过高频ID查询气瓶内编码
- //var gasCylinder model.GasCylinder
- //err = tx.Where("uid = ?", chipUid).First(&gasCylinder).Error
- //if err != nil {
- // return errors.New("获取气瓶信息失败")
- //}
- // 新增操作记录
- operationLog := model.OperationLog{
- ProvOperationLog: model.ProvOperationLog{
- OptType: OptType,
- OptTime: time.Now().Format("2006-01-02 15:04:05"),
- OptUser: order.User.ProvUserId,
- ObjectUser: order.User.ProvUserId,
- InnerCode: chipUid,
- ObjectCustomer: order.CustomerId,
- OptCustomer: order.CustomerId,
- CompanyId: order.Dept.CmpCode,
- CurrentEnterprise: order.Store.CmpCode,
- CurrentStore: order.Store.CmpCode,
- CurrentAddress: order.CustomerId,
- },
- ControlBy: cModel.ControlBy{
- CreateBy: order.UserId,
- },
- DeptBy: cModel.DeptBy{
- DeptId: order.StoreId,
- },
- }
- if OptType == "26" {
- // 新增客户气瓶信息
- err = tx.Create(&model.CustomerGasCylinder{
- CustomerId: order.CustomerId,
- InnerCode: chipUid,
- State: 1,
- BorrowTime: cModel.Time(time.Now()),
- }).Error
- if err != nil {
- tx.Rollback()
- log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- // 删除送气员重瓶
- err = tx.Where("inner_code = ? and user_id = ? and company_id = ? and status = ?",
- chipUid, order.UserId, order.StoreId, model.GasCylinderStatusWeighty).
- Delete(&model.GasCylinderStatus{}).Error
- if err != nil {
- log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- }
- if OptType == "36" {
- // 查询气瓶信息
- var cgc model.CustomerGasCylinder
- err = tx.Where("inner_code = ? and customer_id = ? and state = 1", chipUid, order.CustomerId).First(&cgc).Error
- if err != nil {
- log.Errorf("db error: %s", err)
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return errors.New(fmt.Sprintf("钢瓶%s不存在", chipUid))
- }
- return errors.New(fmt.Sprintf("查询钢瓶信息%s失败", chipUid))
- }
- cgc.ReturnTime = cModel.Time(time.Now())
- cgc.State = 2
- err = tx.Save(&cgc).Error
- if err != nil {
- log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- // 添加送气员重瓶
- gasCylinderStatus := &model.GasCylinderStatus{
- InnerCode: chipUid,
- UserId: order.UserId,
- CompanyId: order.StoreId,
- Status: model.GasCylinderStatusWeighty,
- ControlBy: cModel.ControlBy{
- CreateBy: order.UserId,
- },
- DeptBy: cModel.DeptBy{
- DeptId: order.StoreId,
- },
- }
- err = tx.Where(model.GasCylinderStatus{InnerCode: chipUid, UserId: order.UserId, CompanyId: order.StoreId}).
- Assign(model.GasCylinderStatus{Status: model.GasCylinderStatusWeighty}).FirstOrCreate(&gasCylinderStatus).Error
- if err != nil {
- log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- operationLog.ObjectCustomer = cgc.CustomerId
- operationLog.OptCustomer = cgc.CustomerId
- operationLog.CurrentAddress = cgc.CustomerId
- }
- // TODO 同步省平台 1.1.1.20 新增操作记录
- //ipr := data.GenProvOperationLog()
- err = tx.Create(&operationLog).Error
- if err != nil {
- tx.Rollback()
- log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- }
- return nil
- }
- // ListByInnerCode 通过uid获取OperationLog列表
- func (e *OperationLog) ListByInnerCode(InnerCode string, list *[]model.OperationLog) error {
- var err error
- var data model.OperationLog
- err = e.Orm.Model(&data).
- Where("inner_code = ? and state = 1", InnerCode).
- Preload("OptUserObj").
- Preload("ObjectUserObj").
- Preload("ObjectCustomerObj").
- Preload("OptCustomerObj").
- Preload("CompanyObj").
- Preload("CurrentEnterpriseObj").
- Preload("CurrentStationObj").
- Preload("CurrentStoreObj").
- Preload("CurrentTruckObj").
- Preload("CurrentAddressObj").
- Find(list).Order("opt_time").Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.GetFailedErr
- }
- return nil
- }
- func (e *OperationLog) Check(c *dto.OperationLogCheckReq, p *actions.DataPermission) (flag bool, err error) {
- user, err := model.GetUserCode(p.UserId)
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return
- }
- gasCylinderStatus := model.GasCylinderStatus{}
- switch c.OptType {
- case "26":
- // 送气员送达重瓶 查询送气员重瓶
- status := model.GasCylinderStatus{
- InnerCode: c.ChipUid,
- UserId: user.Id,
- CompanyId: user.Dept.Id,
- Status: model.GasCylinderStatusWeighty,
- }
- err = e.Orm.Where(status).First(&gasCylinderStatus).Error
- if err != nil {
- log.Errorf("db error: %s", err)
- if errors.Is(err, gorm.ErrRecordNotFound) {
- err = errors.New("请先添加重瓶")
- }
- return
- }
- flag = true
- return
- case "10":
- // 门店空瓶出库 查看门店空瓶
- status := model.GasCylinderStatus{
- InnerCode: c.ChipUid,
- UserId: 0,
- CompanyId: user.Dept.Id,
- Status: model.GasCylinderStatusEmpty,
- }
- err = e.Orm.Where(status).First(&gasCylinderStatus).Error
- if err != nil {
- log.Errorf("db error: %s", err)
- if errors.Is(err, gorm.ErrRecordNotFound) {
- err = errors.New("请先添加空瓶")
- }
- return
- }
- flag = true
- return
- case "33", "37", "15":
- // 查询门店/气站重瓶
- status := model.GasCylinderStatus{
- InnerCode: c.ChipUid,
- UserId: 0,
- CompanyId: user.Dept.Id,
- Status: model.GasCylinderStatusWeighty,
- }
- err = e.Orm.Where(status).First(&gasCylinderStatus).Error
- if err != nil {
- log.Errorf("db error: %s", err)
- if errors.Is(err, gorm.ErrRecordNotFound) {
- err = errors.New("请先添加重瓶")
- }
- return
- }
- flag = true
- return
- case "12":
- // 查询司机空瓶
- status := model.GasCylinderStatus{
- InnerCode: c.ChipUid,
- UserId: user.Id,
- CompanyId: user.Dept.Id,
- Status: model.GasCylinderStatusEmpty,
- }
- err = e.Orm.Where(status).First(&gasCylinderStatus).Error
- if err != nil {
- log.Errorf("db error: %s", err)
- if errors.Is(err, gorm.ErrRecordNotFound) {
- err = errors.New("请先添加空瓶")
- }
- return
- }
- flag = true
- return
- case "16", "19":
- // 查询司机重瓶
- status := model.GasCylinderStatus{
- InnerCode: c.ChipUid,
- UserId: user.Id,
- CompanyId: user.Dept.Id,
- Status: model.GasCylinderStatusWeighty,
- }
- err = e.Orm.Where(status).First(&gasCylinderStatus).Error
- if err != nil {
- log.Errorf("db error: %s", err)
- if errors.Is(err, gorm.ErrRecordNotFound) {
- err = errors.New("请先添加重瓶")
- }
- return
- }
- flag = true
- return
- }
- return true, nil
- }
|