request->only(['token', 'list'], 'post', 'trim'); $val_params = Validate::rule(['token' => 'require', 'list' => 'require|array']); if (!$val_params->check($param)) throw new Exception($val_params->getError()); Db::startTrans(); try { //平台订单号不允许重复 //平台订单号允许重复,但是平台订单号和平台商品编号的组合不能重复(2022-05-25 10) // $all_platform_codes = array_column($param['list'], 'platform_code'); // // $temp = Db::name('order_import_from_c') // ->field('id,platform_code') // ->whereIn('platform_code', $all_platform_codes) // ->find(); // // if (!empty($temp)) throw new Exception('平台订单号' . $temp['platform_code'] . '重复'); $userinfo = GetUserInfo($param['token']); $createrid = isset($userinfo["data"]['id']) ? $userinfo["data"]['id'] : 0; $creater = isset($userinfo["data"]['nickname']) ? $userinfo["data"]['nickname'] : ""; $insert_data = []; foreach ($param['list'] as $key => $value) { $value['createrid'] = $createrid; $value['creater'] = $creater; $value['addtime'] = $value['updatetime'] = date('Y-m-d H:i:s'); $insert_data[] = $value; } $res = Db::name('order_import_from_c') ->limit(100) ->insertAll($insert_data); if ($res) { Db::commit(); return app_show(0, "导入成功,共" . (string)$res . '条记录'); } else { Db::rollback(); return error_show(1005, '导入失败'); } } catch (Exception $exception) { Db::rollback(); return error_show(1005, $exception->getMessage()); } } //查看订单录入列表 public function getImportList() { $param = $this->request->only([ 'status' => 0, 'platform_code' => '', 'platform_id' => '', 'po_code' => '', 'platform_time_start' => '', 'platform_time_end' => '', 'addtime_start' => '', 'addtime_end' => '', 'plat_code' => '', 'orderCode' => '', 'page' => 1, 'size' => 15, 'company_name' => '', 'relaComNo' => '', ], 'post', 'trim'); $where = [['c.is_del', '=', OIFCModel::$is_del_normal]]; if ($param['status']) $where[] = ['c.status', '=', $param['status']]; if ($param['platform_code']) $where[] = ['c.platform_code', 'like', '%' . $param['platform_code'] . '%']; if ($param['platform_id']) $where[] = ['c.platform_id', '=', $param['platform_id']]; if ($param['po_code']) $where[] = ['c.po_code', 'like', '%' . $param['po_code'] . '%']; if ($param['platform_time_start'] && $param['platform_time_end']) $where[] = ['c.platform_time', 'between', [$param['platform_time_start'], $param['platform_time_end']]]; if ($param['addtime_start'] && $param['addtime_end']) $where[] = ['c.addtime', 'between', [$param['addtime_start'], $param['addtime_end']]]; if ($param['plat_code']) $where[] = ['c.plat_code', 'like', '%' . $param['plat_code'] . '%']; if ($param['orderCode']) $where[] = ['c.orderCode', 'like', '%' . $param['orderCode'] . '%']; if ($param['company_name']) $where[] = ['c.createrid', 'in', get_company_item_user_by_name($param['company_name'])]; $total = OIFCModel::alias('c') ->leftJoin('good_platform gp', 'gp.plat_code=c.plat_code AND gp.platform_code=c.platform_id AND gp.is_del=0 AND gp.exam_status=3') ->leftJoin('good g', 'g.spuCode=gp.spuCode AND g.is_del=0') ->leftJoin('platform p', 'p.id=c.platform_id AND p.is_del=0') ->where($where) ->count('c.id'); $list = OIFCModel::alias('c') ->field('c.*,g.good_name,p.platform_name platform_title') ->leftJoin('good_platform gp', 'gp.plat_code=c.plat_code AND gp.platform_code=c.platform_id AND gp.is_del=0 AND gp.exam_status=3') ->leftJoin('good g', 'g.spuCode=gp.spuCode AND g.is_del=0') ->leftJoin('platform p', 'p.id=c.platform_id AND p.is_del=0') ->where($where) ->page($param['page'], $param['size']) ->order('id', 'desc') ->select() ->toArray(); $all_createrid = array_column($list,'createrid'); $item = get_company_name_by_uid($all_createrid); foreach ($list as &$value){ $value['company_name']=$item[$value['createrid']]??''; } return app_show(0, "获取成功", ['list' => $list, 'count' => $total]); } //查看订单录入详情 public function getImportInfo() { $id = $this->request->post('id/d', 0, 'trim'); $info = OIFCModel::where('id', $id) ->withoutField('discount_price,after_price') ->where('status', '<>', OIFCModel::$status_wait_validate) ->findOrEmpty() ->toArray(); if (empty($info)) return error_show(1005, '系统尚未解析完成,无法查看详情,请等待'); $res = [ 'id' => $info['id'], 'status' => $info['status'], 'createrid' => $info['createrid'], 'creater' => $info['creater'], 'updateid' => $info['updateid'], 'updater' => $info['updater'], 'orderCode' => $info['orderCode'], 'success_date' => $info['status'] == OIFCModel::$status_success ? $info['updatetime'] : '',//录入成功时间 'addtime' => $info['addtime'], 'error_msg' => $info['remark'],//错误原因 'platform_id' => $info['platform_id'], 'platform_title' => Db::name('platform')->where(['id' => $info['platform_id'], 'is_del' => 0])->value('platform_name', ''), ]; //导入原始数据 $res['import'] = $info; //解析数据 $res['confirm'] = Db::name('order_import_from_c_extend') ->where(['order_import_from_c_id' => $id, 'type' => 1]) ->findOrEmpty(); if ($res['confirm']['addr_code']) { $addr_code = explode(',', $res['confirm']['addr_code']); $res['confirm']['addr_name'] = GetAddr(json_encode(['provice_code' => $addr_code[0], 'city_code' => $addr_code[1], 'area_code' => $addr_code[2]])); } else $res['confirm']['addr_name'] = ''; //用户确认数据 $res['user_update'] = Db::name('order_import_from_c_extend') ->where(['order_import_from_c_id' => $id, 'type' => 2]) ->find(); if ($res['user_update']) { if ($res['user_update']['addr_code']) { $addr_code = explode(',', $res['user_update']['addr_code']); $res['user_update']['addr_name'] = GetAddr(json_encode(['provice_code' => $addr_code[0], 'city_code' => $addr_code[1], 'area_code' => $addr_code[2]])); } else $res['user_update']['addr_name'] = ''; } else $res['user_update'] = $res['confirm']; return app_show(0, '请求成功', $res); } //用户确认订单数据(编辑) public function confirmImportData() { //每次编辑,都新增一条记录到extend表,同时原纪录作废,确保同一个order_import_from_c_id下只有一个正常的记录 $param = $this->request->only([ 'id', 'platform_code', 'platform_id', 'po_code' => '', 'platform_time', 'sale_source', 'plat_code', 'price', 'num', 'before_discount_all_price', 'activity_code' => '', 'activity_name' => '', 'mode', 'order_remark', 'contactor', 'mobile', 'addr', 'addr_code' => '', 'token' ], 'post', 'trim'); $val = Validate::rule([ 'id' => 'require', 'platform_code' => 'require', 'platform_id' => 'require|number', 'platform_time' => 'require', 'sale_source' => 'require', 'plat_code' => 'require', 'price' => 'require', 'num' => 'require', 'before_discount_all_price' => 'require', 'mode' => 'require', 'contactor' => 'require', 'mobile' => 'require', 'addr' => 'require', ]); if (!$val->check($param)) return error_show(1004, $val->getError()); Db::startTrans(); try { $rs = Db::name('order_import_from_c') ->where(['id' => $param['id'], 'is_del' => OIFCModel::$is_del_normal, 'status' => OIFCModel::$status_wait_confirm]) ->find(); if (empty($rs)) throw new Exception('该记录不存在或不允许用户确认'); $userinfo = GetUserInfo($param['token']); //更新主表 Db::name('order_import_from_c') ->where(['id' => $param['id'], 'is_del' => OIFCModel::$is_del_normal, 'status' => OIFCModel::$status_wait_confirm]) ->update([ 'status' => OIFCModel::$status_wait_relation, 'updatetime' => date('Y-m-d H:i:s'), 'remark' => '', 'updateid' => isset($userinfo["data"]['id']) ? $userinfo["data"]['id'] : 0, 'updater' => isset($userinfo["data"]['nickname']) ? $userinfo["data"]['nickname'] : '', ]); //组织从表数据 $extend_insert_data = [ 'order_import_from_c_id' => $param['id'], 'platform_code' => $param['platform_code'], 'platform_id' => $param['platform_id'], 'po_code' => $param['po_code'], 'platform_time' => $param['platform_time'], 'sale_source' => $param['sale_source'], 'price' => $param['price'], 'num' => $param['num'], 'before_discount_all_price' => $param['before_discount_all_price'], 'mode' => $param['mode'], 'order_remark' => $param['order_remark'], 'contactor' => $param['contactor'], 'mobile' => $param['mobile'], 'addr' => $param['addr'], 'discount_reason' => $param['activity_name'],//优惠原因用活动名称 'addr_code' => is_array($param['addr_code']) ? implode(',', $param['addr_code']) : $param['addr_code'], 'activity_code' => $param['activity_code'], 'activity_name' => $param['activity_name'], 'addtime' => date('Y-m-d H:i:s'), 'createrid' => isset($userinfo["data"]['id']) ? $userinfo["data"]['id'] : 0, 'creater' => isset($userinfo["data"]['nickname']) ? $userinfo["data"]['nickname'] : '', 'type' => 2,//1系统解析,2用户确认 ]; $good_platform_info = Db::name('good_platform') ->field('id,spuCode,platform_code,skuCode,plat_code') ->where(['is_del' => 0, 'plat_code' => $param['plat_code'], 'platform_code' => $param['platform_id'], 'exam_status' => 3])//exam_status==6 已上线 ->find(); //商品信息 if (!empty($good_platform_info)) { $extend_insert_data['skuCode'] = $good_platform_info['skuCode']; $extend_insert_data['platform_id'] = $good_platform_info['platform_code']; $extend_insert_data['plat_code'] = $good_platform_info['plat_code']; $good_info = Db::name('good') ->field('good_name,companyNo,supplierNo') ->where(['is_del' => 0]) ->where('spuCode', $good_platform_info['spuCode']) ->find(); if (!empty($good_info)) { $extend_insert_data['companyNo'] = $good_info['companyNo']; $extend_insert_data['supplierNo'] = $good_info['supplierNo']; $extend_insert_data['good_name'] = $good_info['good_name']; } else throw new Exception('该平台商品编号找不到对应商品数据'); } else throw new Exception('该平台商品编号找不到对应平台的商品数据'); //企业客户 $co = Db::name('good_platform') ->alias('gp') ->leftJoin('platform p', 'p.id=gp.platform_code AND p.is_del=0') ->where('gp.plat_code', $rs['plat_code']) ->where('gp.exam_status', 3)//exam_status==3已上线 ->value('p.platform_name',''); if ($co!='') { $itemId= Db::connect('mysql_sys')->name("customer_org1") ->where(["name"=>$co,"is_del"=>0]) ->value("id",0); $customer_code =Db::connect('mysql_sys') ->name('customer_info') ->where(['itemid' => $itemId, 'companyName' => $co . '客户', 'is_del' => 0]) ->value('companyNo', ''); if ($customer_code) $extend_insert_data['customer_code'] = $customer_code; else throw new Exception('对应客户不存在'); } else throw new Exception('对应企业客户不存在'); //从表数据 Db::name('order_import_from_c_extend')->insert($extend_insert_data); Db::commit(); Cache::store("redis")->handler()->lPush('import_order_from_c_key', $param['id']);//校验无误,入队列 return app_show(0, '修改成功'); } catch (Exception $exception) { Db::rollback(); return error_show(1004, $exception->getMessage()); } } //批量关联库存 public function relationStockBatch() { $param = $this->request->only(['ids', 'plat_code'], 'post', 'trim'); $val = Validate::rule([ 'ids' => 'require', 'plat_code|平台商品编号' => 'require', ]); if (!$val->check($param)) return error_show(1004, $val->getError()); // $info = Db::name('order_import_from_c_extend') // ->field('id,spuCode,companyNo') // ->whereIn('order_import_from_c_id', $param['ids']) // ->order('addtime','desc') // ->find(); $good_platform = Db::name('good_platform') ->field('id,spuCode') ->where(['plat_code' => $param['plat_code'], 'is_del' => 0]) ->find(); if (empty($good_platform)) return error_show(1005, '平台商品编号有误'); //判断库存是否充足 $num = Db::name('order_import_from_c') ->whereIn('id', $param['ids']) ->where(['is_del' => OIFCModel::$is_del_normal, 'status' => OIFCModel::$status_stock_not_enough]) ->sum('num'); // $companyNo = Db::name('good') // ->where(['spuCode' => $good_platform['spuCode'], 'is_del' => 0]) // ->value('companyNo', ''); // $wsm_code = Db::name('warehouse_info') // ->where(['companyNo' => $companyNo, 'is_del' => 0]) // ->value('wsm_code', '');//一个供货企业会对应多个仓库,取哪个? $usable_stock = Db::name('good_stock') ->where(['spuCode' => $good_platform['spuCode'], 'is_del' => 0]) ->value('usable_stock', 0); if ($usable_stock < $num) return error_show(1005, '可用库存只有' . $usable_stock . ',库存不足,关联失败'); else { $userinfo = GetUserInfo($this->post['token']); $res = Db::name('order_import_from_c') ->whereIn('id', $param['ids']) ->where(['is_del' => OIFCModel::$is_del_normal, 'status' => OIFCModel::$status_stock_not_enough]) ->update([ 'status' => OIFCModel::$status_wait_relation, 'remark' => '', 'updatetime' => date('Y-m-d H:i:s'), 'updateid' => isset($userinfo["data"]['id']) ? $userinfo["data"]['id'] : 0, 'updater' => isset($userinfo["data"]['nickname']) ? $userinfo["data"]['nickname'] : '', ]); if ($res) { foreach ($param['ids'] as $id) { Cache::store("redis")->handler()->lPush('import_order_from_c_key', $id);//校验无误,入队列 } return app_show(0, '批量关联库存成功'); } else return error_show(1005, '批量关联库存失败'); } } //删除【待用户确认】或者【库存不足】的订单 public function delImportData() { $ids = $this->request->post('ids', [], 'trim'); if (empty($ids)) return error_show(1004, 'ids不能为空'); $rs = OIFCModel::field('id') ->whereNotIn('status', [OIFCModel::$status_wait_confirm, OIFCModel::$status_stock_not_enough]) ->whereIn('id', $ids) ->findOrEmpty() ->isEmpty(); if (!$rs) return error_show(1005, '该状态下不允许删除'); Db::startTrans(); try { $user = GetUserInfo($this->post['token']); Db::name('order_import_from_c') ->where('is_del', OIFCModel::$is_del_normal) ->whereIn('id', $ids) ->whereIn('status', [OIFCModel::$status_wait_confirm, OIFCModel::$status_stock_not_enough]) ->update([ 'updatetime' => date('Y-m-d H:i:s'), 'is_del' => OIFCModel::$is_del_delete, 'updateid' => isset($user['data']['id']) ? $user['data']['id'] : 0, 'updater' => isset($user['data']['nickname']) ? $user['data']['nickname'] : '', ]); Db::name('order_import_from_c_extend') ->whereIn('order_import_from_c_id', $ids) ->where('is_del', 0) ->update(['is_del' => 1]); Db::commit(); return app_show(0, '删除成功'); } catch (Exception $exception) { Db::rollback(); return error_show(1005, '删除失败,' . $exception->getMessage()); } } //【辅助1】获取线上商品详情 public function getOnlineGoodInfoByPlatcode() { $plat_code = isset($this->post['plat_code']) && $this->post['plat_code'] != "" ? trim($this->post['plat_code']) : ""; if ($plat_code == "") { return error_show(1005, "参数plat_code不能为空"); } $where = [['plat_code', '=', $plat_code]]; $platform_id = isset($this->post['platform_id']) && $this->post['platform_id'] != "" ? intval($this->post['platform_id']) : ""; if ($platform_id != '') $where[] = ['platform_code', '=', $platform_id]; $good_platform = Db::name("good_platform") ->where($where) ->where(['is_del' => 0]) ->find(); if ($good_platform == false) { return error_show(1004, "未找到商品数据"); } $data = Db::name("good")->where(["spuCode" => $good_platform['spuCode'], "is_del" => 0])->find(); if ($data == false) { return error_show(1004, "未找到商品数据"); } $plat = Db::name("platform")->where(["id" => $good_platform['platform_code']])->find(); $data['skuCode'] = $good_platform['skuCode']; $data['platform_code'] = $good_platform['platform_code']; $data['platform_code_en'] = isset($plat['platform_code']) ? $plat['platform_code'] : ""; $data['platform_name'] = isset($plat['platform_name']) ? $plat['platform_name'] : ""; $data['online_reason'] = $good_platform['online_reason']; $data['online_time'] = $good_platform['online_time']; $data['online_remark'] = $good_platform['online_remark']; $data['exam_status'] = $good_platform['exam_status']; $data['is_online'] = $good_platform['is_online']; $data['plat_code'] = $good_platform['plat_code']; $data['exclusive'] = makeExcluse($data['is_exclusive']); $unit = Db::name("unit")->where(["id" => $data['good_unit']])->find(); $data['unit'] = isset($unit['unit']) ? $unit['unit'] : ''; $data['cat_info'] = made($data['cat_id'], []); $spec = Db::name("good_spec")->where(["spuCode" => $good_platform['spuCode'], "is_del" => 0])->select()->toArray(); $userCommon = \app\admin\common\User::getIns(); $supplier_temp = $userCommon->handle('getCodeAndName', ['code' => $data['supplierNo']]); // $supplier = Db::name("supplier")->where(["code" => $data['supplierNo']])->find(); $supplier = $supplier_temp['data']??""; $data['supplierName'] = isset($supplier[$data['supplierNo']]) ? $supplier[$data['supplierNo']] : ""; $noble = Config::get('noble'); $data['noble_name'] = isset($data['noble_metal']) && $data['noble_metal'] != 0 ? $noble[$data['noble_metal']] : ""; $company = Db::name("business")->where(["companyNo" => $data['companyNo']])->find(); $data['company'] = isset($company['company']) ? $company['company'] : ""; if ($data['brand_id'] != 0) { $brand = Db::name("brand")->where(["id" => $data['brand_id']])->find(); $data["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : ""; } else { $data["brand_name"] = ""; $data["brand_id"] = ""; } $data['origin_place_cn'] = ""; $data['delivery_place_cn'] = ""; if ($data['delivery_place'] !== "") { $place = ["provice_code" => "", "city_code" => "", "area_code" => ""]; list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $data['delivery_place']); $data['delivery_place_cn'] = GetAddr(json_encode($place)); } if ($data['origin_place'] !== "") { $place = ["provice_code" => "", "city_code" => "", "area_code" => ""]; list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $data['origin_place']); $data['origin_place_cn'] = GetAddr(json_encode($place)); } $excluse = makeExcluse($data['is_exclusive']); $data['exclusive'] = $excluse; $data["good_info_img"] = $data['good_info_img'] != "" ? $data['good_info_img'] : []; $data["good_img"] = $data['good_img'] != "" ? $data['good_img'] : []; $speclist = []; if (!empty($spec)) { foreach ($spec as $value) { $temp = []; $temp['id'] = $value['id']; $temp['spuCode'] = $value['spuCode']; $temp['spec_id'] = $value['spec_id']; $temp['spec_value_id'] = $value['spec_value_id']; $temp['is_del'] = $value['is_del']; $sp = Db::name("specs")->where(["id" => $value['spec_id']])->find(); $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : ""; $spv = Db::name("spec_value")->where(["id" => $value['spec_value_id']])->find(); $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : ""; $speclist[] = $temp; } } $data["speclist"] = empty($speclist) ? [] : $speclist; $ladder = Db::name("good_ladder")->where(['skuCode' => $good_platform['spuCode'], "is_del" => 0])->select()->toArray(); $data["ladderlist"] = !empty($ladder) ? $ladder : []; $nakelist = Db::name("good_nake")->where(['spuCode' => $good_platform['spuCode'], "is_del" => 0])->select()->toArray(); $cat_top_list = made($data['cat_id']); $cat_top_id = isset($cat_top_list[0]['id']) ? $cat_top_list[0]['id'] : 0; $nakearry = []; if (!empty($nakelist)) { //实时金价 $gold_price = Db::name("gold_price1")->where(["type" => $data['noble_metal'], "status" => 1, "is_del" => 0])->order("addtime desc")->value('price', 0); foreach ($nakelist as $value) { $value['sale_price'] = $value['nake_total']; //计算最终售价 if ($data['is_gold_price'] == 1 && $cat_top_id == 6) { //普通商品:直接用财务提交的售价字段; //贵金属( 商品重量* 最新金价 + 工艺费(财务定价提交的)* 商品重量+包装费+加标费+证书费+产品裸价+其他费用)=商品库商品直接下单的售价 $value['sale_price'] = ($data['weight'] * $gold_price + $value['cost_fee'] * $data['weight'] + $value['package_fee'] + $value['mark_fee'] + $value['cert_fee'] + $value['nake_fee'] + $value['other_fee']); } $nakearry[] = $value; } } $proof = Db::name("good_proof")->where(["spuCode" => $good_platform['spuCode'], "is_del" => 0])->order("updatetime desc")->field("id,proof_type,proof_url")->find(); $data['proof'] = isset($proof) && $proof != false ? $proof : []; $data["nakelist"] = $nakearry; //补充采购员字段 //成本表里的创建人,也是线上商品的采购人,为了防止混淆,给creater取个别名 $temp_basic_info = Db::name('good_basic') ->field('id,createrid purchase_id,creater purchase') ->where(['spuCode' => $data['spuCode'], 'is_del' => 0]) ->find(); $data['purchase_id'] = $temp_basic_info['purchase_id']; $data['purchase'] = $temp_basic_info['purchase']; return app_show(0, "获取成功", $data); } //【辅助2】接口2-获取销售渠道备选数据:入参所在平台Id,响应:销售渠道备选数据 public function getSourceDataByPlatformId() { $platform_id = $this->request->post('platform_id/d', 0, 'trim'); $where = ['ps.is_del' => 0]; if ($platform_id != 0) $where['ps.platform_id'] = $platform_id; $list = Db::name('platform_source') ->alias('ps') ->field('ps.id,ps.platform_id,ps.source,p.platform_name') ->leftJoin('platform p', 'p.id=ps.platform_id AND p.is_del=0') ->where($where) ->order('ps.id', 'desc') ->select() ->toArray(); return app_show(0, '获取成功', $list); } }