| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- <?php
- namespace addons\CloudStock\common\services;
- use addons\CloudStock\common\enums\CloudStockEnum;
- use addons\CloudStock\common\models\CloudStockAgent;
- use addons\CloudStock\common\models\CloudStockBuyingOrder;
- use addons\CloudStock\common\models\CloudStockDeductionDetail;
- use addons\CloudStock\common\models\CloudStockGoods;
- use addons\CloudStock\common\models\CloudStockGoodsStockLog;
- use addons\CloudStock\common\models\CloudStockLossAmount;
- use addons\CloudStock\common\models\CloudStockPaymentLog;
- use addons\CloudStock\common\models\CloudStockPriceDifferenceProfit;
- 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\user\User;
- class PriceDifferenceProfitService extends BaseService
- {
- public $agent_price_arr;
- public $order_no;
- public function typeMap()
- {
- //1:上级配货,2:下级补货;3:会员买货
- return [
- 1 => '上级配货',
- 2 => '下级补货',
- 3 => '会员买货'
- ];
- }
- public function index($params)
- {
- $where = [];
- $this->getWhere($params, $where);
- $params['where'] = $where;
- $params['order'] = 'id desc';
- $page_data = CloudStockPriceDifferenceProfit::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 priceDifferenceProfitList($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);
- $params['user_id'] = $this->user_id;
- $this->getWhere($params, $where);
- $params['where'] = $where;
- $params['order'] = 'id desc';
- $page_data = CloudStockPriceDifferenceProfit::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['user_id'])) {
- $where[] = ['user_id' => $params['user_id']];
- }
- if (!empty($params['order_no'])) {
- $where[] = ['order_no' => trim($params['order_no'])];
- }
- 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 PriceDifferenceProfitService(['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 = CloudStockPriceDifferenceProfit::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 $order_no
- * @return void
- */
- public function computeProfitByDelayBuy($type, $order_no)
- {
- $order = \common\models\order\Order::findOne(['order_no' => $order_no]);
- if ($order) {
- $this->order_no = $order['order_no'];
- $goods_ids = array_column($order['detail'], 'goods_id');
- $order_detail_ids = array_column($order['detail'], 'id');
- $buy_order_list = CloudStockBuyingOrder::lists([
- 'where' => [
- ['order_detail_id' => $order_detail_ids]
- ],
- 'index' => 'goods_id'
- ], false);
- $goods_stock_logs = CloudStockGoodsStockLog::lists([
- 'where' => [
- ['goods_id' => $goods_ids],
- ['order_id' => array_column($buy_order_list, 'id')],
- ['order_type' => CloudStockEnum::STOCK_ORDER_TYPE_USER],
- ],
- ]);
- $stock_log_ids = array_column($goods_stock_logs, 'id');
- $deduction_detail_list = CloudStockDeductionDetail::lists([
- 'where' => [
- ['stock_log_id' => $stock_log_ids],
- ['>', 'user_id', 0]
- ],
- ]);
- if (!empty($deduction_detail_list)) {
- $user_ids = array_column($deduction_detail_list, 'user_id');
- $cloud_stock_agent_list = CloudStockAgent::lists(['where' => [['user_id' => $user_ids], ['status' => StatusEnum::ENABLED]], 'index' => 'user_id']);
- foreach ($deduction_detail_list as $value) {
- if (empty($buy_order_list[$value['goods_id']])) {
- continue;
- }
- if (empty($cloud_stock_agent_list[$value['user_id']])) {
- continue;
- }
- $buy_order = $buy_order_list[$value['goods_id']];
- $parent_agent = $cloud_stock_agent_list[$value['user_id']];
- $this->compute($type, $value['num'], $buy_order, $parent_agent);//配货触发差价利润金额
- }
- }
- }
- }
- /**
- * 计算价差利润
- * @param $type
- * @param $num
- * @param $order
- * @param $parent_agent
- * @return true|void
- */
- public function compute($type, $num, $order, $parent_agent = null)
- {
- $cloud_stock_goods = CloudStockGoods::getOne([
- ['goods_id' => $order['goods_id']],
- ['mall_id' => $this->mall_id],
- ['status' => StatusEnum::ENABLED],
- ]);
- if (empty($cloud_stock_goods)) {
- return true;
- }
- $agent_price_arr = json_decode($cloud_stock_goods['agent_price'], true);
- if (empty($agent_price_arr)) {
- return true;
- }
- $agent_price_arr = array_column($agent_price_arr, null, 'level');
- $this->agent_price_arr = $agent_price_arr;
- switch ($type) {
- case 1:
- $user_id = $order['user_id'];//配货者
- $child_id = $order['child_id'];//配货下级
- $this->computeByFill($type, $num, $user_id, $child_id, $order);
- break;
- case 2:
- $user_id = $parent_agent['user_id'];
- $child_id = $order['user_id'];
- $this->computeByFill($type, $num, $user_id, $child_id, $order);
- break;
- case 3:
- $user_id = $parent_agent['user_id'];
- $child_id = $order['user_id'];
- $this->computeByBuy($num, $user_id, $child_id, $order);
- break;
- }
- }
- /**
- * 会员买货
- * @param $num
- * @param $user_id
- * @param $child_id
- * @param $order
- * @return true|void
- */
- public function computeByBuy($num, $user_id, $child_id, $order)
- {
- $mall_id = $order['mall_id'];
- $goods_id = $order['goods_id'];
- $money = $order['after_distribution_price'];
- $order_no = $this->order_no;
- $cloud_stock_agent = CloudStockAgent::getOne([
- ['user_id' => $user_id],
- ['mall_id' => $mall_id],
- ['status' => StatusEnum::ENABLED],
- ]);
- $child_agent_price = $order['after_distribution_price'] / $order['num'];
- $agent_price = $this->agent_price_arr[$cloud_stock_agent['level']]['price'] ?? 0;
- $price_diff = $child_agent_price - $agent_price;
- if ($price_diff <= 0) {
- return true;
- }
- $add = [
- 'mall_id' => $mall_id,
- 'user_id' => $cloud_stock_agent['user_id'],
- 'order_no' => $order_no,
- 'num' => $num,
- 'money' => $price_diff * $num,
- 'goods_id' => $goods_id,
- 'child_id' => $child_id,
- 'type' => 3,
- ];
- CloudStockPriceDifferenceProfit::add($add);
- $cloud_stock_agent->difference_profit += $add['money'];
- $cloud_stock_agent->save();
- }
- /**
- * 补货/配货
- * @param $type
- * @param $num
- * @param $user_id
- * @param $child_id
- * @param $order
- * @return true|void
- */
- public function computeByFill($type, $num, $user_id, $child_id, $order)
- {
- $mall_id = $order['mall_id'];
- $goods_id = $order['goods_id'];
- $order_no = $order['order_no'];
- $cloud_stock_agent = CloudStockAgent::getOne([
- ['user_id' => $user_id],
- ['mall_id' => $mall_id],
- ['status' => StatusEnum::ENABLED],
- ]);
- $child_cloud_stock_agent = CloudStockAgent::getOne([
- ['user_id' => $child_id],
- ['mall_id' => $mall_id],
- ['status' => StatusEnum::ENABLED],
- ]);
- $agent_price = $this->agent_price_arr[$cloud_stock_agent['level']]['price'] ?? 0;
- $child_agent_price = $this->agent_price_arr[$child_cloud_stock_agent['level']]['price'] ?? 0;
- $price_diff = $child_agent_price - $agent_price;
- if ($price_diff <= 0) {
- return true;
- }
- $add = [
- 'mall_id' => $mall_id,
- 'user_id' => $cloud_stock_agent['user_id'],
- 'order_no' => $order_no,
- 'num' => $num,
- 'money' => $price_diff * $num,
- 'goods_id' => $goods_id,
- 'child_id' => $child_id,
- 'type' => $type,
- ];
- CloudStockPriceDifferenceProfit::add($add);
- $cloud_stock_agent->difference_profit += $add['money'];
- $cloud_stock_agent->save();
- }
- }
|