name; //店铺管理员名称 $title = [ 'user_name' => $saasUser->name, 'mobile' => $saasUser->mobile, 'avatar' => $saasUser->avatar ]; //获取昨日凌晨时间 $yet = strtotime(date('Y-m-d')) - (60 * 60 * 24); //获取今日凌晨时间 $td = strtotime(date('Y-m-d')); //获取明日凌晨时间 // $tot = strtotime(date('Y-m-d')) + (60 * 60 * 24); // // $timestamp = time(); //代理产品数量 $agent_goods_query = AgentGoodsBindGoods::find()->where(['goods_agent_admin_id' => $admin->id, 'is_delete' => 0]); //昨日代理产品数量 $yet_agent_goods_query = clone $agent_goods_query; $yet_agent_goods_query->andWhere(['AND', ['>', 'created_at', $yet], ['<', 'created_at', $td]]); $yet_agent_goods_count = $yet_agent_goods_query->count(); $agent_goods_count = $agent_goods_query->count(); //订单数量 $order_query = AgentGoodsInstallLog::find()->alias('ag')->leftJoin(['o' => Order::tableName()], 'ag.order_id = o.id') ->where(['ag.goods_agent_admin_id' => $admin->id])->groupBy('o.id'); //昨日订单数量 $yet_order_query = clone $order_query; $yet_order_query->andWhere(['AND', ['>', 'ag.created_at', $yet], ['<', 'ag.created_at', $td]]); $order_count = $order_query->count(); $yet_order_count = $yet_order_query->count(); //通知 $order = AgentGoodsInstallLog::find()->where(['goods_agent_admin_id' => $admin->id, 'status' => 0]) ->orderBy('created_at')->select('id, is_need_install')->asArray()->all(); $arr = []; $i = 0; foreach($order as $item){ $arr[$i]['id'] = $item['id']; if ($item['is_need_install']) { $arr[$i]['notice'] = '您有新的订单需要上门安装'; } else { $arr[$i]['notice'] = '您有新的订单需要配送'; } $i++; } $notice = $arr; return [ 'code' => 0, 'msg' => "获取成功", 'data' => [ 'notice' => $notice, 'title' => $title, 'total' => [ 'order_num' => [//订单数 'order_count' => $order_count, 'yet_order_count' => $yet_order_count ], 'goods' => [//成交额 'money_count' => $agent_goods_count, 'yet_money_count' => $yet_agent_goods_count??0 ] ] ] ]; } 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('未选择产品'); } $admin_id = $admin->id; $agent_bind = AgentBind::findOne(['admin_id' => $admin->id]); $parent_admin_id = $agent_bind->parent_admin_id; $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->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; } $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 frontAgentGoodsList() { try { $admin = get_admin(); $goods_name = $this->goods_name; if ($admin->type !== 'goods_agent') { 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; } $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 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(); $goods_name = $this->goods_name; if ($admin->type !== 'goods_agent') { throw new \Exception('权限错误'); } //获取当前的产品代理 $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]); // } $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'], ] ]; } 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]); $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')->column(); $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']); } } } $order_table_name = Order::tableName(); $purchase_order_table_name = PurchaseOrder::tableName(); $store_table_name = Store::tableName(); $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 "; if ($order_no) { $sql .= " AND o.order_no LIKE '%{$order_no}%'"; } $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'], 'agil.goods_agent_admin_id' => get_admin()->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; } } } 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; $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['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(['cloud_order_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 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() ]; } } }