|
|
@@ -0,0 +1,114 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Services;
|
|
|
+
|
|
|
+use App\DepartmentObligationOwner;
|
|
|
+use App\Owner;
|
|
|
+use App\Traits\ServiceAppAop;
|
|
|
+use App\Obligation;
|
|
|
+use Carbon\Carbon;
|
|
|
+
|
|
|
+class ObligationService
|
|
|
+{
|
|
|
+ use ServiceAppAop;
|
|
|
+ protected $modelClass=Obligation::class;
|
|
|
+
|
|
|
+ public function getSelection($column = ['id','code'])
|
|
|
+ {
|
|
|
+ return Obligation::query()->select($column)->get();
|
|
|
+ }
|
|
|
+ public function recombineCodeIdArr(){
|
|
|
+ $obligations=$this->getSelection()->toArray();
|
|
|
+ return array_combine(array_column($obligations, 'code'), array_column($obligations, 'id'));
|
|
|
+ }
|
|
|
+ public function createOrUpdate(array $param){
|
|
|
+ $obligation=$this->recombineCodeIdArr();
|
|
|
+ $owner=Owner::query()->with('departmentObligationOwner')->find($param['id']);
|
|
|
+ $departmentObligationOwner=$owner->departmentObligationOwner->toArray()??[];
|
|
|
+ $time=Carbon::now()->subMonths(-1)->startOfMonth()->startOfDay()->toDateTimeString();
|
|
|
+ if (empty($departmentObligationOwner)){//第一次新增 部门和职能及货主关联
|
|
|
+ $insert_param=[];
|
|
|
+ $insert_param[]=['department_id'=>$param['kc'],'obligation_id'=>$obligation['kc'],'obligation_code'=>'kc','owner_id'=>$param['id'],'valid_time'=>$time];
|
|
|
+ $insert_param[]=['department_id'=>$param['jg'],'obligation_id'=>$obligation['jg'],'obligation_code'=>'jg','owner_id'=>$param['id'],'valid_time'=>$time];
|
|
|
+ $insert_param[]=['department_id'=>$param['th'],'obligation_id'=>$obligation['th'],'obligation_code'=>'th','owner_id'=>$param['id'],'valid_time'=>$time];
|
|
|
+ $insert_param[]=['department_id'=>$param['sh'],'obligation_id'=>$obligation['sh'],'obligation_code'=>'sh','owner_id'=>$param['id'],'valid_time'=>$time];
|
|
|
+ $insert_param[]=['department_id'=>$param['fh'],'obligation_id'=>$obligation['fh'],'obligation_code'=>'fh','owner_id'=>$param['id'],'valid_time'=>$time];
|
|
|
+ DepartmentObligationOwner::query()->insert($insert_param);
|
|
|
+ }else{//修改货主中的 仓库组, 加工组, 发货组, 收货组, 退货组信息
|
|
|
+ $kc=DepartmentObligationOwner::query()
|
|
|
+ ->where('obligation_id',$obligation['kc'])
|
|
|
+ ->where('owner_id',$param['id'])
|
|
|
+ ->where('valid_time',$time)->first();
|
|
|
+ if ($kc){
|
|
|
+ $kc->update(['department_id'=>$param['kc']]);
|
|
|
+ }else{
|
|
|
+ DepartmentObligationOwner::query()
|
|
|
+ ->orderByDesc('id')
|
|
|
+ ->where('obligation_id',$obligation['kc'])
|
|
|
+ ->where('owner_id',$param['id'])
|
|
|
+ ->update(['failure_time'=>$time]);
|
|
|
+ DepartmentObligationOwner::query()->insert(['department_id'=>$param['kc'],'obligation_id'=>$obligation['kc'],'obligation_code'=>'kc','owner_id'=>$param['id'],'valid_time'=>$time]);
|
|
|
+ }
|
|
|
+ $jg=DepartmentObligationOwner::query()
|
|
|
+ ->where('obligation_id',$obligation['jg'])
|
|
|
+ ->where('owner_id',$param['id'])
|
|
|
+ ->where('valid_time',$time)->first();
|
|
|
+ if ($jg){
|
|
|
+ $jg->update(['department_id'=>$param['jg']]);
|
|
|
+ }else{
|
|
|
+ DepartmentObligationOwner::query()
|
|
|
+ ->orderByDesc('id')
|
|
|
+ ->where('obligation_id',$obligation['jg'])
|
|
|
+ ->where('owner_id',$param['id'])
|
|
|
+ ->update(['failure_time'=>$time]);
|
|
|
+ DepartmentObligationOwner::query()->insert(['department_id'=>$param['jg'],'obligation_id'=>$obligation['jg'],'obligation_code'=>'jg','owner_id'=>$param['id'],'valid_time'=>$time]);
|
|
|
+ }
|
|
|
+
|
|
|
+ $th=DepartmentObligationOwner::query()
|
|
|
+ ->where('obligation_id',$obligation['th'])
|
|
|
+ ->where('owner_id',$param['id'])
|
|
|
+ ->where('valid_time',$time)->first();
|
|
|
+ if ($th){
|
|
|
+ $th->update(['department_id'=>$param['th']]);
|
|
|
+ }else{
|
|
|
+ DepartmentObligationOwner::query()
|
|
|
+ ->orderByDesc('id')
|
|
|
+ ->where('obligation_id',$obligation['th'])
|
|
|
+ ->where('owner_id',$param['id'])
|
|
|
+ ->update(['failure_time'=>$time]);
|
|
|
+ DepartmentObligationOwner::query()->insert(['department_id'=>$param['th'],'obligation_id'=>$obligation['th'],'obligation_code'=>'th','owner_id'=>$param['id'],'valid_time'=>$time]);
|
|
|
+ }
|
|
|
+
|
|
|
+ $sh=DepartmentObligationOwner::query()
|
|
|
+ ->where('obligation_id',$obligation['sh'])
|
|
|
+ ->where('owner_id',$param['id'])
|
|
|
+ ->where('valid_time',$time)->first();
|
|
|
+ if ($sh){
|
|
|
+ $sh->update(['department_id'=>$param['sh']]);
|
|
|
+ }else{
|
|
|
+ DepartmentObligationOwner::query()
|
|
|
+ ->orderByDesc('id')
|
|
|
+ ->where('obligation_id',$obligation['sh'])
|
|
|
+ ->where('owner_id',$param['id'])
|
|
|
+ ->update(['failure_time'=>$time]);
|
|
|
+ DepartmentObligationOwner::query()->insert(['department_id'=>$param['sh'],'obligation_id'=>$obligation['sh'],'obligation_code'=>'sh','owner_id'=>$param['id'],'valid_time'=>$time]);
|
|
|
+ }
|
|
|
+
|
|
|
+ $fh=DepartmentObligationOwner::query()
|
|
|
+ ->where('obligation_id',$obligation['fh'])
|
|
|
+ ->where('owner_id',$param['id'])
|
|
|
+ ->where('valid_time',$time)->first();
|
|
|
+ if ($fh){
|
|
|
+ $fh->update(['department_id'=>$param['fh']]);
|
|
|
+ }else{
|
|
|
+ DepartmentObligationOwner::query()
|
|
|
+ ->orderByDesc('id')
|
|
|
+ ->where('obligation_id',$obligation['fh'])
|
|
|
+ ->where('owner_id',$param['id'])
|
|
|
+ ->update(['failure_time'=>$time]);
|
|
|
+ DepartmentObligationOwner::query()->insert(['department_id'=>$param['fh'],'obligation_id'=>$obligation['fh'],'obligation_code'=>'fh','owner_id'=>$param['id'],'valid_time'=>$time]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Owner::query()->with('departmentObligationOwner')->find($param['id']);
|
|
|
+ }
|
|
|
+}
|