goods_name; $cat_id = $this->cat_id; $supplier_id = $this->supplier_id; $min_money = $this->min_money; $max_money = $this->max_money; if (!in_array($admin->type, ['goods_agent', 'admin'])) { throw new \Exception('权限错误'); } //获取商品 $form = new PlatformForm(); $form->page = $this->page; $form->limit = $this->limit; $form->send_type = 3; //商品名称 if (!empty($goods_name)) { $form->name = $goods_name; } //供货商id if (!empty($supplier_id)) { $form->supplier_id = $supplier_id; } //分类 // if (!empty($cat_id)) { // $form->cat_id = $cat_id; // } //最大金额 if (!empty($min_money)) { $form->min_money = $min_money; } //最大金额 if (!empty($max_money)) { $form->max_money = $max_money; } //如果为区域代理 if ($admin->type === 'admin') { $result = $this->checkGoodsBind($admin->id); if ($result['code'] === 0) { $filter_cloud_goods_id = $result['data']; } else { throw new \Exception($result['msg']); } //剔除被代理的产品 $form->filter_goods_id = implode(',', $filter_cloud_goods_id); return $form->goodsList(); } else { $agent_bind = AgentBind::findOne(['admin_id' => $admin->id]); $parent_admin_id = $agent_bind->parent_admin_id; //产品代理 // $result = $this->getSupplier($parent_admin_id); // // if ($result['code'] === 0) { // if (!empty($supplier_id)) { // $supplier_id = array_intersect($result['data'], [$supplier_id]); // } else { // $supplier_id = $result['data']; // } //// $supplier_id = $result['data']; // } else { // throw new \Exception($result['msg']); // } //找到同级产品代理,检测某个商品是否已经被代理 $result = $this->checkGoodsBind($parent_admin_id); if ($result['code'] === 0) { $filter_cloud_goods_id = $result['data']; } else { throw new \Exception($result['msg']); } //获取商品 $form->type = 1; //剔除被代理的产品 $form->filter_goods_id = implode(',', $filter_cloud_goods_id); return $form->goodsList(); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 绑定商品 */ public function bindFrontAgentGoods() { try { $admin = get_admin(); //获取商品 $form = new PlatformForm(); if (!$this->cloud_goods_id) { throw new \Exception('未选择产品'); } if ($admin->type === 'admin') { $admin_id = $this->admin_id; $result = $this->checkGoodsBind($admin->id, $admin_id); if ($result['code'] === 0) { if (in_array($this->cloud_goods_id, $result['data'])) { throw new \Exception('该商品已经绑定其他产品代理'); } } else { throw new \Exception($result['msg']); } } else { $admin_id = $admin->id; $agent_bind = AgentBind::findOne(['admin_id' => $admin->id]); $parent_admin_id = $agent_bind->parent_admin_id; // $result = $this->getSupplier($parent_admin_id); // if ($result['code'] === 0) { // $supplier_id = $result['data']; // } else { // throw new \Exception($result['msg']); // } $result = $this->checkGoodsBind($parent_admin_id, $admin_id); if ($result['code'] === 0) { if (in_array($this->cloud_goods_id, $result['data'])) { throw new \Exception('该商品已经绑定其他产品代理'); } } else { throw new \Exception($result['msg']); } $form->type = 1; // $form->supplier_id = implode(',', $supplier_id); } $form->send_type = 3; $form->id = $this->cloud_goods_id; $result = $form->goodsList(); if ($result['code'] !== 0 || empty($result['data']['list'])) { throw new \Exception('商品查询失败'); } //绑定产品 if (empty($admin_id)) { throw new \Exception('未选择产品代理'); } $agent_goods_bind = AgentGoodsBindGoods::findOne(['goods_agent_admin_id' => $admin_id, 'is_delete' => 0, 'status' => [0, 1], 'cloud_goods_id' => $this->cloud_goods_id]); if (!$agent_goods_bind) { $agent_goods_bind = new AgentGoodsBindGoods(); $agent_goods_bind->status = 0; } if ($admin->type === 'admin') { $agent_goods_bind->status = 1; } $agent_goods_bind->goods_agent_admin_id = $admin_id; $agent_goods_bind->cloud_goods_id = $this->cloud_goods_id; $agent_goods_bind->cloud_supplier_id = $result['data']['list'][0]['supplier_id']; if (!$agent_goods_bind->save()) { throw new \Exception(json_encode($agent_goods_bind->errors)); } return [ 'code' => 0, 'msg' => '申请成功,等待审核' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 绑定商品 */ public function applyBindFrontAgentGoods() { try { $id = $this->id; $status = $this->status; $agent_goods_bind = AgentGoodsBindGoods::findOne(['id' => $id, 'is_delete' => 0]); if (!$agent_goods_bind) { throw new \Exception('绑定信息未找到'); } if (!in_array($status, [1, 2]) || (int)$agent_goods_bind->status !== 0) { throw new \Exception('状态错误'); } $agent_bind = AgentBind::findOne(['admin_id' => $agent_goods_bind->goods_agent_admin_id, 'type' => 1, 'is_delete' => 0]); if ((int)$agent_bind->parent_admin_id !== (int)get_admin()->id) { throw new \Exception('权限验证错误'); } $agent_goods_bind->status = $status; if (!$agent_goods_bind->save()) { throw new \Exception(json_encode($agent_goods_bind->errors)); } return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 删除绑定商品 */ public function delBindFrontAgentGoods() { try { $id = $this->id; $agent_goods_bind_goods = AgentGoodsBindGoods::findOne(['id' => $id, 'is_delete' => 0]); if (!$agent_goods_bind_goods) { throw new \Exception('未查找到信息'); } $agent_goods_bind_goods->is_delete = 1; if (!$agent_goods_bind_goods->save()) { throw new \Exception(json_encode($agent_goods_bind_goods->errors)); } return [ 'code' => 0, 'msg' => '删除成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 获取绑定产品列表 */ public function getBindGoodsList() { //查询条件 商品名称 代理名称 供货商 try { $admin = get_admin(); $status = $this->status; $goods_name = $this->goods_name; $admin_name = $this->admin_name; $supplier_name = $this->supplier_name; if (!in_array($admin->type, ['goods_agent', 'admin'])) { throw new \Exception('权限错误'); } if ($admin->type === 'admin' && $admin->username !== 'admin') { //获取子产品代理 $goods_agent_admin_id = AgentBind::find()->where(['parent_admin_id' => $admin->id, 'is_delete' => 0, 'type' => 1]) ->select('admin_id')->column(); $goods_agent_admin_id = array_merge($goods_agent_admin_id, [0]); } else { //获取当前的产品代理 $goods_agent_admin_id = $admin->id; } //查询绑定的商品信息 $query = AgentGoodsBindGoods::find()->alias('agbg') ->where(['agbg.is_delete' => 0]) ->leftJoin(['a' => Admin::tableName()], 'agbg.goods_agent_admin_id = a.id') ->leftJoin(['su' => SaasUser::tableName()], 'a.saas_user_id = su.id') ->select('agbg.id, agbg.cloud_goods_id, agbg.goods_agent_admin_id, a.saas_user_id, agbg.status, a.name user_name, su.avatar, agbg.cloud_supplier_id, agbg.status bind_status');//->asArray()->all(); //搜索状态 if ($goods_agent_admin_id) { $query->andWhere(['agbg.goods_agent_admin_id' => $goods_agent_admin_id]); } if ($status != -1 && $status !== null) { $query->andWhere(['agbg.status' => $status]); } //搜索代理名称 if ($admin_name) { $query->andWhere(['LIKE', 'su.name', $admin_name]); } //搜索供货商 if ($supplier_name) { $supplier_id = Supplier::find()->where(['like', 'supplier_name', $supplier_name])->select('cloud_supplier_id')->column(); $query->andWhere(['agbg.cloud_supplier_id' => $supplier_id]); } $sql = (clone $query)->createCommand()->getRawSql(); $list = $query->asArray()->all(); // $pagination = pagination_make($query); // $list = $pagination['list']; if (!empty($list)) { $form = new PlatformForm(); $form->type = 1; $goods_ids = array_column($list, 'cloud_goods_id'); $goods_ids = implode(',', $goods_ids); // foreach ($pagination['list'] as &$goods) { // $saas_user = SaasUser::findOne($goods['saas_user_id']); // $goods['saas_user'] = [ // 'name' => $saas_user->name, // 'avatar' => $saas_user->avatar, // ]; $form->ids = $goods_ids; $form->limit = $this->limit; $form->page = $this->page; $form->send_type = 3; if ($goods_name) { $form->name = $goods_name; } $result = $form->goodsList(); if ($result['code'] !== 0) { throw new \Exception($result['msg']); } foreach ($result['data']['list'] as &$goods) { foreach ($list as $value) { $value['bind_status'] *= 1; if ($value['cloud_supplier_id']) { $supplier = Supplier::findOne(['cloud_supplier_id' => $value['cloud_supplier_id']]); $value['supplier_name'] = $supplier->supplier_name; } else { $value['supplier_name'] = '-'; } if ((int)$goods['id'] === (int)$value['cloud_goods_id']) { $goods = array_merge($goods, $value); } } } // $goods = array_merge($goods, $result['data']['list'][0]); // } return [ 'code' => 0, 'msg' => '获取成功', 'data' => [ 'list' => $result['data']['list'], 'pageNo' => $this->page, 'totalCount' => $result['data']['count'], 'sql' =>$sql ] ]; } else { return [ 'code' => 0, 'msg' => '获取成功', 'data' => [ 'list' => [], 'pageNo' => 0, 'totalCount' => 0, ] ]; } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 获取安装记录 */ public function getInstallLog() { try { $status = $this->status; $order_no = $this->order_no; $admin_name = $this->admin_name; $store_name = $this->store_name; $start_time = $this->start_time; $end_time = $this->end_time; $min_money = $this->min_money; $max_money = $this->max_money; $admin = get_admin(); $order_query = AgentGoodsInstallLog::find()->alias('agil') ->leftJoin(['a' => Admin::tableName()], 'agil.goods_agent_admin_id = a.id') ->where(['is_delete' => 0]); if (get_supplier_id()) { $supplier = Supplier::findOne(get_supplier_id()); $cloud_goods_id = AgentGoodsBindGoods::find() ->where(['cloud_supplier_id' => $supplier->cloud_supplier_id, 'status' => 1, 'is_delete' => 0]) ->select('cloud_goods_id')->column(); $supplier_order_id = OrderDetail::find()->alias('od')->leftJoin(['g' => Goods::tableName()], 'od.goods_id = g.id') ->where(['g.cloud_goods_id' => $cloud_goods_id])->select('order_id')->column(); $supplier_purchase_order_id = PurchaseOrderDetail::find()->alias('od') ->where(['goods_id' => $cloud_goods_id])->select('order_id')->column() ?: [0]; $order_query->andWhere(['OR', ['order_type' => 0, 'order_id' => $supplier_order_id], ['order_type' => 1, 'order_id' => $supplier_order_id]]); } else { if ($admin->type === Admin::ADMIN_TYPE_GOODS_AGENT) { $order_query->andWhere(['goods_agent_admin_id' => $admin->id]); } } ;//->select('order_id')->column(); if ($status !== null && (int)$status !== -1) { $order_query->andWhere(['agil.status' => $status]); } if ($admin_name) { $order_query->andWhere(['LIKE', 'a.name', $admin_name]); } $order_arr = $order_query->groupBy('order_id')->select('agil.order_id, agil.order_type')->asArray()->all(); $order_id = [0]; $purchase_order_id = [0]; if (!empty($order_arr)) { foreach ($order_arr as $install_order_item) { if (intval($install_order_item['order_type']) === AgentGoodsInstallLog::ORDER_TYPE_NORMAL) { array_push($order_id, $install_order_item['order_id']); } if (intval($install_order_item['order_type']) === AgentGoodsInstallLog::ORDER_TYPE_PURCHASE) { array_push($purchase_order_id, $install_order_item['order_id']); } } if (!empty($supplier_order_id)) { array_push($supplier_order_id, 0); $order_id = array_intersect($supplier_order_id, $order_id); } if (!empty($supplier_purchase_order_id)) { $purchase_order_id = array_intersect($supplier_purchase_order_id, $purchase_order_id); } } $order_table_name = Order::tableName(); $purchase_order_table_name = PurchaseOrder::tableName(); $store_table_name = Store::tableName(); if (empty($order_id)) { $order_id = [0]; } if (empty($purchase_order_id)) { $purchase_order_id = [0]; } $order_id = implode(',', $order_id); $purchase_order_id = implode(',', $purchase_order_id); $fields = " id, order_no, created_at, confirm_time, pay_price, `name`, mobile, address_data, store_id "; $order_type_normal = AgentGoodsInstallLog::ORDER_TYPE_NORMAL; $order_type_purchase = AgentGoodsInstallLog::ORDER_TYPE_PURCHASE; $sql = "SELECT o.* FROM (SELECT {$fields}, {$order_type_normal} AS order_type FROM {$order_table_name} WHERE id IN ({$order_id}) AND is_delete = 0 AND trade_status IN (0, 2, 3) UNION ALL SELECT {$fields}, {$order_type_purchase} AS order_type FROM {$purchase_order_table_name} WHERE id IN ({$purchase_order_id}) AND is_delete = 0 AND trade_status IN (0, 2, 3)) AS o LEFT JOIN {$store_table_name} s ON o.store_id = s.id WHERE o.id > 0 "; $query = Order::find()->alias('o') ->where(['o.id' => $order_id, 'o.is_delete' => 0, 'o.trade_status' => [0, 2, 3]]) ->leftJoin(['s' => Store::tableName()], 'o.store_id = s.id') ->select('o.id, o.order_no, o.name, o.created_at, o.confirm_time, o.pay_price, o.name, o.mobile, o.address_data, o.store_id'); if ($order_no) { $sql .= " AND o.order_no LIKE '%{$order_no}%'"; } if ($store_name) { $sql .= " AND s.name LIKE '%{$store_name}%'"; } if ($start_time) { $start_time = strtotime($start_time); $sql .= " AND o.created_at >= {$start_time}"; } if ($end_time) { $end_time = strtotime($end_time); $sql .= " AND o.created_at <= {$end_time}"; } if ($min_money) { $sql .= " AND o.pay_price >= {$min_money}"; } if ($max_money) { $sql .= " AND o.pay_price <= {$max_money}"; } $pageNo = get_params('pageNo', get_params('page', 1)); $pageSize = get_params('pageSize', \Yii::$app->params['pageSize']); $pageInitNum = ($pageNo - 1) * $pageSize; $count = \Yii::$app->db->createCommand($sql)->query()->count(); $sql .= " ORDER BY created_at desc LIMIT {$pageSize} OFFSET {$pageInitNum}"; $list = \Yii::$app->db->createCommand($sql)->queryAll(); foreach ($list as &$item) { $item['status'] = 1; $item['created_at'] = !empty($item['created_at']) ? date('Y-m-d H:i:s', $item['created_at']) : '-'; $item['confirm_time'] = !empty($item['confirm_time']) ? date('Y-m-d H:i:s', $item['confirm_time']) : '-'; $order_detail_table_name = $item['order_type'] ? PurchaseOrderDetail::tableName() : OrderDetail::tableName(); $item['goods_list'] = AgentGoodsInstallLog::find()->alias('agil')->where(['agil.order_id' => $item['id']]) ->leftJoin(['od' => $order_detail_table_name], 'agil.order_detail_id = od.id') ->leftJoin(['a' => Admin::tableName()], 'agil.goods_agent_admin_id = a.id') ->select('od.goods_name, od.attr, od.pic, od.num, od.total_price, agil.status, agil.is_need_install, a.name admin_name')->asArray()->all(); foreach ($item['goods_list'] as &$value) { $value['status'] *= 1; $value['is_need_install'] *= 1; $value['attr'] = json_decode($value['attr'], true); if ($value['status'] === 0) { $item['status'] = 0; } if ($value['is_need_install'] === 1) { if ($item['address_data']) { $address_data = json_decode($item['address_data'], true); $address_data = $address_data['province'] . $address_data['city'] . $address_data['district'] . $address_data['detail'] ; } else { $address_data = ''; } $value['address'] = $address_data; $value['mobile'] = $item['mobile']; $value['name'] = $item['name']; } else { $store = Store::findOne($item['store_id']); $province_id = $store->province_id; $city_id = $store->city_id; $district_id = $store->district_id; $district = District::find()->where(['id' => [$province_id, $city_id, $district_id]])->select('name')->column(); $district = implode('', $district); $value['address'] = $district . $store->address; $value['mobile'] = $store->contact_tel; $value['name'] = $store->name; } } // $item['status'] *= 1; // $item['is_need_install'] *= 1; // $item['attr'] = json_decode($item['attr'], true); } return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $list, 'pageNo' => $pageNo, 'totalCount' => $count, ], ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 点击送达 */ public function setInstallStatus($order_type = 0) { $t = \Yii::$app->db->beginTransaction(); try { $id = $this->id; $admin = get_admin(); $agent_order_type = AgentGoodsInstallLog::ORDER_TYPE_NORMAL; if (intval($order_type) !== 0) { $agent_order_type = AgentGoodsInstallLog::ORDER_TYPE_PURCHASE; } $install_log = AgentGoodsInstallLog::find()->where(['order_id' => $id, 'status' => 0, 'order_type' => $agent_order_type])->asArray()->all(); if (empty($install_log)) { throw new \Exception('未查找到数据'); } $is_finish = true; foreach ($install_log as $value) { if ((int)$value['goods_agent_admin_id'] !== (int)$admin->id) { throw new \Exception('权限错误'); } if ((int)$value['status'] !== 0) { throw new \Exception('订单已经送达'); } $item = AgentGoodsInstallLog::findOne($value['id']); $item->status = 1; if (!$item->save()) { throw new \Exception(json_encode($item->errors)); } if (!$item->is_need_install) { $is_finish = false; } } //如果不需要上门安装 则发货操作由商家完成 //模拟供货商发货即可(转单) if (intval($order_type)) { $purchase_order = PurchaseOrder::findOne(['id' => $id, 'is_delete' => 0]); $cloud_supplier_id = $purchase_order->cloud_supplier_id; $cloud_order_id = $purchase_order->cloud_order_id; $goods_url = "/cloud/supplier/supplierOrderSend"; } else { $order_transit = OrderTransit::findOne(['order_id' => $id, 'is_delete' => 0]); $cloud_supplier_id = $order_transit->cloud_supplier_id; $cloud_order_id = $order_transit->cloud_order_id; $goods_url = "/cloud/supplier/supplierPurchaseSend"; } $param = [ 'order_ids' => (string)$cloud_order_id, 'access_token' => get_supplier_token($cloud_supplier_id), 'express_name' => '', 'express_no' => '', 'send_type' => 3, 'seller_desc' => '', ]; //请求接口 $domain = (new OptionSetting)->getCloudDomainName(); $goodsInfo = cloud_post($domain . $goods_url, $param); $goodsInfo = json_decode($goodsInfo, true); if ((int)$goodsInfo['code'] !== 0) { throw new \Exception($goodsInfo['msg']); } $t->commit(); return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 获取前置仓绑定的供货商 */ public function getSupplier($parent_admin_id = 0) { try { //查询前置仓 以及绑定的供货商 $front_agent = AgentBind::find()->where(['parent_admin_id' => $parent_admin_id, 'is_delete' => 0, 'type' => 2]) ->select('admin_id')->column(); if ($front_agent) { $supplier_id = AgentFrontBind::find()->where(['type' => 0, 'is_delete' => 0, 'front_agent_admin_id' => $front_agent, 'status' => 1]) ->select('type_id')->column(); } if (!empty($supplier_id)) { $supplier_id = Supplier::find()->where(['id' => $supplier_id])->select('cloud_supplier_id')->column(); } return [ 'code' => 0, 'msg' => '获取成功', 'data' => $supplier_id ?? [] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 找到同级产品代理,检测某个商品是否已经被代理并剔除 */ public function checkGoodsBind($parent_admin_id = 0, $admin_id = 0) { try { //找到同级产品代理,检测被代理的产品 $front_agent = AgentBind::find()->where(['parent_admin_id' => $parent_admin_id, 'is_delete' => 0, 'type' => 1]) ->select('admin_id')->column(); if ($admin_id) { $front_agent = array_diff($front_agent, [$admin_id]); } $bind_goods = AgentGoodsBindGoods::find()->where(['goods_agent_admin_id' => $front_agent, 'is_delete' => 0, 'status' => [0, 1]]) ->select('cloud_goods_id')->column(); return [ 'code' => 0, 'msg' => '获取成功', 'data' => $bind_goods ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }