| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 | <?phpnamespace App\Service;use App\Jobs\ProcessDataJob;use App\Model\ProductSnInfo;use App\Model\PurchaseOrder;use App\Model\Setting;use App\Model\U8Job;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 getSnFromU8($data, $user){        if(empty($data['sn_type'])) return [false, 'sn码来源依据不能为空'];        if(empty($data['code'])) return [false, '产品编码不能为空'];        list($status,$return) = $this->getSnList($data, $user);        return [$status, $return];    }    public function getSnList($data, $user){        $header = ['Content-Type:application/json'];        $post = [            'urlFromT9' => 'getSnList',            'code' => $data['code'],            'sn' => $data['sn'] ?? "",            'sn_type' => $data['sn_type'],            'depart_title' => $this->getMyTopDepart($user,true),            '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($msg['code'] != 200) return [false, $msg['msg']];        return [true, $msg['data'] ?? []];    }    //校验sn码 施工单    public function checkSnConstructionRule($data){        $code = $sn = [];        foreach ($data['product'] as $value){            //没有sn码信息直接返回            if(empty($value['product_sn_info'])) continue;            if(count($value['product_sn_info']) > $value['number']) return [false, "产品编码:" . $value['code'] . "选择的sn码数量不能超过产品数量"];            if(empty($value['code'])) return [false, '产品编码不能为空'];            $code[] = $value['code'];            foreach ($value['product_sn_info'] as $sn_val){                $sn[] = $sn_val;            }        }        if(empty($sn)) return [true, ''];        $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($msg['code'] != 200) return [false, $msg['msg']];        //sn码        $sn_list = $msg['data'];        $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序列码:" . $sn_val . "在用友中不存在"];            }        }        $save = ProductSnInfo::where('del_time',0)            ->whereIn("code", $code)            ->whereIn('sn', $sn)            ->select('code','sn','data_id','type')            ->get()->toArray();        $construction_id = $data['id'] ?? 0;        foreach ($save as $value){            $key = $value['code'] . $value['sn'];            if(in_array($key, $submit_info)) {                if(! $construction_id){                    return [false, '产品编码:' . $value['code'] . '的sn码已被' .ProductSnInfo::$type_name[$value['type']]] . '使用';                }else{                    if($value['data_id'] != $construction_id || $value['type'] != ProductSnInfo::type_one) return [false, '产品编码:' . $value['code'] . '的sn码已被' .ProductSnInfo::$type_name[$value['type']]] . '使用';                }            }        }        return [true, ''];    }    //保存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]);        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,                        'crt_time' => $time,                        'data_id' => $data_id,                        'type' => $type,                    ];                }            }            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')            ->get()->toArray();        foreach ($sn as $value){            $map[$value['code']][] = $value['sn'];        }        return $map;    }}
 |