| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381 | 
							- <?php
 
- namespace App\Service;
 
- use App\Model\BasicType;
 
- use App\Model\Construction;
 
- use App\Model\ConstructionInfo;
 
- use App\Model\ConstructionProductInfo;
 
- use App\Model\Customer;
 
- use App\Model\Employee;
 
- use App\Model\SalesOrder;
 
- use Illuminate\Support\Facades\DB;
 
- class ConstructionService extends Service
 
- {
 
-     public function constructionEdit($data,$user){
 
-         list($status,$msg) = $this->constructionRule($data, $user, false);
 
-         if(!$status) return [$status,$msg];
 
-         try {
 
-             DB::beginTransaction();
 
-             $model = Construction::where('id', $data['id'])->first();
 
-             $model->model_type = $data['model_type'];
 
-             $model->order_number = $data['order_number'];
 
-             $model->title = $data['title'] ?? '';
 
-             $model->customer_id = $data['customer_id'] ?? 0;
 
-             $model->customer_contact_id = $data['customer_contact_id'] ?? 0;
 
-             $model->install_method = $data['install_method'] ?? 0;
 
-             $model->install_position = $data['install_position'] ?? '';
 
-             $model->sales_order_id = $data['sales_order_id'] ?? 0;
 
-             $model->construction_fee = $data['construction_fee'] ?? 0;
 
-             $model->service_price = $data['service_price'] ?? 0;
 
-             $model->construction_time = $data['construction_time'] ?? 0;
 
-             $model->handover_time = $data['handover_time'] ?? 0;
 
-             $model->urgency = $data['urgency'] ?? 0;
 
-             $model->mark = $data['mark'] ?? '';
 
-             $model->address1 = $data['address1'] ?? '';
 
-             $model->address2 = $data['address2'] ?? '';
 
-             $model->introduction = $data['introduction'] ?? '';
 
-             $model->depart_id = $data['depart_id'] ?? 0;
 
-             $model->top_depart_id = $data['top_depart_id'] ?? 0;
 
-             $model->save();
 
-             $time = time();
 
-             ConstructionInfo::where('del_time',0)
 
-                 ->where('construction_id',$data['id'])
 
-                 ->update(['del_time' => $time]);
 
-             ConstructionProductInfo::where('del_time',0)
 
-                 ->where('construction_id',$data['id'])
 
-                 ->update(['del_time' => $time]);
 
-             if(! empty($data['construction_contact'])){
 
-                 $insert = [];
 
-                 foreach ($data['construction_contact'] as $value){
 
-                     $insert[] = [
 
-                         'construction_id' => $model->id,
 
-                         'contact_type' => $value['id'],
 
-                         'contact_info' => $value['info'],
 
-                         'type' => ConstructionInfo::type_one,
 
-                         'crt_time' => $time,
 
-                     ];
 
-                 }
 
-                 ConstructionInfo::insert($insert);
 
-             }
 
-             if(! empty($data['employee_one'])){
 
-                 $insert = [];
 
-                 foreach ($data['employee_one'] as $value){
 
-                     $insert[] = [
 
-                         'construction_id' => $model->id,
 
-                         'employee_id' => $value,
 
-                         'type' => ConstructionInfo::type_two,
 
-                         'crt_time' => $time,
 
-                     ];
 
-                 }
 
-                 ConstructionInfo::insert($insert);
 
-             }
 
-             if(! empty($data['product'])){
 
-                 $insert = [];
 
-                 foreach ($data['product'] as $value){
 
-                     $insert[] = [
 
-                         'construction_id' => $model->id,
 
-                         'product_id' => $value['product_id'],
 
-                         'price' => $value['price'],
 
-                         'number' => $value['number'],
 
-                         'mark' => $value['mark'] ?? '',
 
-                         'crt_time' => $time,
 
-                     ];
 
-                 }
 
-                 ConstructionProductInfo::insert($insert);
 
-             }
 
-             DB::commit();
 
-         }catch (\Exception $exception){
 
-             DB::rollBack();
 
-             return [false,$exception->getMessage()];
 
-         }
 
-         return [true,''];
 
-     }
 
-     public function constructionAdd($data,$user){
 
-         list($status,$msg) = $this->constructionRule($data,$user);
 
-         if(!$status) return [$status,$msg];
 
-         try {
 
-             DB::beginTransaction();
 
-             $model = new Construction();
 
-             $model->model_type = $data['model_type'];
 
-             $model->order_number = $data['order_number'];
 
-             $model->title = $data['title'] ?? '';
 
-             $model->customer_id = $data['customer_id'] ?? 0;
 
-             $model->customer_contact_id = $data['customer_contact_id'] ?? 0;
 
-             $model->install_method = $data['install_method'] ?? 0;
 
-             $model->install_position = $data['install_position'] ?? '';
 
-             $model->sales_order_id = $data['sales_order_id'] ?? 0;
 
-             $model->construction_fee = $data['construction_fee'] ?? 0;
 
-             $model->service_price = $data['service_price'] ?? 0;
 
-             $model->construction_time = $data['construction_time'] ?? 0;
 
-             $model->handover_time = $data['handover_time'] ?? 0;
 
-             $model->urgency = $data['urgency'] ?? 0;
 
-             $model->mark = $data['mark'] ?? '';
 
-             $model->address1 = $data['address1'] ?? '';
 
-             $model->address2 = $data['address2'] ?? '';
 
-             $model->introduction = $data['introduction'] ?? '';
 
-             $model->crt_id = $user['id'];
 
-             $model->depart_id = $data['depart_id'] ?? 0;
 
-             $model->top_depart_id = $data['top_depart_id'] ?? 0;
 
-             $model->save();
 
-             $time = time();
 
-             if(! empty($data['construction_contact'])){
 
-                 $insert = [];
 
-                 foreach ($data['construction_contact'] as $value){
 
-                     $insert[] = [
 
-                         'construction_id' => $model->id,
 
-                         'contact_type' => $value['id'],
 
-                         'contact_info' => $value['info'],
 
-                         'type' => ConstructionInfo::type_one,
 
-                         'crt_time' => $time,
 
-                     ];
 
-                 }
 
-                 ConstructionInfo::insert($insert);
 
-             }
 
-             if(! empty($data['employee_one'])){
 
-                 $insert = [];
 
-                 foreach ($data['employee_one'] as $value){
 
-                     $insert[] = [
 
-                         'construction_id' => $model->id,
 
-                         'employee_id' => $value,
 
-                         'type' => ConstructionInfo::type_two,
 
-                         'crt_time' => $time,
 
-                     ];
 
-                 }
 
-                 ConstructionInfo::insert($insert);
 
-             }
 
-             if(! empty($data['product'])){
 
-                 $insert = [];
 
-                 foreach ($data['product'] as $value){
 
-                     $insert[] = [
 
-                         'construction_id' => $model->id,
 
-                         'product_id' => $value['product_id'],
 
-                         'price' => $value['price'],
 
-                         'number' => $value['number'],
 
-                         'mark' => $value['mark']?? '',
 
-                         'crt_time' => $time,
 
-                     ];
 
-                 }
 
-                 ConstructionProductInfo::insert($insert);
 
-             }
 
-             DB::commit();
 
-         }catch (\Exception $exception){
 
-             DB::rollBack();
 
-             return [false,$exception->getMessage()];
 
-         }
 
-         return [true,''];
 
-     }
 
-     public function constructionDel($data){
 
-         if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
 
-         try {
 
-             DB::beginTransaction();
 
-             Construction::whereIn('id',$data['id'])->update([
 
-                 'del_time'=> time()
 
-             ]);
 
-             ConstructionInfo::where('del_time',0)
 
-                 ->where('construction_id',$data['id'])
 
-                 ->update(['del_time' => time()]);
 
-             ConstructionProductInfo::where('del_time',0)
 
-                 ->where('construction_id',$data['id'])
 
-                 ->update(['del_time' => time()]);
 
-             DB::commit();
 
-         }catch (\Exception $exception){
 
-             DB::rollBack();
 
-             return [false,$exception->getMessage()];
 
-         }
 
-         return [true,''];
 
-     }
 
-     public function constructionDetail($data){
 
-         if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
 
-         $construction = Construction::where('del_time',0)
 
-             ->where('id',$data['id'])
 
-             ->first();
 
-         if(empty($construction)) return [false,'施工订单不存在或已被删除'];
 
-         $construction = $construction->toArray();
 
-         $sales = SalesOrder::where('id',$construction['sales_order_id'])->value('order_number');
 
-         $construction['sales_order_number'] = $sales;
 
-         $customer_title = Customer::where('id',$construction['customer_id'])->value('title');
 
-         $construction['customer_title'] = $customer_title;
 
-         $emp_title = Employee::where('id',$construction['customer_contact_id'])->value('emp_name');
 
-         $construction['customer_contact_title'] = $emp_title;
 
-         $construction['employee_one'] = $construction['construction_contact'] = $construction['product'] = [];
 
-         $array = [
 
-             $construction['install_method'],
 
-             $construction['urgency'],
 
-         ];
 
-         $basic_map = BasicType::whereIn('id',$array)
 
-             ->pluck('title','id')
 
-             ->toArray();
 
-         $construction = [$construction];
 
-         foreach ($construction as $key => $value){
 
-             $construction[$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
 
-             $construction[$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
 
-         }
 
-         $construction = $construction[0];
 
-         $construction_info = ConstructionInfo::where('del_time',0)
 
-             ->where('construction_id',$construction['id'])
 
-             ->select('id','construction_id','employee_id','type','contact_type','contact_info')
 
-             ->get()->toArray();
 
-         $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive([$construction['crt_id']],array_column($construction_info,'employee_id'))))
 
-             ->pluck('emp_name','id')
 
-             ->toArray();
 
-         $basic_map2 = BasicType::whereIn('id',array_unique(array_column($construction_info,'contact_type')))
 
-             ->pluck('title','id')
 
-             ->toArray();
 
-         foreach ($construction_info as $value){
 
-             if($value['type'] == ConstructionInfo::type_one){
 
-                 $tmp = [
 
-                     'id' => $value['contact_type'],
 
-                     'title' => $basic_map2[$value['contact_type']] ?? '',
 
-                     'info' => $value['contact_info']
 
-                 ];
 
-                 $construction['customer_contact'][] = $tmp;
 
-             }elseif ($value['type'] == ConstructionInfo::type_two){
 
-                 $tmp = [
 
-                     'id' => $value['employee_id'],
 
-                     'name' => $emp_map[$value['employee_id']],
 
-                 ];
 
-                 $construction['employee_one'][] = $tmp;
 
-             }
 
-         }
 
-         $p_info = ConstructionProductInfo::where('del_time',0)
 
-             ->where('construction_id',$construction['id'])
 
-             ->select('id','construction_id','product_id','mark','price','number')
 
-             ->get()->toArray();
 
-         foreach ($p_info as $value){
 
-             $construction['product'][] = [
 
-                 'product_id' => $value['product_id'],
 
-                 'mark' => $value['mark'],
 
-                 'price' => $value['price'],
 
-                 'number' => $value['number'],
 
-             ];
 
-         }
 
-         $construction['crt_name'] = $emp_map[$construction['crt_id']] ?? '';
 
-         $construction['crt_time'] = $construction['crt_time'] ? date("Y-m-d H:i:s",$construction['crt_time']): '';
 
-         return [true, $construction];
 
-     }
 
-     public function constructionList($data,$user){
 
-         $model = new Construction(['userData' => $user]);
 
-         $model = $model->where('del_time',0)
 
-             ->select('title','id','model_type','order_number','customer_id','customer_contact_id','install_method','install_position','sales_order_id','construction_fee','construction_time','handover_time','urgency','crt_id','crt_time','mark','state','address1','address2','introduction','service_price')
 
-             ->orderby('id', 'desc')
 
-             ->where('model_type',$data['model_type']);
 
-         if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
 
-         $list = $this->limit($model,'',$data);
 
-         $list = $this->fillData($list);
 
-         return [true, $list];
 
-     }
 
-     public function constructionRule(&$data, $user, $is_add = true){
 
-         if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
 
-         if(! in_array($data['model_type'],Construction::$model_type)) return [false,'工单模板类型错误'];
 
-         if(empty($data['order_number'])) return [false,'工单编号不能为空'];
 
-         if(empty($data['construction_time'])) return [false,'实施日期不能为空'];
 
-         $data['construction_time'] = $this->changeDateToDateMin($data['construction_time']);
 
-         if(empty($data['product'])) return [false,'请选择产品'];
 
-         foreach ($data['product'] as $value){
 
-             if(empty($value['number'])) return [false,'产品数量不能为空'];
 
-             $res = $this->checkNumber($value['number']);
 
-             if(! $res) return [false,'请输入正确的产品数量'];
 
-             if(empty($value['price'])) return [false,'产品价格不能为空'];
 
-             $res = $this->checkNumber($value['price']);
 
-             if(! $res) return [false,'产品价格请输入不超过两位小数并且大于0的数值'];
 
-         }
 
-         if(empty($data['construction_contact'])) return [false,'联系方式不能为空'];
 
-         if(! empty($data['construction_fee'])){
 
-             $res = $this->checkNumber($data['construction_fee']);
 
-             if(! $res) return [false,'施工费用请输入不超过两位小数并且大于0的数值'];
 
-         }
 
-         if(! empty($data['service_price'])){
 
-             $res = $this->checkNumber($data['service_price']);
 
-             if(! $res) return [false,'服务价格请输入不超过两位小数并且大于0的数值'];
 
-         }
 
-         if($data['model_type'] == Construction::Model_type_one){
 
-             if(empty($data['install_method'])) return [false,'安装方式不能为空'];
 
-             if(empty($data['install_position'])) return [false,'安装地点不能为空'];
 
-         }else{
 
-             if(empty($data['address1']) || empty($data['address2'])) return [false,'地址不能为空'];
 
-         }
 
-         //所属部门 以及  顶级部门
 
-         if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
 
-         $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
 
-         if($is_add){
 
-             $bool = Construction::where('del_time',0)->where('order_number',$data['order_number'])->exists();
 
-             if($bool) return [false,'工单编号已存在,请重新获取'];
 
-         }else{
 
-             if(empty($data['id'])) return [false,'ID不能为空'];
 
-         }
 
-         return [true, $data];
 
-     }
 
-     public function fillData($data){
 
-         if(empty($data['data'])) return $data;
 
-         $array = array_unique(array_merge_recursive(array_column($data['data'],'install_method'),array_column($data['data'],'urgency')));
 
-         $basic_map = BasicType::whereIn('id',$array)
 
-             ->pluck('title','id')
 
-             ->toArray();
 
-         $emp = Employee::whereIn('id',array_unique(array_merge_recursive(array_column($data['data'],'crt_id'),array_column($data['data'],'customer_contact_id'))))
 
-             ->pluck('emp_name','id')
 
-             ->toArray();
 
-         $customer = Customer::whereIn('id',array_unique(array_column($data['data'],'customer_id')))
 
-             ->pluck('title','id')
 
-             ->toArray();
 
-         foreach ($data['data'] as $key => $value){
 
-             $data['data'][$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
 
-             $data['data'][$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
 
-             $data['data'][$key]['customer_title'] = $customer[$value['customer_id']] ?? '';
 
-             $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
 
-             $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
 
-             $data['data'][$key]['customer_contact_title'] = $emp[$value['customer_contact_id']] ?? '';
 
-         }
 
-         return $data;
 
-     }
 
-     public function constructionGet($data){
 
-         if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
 
-         if(! isset(Construction::$prefix[$data['model_type']])) return [false,'工单模板类型错误'];
 
-         $prefix = Construction::$prefix[$data['model_type']];
 
-         $order_number = OrderNoService::createConstructionOrderNumber($prefix);
 
-         if(! $order_number) return [false,'工单编号生成失败!'];
 
-         return [true,['order_number' => $order_number]];
 
-     }
 
- }
 
 
  |