| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- <?php
- namespace addons\CloudStock\common\services;
- use addons\CloudStock\common\models\CloudStockAgent;
- use addons\CloudStock\common\models\CloudStockGoods;
- use addons\CloudStock\common\models\CloudStockLossAmount;
- use addons\CloudStock\common\models\CloudStockPaymentLog;
- use common\components\export\CsvTool;
- use common\enums\DownloadEnum;
- use common\enums\StatusEnum;
- use common\helpers\DateHelper;
- use common\models\common\Download;
- use common\models\goods\Goods;
- use common\models\order\Order;
- use common\models\user\User;
- use common\models\user\UserPartitionRelationship;
- class LossAmountService extends BaseService
- {
- public $order_no;
- public function typeMap()
- {
- //1:上级配货,2:下级补货;3:会员买货
- return [
- 1 => '上级配货',
- 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();
- }
- }
|