'上级配货', 2 => '下级补货', 3 => '会员买货' ]; } public function index($params) { $this->getWhere($params, $where); $params['where'] = $where; $params['order'] = 'id desc'; $page_data = CloudStockLossAmount::listPage($params, false, true); if (!empty($page_data['list'])) { $user_ids = array_column($page_data['list'], 'user_id'); $child_ids = array_column($page_data['list'], 'child_id'); $goods_ids = array_column($page_data['list'], 'goods_id'); $goods_list = Goods::lists(['where' => [ ['id' => $goods_ids], ['mall_id' => $this->mall_id] ], 'index' => 'id', ]); $user_list = User::lists(['where' => [ ['id' => array_merge($user_ids, $child_ids)], ['mall_id' => $this->mall_id] ], 'index' => 'id', ]); foreach ($page_data['list'] as &$item) { $item['type_text'] = $this->typeMap()[$item['type']] ?? ''; $item['user'] = $item['from_user'] = []; $item['cover_pic'] = $item['goods_name'] = ''; if (isset($user_list[$item['user_id']])) { $item['user'] = [ 'id' => $user_list[$item['user_id']]['id'], 'nickname' => $user_list[$item['user_id']]['nickname'], 'mobile' => $user_list[$item['user_id']]['mobile'], 'avatar_url' => $user_list[$item['user_id']]['avatar_url'], ]; } if (isset($goods_list[$item['goods_id']])) { $item['cover_pic'] = $goods_list[$item['goods_id']]['cover_pic']; $item['goods_name'] = $goods_list[$item['goods_id']]['goods_name']; } if (isset($user_list[$item['child_id']])) { $item['from_user'] = [ 'id' => $user_list[$item['child_id']]['id'], 'nickname' => $user_list[$item['child_id']]['nickname'], 'mobile' => $user_list[$item['child_id']]['mobile'], 'avatar_url' => $user_list[$item['child_id']]['avatar_url'], ]; } } } return $page_data; } public function lossAmountList($params) { if (empty($params['month'])) { $month = date('Y-m'); } else { $month = $params['month']; } $start_time = strtotime($month . '-01 00:00:00'); $end_time = DateHelper::getMonthLastDayTime($start_time); $params['date'][0] = date('Y-m-d', $start_time); $params['date'][1] = date('Y-m-d', $end_time); $this->getWhere($params, $where); $params['where'] = $where; $params['order'] = 'id desc'; $page_data = CloudStockLossAmount::listPage($params, false, true); if (!empty($page_data['list'])) { $user_ids = array_column($page_data['list'], 'user_id'); $child_ids = array_column($page_data['list'], 'child_id'); $goods_ids = array_column($page_data['list'], 'goods_id'); $goods_list = Goods::lists(['where' => [ ['id' => $goods_ids], ['mall_id' => $this->mall_id] ], 'index' => 'id', ]); $user_list = User::lists(['where' => [ ['id' => array_merge($user_ids, $child_ids)], ['mall_id' => $this->mall_id] ], 'index' => 'id', ]); foreach ($page_data['list'] as &$item) { $item['type_text'] = $this->typeMap()[$item['type']] ?? ''; $item['from_user_id'] = $item['from_user_mobile'] = ''; $item['goods_name'] = ''; if (isset($goods_list[$item['goods_id']])) { $item['goods_name'] = $goods_list[$item['goods_id']]['goods_name']; } if (isset($user_list[$item['child_id']])) { $item['from_user_id'] = $user_list[$item['child_id']]['id']; $item['from_user_mobile'] = $user_list[$item['child_id']]['mobile']; } } } return $page_data; } protected function getWhere($params, &$where) { $where[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]; if (!empty($params['keyword'])) { $user_list = User::lists(['where' => [ ['mall_id' => $this->mall_id], ['or', ['like', 'nickname', $params['keyword']], ['like', 'mobile', $params['keyword']]]], 'select' => 'id']); if (!empty($user_list)) { $user_ids = array_column($user_list, 'id'); $where[] = ['user_id' => $user_ids]; } else { $where[] = ['user_id' => []]; } } if (!empty($params['order_no'])) { $where[] = ['order_no' => trim($params['order_no'])]; } if (!empty($params['user_id'])) { $where[] = ['user_id' => $params['user_id']]; } if (!empty($params['type'])) { $where[] = ['type' => trim($params['type'])]; } if (!empty($params['date'][0])) { $where[] = ['>=', 'created_at', strtotime($params['date'][0] . ' 00:00:00')]; } if (!empty($params['date'][1])) { $where[] = ['<=', 'created_at', strtotime($params['date'][1] . ' 23:59:59')]; } } public function getListExportFieldList() { return $this->exportFieldsList(); } public function exportFieldsList() { return [ 'user_id' => '用户ID', 'nickname' => '用户昵称', 'mobile' => '手机号', 'type_text' => '业务类型', 'money' => '流失金额', 'order_no' => '订单编号', 'created_at' => '创建时间', ]; } public static function exportData($params) { (new LossAmountService(['mall_id' => $params['mall_id']]))->export($params); } public function add($params) { $res = CloudStockPaymentLog::add($params); if (empty($res)) { throw new \Exception(CloudStockPaymentLog::$static_error); } if (empty($params['status'])) { $params['is_frozen'] = true; } else { $params['is_frozen'] = false; } WithdrawService::handle($params); return $res; } public function export($data) { ini_set('memory_limit', '512M'); $download_id = $data['download_id'] ?? 0; $download = Download::findOne(['id' => $download_id]); if ($download) { try { $params = json_decode($download->params, true); $filename = $download->name; $type = $download->type; $where = []; $this->getWhere($params, $where); $params['where'] = $where; $params['order'] = 'id desc'; $list = CloudStockLossAmount::lists($params); $fields = $params['fields']; $fields_arr = $this->exportFieldsList(); $have_select_field_arr = []; foreach ($fields as $value) { $have_select_field_arr[] = $fields_arr[$value]; } $csv = new CsvTool(); $csv->filename = $filename; $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type); $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC; $csv->header = $have_select_field_arr; $user_id_arr = array_column($list, 'user_id'); $user_arr = User::lists([ 'where' => [ ['id' => $user_id_arr] ], 'index' => 'id', ]); $csv->data = array_map(function ($item) use ($fields, $user_arr) { $row = []; foreach ($fields as $field) { switch (true) { case $field == 'created_at': $row[] = !empty($item[$field]) ? date('Y-m-d H:i:s', $item[$field]) : ''; break; case $field == 'mobile': $row[] = $user_arr[$item['user_id']]['mobile'] ?? ''; break; case $field == 'type_text': $row[] = $this->typeMap()[$item['type']] ?? ''; break; case $field == 'nickname': $row[] = $user_arr[$item['user_id']]['nickname'] ?? ''; break; default: $row[] = !empty($item[$field]) ? $item[$field] : ''; } } return $row; }, $list); $csv->export(); $csv->updateDown($download, $params['mall_id']); return true; } catch (\Exception $e) { $download->status = 3; $res = $download->save(); return $e->getMessage(); } } } /** * 计算流失金额 * @param $type * @param $num * @param $order * @param $parent_agent * @return true|void */ public function compute($type, $num, $order, $parent_agent = null) { switch ($type) { case 1: $this->computeByDispense($num, $order); break; case 2: $this->order_no = $order['order_no']; $agent = CloudStockAgent::findOne(['user_id' => $order['user_id'], 'status' => StatusEnum::ENABLED]); $cloud_stock_goods = CloudStockGoods::findOne(['goods_id' => $order['goods_id'], 'status' => StatusEnum::ENABLED]); if (empty($cloud_stock_goods)) { throw new \Exception('商品不存在'); } $agent_price_list = json_decode($cloud_stock_goods['agent_price'], true); $agent_price_list = array_column($agent_price_list, null, 'level'); $parent_agent_price = 0; $child_agent_price = 0; if (isset($agent_price_list[$parent_agent['level']])) { $parent_agent_price = $agent_price_list[$parent_agent['level']]['price']; } if (isset($agent_price_list[$agent['level']])) { $child_agent_price = $agent_price_list[$agent['level']]['price']; } $money = ($child_agent_price - $parent_agent_price) * $order['num']; if ($money <= 0) { return true; } $this->computeByBuyOrFill($type, $num, $money, $order, $parent_agent); break; case 3: $cloud_stock_goods = CloudStockGoods::findOne(['goods_id' => $order['goods_id'], 'status' => StatusEnum::ENABLED]); if (empty($cloud_stock_goods)) { throw new \Exception('商品不存在'); } $agent_price_list = json_decode($cloud_stock_goods['agent_price'], true); $agent_price_list = array_column($agent_price_list, null, 'level'); $parent_agent_price = 0; if (isset($agent_price_list[$parent_agent['level']])) { $parent_agent_price = $agent_price_list[$parent_agent['level']]['price']; } $money = ($order['after_distribution_price'] / $order['num'] - $parent_agent_price) * $order['num']; if ($money <= 0) { return true; } $main_order = Order::findOne(['id' => $order['order_id']]); $this->order_no = $main_order['order_no']; $this->computeByBuyOrFill($type, $num, $money, $order, $parent_agent); break; } } /** * 配货 * @param $num * @param $order * @return void */ public function computeByDispense($num, $order) { $goods_id = $order['goods_id']; $order_no = $order['order_no']; $user_id = $order['user_id'];//配货者 $child_id = $order['child_id'];//配货下级 $cloud_stock_goods = CloudStockGoods::findOne(['goods_id' => $goods_id, 'status' => StatusEnum::ENABLED]); if (empty($cloud_stock_goods)) { throw new \Exception('商品不存在'); } $agent_price_list = json_decode($cloud_stock_goods['agent_price'], true); $agent_price_list = array_column($agent_price_list, null, 'level'); $parent_id_arr = UserPartitionRelationship::getParentIdArr($child_id); $parent_id_arr = array_reverse($parent_id_arr); $cloud_stock_agent = CloudStockAgent::getOne([ ['user_id' => $child_id], ['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED], ]); $parent_agent_list = CloudStockAgent::lists([ 'where' => [ ['user_id' => $parent_id_arr], ['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED], ], 'index' => 'user_id' ], false); foreach ($parent_id_arr as $uid) { if ($uid == $user_id) { break; } $agent = $parent_agent_list[$uid]; if (empty($agent)) { continue; } if ($agent['level'] <= $cloud_stock_agent['level']) { continue; } $cloud_stock_agent = $agent; if ($agent['user_id'] == $user_id) { break; } $money = 0; if (isset($agent_price_list[$agent['level']])) { $money = ($order['child_price'] - $agent_price_list[$agent['level']]['price']) * $order['num']; } $add = [ 'mall_id' => $this->mall_id, 'user_id' => $agent['user_id'], 'order_no' => $order_no, 'num' => $num, 'money' => $money, 'goods_id' => $goods_id, 'child_id' => $child_id, 'type' => 1, ]; CloudStockLossAmount::add($add); $agent->loss_amount += $money; $agent->save(); } } /** * 会员买货/补货 * @param $type * @param $num * @param $money * @param $order * @param $parent_agent * @return void */ public function computeByBuyOrFill($type, $num, $money, $order, $parent_agent) { $child_id = $order['user_id']; $add = [ 'mall_id' => $this->mall_id, 'user_id' => $parent_agent['user_id'], 'order_no' => $this->order_no, 'num' => $num, 'money' => $money, 'goods_id' => $order['goods_id'], 'child_id' => $child_id, 'type' => $type, ]; CloudStockLossAmount::add($add); $parent_agent->loss_amount += $money; $parent_agent->save(); } }