2024-01-26 09:39:36 +08:00

221 lines
10 KiB
PHP
Executable File

<?php
namespace app\controller\api\dataview;
use app\common\dao\system\merchant\MerchantDao;
use app\common\model\store\order\StoreOrderProduct;
use app\common\model\store\product\Product as model;
use app\common\model\user\UserVisit;
use app\common\repositories\store\order\StoreOrderProductRepository;
use app\common\repositories\store\order\StoreOrderRepository;
use app\common\repositories\store\product\ProductRepository;
use app\common\repositories\user\UserRelationRepository;
use app\common\repositories\user\UserVisitRepository;
use crmeb\basic\BaseController;
use app\common\repositories\system\merchant\MerchantRepository as repository;
use think\App;
use think\facade\Db;
use think\exception\ValidateException;
use think\facade\Cache;
class Merchant extends BaseController
{
/**
* @var repository
*/
protected $repository;
public $areaCode; // 区县地区码
public $streetCode; // 镇街道地区码
public function __construct(App $app, repository $repository)
{
parent::__construct($app);
$this->repository = $repository;
$this->areaCode = $this->request->param('areaCode', '');
$this->streetCode = $this->request->param('streetCode', '');
if ($this->areaCode == '' && $this->streetCode == '') {
throw new ValidateException('请选择地区');
}
}
// 商户列表
public function merchantList()
{
[$page, $limit] = $this->getPage();
$where = $this->request->params([ 'status', 'statusTag', 'is_trader', 'category_id', 'type_id', 'area_id', 'street_id']);
$dao = new MerchantDao();
$query = $dao->search($where);
$count = $query->where(function ($query) {
if ($this->streetCode != '') {
$query->where('street_id', $this->streetCode);
} else {
$query->where('area_id', $this->areaCode);
}
})->count($dao->getPk());
$list = $query->page($page, $limit)->setOption('field', [])
->where(function ($query) {
if ($this->streetCode != '') {
$query->where('street_id', $this->streetCode);
} else {
$query->where('area_id', $this->areaCode);
}
})
->with([
'admin' => function ($query) {
$query->field('mer_id,account');
},
'merchantCategory',
'merchantType'
])
->order('mer_id','desc')
->field('sort, mer_id, mer_name, real_name, mer_phone, mer_address, mark, status, create_time,is_best,is_trader,type_id,category_id,copy_product_num,export_dump_num,is_margin,margin,mer_avatar')->select();
return app('json')->success(compact('count', 'list'));
}
// 商户统计
public function merchantCountMain()
{
$merId = $this->request->param('mer_id');
$today = $this->mainGroup('today', $merId);
$yesterday = $this->mainGroup('yesterday', $merId);
$lastWeek = $this->mainGroup(date('Y-m-d', strtotime('- 7day')), $merId);
$lastWeekRate = [];
foreach ($lastWeek as $k => $item) {
if ($item == $today[$k])
$lastWeekRate[$k] = 0;
else if ($item == 0)
$lastWeekRate[$k] = $today[$k];
else if ($today[$k] == 0)
$lastWeekRate[$k] = -$item;
else
$lastWeekRate[$k] = (float)bcdiv(bcsub($today[$k], $item, 4), $item, 4);
}
$day = date('Y-m-d');
return app('json')->success(compact('today', 'yesterday', 'lastWeekRate', 'day'));
}
public function mainGroup($date, $merId)
{
$userVisitRepository = app()->make(UserVisitRepository::class);
$repository = app()->make(StoreOrderRepository::class);
$relationRepository = app()->make(UserRelationRepository::class);
$payPrice = (float)$repository->dayOrderPrice($date, $merId);
$payUser = (float)$repository->dayOrderUserNum($date, $merId);
$visitNum = (float)$userVisitRepository->dateVisitUserNum($date, $merId);
$likeStore = (float)$relationRepository->dayLikeStore($date, $merId);
$productNum = \think\facade\Db::name('store_product')->alias('p')->field('count(p.product_id) as think_count')
->join('merchant m', 'm.mer_id = p.mer_id')
->whereTime('p.create_time', '<=', $date)
->where('m.mer_id', $merId)
->count();
// Db::name('store_product')->where('mer_id', $merId)->count();
return compact('productNum','payPrice', 'payUser', 'visitNum', 'likeStore');
}
public function merchantProductRanking(StoreOrderProductRepository $repository)
{
$startDate = $this->request->param('start_date');
$endDate = $this->request->param('end_date');
$merId = $this->request->param('mer_id');
$endDate = date("$endDate H:i:s", time());
// $res = Cache::store('file')->remember(self::class . '@merchantProductRanking' . $merId. $startDate. $endDate, function () use ($repository, $startDate, $endDate, $merId) {
// $list = StoreOrderProduct::getDB()->alias('A')->leftJoin('StoreOrder B', 'A.order_id = B.order_id')
// ->field(\think\facade\Db::raw('sum(A.product_num) as total,A.product_id,cart_info'))
// ->whereBetweenTime('B.pay_time', $startDate, $endDate)
// ->where('B.mer_id', $merId)
// ->where('B.paid', 1)->group('A.product_id')->limit(30)->order('total DESC')->select();
// $totalCount = 0;
// foreach ($list as $item) {
// $totalCount += $item['total'];
// }
// return compact('list', 'totalCount');
// }, 2000 + random_int(600, 1200));
$list = StoreOrderProduct::getDB()->alias('A')->leftJoin('StoreOrder B', 'A.order_id = B.order_id')
->field(\think\facade\Db::raw('sum(A.product_num) as total,A.product_id,cart_info'))
->whereBetweenTime('B.pay_time', $startDate, $endDate)
->where('B.mer_id', $merId)
->where('B.paid', 1)->group('A.product_id')->limit(30)->order('total DESC')->select();
$totalCount = 0;
foreach ($list as $item) {
$totalCount += $item['total'];
}
return app('json')->success(compact('list', 'totalCount'));
}
public function merchantProductVisit(UserVisitRepository $repository)
{
$startDate = $this->request->param('start_date');
$endDate = $this->request->param('end_date');
$merId = $this->request->param('mer_id');
$endDate = date("$endDate H:i:s", time());
// $res = Cache::store('file')->remember(self::class . '@merchantProductVisit' . $merId . $startDate. $endDate, function () use ($repository, $startDate, $endDate, $merId) {
// $list = UserVisit::getDB()->alias('A')->join('StoreProduct B', 'A.type_id = B.product_id')
// ->join('Merchant C', 'C.mer_id = B.mer_id')
// ->field(\think\facade\Db::raw('count(A.type_id) as total,B.image,B.store_name'))
// ->whereBetweenTime('A.create_time', $startDate, $endDate)
// ->where('A.type', 'product')->where('B.mer_id', $merId)->group('A.type_id')->order('total DESC')
// ->limit(30)->select();
// $totalCount = 0;
// foreach ($list as $item) {
// $totalCount += $item['total'];
// }
// return compact('list', 'totalCount');
// }, 2000 + random_int(600, 1200));
$list = UserVisit::getDB()->alias('A')->join('StoreProduct B', 'A.type_id = B.product_id')
->join('Merchant C', 'C.mer_id = B.mer_id')
->field(\think\facade\Db::raw('count(A.type_id) as total,B.image,B.store_name'))
->whereBetweenTime('A.create_time', $startDate, $endDate)
->where('A.type', 'product')->where('B.mer_id', $merId)->group('A.type_id')->order('total DESC')
->limit(30)->select();
$totalCount = 0;
foreach ($list as $item) {
$totalCount += $item['total'];
}
return app('json')->success(compact('list', 'totalCount'));
}
public function merchantProductCart(ProductRepository $repository)
{
$startDate = $this->request->param('start_date');
$endDate = $this->request->param('end_date');
$merId = $this->request->param('mer_id');
$endDate = date("$endDate H:i:s", time());
// $res = Cache::store('file')->remember(self::class . '@merchantProductCart' . $merId . $startDate. $endDate, function () use ($repository, $startDate, $endDate, $merId) {
// $list = \app\common\model\store\product\Product::getDB()->alias('A')->leftJoin('StoreCart B', 'A.product_id = B.product_id')
// ->field(\think\facade\Db::raw('sum(B.cart_num) as total,A.product_id,A.store_name,A.image'))
// ->whereBetweenTime('B.create_time', $startDate, $endDate)
// ->where('A.mer_id', $merId)
// ->where('B.product_type', 0)->where('B.is_pay', 0)->where('B.is_del', 0)
// ->where('B.is_new', 0)->where('B.is_fail', 0)->group('A.product_id')->limit(30)->order('total DESC')->select();
// $totalCount = 0;
// foreach ($list as $item) {
// $totalCount += $item['total'];
// }
// return compact('list', 'totalCount');
// }, 2000 + random_int(600, 1200));
$list = \app\common\model\store\product\Product::getDB()->alias('A')->leftJoin('StoreCart B', 'A.product_id = B.product_id')
->field(\think\facade\Db::raw('sum(B.cart_num) as total,A.product_id,A.store_name,A.image'))
->whereBetweenTime('B.create_time', $startDate, $endDate)
->where('A.mer_id', $merId)
->where('B.product_type', 0)->where('B.is_pay', 0)->where('B.is_del', 0)
->where('B.is_new', 0)->where('B.is_fail', 0)->group('A.product_id')->limit(30)->order('total DESC')->select();
$totalCount = 0;
foreach ($list as $item) {
$totalCount += $item['total'];
}
return app('json')->success(compact('list', 'totalCount'));
}
public function merchant()
{
$merId = $this->request->param('mer_id');
$merchant = Db::name('merchant')->where('mer_id', $merId)->find();
return app('json')->success(compact('merchant'));
}
}