123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- <?php
- namespace App\Service;
- use App\Model\Supplier;
- use App\Model\SupplierInfo;
- use App\Model\Depart;
- use App\Model\Employee;
- use Illuminate\Support\Facades\DB;
- class SupplierService extends Service
- {
- /**
- * 供应商编辑
- * @param $data
- * @param $user
- * @return array
- */
- public function customerEdit($data,$user){
- list($status,$msg) = $this->customerRule($data,$user, false);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- $model = Supplier::where('id',$data['id'])->first();
- $model->title = $data['title'];
- $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
- $model->address2 = $data['address2'] ?? '';
- $model->mark = $data['mark'] ?? '';
- $model->is_main = $data['is_main'] ?? 0;
- $model->save();
- $time = time();
- SupplierInfo::where('del_time',0)
- ->where('supplier_id',$data['id'])
- ->update(['del_time' => $time]);
- if(! empty($data['employee'])){
- $insert = [];
- foreach ($data['employee'] as $value){
- $insert[] = [
- 'supplier_id' => $model->id,
- 'data_id' => $value,
- 'type' => SupplierInfo::type_two,
- 'crt_time' => $time,
- ];
- }
- SupplierInfo::insert($insert);
- }
- if(! empty($data['depart'])){
- $insert = [];
- foreach ($data['depart'] as $value){
- $insert[] = [
- 'supplier_id' => $model->id,
- 'data_id' => $value,
- 'type' => SupplierInfo::type_one,
- 'crt_time' => $time,
- ];
- }
- SupplierInfo::insert($insert);
- }
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true,''];
- }
- /**
- * 供应商新增
- * @param $data
- * @param $user
- * @return array
- */
- public function customerAdd($data,$user){
- list($status,$msg) = $this->customerRule($data,$user);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- $model = new Supplier();
- $model->title = $data['title'];
- $model->mobile = $data['mobile'] ?? "";
- $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
- $model->address2 = $data['address2'] ?? '';
- $model->crt_id = $user['id'];
- $model->mark = $data['mark'] ?? '';
- $model->depart_id = $data['depart_id'] ?? 0;
- $model->top_depart_id = $data['top_depart_id'] ?? 0;
- $model->is_main = $data['is_main'] ?? 0;
- $model->save();
- $time = time();
- if(! empty($data['employee'])){
- $insert = [];
- foreach ($data['employee'] as $value){
- $insert[] = [
- 'supplier_id' => $model->id,
- 'data_id' => $value,
- 'type' => SupplierInfo::type_two,
- 'crt_time' => $time,
- ];
- }
- SupplierInfo::insert($insert);
- }
- if(! empty($data['depart'])){
- $insert = [];
- foreach ($data['depart'] as $value){
- $insert[] = [
- 'supplier_id' => $model->id,
- 'data_id' => $value,
- 'type' => SupplierInfo::type_one,
- 'crt_time' => $time,
- ];
- }
- SupplierInfo::insert($insert);
- }
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true,''];
- }
- /**
- * 供应商删除
- * @param $data
- * @return array
- */
- public function customerDel($data){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- try {
- DB::beginTransaction();
- Supplier::where('id',$data['id'])->update([
- 'del_time'=> time()
- ]);
- SupplierInfo::where('del_time',0)
- ->where('supplier_id',$data['id'])
- ->update(['del_time' => time()]);
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true,''];
- }
- /**
- * 供应商详情
- * @param $data
- * @return array
- */
- public function customerDetail($data){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- $customer = Supplier::where('del_time',0)
- ->where('id',$data['id'])
- ->first();
- if(empty($customer)) return [false,'供应商不存在或已被删除'];
- $customer = $customer->toArray();
- $address = '';
- if(! empty($customer['address1'])) {
- $tmp = json_decode($customer['address1'],true);
- $customer['address1'] = $tmp;
- $tmp = implode(' ',$tmp);
- $tmp .= ' ' . $customer['address2'];
- $address = $tmp;
- }
- $customer['address'] = $address;
- $customer_info = SupplierInfo::where('del_time',0)
- ->where('supplier_id',$customer['id'])
- ->get()->toArray();
- $emp_id = [];
- $emp_id[] = $customer['crt_id'];
- $depart = [];
- foreach ($customer_info as $value){
- if($value['type'] == SupplierInfo::type_two){
- $emp_id[] = $value['data_id'];
- }else{
- $depart[] = $value['data_id'];
- }
- }
- $emp_map = Employee::whereIn('id',array_unique($emp_id))
- ->pluck('emp_name','id')
- ->toArray();
- $depart_map = Depart::whereIn('id',array_unique($depart))
- ->pluck('title','id')
- ->toArray();
- foreach ($customer_info as $value){
- if($value['type'] == SupplierInfo::type_one){
- $tmp = [
- 'id' => $value['data_id'],
- 'name' => $depart_map[$value['data_id']],
- ];
- $customer['depart'][] = $tmp;
- }elseif ($value['type'] == SupplierInfo::type_two){
- $tmp = [
- 'id' => $value['data_id'],
- 'name' => $emp_map[$value['data_id']] ?? '',
- ];
- $customer['employee'][] = $tmp;
- }
- }
- $customer['crt_name'] = $emp_map[$customer['crt_id']] ?? '';
- $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
- return [true, $customer];
- }
- /**
- * 供应商列表
- * @param $data
- * @param $user
- * @return array
- */
- public function customerList($data,$user){
- $model = new Supplier(['userData' => $user,'search'=> $data]);
- $model = $model->where('del_time',0)
- ->select('id','title','address1','address2','mobile','crt_id','crt_time','is_main','mark')
- ->orderby('id', 'asc');
- if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
- $list = $this->limit($model,'',$data);
- $list = $this->fillData($list);
- return [true, $list];
- }
- /**
- * 供应商参数规则
- * @param $data
- * @param $is_add
- * @return array
- */
- public function customerRule(&$data, $user, $is_add = true){
- if(empty($data['title'])) 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 = Supplier::where('del_time',0)
- ->where('title',$data['title'])
- ->where('top_depart_id',$data['top_depart_id'])
- ->exists();
- }else{
- if(empty($data['id'])) return [false,'ID不能为空'];
- $top_depart_id = Supplier::where('id',$data['id'])->value('top_depart_id');
- $bool = Supplier::where('del_time',0)
- ->where('id','<>',$data['id'])
- ->where('top_depart_id',$top_depart_id)
- ->where('title',$data['title'])
- ->exists();
- }
- if($bool) return [false,'供应商名称不能重复'];
- return [true, ''];
- }
- /**
- * 拼接数据
- * @param $data
- * @return array
- */
- public function fillData($data){
- if(empty($data['data'])) return $data;
- $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
- ->pluck('emp_name','id')
- ->toArray();
- foreach ($data['data'] as $key => $value){
- $address = '';
- if(! empty($value['address1'])) {
- $tmp = json_decode($value['address1'],true);
- $tmp = implode(' ',$tmp);
- $tmp .= ' ' . $value['address2'];
- $address = $tmp;
- }
- $data['data'][$key]['address'] = $address;
- $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']] ?? '';
- }
- return $data;
- }
- }
|