| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429 | <?phpnamespace App\Service;use App\Jobs\ProcessDataJob;use App\Model\Construction;use App\Model\Product;use App\Model\ProductCategory;use App\Model\ProductSnInfo;use App\Model\PurchaseOrder;use App\Model\Setting;use App\Model\U8Job;use App\Model\Warranty;class DataSyncToU8Service extends Service{    public function add($data,$user){        list($status,$msg) = $this->orderRule($data);        if(!$status) return [$status,$msg];        //操作人        $data['user_name'] = $user['emp_name'];//        dd((new U8ServerService())->U8PO_PomainSave($data['id']));        try{            $job = ProcessDataJob::dispatch($data)->onQueue($data['job']);            if(! $job) return [false,'任务没有进入队列!'];        }catch (\Throwable $e){            return [false,$e->getMessage()];        }        return [true,''];    }    public function orderRule(&$data){        if(empty($data['type'])) return [false,'type不能为空!'];        if(! in_array($data['type'],U8Job::$type)) return [false,'type不能存在!'];        if(empty($data['id'])) return [false,'同步数据不能为空!'];        $data['job'] = U8Job::$job[$data['type']] ?? "";        if(empty($data['job'])) return [false,'未找到同步任务!'];        if($data['type'] == U8Job::one){            //采购同步校验            $bool = PurchaseOrder::whereIn('id',$data['id'])                ->where('del_time',0)                ->where('supplier',0)                ->exists();            if($bool) return [false,'同步的采购单供应商不能为空!'];        }        return [true, ''];    }    public function snListAccording($data,$user){        //特殊的门店        $setting = Setting::where('setting_name','bt_top_depart_id')->first();        $bt_top_depart_id = $setting['setting_value'] ?? [];        $bt_top_depart_id = json_decode($bt_top_depart_id,true);        //当前门店        $current_top_depart_id = $user['depart_top'][0] ?? [];        $current_top_depart_id = $current_top_depart_id['depart_id'] ?? 0;        if(in_array($current_top_depart_id, $bt_top_depart_id)){            //总社 直接读取sn码表            $sn_type = 1;//总社        }else{            $sn_type = 2;//分社            //发货单 sn码        }        return [true, ['sn_type' => $sn_type]];    }    public function snListAccordingForOrder($data_id){        //特殊的门店        $setting = Setting::where('setting_name','bt_top_depart_id')->first();        $bt_top_depart_id = $setting['setting_value'] ?? [];        $bt_top_depart_id = json_decode($bt_top_depart_id,true);        //当前门店        $current_top_depart_id = Construction::where('id', $data_id)->value('top_depart_id');        if(in_array($current_top_depart_id, $bt_top_depart_id)){            //总社 直接读取sn码表            $sn_type = 1;//总社        }else{            $sn_type = 2;//分社            //发货单 sn码        }        return $sn_type;    }    public function getSnFromU8($data, $user){        if(empty($data['sn_type'])) return [false, 'sn码来源依据不能为空'];        if(empty($data['code'])) return [false, '产品编码不能为空'];        $data['depart_title'] = $this->getMyTopDepart($user,true);        $service = new U8ServerService(true);        if(! empty($service->error)) return [false, $service->error];        list($status,$return) = $service->getSnList($data, $user);//        list($status,$return) = $this->getSnList($data, $user);        return [$status, $return];    }    //todo 废弃一    public function getSnList($data, $user){        $header = ['Content-Type:application/json'];        $construction_id = $data['construction_id'] ?? 0;        $post = [            'urlFromT9' => 'getSnList',            'code' => $data['code'],            'sn' => $data['sn'] ?? "",            'sn_type' => $data['sn_type'],            'depart_title' => $this->getMyTopDepart($user,true),            'construction_id' => $construction_id,            'page_size' => $data['page_size'] ?? 10,            'page_index' => $data['page_index'] ?? 1,        ];        $post = json_encode($post);        list($status, $msg) = $this->post_helper("https://workapi.qingyaokeji.com/api/getSnList", $post, $header);        if(! $status) return [false, "用友数据获取失败"];        if($msg['code'] != 200) return [false, $msg['msg']];        return [true, $msg['data'] ?? []];    }    /**     * 校验sn码 施工单     * @param $data     * @param false $is_edit 是否补录     * @return array|string     */    public function checkSnConstructionRule(&$data){        //产品字典        $map = (new ProductService())->getProductDetail(array_column($data['product'],'product_id'));        $data_id = $data['id'] ?? 0;        $sn_map = [];        if($data_id) $sn_map = $this->getSn($data, ProductSnInfo::type_one);        $code = $sn = [];        foreach ($data['product'] as $value){            if(empty($value['code'])) return [false, '产品编码不能为空'];            $code[] = $value['code'];            //校验是否有不需要剔除的sn            if($data_id){                $sn_tmp = $sn_map[$value['code']] ?? [];                foreach ($sn_tmp as $val){                    if(! empty($val['warranty_id']) && ! in_array($val['sn'], $value['product_sn_info'])) return [false,  "产品编码:" . $value['code'] . "选择的sn码". $val['sn'] . "已生成质保信息,不允许删除"];                }            }            //没有sn码信息直接跳过            if(empty($value['product_sn_info'])) continue;            $tmp = $map[$value['product_id']] ?? [];            if(empty($tmp['warranty_time'])) return [false, "产品编码:" . $value['code'] . "质保时长(月)暂未设置"];            $bool = $this->forCheck($tmp['product_category']);            //非车窗膜需校验sn码            if(! $bool){                if(count($value['product_sn_info']) > $value['number']) return [false, "产品编码:" . $value['code'] . "选择的sn码数量不能超过产品数量"];            }            foreach ($value['product_sn_info'] as $sn_val){                $sn[] = $sn_val;            }        }        if(empty($sn)) return [true, ''];        if(empty($data['sn_for_u8'])) return [false, "校验用友数据信息不能为空"];        foreach ($data['sn_for_u8'] as $key => $value){            if(empty($value['auto_id'])) return [false, 'AutoID不能为空'];            if(empty($value['product_id'])) return [false, '产品ID不能为空'];            $tmp = $map[$value['product_id']] ?? [];            $bool = $this->forCheck($tmp['product_category']);            if($bool) unset($data['sn_for_u8'][$key]);        }        //获取在库的sn码信息        list($status, $msg) = $this->getSnForMap($code, $sn);        if(! $status) return [false, $msg];        //sn码map        $sn_list = $msg;        $sn_map = [];        foreach ($sn_list as $value){            $key = $value['code'] . $value['sn'];            $sn_map[$key] = "";        }        //校验用友        $submit_info = [];        foreach ($data['product'] as $value){            if(empty($value['product_sn_info'])) continue;            foreach ($value['product_sn_info'] as $sn_val){                $key = $value['code'] . $sn_val;                $submit_info[] = $key;                if(! isset($sn_map[$key])) return [false, "产品编码:" . $value['code'] . "的产品序列码:" . $sn_val . "在用友中不存在"];            }        }        //校验sn是否被占用        list($status, $msg) = $this->snForCheck($code, $sn, $submit_info, $data);        if(! $status) return [false, $msg];        return [true, ''];    }    public function forCheck($product_category){        $category = json_decode($product_category,true);        if(in_array(ProductCategory::Special_for_sn, $category)) return true;        return false;    }    public function forCheck2($product_category){        $category = json_decode($product_category,true);        if(in_array(ProductCategory::Special_for_roll, $category)) return true;        return false;    }    //保存sn    public function saveSn($data, $type, $time){        $data_id = $data['id'] ?? 0;        ProductSnInfo::where('del_time',0)            ->where('data_id', $data_id)            ->where('type', $type)            ->update(['del_time' => $time]);        $sn_type = $this->snListAccordingForOrder($data_id);        $update = [];        if(! empty($data['product'])){            $insert = [];            foreach ($data['product'] as $value){                //没有sn码信息直接返回                if(empty($value['product_sn_info'])) continue;                foreach ($value['product_sn_info'] as $sn_val){                    $insert[] = [                        'product_id' => $value['product_id'],                        'code' => $value['code'],                        'sn' => $sn_val['sn'],                        'auto_id' => $sn_val['auto_id'],                        'crt_time' => $time,                        'data_id' => $data_id,                        'type' => $type,                        'sn_type' => $sn_type,                    ];                    if(! empty($sn_val['for_update'])) $update[] = $sn_val['auto_id'];                }            }            if(! empty($update)) $this->updateSnInfo($sn_type, $update,$data_id);            if(! empty($insert)) ProductSnInfo::insert($insert);        }    }    //获取sn详情    public function getSn($data, $type){        $data_id = $data['id'] ?? 0;        $map = [];        $sn = ProductSnInfo::where('del_time',0)            ->where('data_id', $data_id)            ->where('type', $type)            ->select('product_id','code','sn','warranty_id')            ->get()->toArray();        foreach ($sn as $value){            $map[$value['code']][] = [                'sn' => $value['sn'],                'warranty_id' => $value['warranty_id'],            ];        }        return $map;    }    //获取在库的sn码信息通过sn码和产品编码    public function getSnForMap($code, $sn){        $service = new U8ServerService(true);        if(! empty($service->error)) return [false, $service->error];        list($status, $msg) = $service->getSnListFormU8ForMap(['code' => $code, 'sn' => $sn]);        return [$status, $msg];        // todo 废弃二        $header = ['Content-Type:application/json'];        $post = [            'urlFromT9' => 'getSnMap',            'code' => $code,            'sn' => $sn,        ];        $post = json_encode($post);        list($status, $msg) = $this->post_helper("https://workapi.qingyaokeji.com/api/getSnforMap", $post, $header);        if(! $status) return [false, "用友数据获取失败"];        if($msg['code'] != 200) return [false, $msg['msg']];        return [true, $msg['data'] ?? []];    }    //获取在库的sn码信息通过sn码 客户手动创建质保    public function getSnForWarranty($data){        if(empty($data['sn'])) return [false, "产品序列码不能为空"];        list($status, $msg) = $this->snForWarranty($data);        if(! $status) return [false, $msg];        $return = $msg;        $product = Product::where('del_time',0)            ->where('code', $return['code'])            ->select('id','title','code','warranty_time')            ->first();        if(empty($product)) return [false, '根据产品序列码查找到的产品编码:' . $return['code'] . '在门店系统中未找到该产品'];        $product = $product->toArray();        $product['sn'] = $data['sn'];        $product['auto_id'] = $return['auto_id'] ?? 0;        return [true, $product];    }    //更新sn信息    public function updateSnInfo($sn_type, $sn_for_u8,$data_id){        $service = new U8ServerService(true);        if(! empty($service->error)) return [false, $service->error];        list($status, $msg) = $service->saveSnUseDataDetail($sn_type, $sn_for_u8, $data_id);        return [$status, $msg];        //todo 废弃三        $header = ['Content-Type:application/json'];        $post = [            'urlFromT9' => 'saveSnUseData',            'sn_type' => $sn_type,            'sn_for_u8' => $sn_for_u8,            'data_id' => $data_id,        ];        $post = json_encode($post);        list($status, $msg) = $this->post_helper("https://workapi.qingyaokeji.com/api/saveSnUseData", $post, $header);        if(! $status) return [false, "用友数据获取失败"];        if($msg['code'] != 200) return [false, $msg['msg']];        return [true, ''];    }    public function snForWarranty($data){        $service = new U8ServerService(true);        if(! empty($service->error)) return [false, $service->error];        list($status, $msg) = $service->getSnForWarrantyData($data);        if(! $status) return [false, $msg];        if(empty($msg)) return [false, '产品序列码不存在'];        return [$status, $msg];        //todo 废弃四        $header = ['Content-Type:application/json'];        $post = [            'urlFromT9' => 'getSnForWarranty',            'sn' => $data['sn'],        ];        $post = json_encode($post);        list($status, $msg) = $this->post_helper("https://workapi.qingyaokeji.com/api/getSnForWarranty", $post, $header);        if(! $status) return [false, "用友数据获取失败"];        if($msg['code'] != 200) return [false, $msg['msg']];        $return = $msg['data'] ?? [];        if(empty($return)) return [false, '产品序列码不存在'];        return [true, $return];    }    public function hasWarrantyInfo($data){        if(empty($data['customer_contact'])) return [false, "手机号码不能为空"];        $bool = Warranty::where('del_time',0)            ->where('customer_contact', $data['customer_contact'])            ->exists();        return [true, ['hasWarrantyInfo' => $bool]];    }    //校验sn是否被占用    public function snForCheck($code = [], $sn = [], $submit_info = [], $data = []){        $save = ProductSnInfo::where('del_time',0)            ->whereIn("code", $code)            ->whereIn('sn', $sn)            ->select('code','sn','data_id','type','product_id','warranty_id')            ->get()->toArray();        $category_map = Product::whereIn('id',array_unique(array_column($save,'product_id')))            ->pluck('product_category','id')            ->toArray();        $data_id = $data['id'] ?? 0;        $data_type = $data['data_type'] ?? ProductSnInfo::type_one;        foreach ($save as $value){            //车窗膜不管sn使用次数            $category = $category_map[$value['product_id']] ?? "";            $bool = $this->forCheck($category);            if($bool) continue;            $key = $value['code'] . $value['sn'];            if(in_array($key, $submit_info)) {                if(! $data_id){                    return [false, '产品编码:' . $value['code'] . '的sn码已在(' .ProductSnInfo::$type_name[$value['type']]. ')中使用'];                }else{                    if($value['type'] == ProductSnInfo::type_three){                        if($value['warranty_id'] != $data_id || $value['type'] != $data_type) return [false, '产品编码:' . $value['code'] . '的sn码已被' .ProductSnInfo::$type_name[$data_type] . '使用'];                    }else{                        if($value['data_id'] != $data_id || $value['type'] != $data_type) return [false, '产品编码:' . $value['code'] . '的sn码已被' .ProductSnInfo::$type_name[$data_type] . '使用'];                    }                }            }        }        return [true, ''];    }    public function updateByVinNo($data){        if(empty($data['vin_no']) || empty($data['customer_name']) || empty($data['customer_contact'])) return;        $vin_no = $data['vin_no'];        $customer_name = $data['customer_name'];        $customer_contact = $data['customer_contact'];        Warranty::where('del_time',0)            ->where('vin_no',$vin_no)            ->update(["customer_name" => $customer_name, 'customer_contact' => $customer_contact]);    }}
 |