| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110 | <?phpnamespace App\Service;use App\Model\BasicType;use App\Model\Construction;use App\Model\ConstructionProductInfo;use App\Model\Customer;use App\Model\CustomerInfo;use App\Model\Product;use App\Model\ProductCategory;use App\Model\ProductSnInfo;use App\Model\ReturnExchangeOrder;use App\Model\ReturnExchangeOrderProductInfo;use App\Model\SalesOrder;use App\Model\SalesOrderProductInfo;use App\Model\TSpaceSet;use App\Model\Warranty;use Illuminate\Support\Facades\DB;class TSpaceService extends Service{    public function add($data, $user){        return [true, ''];    }    public function edit($data, $user){        list($status, $msg) = $this->constructionEditOtherRule($data, $user);        if(! $status) return [false, $msg];        DB::beginTransaction();        try{            $set = TSpaceSet::where('del_time',0)                ->where('type', $data['type'])                ->select('file', 'id')                ->get()->toArray();            $set_map = array_column($set,'file','id');            $time = time();            $insert = $update = $new = $old = $id = [];            foreach ($data['data'] as $value){                $text = "";                if(! empty($value['text'])) $text = json_encode($value['text']);                if(! empty($value['id'])){                    $file = $set_map[$value['id']] ?? "";                    if($value['file'] != $file){                        $old[] = $file;                        $new[] = $value['file'];                    }                    $update[] = [                        'id' => $value['id'],                        'type' => $data['type'],                        'text' => $text,                        'file' => $value['file'],                        'crt_time' => $time,                    ];                    $id[] = $value['id'];                }else{                    $new[] = $value['file'];                    $insert[] = [                        'type' => $data['type'],                        'text' => $text,                        'file' => $value['file'],                        'crt_time' => $time,                    ];                }            }            foreach ($set as $value){                if(! in_array($value['id'], $id)){                    $update[] = [                        'id' => $value['id'],                        'del_time' => $time,                    ];                    $old[] = $value['file'];                }            }            if(! empty($update)){                foreach ($update as $value){                    TSpaceSet::where('id',$value['id'])                        ->update($value);                }            }            if(! empty($insert)) TSpaceSet::insert($insert);            DB::commit();        }catch (\Exception $exception){            DB::rollBack();            return [false, $exception->getMessage()];        }        return [true, ['file' => ['new' => $new, 'old' => $old]]];    }    public function constructionEditOtherRule($data,$user){        if(empty($data['type'])) return [false,'TSpace首页设置类型不能为空'];        if(! isset(TSpaceSet::$model_type_title[$data['type']])) return [false,'TSpace首页设置类型错误'];        if(empty($data['data'])) return [false, '设置内容不能为空'];        foreach ($data['data'] as $value){            if(empty($value['file'])) return [false, '文件不能为空'];        }        return [true, ''];    }    public function detail($data, $user){        if(empty($data['type'])) return [false,'TSpace首页设置类型不能为空'];        if(! isset(TSpaceSet::$model_type_title[$data['type']])) return [false,'TSpace首页设置类型错误'];        $set = TSpaceSet::where('del_time',0)            ->where('type', $data['type'])            ->select('file', 'id', 'text')            ->get()->toArray();        $fileUploadService = new FileUploadService();        foreach ($set as $key => $value){            if(! empty($value['text'])) $set[$key]['text'] = json_decode($value['text'], true);            $url = "";            if(! empty($value['file'])) $url = $fileUploadService->getFileShow($value['file']);            $set[$key]['file_url'] = $url;        }        return [true, $set];    }    public function tSpacelist($data){        if(empty($data['type'])){            $type = [];        }else{            if(! is_array($data['type'])){                $type = [$data['type']];            }else{                $type = $data['type'];            }        }        $set = TSpaceSet::where('del_time',0)            ->when(! empty($type), function ($query) use ($type) {                return $query->whereIn('type',$type);            })            ->select('file', 'id', 'text', 'type')            ->get()->toArray();        $fileUploadService = new FileUploadService();        foreach ($set as $key => $value){            if(! empty($value['text'])) $set[$key]['text'] = json_decode($value['text'], true);            $url = "";            if(! empty($value['file'])) $url = $fileUploadService->getFileShow($value['file']);            $set[$key]['file_url'] = $url;        }        return [true, $set];    }    public function warrantyGetProduct($data, $user){        if(empty($data['sale_order_id'])) return [false, '合同ID不能为空'];        $return = $this->warrantyGetProductList($data, $user);        return [true, $return];    }    public function warrantyGetProductList($data, $user){        $return = [];        $data_id = $data['sale_order_id'];        //销售订单对应产品总数        $product = SalesOrderProductInfo::from('sales_order_product_info as a')            ->join('product as b','a.product_id','b.id')            ->where('a.del_time',0)            ->where('a.sales_order_id', $data_id)            ->where('b.warranty_time', '>', 0)            ->select('a.number','a.product_id','b.product_category')            ->get()->toArray();        if(empty($product)) return $return;        $product_id = array_column($product,'product_id');        $map = (new ProductService())->getProductDetail($product_id);        //合同质保产品        $sn_product = ProductSnInfo::where("del_time",0)            ->where('data_id', $data_id)            ->where('type',ProductSnInfo::type_two)            ->whereIn('product_id',$product_id)            ->select('product_id')            ->get()->toArray();        $product_map1 = [];        foreach ($sn_product as $value){            $key = $value['product_id'];            if(isset($product_map1[$key])){                $number = bcadd(1, $product_map1[$key],2);                $product_map1[$key] = $number;            }else{                $product_map1[$key] = 1;            }        }        //合同退货产品        $product_map2 = [];        $return_id = ReturnExchangeOrder::where('del_time',0)            ->where('type', ReturnExchangeOrder::Order_type)            ->where('model_type', ReturnExchangeOrder::Model_type_one)            ->where('data_id', $data_id)            ->select('id')            ->get()->toArray();        $save2 = ReturnExchangeOrderProductInfo::where('del_time',0)            ->whereIn('return_exchange_id', array_column($return_id,'id'))            ->where('return_or_exchange',ReturnExchangeOrderProductInfo::type_one)            ->select('number', 'return_exchange_id','product_id')            ->get()->toArray();        foreach ($save2 as $value){            $key = $value['product_id'];            if(isset($product_map2[$key])){                $number = bcadd($value['number'], $product_map2[$key],2);                $product_map2[$key] = $number;            }else{                $product_map2[$key] = $value['number'];            }        }        $service = new DataSyncToU8Service();        foreach ($product as $value){            $bool = $service->forCheck($value['product_category']);            //车窗膜不管数量算一个            if($bool) $value['number'] = 1;            //合同质保产品            $p1 = $product_map1[$value['product_id']] ?? 0;            //合同退货产品            $p2 = $product_map2[$value['product_id']] ?? 0;            $number = bcsub($value['number'], $p1,2);            $number = bcsub($number, $p2,2);            if($number <= 0) continue;            $tmp = $map[$value['product_id']] ?? [];            $return[] = [                'number' => $number, //可出数量                'product_id' => $value['product_id'],                'title' => $tmp['title'] ?? "",                'code' => $tmp['code'] ?? "",                'size' => $tmp['size'] ?? "",                'unit' => $tmp['unit'] ?? "",            ];        }        return $return;    }    public function needSnFix($data, $user){        if(empty($data['id'])) return [false, '施工单id不能为空'];        //产品        $p_info = ConstructionProductInfo::where('del_time',0)            ->where('construction_id',$data['id'])            ->get()->toArray();        $basic_price = BasicType::whereIn('id',array_unique(array_column($p_info,'basic_type_id')))->pluck('title','id')->toArray();        $map = (new ProductService())->getProductDetail(array_column($p_info,'product_id'));        $product = [];        //sn码信息        $sn_map = (new DataSyncToU8Service())->getSn($data, ProductSnInfo::type_one);        $service = new DataSyncToU8Service();        $needSnFix = false;        foreach ($p_info as $value){            $tmp = $map[$value['product_id']] ?? [];            $value['title'] = $tmp['title'] ?? "";            $value['code'] = $tmp['code'] ?? "";            $value['size'] = $tmp['size'] ?? "";            $value['unit'] = $tmp['unit'] ?? "";            $value['bar_code'] = $tmp['bar_code'] ?? "";            $value['basic_type_title'] = $basic_price[$value['basic_type_id']] ?? "";            $s_t = $sn_map[$value['code']] ?? [];            $sn = array_column($s_t,'sn');            $value['product_sn_info'] = $sn;            $bool = $service->forCheck($tmp['product_category']);            $count = count($sn);            if($bool){                //如果车窗膜已经有sn码 则跳过                if($count > 0) {                    $product_type_for_sn = 0;                }else{                    if(! $needSnFix) $needSnFix = true;                    $product_type_for_sn = 1;                }            }else{                $bool = $service->forCheck2($tmp['product_category']);                if($bool){                    //如果除车窗膜外已经有sn码 则跳过                    if($count > 0) {                        $product_type_for_sn = 0;                    }else{                        if(! $needSnFix) $needSnFix = true;                        $product_type_for_sn = 1;                    }                }else{                    //其它产品数量达到 则跳过                    if($count >= $p_info['number']) {                        $product_type_for_sn = 0;                    }else{                        if(! $needSnFix) $needSnFix = true;                        $product_type_for_sn = 2;                    }                }            }            $value['product_type_for_sn'] = $product_type_for_sn;            $product[] = $value;        }        if(empty($needSnFix)) return [true, ['needSnFix' => $needSnFix, 'product' => $product]];        return [true, ['needSnFix' => $needSnFix, 'product' => $product]];    }    public function warrantyAdd($data, $user){        list($status, $msg) = $this->warrantyAddRule($data,$user);        if(! $status) return [false, $msg];        list($warranty, $sn) = $msg;        try {            DB::beginTransaction();            Warranty::insert($warranty);            if(! empty($sn)){                $warranty_list = Warranty::where('del_time',0)                    ->whereIn("sn",$sn)                    ->select('id','sn')                    ->get()->toArray();                foreach ($warranty_list as $value){                    ProductSnInfo::where('del_time',0)                        ->where('sn', $value['sn'])                        ->update(['warranty_id' => $value['id']]);                }            }//            if($data['type'] == ProductSnInfo::type_two){//                //同一个合同,多次填写的客户基本信息按最后一次更新为主//                Warranty::where('del_time',0)//                    ->where('data_id',$data['data_id'])//                    ->where('type', $data['type'])//                    ->update(['customer_name' => $data['customer_name'], 'customer_contact' => $data['customer_contact']]);//            }            DB::commit();        }catch (\Throwable $exception){            DB::rollBack();            return [false, $exception->getMessage()];        }        return [true, ''];    }    private function warrantyAddRule($data, $user){        if(empty($data['type'])) return [false, 'type不能为空'];        if(! isset(ProductSnInfo::$type_name[$data['type']])) return [false,'type不存在'];        if(empty($data['data_id'])) return [false, 'data_id不能为空'];        $time = time();        $warranty = $sn_update = [];        if($data['type'] == ProductSnInfo::type_one){            $order = Construction::where('del_time',0)->where('id',$data['data_id'])->first();            if(empty($order)) return [false, '施工单不存在或已被删除'];            $order = $order->toArray();            if($order['state'] != Construction::STATE_FOUR) return [false, '施工单暂未完结,生成质保信息失败'];            if(empty($order['vin_no'])) return [false, '车架号不能为空'];            $bool = $this->isValidVin($order['vin_no']);            if(! $bool) return [false, '车架号错误,请查看原施工单车架号,编辑输入完整车架号'];            if(empty($order['customer_id'])) return [false, '客户信息不存在'];            $customer = Customer::where('del_time', 0)->where('id',$order['customer_id'])->first();            if(empty($customer)) return [false, '客户不存在或已被删除'];            $customer = $customer->toArray();            $info = CustomerInfo::where('del_time',0)                ->where('type',CustomerInfo::type_one)                ->where('customer_id', $customer['id'])                ->where('contact_info', "<>", "")                ->select('contact_info')                ->first();            if(empty($info)) return [false, "客户的联系方式不能为空"];            $info = $info->toArray();            $customer_info = $info['contact_info'];            $product_sn = ProductSnInfo::from('product_sn_info as a')                ->leftJoin('product as b','b.id','a.product_id')                ->select('a.*','b.title','b.warranty_time')                ->where('a.del_time',0)                ->where('a.data_id',$data['data_id'])                ->where('a.type', ProductSnInfo::type_one)                ->where('a.warranty_id', 0)                ->get()->toArray();            if(empty($product_sn)) return [false, '施工单暂无未生成质保信息的sn码'];            $construction_site = BasicType::where('id',$order['install_position'])->value('title') ?? "";            //质保单数据 需要更新的sn码信息            foreach ($product_sn as $value){                $warranty[] = [                    'data_id' => $order['id'],                    'data_title' => $order['order_number'],                    'type' => ProductSnInfo::type_one,                    'product_id' => $value['product_id'],                    'code' => $value['code'],                    'title' => $value['title'],                    'sn' => $value['sn'],                    'customer_id' => $order['customer_id'],                    'customer_name' => $customer['title'],                    'customer_contact' => $customer_info,                    'vin_no' => $order['vin_no'],                    'warranty_time' => $value['warranty_time'],                    'construction_site_title' => $construction_site,                    'start_time' => $order['end_time'],                    'crt_time' => $time,                    'crt_id' => $user['id'],                ];                $sn_update[] = $value['sn'];            }        }elseif($data['type'] == ProductSnInfo::type_two){            $order = SalesOrder::where('del_time',0)->where('id',$data['data_id'])->first();            if(empty($order)) return [false, '合同不存在或已被删除'];            $order = $order->toArray();            if(empty($order['customer_id'])) return [false, '客户不能为空'];            $customer_name = Customer::where('id', $order['customer_id'])->value('title');            if(empty($order['customer_contact'])) return [false, '客户联系方式不能为空'];            $product = (new SalesOrderService())->getSalesProductForWarranty($data['data_id']);            if(empty($product)) return [false, '合同已全部或暂无可生成质保信息的产品'];            foreach ($product as $value){                for($i = 0 ;$i < $value['number']; $i++){                    $warranty[] = [                        'data_id' => $order['id'],                        'data_title' => $order['order_number'],                        'type' => ProductSnInfo::type_two,                        'product_id' => $value['product_id'],                        'code' => $value['code'],                        'title' => $value['title'],                        'sn' => "",                        'customer_id' => $order['customer_id'],                        'customer_name' => $customer_name,                        'customer_contact' => $order['customer_contact'],                        'vin_no' => "",                        'warranty_time' => $value['warranty_time'],                        'construction_site_title' => "",                        'start_time' => $time,                        'crt_time' => $time,                        'crt_id' => $user['id'],                    ];                }            }        }else{            return [false, '质保生成类型错误'];        }        return [true, [$warranty, $sn_update]];    }    public function ori()    {//生成质保 合同原来的代码        $order = SalesOrder::where('del_time', 0)->where('id', $data['data_id'])->first();        if (empty($order)) return [false, '合同不存在或已被删除'];        $order = $order->toArray();        if (empty($data['customer_name'])) return [false, '客户名称不能为空'];        if (empty($data['customer_contact'])) return [false, '客户联系方式不能为空'];        $bool = $this->isValidMobile($data['customer_contact']);        if (!$bool) return [false, '手机号错误,请输入完整的手机号'];        $vin_no = "";        if (!empty($data['vin_no'])) {            $bool = $this->isValidVin($data['vin_no']);            if (!$bool) return [false, '车架号错误,请填写完整车架号'];            $vin_no = $data['vin_no'];        }        if (empty($data['product'])) return [false, '合同无生成质保的产品信息'];        $product = (new SalesOrderService())->getSalesProductForWarranty($data['data_id']);        if (empty($product)) return [false, '合同暂无能生成质保信息的产品'];        foreach ($data['product'] as $value) {            $tmp = $product[$value['product_id']] ?? [];            if (empty($tmp)) return [false, '存在不能生成质保的产品'];            if ($tmp['number'] > $value['number']) return [false, '产品编码:' . $tmp['code'] . '本次可生成质保信息的产品最多' . $tmp['number']];            $warranty[] = [                'data_id' => $order['id'],                'data_title' => $order['order_number'],                'type' => ProductSnInfo::type_two,                'product_id' => $tmp['product_id'],                'code' => $tmp['code'],                'title' => $tmp['title'],                'sn' => "",                'customer_id' => $order['customer_id'],                'customer_name' => $data['customer_name'],                'customer_contact' => $data['customer_contact'],                'vin_no' => $vin_no,                'warranty_time' => $tmp['warranty_time'],                'construction_site_title' => "",                'start_time' => $time,                'crt_time' => $time,                'crt_id' => $user['id'],            ];        }    }    public function saveWarrantyByMyself($data){        list($status, $msg) = $this->saveWarrantyByMyselfRule($data);        if(! $status) return [false, $msg];        list($warranty, $sn) = $msg;        try {            DB::beginTransaction();            $warranty_id = DB::table('warranty')->insertGetId($warranty);            $sn['warranty_id'] = $warranty_id;            ProductSnInfo::insert($sn);            (new DataSyncToU8Service())->updateByVinNo($data);            if(! empty($data['auto_id'])) (new DataSyncToU8Service())->updateSnInfo(1, [$data['auto_id']], 1);            DB::commit();        }catch (\Throwable $exception){            DB::rollBack();            return [false, $exception->getMessage()];        }        return [true, ''];    }    private function saveWarrantyByMyselfRule($data){        if(empty($data['product_id'])) return [false, '产品ID不能为空'];        $product = Product::where('del_time',0)            ->where('id',$data['product_id'])            ->first();        if(empty($product)) return [false, '产品不存在或已被删除'];        $product = $product->toArray();        if(empty($data['start_time'])) return [false, '施工日期不能为空'];        if(empty($product['warranty_time'])) return [false, '质保时长不能为空'];        if(empty($data['sn'])) return [false, '产品序列号不能为空'];        list($status, $msg) = (new DataSyncToU8Service())->snForWarranty($data);        if(! $status) return [false, $msg];        //校验sn是否被占用        $submit_info[] = $product['code'] . $data['sn'];        list($status, $msg) = (new DataSyncToU8Service())->snForCheck([$product['code']], [$data['sn']], $submit_info);        if(! $status) return [false, "产品序列号:" . $data['sn'] . "已存在于门店系统"];        if(empty($data['customer_name'])) return [false, '客户名称不能为空'];        if(empty($data['customer_contact'])) return [false, '客户联系方式不能为空'];        $bool = $this->isValidMobile($data['customer_contact']);        if(! $bool) return [false, '手机号错误,请填写完整的11位手机号'];        if(empty($data['vin_no'])) return [false, '车架号不能为空'];        $bool = $this->isValidVin($data['vin_no']);        if(! $bool) return [false, '车架号错误,请填写完整的17位车架号'];        $time = time();        //质保单数据 sn码数据        $warranty = [            'data_id' => 0,            'data_title' => '',            'type' => ProductSnInfo::type_three,            'product_id' => $product['id'],            'code' => $product['code'],            'title' => $product['title'],            'sn' => $data['sn'],            'customer_id' => 0,            'customer_name' => $data['customer_name'],            'customer_contact' => $data['customer_contact'],            'vin_no' => $data['vin_no'],            'warranty_time' => $product['warranty_time'],            'construction_site_title' => $data['construction_site_title'] ?? "",            'start_time' => $data['start_time'],            'crt_time' => $time,            'crt_id' => 0,            'is_active' => Warranty::type_one,            'sn_type' => 1,        ];        $sn_update = [            'data_id' => 0,            'type' => ProductSnInfo::type_three,            'product_id' => $product['id'],            'code' => $product['code'],            'sn' => $data['sn'],            'crt_time' => $time,            'warranty_id' => 0,            'auto_id' => $data['auto_id'] ?? 0,        ];        return [true, [$warranty, $sn_update]];    }    //获取未激活列表    public function getWarrantyNotActiveList($data){        if(empty($data['customer_contact'])) return [false, '手机号不能为空'];        $model = Warranty::where('del_time',0)            ->where('customer_contact',$data['customer_contact'])            ->where('is_active',Warranty::type_zero)            ->whereIn('type',[ProductSnInfo::type_one, ProductSnInfo::type_two])            ->where(function ($query) {                $query->where('type', '!=', ProductSnInfo::type_one)                    ->orWhere(function ($q) {                        $q->where('type', ProductSnInfo::type_one)                            ->where('sn', '<>', '');                    });            })//sn限制            ->select('id','data_id','data_title','type','product_id','title','sn','customer_name','customer_contact','vin_no','warranty_time','construction_site_title','start_time');        $list = $this->limit($model,'', $data);        $list = $this->fillGetWarrantyNotActiveList($list);        return [true, $list];    }    public function fillGetWarrantyNotActiveList($data){        if(empty($data['data'])) return $data;        foreach ($data['data'] as $key => $value){            $data['data'][$key]['start_time'] = $value['start_time'] ? date('Y-m-d',$value['start_time']) : '';        }        return $data;    }    //质保激活    public function warrantyActivationCustomer($data){        list($status, $msg) = $this->warrantyActivationCustomerRule($data);        if(! $status) return [false, $msg];        $order = $msg;        try {            DB::beginTransaction();            if($order['type'] == ProductSnInfo::type_two){                $update = [                    'sn' => $data['sn'] ?? '',                    'construction_site_title' => $data['construction_site_title'] ?? '',                    'vin_no' => $data['vin_no'],                    'is_active' => Warranty::type_one                ];                Warranty::where('id',$data['id'])->update($update);                if(! empty($data['sn'])){                    $order = Warranty::where('id',$data['id'])->first()->toArray();                    $sn_update = [                        'data_id' => $order['data_id'],                        'type' => $order['type'],                        'product_id' => $order['product_id'],                        'code' => $order['code'],                        'sn' => $order['sn'],                        'crt_time' => time(),                        'warranty_id' => $order['id'],                    ];                    ProductSnInfo::insert($sn_update);                }            }elseif($order['type'] == ProductSnInfo::type_one){                $update = [                    'construction_site_title' => $data['construction_site_title'] ?? '',                    'is_active' => Warranty::type_one                ];                Warranty::where('id',$data['id'])->update($update);            }            (new DataSyncToU8Service())->updateByVinNo($data);            DB::commit();        }catch (\Throwable $exception){            DB::rollBack();            return [false, $exception->getMessage()];        }        return [true, ''];    }    public function warrantyActivationCustomerRule($data){        if(empty($data['id'])) return [false, '质保ID不能为空'];        $order = Warranty::where('del_time',0)            ->where('id', $data['id'])            ->first();        if(empty($order)) return [false, '质保卡不存在或已被删除'];        $order = $order->toArray();        if($order['is_active'] > Warranty::type_zero) return [false, '质保卡已激活,请勿重复操作'];        if(empty($data['customer_name'])) return [false, '车主姓名不能为空'];        if(empty($data['customer_contact'])) return [false, '联系方式不能为空'];        if(empty($data['vin_no'])) return [false, '车架号不能为空'];        if($order['type'] == ProductSnInfo::type_one){        }else{            if(! empty($data['sn'])){                list($status, $msg) = (new DataSyncToU8Service())->snForWarranty($data);                if(! $status) return [false, $msg];                $submit_info[] = $order['code'] . $data['sn'];                list($status, $msg) = (new DataSyncToU8Service())->snForCheck([$order['code']], [$data['sn']], $submit_info);                if(! $status) return [false, "产品序列号:" . $data['sn'] . "已存在于门店系统"];            }            $bool = $this->isValidVin($data['vin_no']);            if(! $bool) return [false, '车架号错误,请填写完整车架号'];        }        return [true, $order];    }    //后台列表    public function warrantyList($data,$user){        $model = Warranty::where('del_time',0)            ->select('id','data_id','data_title','code','type','product_id','title','sn','customer_name','customer_contact','vin_no','warranty_time','construction_site_title','start_time','crt_id','is_active','active_id','sy_code')            ->orderBy('id','desc');        if(! empty($data['type'])) $model->where('type', $data['type']);        if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');        if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');        if(! empty($data['data_title'])) $model->where('data_title', 'LIKE', '%'.$data['data_title'].'%');        if(! empty($data['sn'])) $model->where('sn', 'LIKE', '%'.$data['sn'].'%');        if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');        if(! empty($data['customer_contact'])) $model->where('customer_contact', 'LIKE', '%'.$data['customer_contact'].'%');        if(! empty($data['vin_no'])) $model->where('vin_no', 'LIKE', '%'.$data['vin_no'].'%');        if(! empty($data['construction_site_title'])) $model->where('construction_site_title', 'LIKE', '%'.$data['construction_site_title'].'%');        if(! empty($data['warranty_time'])) $model->where('warranty_time', $data['warranty_time']);        if(isset($data['is_active'])) $model->where('is_active', $data['is_active']);        if(! empty($data['crt_id'])) $model->where('crt_id', $data['crt_id']);        if(! empty($data['id'])) $model->where('id', $data['id']);        if(! empty($data['sy_code'])) $model->where('sy_code', 'LIKE', '%'.$data['sy_code'].'%');        $list = $this->limit($model,'', $data);        $list = $this->fillWarrantyList($list);        return [true, $list];    }    public function fillWarrantyList($data){        if(empty($data['data'])) return $data;        foreach ($data['data'] as $key => $value){            $data['data'][$key]['start_time'] = $value['start_time'] ? date('Y-m-d',$value['start_time']) : '';            $data['data'][$key]['type_title'] = ProductSnInfo::$type_name[$value['type']] ?? "";        }        return $data;    }    //后台质保编辑以及激活    public function warrantyEditAndActivation($data,$user){        list($status, $msg) = $this->warrantyEditAndActivationRule($data);        if(! $status) return [false, $msg];        $order = $msg;        try {            DB::beginTransaction();            $time = time();            $sn = $data['sn'] ?? "";            $order_sn = $order['sn'];            if(! empty($order_sn) && ! empty($sn) && $order_sn != $sn){                ProductSnInfo::where('del_time',0)                    ->where('data_id', $order['data_id'])                    ->where('type', $order['type'])                    ->where('sn', $order_sn)                    ->update(['del_time' => $time]);                $sn_update = [                    'data_id' => $order['data_id'],                    'type' => $order['type'],                    'product_id' => $order['product_id'],                    'code' => $order['code'],                    'sn' => $order['sn'],                    'crt_time' => time(),                    'warranty_id' => $order['id'],                ];                ProductSnInfo::insert($sn_update);            }            $update = [                'sn' => $sn,                'construction_site_title' => $data['construction_site_title'] ?? '',                'vin_no' => $data['vin_no'],                'start_time' => $data['start_time'],                'customer_name' => $data['customer_name'],                'customer_contact' => $data['customer_contact'],                'warranty_time' => $data['warranty_time'],            ];            if(empty($data['is_active'])) {                $update['is_active'] = Warranty::type_one;                $update['active_id'] = $user['id'];            }            Warranty::where('id',$data['id'])->update($update);            (new DataSyncToU8Service())->updateByVinNo($data);            DB::commit();        }catch (\Throwable $exception){            DB::rollBack();            return [false, $exception->getMessage()];        }        return [true, ''];    }    public function warrantyEditAndActivationRule(&$data){        if(empty($data['id'])) return [false, '质保ID不能为空'];        $order = Warranty::where('del_time',0)            ->where('id', $data['id'])            ->first();        if(empty($order)) return [false, '质保卡不存在或已被删除'];        $order = $order->toArray();//        if(! empty($data['is_active']) && $order['is_active'] > Warranty::type_zero) return [false, '质保卡已激活,请勿重复操作'];        if(empty($data['start_time'])) return [false, '请填写施工日期/生效日期'];        $data['start_time'] = $this->changeDateToDateMin($data['start_time']);        if(empty($data['customer_name'])) return [false, '车主姓名不能为空'];        if(empty($data['customer_contact'])) return [false, '联系方式不能为空'];        if(empty($data['warranty_time'])) return [false, '质保时长(月)不能为空'];        if(empty($data['vin_no'])) return [false, '车架号不能为空'];        $bool = $this->isValidVin($data['vin_no']);        if(! $bool) return [false, '车架号错误,请填写完整车架号'];        if(! empty($data['sn'])){//            list($status, $msg) = (new DataSyncToU8Service())->snForWarranty($data);//            if(! $status) return [false, $msg];            $submit_info[] = $order['code'] . $data['sn'];            $message = [                'id' => $order['data_id'],                'data_type' => $order['type']            ];            list($status, $msg) = (new DataSyncToU8Service())->snForCheck([$order['code']], [$data['sn']], $submit_info, $message);            if(! $status) return [false, "产品序列号:" . $data['sn'] . "已存在于门店系统"];        }        return [true, $order];    }    //后台质保激活    public function warrantyActivation($data,$user){        list($status, $msg) = $this->warrantyActivationRule($data);        if(! $status) return [false, $msg];        $order = $msg;        try {            DB::beginTransaction();            $update['is_active'] = Warranty::type_one;            $update['active_id'] = $user['id'];            Warranty::where('id', $data['id'])->update($update);            (new DataSyncToU8Service())->updateByVinNo($order);            DB::commit();        }catch (\Throwable $exception){            DB::rollBack();            return [false, $exception->getMessage()];        }        return [true, ''];    }    public function warrantyActivationRule($data){        if(empty($data['id'])) return [false, '质保ID不能为空'];        $order = Warranty::where('del_time',0)            ->where('id', $data['id'])            ->first();        if(empty($order)) return [false, '质保卡不存在或已被删除'];        $order = $order->toArray();        if(! empty($data['is_active']) && $order['is_active'] > Warranty::type_zero) return [false, '质保卡已激活,请勿重复操作'];        if(empty($order['start_time'])) return [false, '施工日期/生效日期不能为空'];        if(empty($order['customer_name'])) return [false, '车主姓名不能为空'];        if(empty($order['customer_contact'])) return [false, '联系方式不能为空'];        if(empty($order['warranty_time'])) return [false, '质保时长(月)不能为空'];        if(empty($order['vin_no'])) return [false, '车架号不能为空'];        $bool = $this->isValidVin($order['vin_no']);        if(! $bool) return [false, '车架号错误,请填写完整车架号'];        return [true, $order];    }    //质保卡作废    public function warrantyDel($data,$user){        list($status, $msg) = $this->warrantyDelRule($data);        if(! $status) return [false, $msg];        $order = $msg;        $time = time();        try {            DB::beginTransaction();            Warranty::where('id', $data['id'])->update(['del_time' => $time]);            if(! empty($order['sn'])) ProductSnInfo::where('del_time',0)->where('sn', $order['sn'])->update(['del_time' => $time]);            DB::commit();        }catch (\Throwable $exception){            DB::rollBack();            return [false, $exception->getMessage()];        }        return [true, ''];    }    public function warrantyDelRule($data){        if(empty($data['id'])) return [false, '质保ID不能为空'];        $order = Warranty::where('del_time',0)            ->where('id', $data['id'])            ->first();        if(empty($order)) return [false, '质保卡不存在或已被删除'];        $order = $order->toArray();        return [true, $order];    }    public function searchWarranty($data){        list($status, $msg) = $this->searchWarrantyRule($data);        if(! $status) return [false, $msg];        list($search_field, $search) = $msg;        $order_list = Warranty::where('del_time',0)            ->when($search_field == 1, function ($query) use ($search) {                return $query->where("customer_contact", $search);            })            ->when($search_field == 2, function ($query) use ($search) {                return $query->where("vin_no", $search);            })            ->where(function ($query) {                $query->where('type', '!=', ProductSnInfo::type_one)                    ->orWhere(function ($q) {                        $q->where('type', ProductSnInfo::type_one)                            ->where('sn', '<>', '');                    });            })//sn限制            ->orderBy('id','desc')            ->get()->toArray();        if(empty($order_list)) return [false , "暂无质保信息"];        $first = $order_list[0];        $order['main'] = [            'customer_name' => $first['customer_name'],            'customer_contact' => $first['customer_contact'],            'vin_no' => $first['vin_no'],        ];        foreach ($order_list as $key => $value){            $order_list[$key]['start_time'] = $value['start_time'] ? date('Y-m-d',$value['start_time']) : '';        }        $order['list'] = $order_list;        return [true, $order];    }    public function searchWarrantyRule($data){        if(empty($data['from'])) return [false, '查询来源不能为空'];        $search = $data['customer_contact_or_vin_no'];        if($data['from'] == 1){            if(empty($data['customer_contact_or_vin_no'])) return [false, '手机号不能为空'];            $length = strlen($data['customer_contact_or_vin_no']);            if($length != 11) return [false, '请输入完整的11位手机号'];            list($status,$msg) = $this->searchWarrantyCommon($data['customer_contact_or_vin_no']);            if(! $status) return [false, $msg];            $search_field = 1;        }elseif($data['from'] == 2){            if(empty($data['customer_contact_or_vin_no'])) return [false, '手机号或车架号不能为空'];            $length = strlen($data['customer_contact_or_vin_no']);            if($length != 11 && $length != 17) return [false, '请输入完整的11位手机号或完整的17位车架号'];            if($length == 11){                list($status,$msg) = $this->searchWarrantyCommon($data['customer_contact_or_vin_no']);                if(! $status) return [false, $msg];                $search_field = 1;            }else{                $search_field = 2;            }        }else{            return [false, '查询来源错误'];        }        return [true, [$search_field, $search]];    }    public function searchWarrantyCommon($customer_contact){        $vinCount = Warranty::where('del_time', 0)            ->where('customer_contact', $customer_contact)            ->where('vin_no', '<>', '')            ->distinct()            ->count('vin_no');        if($vinCount > 1) return [false, '手机号:' . $customer_contact . '下有个多个车架号,请去质保查询功能按需查询质保信息'];        return [true, ''];    }    public function warrantyAddNew($order, $data, $user){        try {            DB::beginTransaction();            $data['id'] = $order['id'];            $time = time();            //保存sn关联关系            (new DataSyncToU8Service())->saveSn($data, ProductSnInfo::type_one, $time);            $product_sn = ProductSnInfo::from('product_sn_info as a')                ->leftJoin('product as b','b.id','a.product_id')                ->select('a.*','b.title','b.warranty_time')                ->where('a.del_time',0)                ->where('a.data_id',$data['id'])                ->where('a.type', ProductSnInfo::type_one)                ->where('a.warranty_id', 0)                ->get()->toArray();            $construction_site = BasicType::where('id',$order['install_position'])->value('title') ?? "";            //生成质保单数据 需要更新的sn码信息            if(! empty($product_sn)){                $warranty = $sn_update = [];                foreach ($product_sn as $value){                    $warranty[] = [                        'data_id' => $order['id'],                        'data_title' => $order['order_number'],                        'type' => ProductSnInfo::type_one,                        'product_id' => $value['product_id'],                        'code' => $value['code'],                        'title' => $value['title'],                        'sn' => $value['sn'],                        'customer_id' => $order['customer_id'],                        'customer_name' => $order['customer_title'],                        'customer_contact' => $order['customer_info'],                        'vin_no' => $order['vin_no'],                        'warranty_time' => $value['warranty_time'],                        'construction_site_title' => $construction_site,                        'start_time' => $order['end_time'],                        'crt_time' => $time,                        'crt_id' => $user['id'],                        'sn_type' => $value['sn_type'],                        'sy_code' => ""                    ];                    $sn_update[] = $value['sn'];                }                if(! empty($warranty)) Warranty::insert($warranty);                if(! empty($sn_update)){                    $warranty_list = Warranty::where('del_time',0)                        ->whereIn("sn",$sn_update)                        ->select('id','sn')                        ->get()->toArray();                    foreach ($warranty_list as $value){                        ProductSnInfo::where('del_time',0)                            ->where('sn', $value['sn'])                            ->update(['warranty_id' => $value['id']]);                    }                }            }            //生成溯源码质保卡            $insert2 = $this->getInsert($data,$order,$construction_site,$time,$user);            if(! empty($insert2)) Warranty::insert($insert2);            DB::commit();        }catch (\Throwable $exception){            DB::rollBack();            return [false, $exception->getMessage()];        }        return [true, ''];    }    public function getInsert($data,$order,$construction_site,$time,$user){        $map = (new ProductService())->getProductDetail(array_column($data['product'],'product_id'));        $service = new DataSyncToU8Service();        $insert2 = [];        foreach ($data['product'] as $value){            $tmp = $map[$value['product_id']] ?? [];            $bool = $service->forCheck($tmp['product_category']);            $value['warranty_time'] = $tmp['warranty_time'];            //没有sn码信息直接返回            if(empty($value['product_sn_info'])) {                if($bool){                    $this->fillInsert($insert2,$order,$value,$construction_site,$time,$user);                }else{                    $bool = $service->forCheck2($tmp['product_category']);                    if($bool){                        $this->fillInsert($insert2,$order,$value,$construction_site,$time,$user);                    }else{                        for ($i = 0; $i < intval($value['number']); $i++){                            $this->fillInsert($insert2,$order,$value,$construction_site,$time,$user);                        }                    }                }            }else{                $num = count(array_column($value['product_sn_info'],'sn'));                if($bool){                    if($num < 1){                        $this->fillInsert($insert2,$order,$value,$construction_site,$time,$user);                    }                }else{                    $bool = $service->forCheck2($tmp['product_category']);                    if($bool){                        if($num < 1){                            $this->fillInsert($insert2,$order,$value,$construction_site,$time,$user);                        }                    }else{                        $n = bcsub($value['number'], $num,2);                        if(intval($n) > 0){                            for ($i = 0; $i < intval($value['number']); $i++){                                $this->fillInsert($insert2,$order,$value,$construction_site,$time,$user);                            }                        }                    }                }            }        }        return $insert2;    }    public function fillInsert(&$insert2, $order, $value, $construction_site,$time,$user){        $insert2[] = [            'data_id' => $order['id'],            'data_title' => $order['order_number'],            'type' => ProductSnInfo::type_one,            'product_id' => $value['product_id'],            'code' => $value['code'],            'title' => $value['title'],            'sn' => "",            'customer_id' => $order['customer_id'],            'customer_name' => $order['customer_title'],            'customer_contact' => $order['customer_info'],            'vin_no' => $order['vin_no'],            'warranty_time' => $value['warranty_time'],            'construction_site_title' => $construction_site,            'start_time' => $order['end_time'],            'crt_time' => $time,            'crt_id' => $user['id'],//            'sn_type' => $value['sn_type'] ?? 0,            'sy_code' => $value['sy_code'] ?? "",        ];    }}
 |