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

933 lines
38 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\controller\api\community;
use app\common\model\system\merchant\Merchant;
use app\common\repositories\community\CommunityRepository;
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\system\RelevanceRepository;
use app\common\repositories\user\UserHistoryRepository;
use app\common\repositories\user\UserRelationRepository;
use app\common\repositories\user\UserRepository;
use app\common\service\JgPush;
use app\validate\api\CommunityValidate;
use crmeb\basic\BaseController;
use crmeb\services\MiniProgramService;
use think\App;
use app\common\repositories\community\CommunityRepository as repository;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Log;
class Community extends BaseController
{
/**
* @var CommunityRepository
*/
protected $repository;
protected $user;
/**
* User constructor.
* @param App $app
* @param $repository
*/
public function __construct(App $app, repository $repository)
{
parent::__construct($app);
$this->repository = $repository;
$this->user = $this->request->isLogin() ? $this->request->userInfo() : null;
if (!systemConfig('community_status') ) throw new ValidateException('未开启社区功能');
}
/**
* TODO 文章列表
* @return \think\response\Json
* @author Qinii
* @day 10/29/21
*/
public function lst()
{
$where = $this->request->params(['keyword','topic_id','is_hot','category_id','spu_id', 'is_type', 'resale_type']);
if (!$where['category_id']) {
unset($where['category_id']);
} else if ($where['category_id'] == -1) {
$where['is_type'] = $this->repository::COMMUNIT_TYPE_VIDEO;
unset($where['category_id']);
}
$where = array_merge($where,$this->repository::IS_SHOW_WHERE);
[$page, $limit] = $this->getPage();
return app('json')->success($this->repository->getApiList($where, $page, $limit, $this->user));
}
/**
* TODO 视频列表
* @return \think\response\Json
* @author Qinii
* @day 2022/11/29
*/
public function videoShow()
{
[$page, $limit] = $this->getPage();
$where['community_id'] = $this->request->param('id','');
$where = array_merge($where,$this->repository::IS_SHOW_WHERE);
return app('json')->success($this->repository->getApiVideoList($where, $page, $limit, $this->user));
}
/**
* TODO 关注的人的文章
* @param RelevanceRepository $relevanceRepository
* @return \think\response\Json
* @author Qinii
* @day 11/2/21
*/
public function focuslst(RelevanceRepository $relevanceRepository)
{
$where = $this->repository::IS_SHOW_WHERE;
$where_ = [
'left_id' => $this->user->uid ?? null ,
'type' => RelevanceRepository::TYPE_COMMUNITY_FANS,
];
$where['uids'] = $relevanceRepository->getSearch($where_)->column('right_id');
[$page, $limit] = $this->getPage();
$type = $this->request->param('type');
if ($type) $where['is_type'] = $this->repository::COMMUNIT_TYPE_VIDEO;
return app('json')->success($this->repository->getApiList($where, $page, $limit, $this->user));
}
/**
* TODO 某个用户的文章
* @param $id
* @return \think\response\Json
* @author Qinii
* @day 10/29/21
*/
public function userCommunitylst($id)
{
$where = [];
if (!$this->user || $this->user->uid != $id) {
$where = $this->repository::IS_SHOW_WHERE;
}
$where['uid'] = $id;
[$page, $limit] = $this->getPage();
return app('json')->success($this->repository->getApiList($where, $page, $limit, $this->user));
}
/**
* TODO 某个用户的视频
* @param $id
* @return \think\response\Json
* @author Qinii
* @day 10/29/21
*/
public function userCommunityVideolst($id)
{
$where = [];
[$page, $limit] = $this->getPage();
$is_start = $this->request->param('is_star',0);
if ($is_start) {
//某人赞过的视频
$where = $this->repository::IS_SHOW_WHERE;
} else {
//某个人的视频
if (!$this->user || $this->user->uid != $id) {
$where =$this->repository::IS_SHOW_WHERE;
}
$where['uid'] = $id;
}
$where['is_del'] = 0;
$where['community_id'] = $this->request->param('community_id','');
$data = $this->repository->getApiVideoList($where, $page, $limit, $this->user,$is_start);
return app('json')->success($data);
}
/**
* TODO 我赞过的文章
* @param RelevanceRepository $relevanceRepository
* @return \think\response\Json
* @author Qinii
* @day 10/28/21
*/
public function getUserStartCommunity(RelevanceRepository $relevanceRepository)
{
[$page, $limit] = $this->getPage();
$where['uid'] = $this->user->uid;
$data = $relevanceRepository->getUserStartCommunity($where,$page, $limit);
return app('json')->success($data);
}
/**
* @param $id
* @return mixed
* @author Qinii
*/
public function show($id)
{
return app('json')->success($this->repository->show($id, $this->user));
}
/**
* TODO 已购商品
* @return \think\response\Json
* @author Qinii
* @day 10/28/21
*/
public function payList()
{
[$page, $limit] = $this->getPage();
$keyword = $this->request->param('keyword');
$data = app()->make(StoreOrderProductRepository::class)->getUserPayProduct($keyword, $this->user->uid, $page, $limit);
return app('json')->success($data);
}
/**
* TODO 收藏商品
* @return \think\response\Json
* @author Qinii
* @day 10/28/21
*/
public function relationList()
{
[$page, $limit] = $this->getPage();
$keyword = $this->request->param('keyword');
$data = app()->make(UserRelationRepository::class)->getUserProductToCommunity($keyword, $this->user->uid, $page, $limit);
return app('json')->success($data);
}
public function historyList()
{
[$page, $limit] = $this->getPage();
$where['keyword'] = $this->request->param('keyword');
$where['uid'] = $this->request->userInfo()->uid;
$where['type'] = 1;
$data = app()->make(UserHistoryRepository::class)->historyLst($where, $page,$limit);
return app('json')->success($data);
}
/**
* TODO 发布文章
* @return \think\response\Json
* @author Qinii
* @day 10/29/21
*/
public function create()
{
$data = $this->checkParams();
$this->checkUserAuth();
$data['uid'] = $this->request->uid();
$res = $this->repository->create($data);
return app('json')->success(['community_id' => $res]);
}
/**
* TODO
* @return bool|\think\response\Json
* @author Qinii
* @day 10/30/21
*/
public function checkUserAuth()
{
$user = $this->request->userInfo();
if ( systemConfig('community_auth') ) {
if ($user->phone) {
return true;
}
throw new ValidateException('请先绑定您的手机号');
} else {
return true;
}
}
/**
* TODO 编辑
* @param $id
* @return \think\response\Json
* @author Qinii
* @day 10/29/21
*/
public function update($id)
{
$data = $this->checkParams();
$this->checkUserAuth();
if(!$this->repository->uidExists($id, $this->user->uid))
return app('json')->success('内容不存在或不属于您');
$this->repository->edit($id, $data);
return app('json')->success(['community_id' => $id]);
}
public function checkParams()
{
$data = $this->request->params(['image','topic_id','content','spu_id','order_id',['is_type',1],'video_link', 'resale_type', 'product_info', 'entrust_mer_id', 'entrust_day']);
$config = systemConfig(["community_app_switch",'community_audit','community_video_audit']);
$data['status'] = 0;
$data['is_show'] = 0;
if ($data['is_type'] == 1) {
if (!in_array($this->repository::COMMUNIT_TYPE_FONT,$config['community_app_switch']))
throw new ValidateException('社区图文未开启');
if ($config['community_audit']) {
$data['status'] = 1;
$data['is_show'] = 1;
$data['status_time'] = date('Y-m-d H:i:s', time());
}
} elseif ($data['is_type'] == 2) {
if (!in_array($this->repository::COMMUNIT_TYPE_VIDEO,$config['community_app_switch']))
throw new ValidateException('短视频未开启');
if ($config['community_video_audit']) {
$data['status'] = 1;
$data['is_show'] = 1;
$data['status_time'] = date('Y-m-d H:i:s', time());
}
if (!$data['video_link']) throw new ValidateException('请上传视频');
} elseif ($data['is_type'] == 4) {
if (empty($data['entrust_mer_id']) || empty($data['entrust_day'])) {
throw new ValidateException('供应链商家及委托天数不能为空');
}
$merchantInfo = Db::name('merchant')->where('uid', $this->request->uid())->find();
if ($data['entrust_mer_id'] == $merchantInfo['mer_id']) {
throw new ValidateException('委托商家不能选择自己');
}
if ($merchantInfo['credit_buy'] && ($data['entrust_day'] < $merchantInfo['settle_cycle'])) {
throw new ValidateException('委托天数不能小于结算周期');
}
if (!$merchantInfo['credit_buy'] && ($data['entrust_day'] < 15 || $data['entrust_day'] > 90)) {
throw new ValidateException('委托天数区间范围[15-90]天');
}
}
$data['content'] = filter_emoji($data['content']);
MiniProgramService::create()->msgSecCheck($this->request->userInfo(), $data['content'],3,0);
app()->make(CommunityValidate::class)->check($data);
$arr = explode("\n", $data['content']);
$title = rtrim(ltrim($arr[0]));
if (mb_strlen($title) > 40 ){
$data['title'] = mb_substr($title,0,30,'utf-8');
} else {
$data['title'] = $title;
}
if ($data['image']) $data['image'] = implode(',',$data['image']);
return $data;
}
/**
* @param $id
* @return mixed
* @author Qinii
*/
public function delete($id)
{
if (!$this->repository->uidExists($id, $this->user->uid))
return app('json')->fail('内容不存在或不属于您');
$this->repository->destory($id, $this->user);
return app('json')->success('删除成功');
}
/**
* TODO 文章点赞/取消
* @param $id
* @param RelevanceRepository $relevanceRepository
* @return \think\response\Json
* @author Qinii
* @day 10/28/21
*/
public function startCommunity($id)
{
$status = $this->request->param('status') == 1 ? 1 :0;
if (!$this->repository->exists($id))
return app('json')->fail('内容不存在');
$this->repository->setCommunityStart($id, $this->user, $status);
if ($status) {
return app('json')->success('点赞成功');
} else {
return app('json')->success('取消点赞');
}
}
/**
* TODO 用户关注/取消
* @param $id
* @param RelevanceRepository $relevanceRepository
* @return \think\response\Json
* @author Qinii
* @day 10/28/21
*/
public function setFocus($id)
{
$id = (int)$id;
$status = $this->request->param('status') == 1 ? 1 :0;
if ($this->user->uid == $id)
return app('json')->fail('请勿关注自己');
$make = app()->make(UserRepository::class);
if (!$user = $make->get($id)) return app('json')->fail('未查询到该用户');
$this->repository->setFocus($id, $this->user->uid, $status);
if ($status) {
return app('json')->success('关注成功');
} else {
return app('json')->success('取消关注');
}
}
/**
* TODO 我的粉丝
* @param RelevanceRepository $relevanceRepository
* @return \think\response\Json
* @author Qinii
* @day 10/28/21
*/
public function getUserFans(RelevanceRepository $relevanceRepository)
{
[$page, $limit] = $this->getPage();
$fans = $relevanceRepository->getUserFans($this->user->uid, $page, $limit);
return app('json')->success($fans);
}
/**
* TODO 我的关注
* @param RelevanceRepository $relevanceRepository
* @return \think\response\Json
* @author Qinii
* @day 10/28/21
*/
public function getUserFocus(RelevanceRepository $relevanceRepository)
{
[$page, $limit] = $this->getPage();
$start = $relevanceRepository->getUserFocus($this->user->uid, $page, $limit);
return app('json')->success($start);
}
/**
* TODO 用户信息
* @param $id
* @return \think\response\Json
* @author Qinii
* @day 10/28/21
*/
public function userInfo($id)
{
if (!$id) return app('json')->fail('缺少参数');
$data = $this->repository->getUserInfo($id, $this->user);
return app('json')->success($data);
}
public function getSpuByOrder($id)
{
$data = $this->repository->getSpuByOrder($id,7233);
return app('json')->success($data);
}
public function qrcode($id)
{
$id = (int)$id;
$type = $this->request->param('type');
if ($this->request->isLogin()) {
$url = $this->repository->qrcode($id, $type, $this->request->userInfo());
}
if (!$url) return app('json')->fail('二维码生成失败');
return app('json')->success(compact('url'));
}
public function getOrderList()
{
[$page, $limit] = $this->getPage();
$keyword = $this->request->param('keyword');
/** @var StoreOrderRepository $repo */
$repo = app()->make(StoreOrderRepository::class);
$list = $repo->purchaseOrder(['uid' => $this->request->uid()], $keyword, $page, $limit);
return app('json')->success($list);
}
public function product()
{
[$page, $limit] = $this->getPage();
$keyword = $this->request->param('keyword');
$list = app()->make(ProductRepository::class)->communityProduct(['uid' => $this->request->uid()], $keyword, $page, $limit);
return app('json')->success($list);
}
/**
* 转售加购
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function addCart()
{
$communityId = $this->request->param('community_id');
$cartIds = $this->repository->addCart($this->request->uid(), $communityId);
return app('json')->success(['cart_id' => $cartIds]);
}
/**
* 委托商品加购
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function addEntrustCart()
{
$communityId = $this->request->param('community_id');
$cartIds = $this->repository->addEntrustCart($this->request->uid(), $communityId);
return app('json')->success(['cart_id' => $cartIds]);
}
/**
* 转售商品列表
* @return mixed
*/
public function resaleList()
{
[$page, $limit] = $this->getPage();
$status = $this->request->param('status');
$queryBuilder = Db::name('community')->alias('c')->leftJoin('resale r','c.community_id = r.community_id')->where('c.is_type', 3)->where('c.is_del', 0)->where('c.uid', $this->request->uid());
// 在售商品
if ($status == 1) {
$queryBuilder->where('c.status', 1)->where('c.is_show', 1)->where(function ($query) {
$query->where(function ($conn) {
$conn->where('c.resale_type', 1)->where('r.status', 0);
});
$query->whereOr(function ($conn) {
$conn->where('c.resale_type', 2)->where('c.mer_status', 1)->where('r.status', 0);
});
});
}
// 已售出商品
if ($status == 2) {
$queryBuilder->where('c.status', 1)->where('r.status', 1);
}
// 待审核
if ($status == 3) {
$queryBuilder->where('c.status', 0);
}
// 审核未通过
if ($status == 4) {
$queryBuilder->where('c.status', -1);
}
// 审核通过
if ($status == 5) {
$queryBuilder->where('c.status', 1);
}
$count = $queryBuilder->count();
$list = $queryBuilder->setOption('field', [])->field(['c.community_id', 'c.title', 'c.image', 'c.resale_type', 'c.mer_status', 'SUM(`r`.`number` * `r`.`price`) AS total_price', 'SUM(`r`.`number` * `r`.`price` * (100 - `r`.`float_rate`) / 100) AS discount_price'])->group('c.community_id')->order('c.community_id', 'desc')->page($page, $limit)->fetchSql(false)->select();
if ($list) $list = $list->toArray();
foreach($list as $k => $v) {
$list[$k]['discount_price'] = round($v['discount_price'], 2);
}
return app('json')->success(compact('count', 'list'));
}
/**
* 删除转售商品
* @return mixed
*/
public function deleteResale($id)
{
$communityInfo = Db::name('community')->where('uid', $this->request->uid())->where('community_id', $id)->where('is_del', 0)->find();
if (!$communityInfo) {
return app('json')->fail('转售商品不存在');
}
Db::startTrans();
try {
$list = Db::name('resale')->where('community_id', $id)->where('is_del', 0)->where('status', 0)->select();
foreach($list as $prod) {
Db::name('store_product')->where('product_id', $prod['product_id'])->inc('stock', $prod['number'])->update();
Db::name('store_product_attr_value')->where('product_id', $prod['product_id'])->where('unique', $prod['product_attr_unique'])->inc('stock', $prod['number'])->update();
}
Db::name('resale')->where('community_id', $id)->where('status', 0)->update(['is_del' => 1]);
Db::name('community')->where('uid', $this->request->uid())->where('community_id', $id)->update(['is_del' => 1]);
Db::commit();
} catch (\Exception $e) {
Db::rollback();
return app('json')->fail('删除转售商品失败');
}
return app('json')->success('删除成功');
}
/**
* 审核转售商品结算价
* @return mixed
*/
public function checkResale($id)
{
$communityInfo = Db::name('community')->where('uid', $this->request->uid())->where('community_id', $id)->where('is_del', 0)->find();
if (!$communityInfo) {
return app('json')->fail('转售商品不存在');
}
$status = $this->request->param('status');
if (!$status) {
return app('json')->fail('请设置审核状态');
}
if ($status == 1) {
$res = Db::name('community')->where('uid', $this->request->uid())->where('community_id', $id)->where('is_del', 0)->update(['status' => $status, 'mer_status' => 1]);
if (!$res) {
return app('json')->fail('审核失败');
}
}
if ($status == 2) {
Db::startTrans();
try {
$list = Db::name('resale')->where('community_id', $id)->where('is_del', 0)->where('status', 0)->select();
foreach($list as $prod) {
Db::name('store_product')->where('product_id', $prod['product_id'])->inc('stock', $prod['number'])->update();
Db::name('store_product_attr_value')->where('product_id', $prod['product_id'])->where('unique', $prod['product_attr_unique'])->inc('stock', $prod['number'])->update();
}
Db::name('resale')->where('community_id', $id)->where('status', 0)->update(['is_del' => 1]);
Db::name('community')->where('uid', $this->request->uid())->where('community_id', $id)->where('is_del', 0)->update(['status' => -1, 'mer_status' => 2]);
Db::commit();
} catch (\Exception $e) {
Db::rollback();
return app('json')->fail('审核商品折扣价失败');
}
}
return app('json')->success('审核操作成功');
}
/**
* 转售商品详情
* @return mixed
*/
public function resaleDetail($id)
{
$communityInfo = Db::name('community')->where('uid', $this->request->uid())->where('community_id', $id)->where('is_del', 0)->find();
if (!$communityInfo) {
return app('json')->fail('转售商品不存在');
}
return app('json')->success($this->repository->show($id, $this->user));
}
/**
* 编辑转售商品
* @return mixed
*/
public function editResale($id)
{
$communityInfo = Db::name('community')->where('uid', $this->request->uid())->where('community_id', $id)->where('is_del', 0)->find();
if (!$communityInfo) {
return app('json')->fail('转售商品不存在');
}
$data = $this->checkParams();
$this->checkUserAuth();
Db::startTrans();
try {
$list = Db::name('resale')->where('community_id', $id)->where('is_del', 0)->where('status', 0)->select();
foreach($list as $prod) {
Db::name('store_product')->where('product_id', $prod['product_id'])->inc('stock', $prod['number'])->update();
Db::name('store_product_attr_value')->where('product_id', $prod['product_id'])->where('unique', $prod['product_attr_unique'])->inc('stock', $prod['number'])->update();
}
Db::name('resale')->where('community_id', $id)->where('status', 0)->update(['is_del' => 1]);
Db::name('community')->where('uid', $this->request->uid())->where('community_id', $id)->update(['is_del' => 1, 'status' => -2]);
Db::commit();
} catch (\Exception $e) {
Db::rollback();
return app('json')->fail('编辑转售商品失败');
}
$data['uid'] = $this->request->uid();
$res = $this->repository->create($data);
return app('json')->success(['community_id' => $res]);
}
/**
* 发布委托商品
* @return \think\response\Json
*/
public function entrust()
{
$data = $this->checkParams();
$typeSupplyChainId = Db::name('MerchantType')->where('type_code', Merchant::TypeCode['TypeSupplyChain'])->value('mer_type_id');
$merchantInfo = Db::name('merchant')->where('mer_id', $data['entrust_mer_id'] ?? 0)->where('type_id', $typeSupplyChainId)->fetchSql(false)->find();
if (!$merchantInfo) {
return app('json')->fail('此商户不支持委托');
}
$this->checkUserAuth();
$data['uid'] = $this->request->uid();
$res = $this->repository->create($data);
return app('json')->success(['community_id' => $res]);
}
/**
* 审核委托商品
* @return mixed
*/
public function checkEntrust($id)
{
$communityInfo = Db::name('community')->where('community_id', $id)->where('is_del', 0)->where('mer_status', 0)->find();
if (!$communityInfo) {
return app('json')->fail('委托商品不存在');
}
if ($communityInfo['mer_status'] > 0) {
return app('json')->fail('该委托商品已审核');
}
$status = $this->request->param('status');
if (!$status) {
return app('json')->fail('请设置审核状态');
}
// 同意
if ($status == 1) {
$merchantId = Db::name('merchant')->where('uid', $this->request->uid())->value('mer_id');
if ($merchantId != $communityInfo['entrust_mer_id']) {
return app('json')->fail('当前商户无审核此委托商品权限');
}
$res = Db::name('community')->where('community_id', $id)->where('is_del', 0)->update(['status' => $status, 'mer_status' => 1, 'entrust_start_date' =>date('Y-m-d H:i:s')]);
if (!$res) {
return app('json')->fail('审核失败');
}
}
// 拒绝
if ($status == 2) {
$merchantId = Db::name('merchant')->where('uid', $this->request->uid())->value('mer_id');
if ($merchantId != $communityInfo['entrust_mer_id']) {
return app('json')->fail('当前商户无审核此委托商品权限');
}
$refusal = $this->request->param('refusal', '');
Db::startTrans();
try {
$list = Db::name('entrust')->where('community_id', $id)->where('is_del', 0)->where('status', 0)->select();
foreach($list as $prod) {
Db::name('store_product')->where('product_id', $prod['product_id'])->inc('stock', $prod['number'])->update();
Db::name('store_product_attr_value')->where('product_id', $prod['product_id'])->where('unique', $prod['product_attr_unique'])->inc('stock', $prod['number'])->update();
}
Db::name('entrust')->where('community_id', $id)->where('status', 0)->update(['is_del' => 1]);
Db::name('community')->where('community_id', $id)->where('is_del', 0)->update(['refusal' => $refusal, 'status' => -1, 'mer_status' => 2]);
Db::commit();
} catch (\Exception $e) {
Db::rollback();
return app('json')->fail('审核委托商品失败');
}
}
// 删除
if ($status == 3) {
if ($communityInfo['uid'] != $this->request->uid()) {
return app('json')->fail('当前商户无删除此委托商品权限');
}
Db::startTrans();
try {
$list = Db::name('entrust')->where('community_id', $id)->where('is_del', 0)->where('status', 0)->select();
foreach($list as $prod) {
Db::name('store_product')->where('product_id', $prod['product_id'])->inc('stock', $prod['number'])->update();
Db::name('store_product_attr_value')->where('product_id', $prod['product_id'])->where('unique', $prod['product_attr_unique'])->inc('stock', $prod['number'])->update();
}
Db::name('entrust')->where('community_id', $id)->where('status', 0)->update(['is_del' => 1]);
Db::name('community')->where('community_id', $id)->where('is_del', 0)->update(['status' => -2, 'is_del' => 1]);
Db::commit();
} catch (\Exception $e) {
Db::rollback();
return app('json')->fail('删除委托商品失败');
}
}
return app('json')->success('审核操作成功');
}
/**
* 供应链商家列表
* @return mixed
*/
public function supplychainList()
{
$typeSupplyChainId = Db::name('MerchantType')->where('type_code', Merchant::TypeCode['TypeSupplyChain'])->value('mer_type_id');
[$page, $limit] = $this->getPage();
$queryBuilder = Db::name('merchant')->where('type_id', $typeSupplyChainId)->where('is_del', 0)->where('status', 1);
$count = $queryBuilder->count();
$list = $queryBuilder->setOption('field', [])->field(['mer_id', 'mer_name', 'area_id', 'street_id', 'village_id', 'mer_avatar', 'mer_banner', 'mer_address'])->order('mer_id', 'desc')->page($page, $limit)->fetchSql(false)->select();
return app('json')->success(compact('count', 'list'));
}
/**
* 委托商品列表
* @return mixed
*/
public function entrustList()
{
[$page, $limit] = $this->getPage();
$status = $this->request->param('status', -1);
$type = $this->request->param('type');
$queryBuilder = Db::name('community')->alias('c')->leftJoin('merchant m','m.mer_id = c.entrust_mer_id')->where('c.is_type', 4)->where('c.is_del', 0);
// type1发起的委托 2收到的委托
if ($type == 1) {
$queryBuilder = $queryBuilder->where('c.uid', $this->request->uid());
} else {
$merchantId = Db::name('merchant')->where('uid', $this->request->uid())->value('mer_id', -1);
$queryBuilder = $queryBuilder->where('c.entrust_mer_id', $merchantId);
}
// 待审核
if ($status == 0) {
$queryBuilder->where('c.mer_status', 0);
}
// 审核通过
if ($status == 1) {
$queryBuilder->where('c.mer_status', 1);
}
// 审核拒绝
if ($status == 2) {
$queryBuilder->where('c.mer_status', 2);
}
$count = $queryBuilder->count();
$list = $queryBuilder->setOption('field', [])->field(['c.community_id', 'c.uid', 'c.title', 'c.image', 'c.entrust_mer_id', 'c.mer_status', 'c.entrust_order_id', 'c.entrust_finish', 'c.entrust_start_date'])->order('c.community_id', 'desc')->page($page, $limit)->fetchSql(false)->select();
if ($list) $list = $list->toArray();
foreach($list as $k => $v) {
$list[$k]['entrust_day'] = Db::name('entrust')->where('community_id', $v['community_id'])->value('entrust_day', 0);
$list[$k]['mer_info'] = Db::name('merchant')->where('uid', $v['uid'])->where('is_del', 0)->field(['mer_id', 'mer_name', 'mer_address', 'mer_avatar', 'credit_buy', 'settle_cycle', 'interest_rate'])->find();
$list[$k]['entrust_mer_info'] = Db::name('merchant')->where('mer_id', $v['entrust_mer_id'])->where('is_del', 0)->field(['mer_id', 'mer_name', 'mer_address', 'mer_avatar', 'credit_buy', 'settle_cycle', 'interest_rate'])->find();
}
return app('json')->success(compact('count', 'list'));
}
/**
* 委托商品详情
* @return mixed
*/
public function entrustDetail($id)
{
$communityInfo = Db::name('community')->where('community_id', $id)->where('is_del', 0)->field(['community_id', 'title', 'image', 'content', 'uid', 'mer_status', 'entrust_mer_id', 'entrust_start_date', 'entrust_finish', 'entrust_finish_refusal'])->find();
if (!$communityInfo) {
return app('json')->fail('委托商品不存在');
}
// $uid = $this->request->uid();
$entrust = Db::name('entrust')->where('community_id', $id)->find();
if (!$entrust) {
return app('json')->fail('委托商品不存在');
}
$entrustDay=$entrust['entrust_day'];
$communityInfo['mer_info'] = Db::name('merchant')->where('mer_id', $entrust['mer_id'])->where('is_del', 0)->field(['mer_id', 'mer_name', 'mer_address', 'mer_avatar', 'settle_cycle', 'interest_rate'])->find();
$communityInfo['entrust_mer_info'] = Db::name('merchant')->where('mer_id', $communityInfo['entrust_mer_id'])->where('is_del', 0)->field(['mer_id', 'mer_name', 'mer_address', 'mer_avatar'])->find();
if (empty($communityInfo['mer_info']) || empty($communityInfo['entrust_mer_info'])) {
return app('json')->fail('无权限查看委托商品');
}
$communityInfo['entrust_day'] = $entrustDay;
$communityInfo['product_list'] = Db::name('entrust')->alias('e')->leftJoin('store_product sp','e.product_id = sp.product_id')->leftJoin('store_product_attr_value spav','spav.unique = e.product_attr_unique')->where('e.community_id', $id)->where('sp.is_del', 0)->setOption('field', [])->field(['e.product_id, e.product_attr_unique, e.number, e.price, e.status, spav.price as old_price, sp.store_name, sp.image'])->select();
if ($communityInfo['product_list']) $communityInfo['product_list'] = $communityInfo['product_list']->toArray();
foreach($communityInfo['product_list'] as $k => $v) {
$communityInfo['product_list'][$k]['image'] = explode(',', $v['image']);;
}
return app('json')->success($communityInfo);
}
/**
* 编辑委托商品
* @return mixed
*/
public function editEntrust($id)
{
$communityInfo = Db::name('community')->where('community_id', $id)->where('is_del', 0)->where('mer_status', 0)->find();
if (!$communityInfo) {
return app('json')->fail('委托商品不存在');
}
if ($communityInfo['mer_status'] > 0) {
return app('json')->fail('该委托商品已审核');
}
if ($communityInfo['uid'] != $this->request->uid()) {
return app('json')->fail('当前商户无编辑此委托商品权限');
}
$this->checkUserAuth();
Db::startTrans();
try {
$list = Db::name('entrust')->where('community_id', $id)->where('is_del', 0)->where('status', 0)->select();
foreach($list as $prod) {
Db::name('store_product')->where('product_id', $prod['product_id'])->inc('stock', $prod['number'])->update();
Db::name('store_product_attr_value')->where('product_id', $prod['product_id'])->where('unique', $prod['product_attr_unique'])->inc('stock', $prod['number'])->update();
}
Db::name('entrust')->where('community_id', $id)->where('status', 0)->update(['is_del' => 1]);
Db::name('community')->where('community_id', $id)->where('is_del', 0)->update(['status' => -2, 'is_del' => 1]);
Db::commit();
} catch (\Exception $e) {
Db::rollback();
return app('json')->fail('编辑委托商品失败');
}
$data = $this->checkParams();
$typeSupplyChainId = Db::name('MerchantType')->where('type_code', Merchant::TypeCode['TypeSupplyChain'])->value('mer_type_id');
$merchantInfo = Db::name('merchant')->where('mer_id', $data['entrust_mer_id'] ?? 0)->where('type_id', $typeSupplyChainId)->fetchSql(false)->find();
if (!$merchantInfo) {
return app('json')->fail('此商户不支持委托');
}
$data['uid'] = $this->request->uid();
$res = $this->repository->create($data);
return app('json')->success(['community_id' => $res]);
}
/**
* 供应链商家申请结束商品委托
* @return mixed
*/
public function applyFinishEntrust($id)
{
$communityInfo = Db::name('community')->where('community_id', $id)->where('mer_status', 1)->whereIn('entrust_finish', [0, 2])->where('is_del', 0)->find();
if (!$communityInfo) {
return app('json')->fail('当前状态委托商品不存在');
}
$merchantId = Db::name('merchant')->where('uid', $this->request->uid())->value('mer_id');
if ($merchantId != $communityInfo['entrust_mer_id']) {
return app('json')->fail('当前商户无申请结束此委托商品权限');
}
$entrustInfo = Db::name('entrust')->where('community_id', $id)->where('is_del', 0)->find();
//委托周期截止5天内可申请结束委托
$limitTime = strtotime($communityInfo['entrust_start_date']) + ($entrustInfo['entrust_day'] ?? 0) * 86400 - 5 * 86400;
if (time() < $limitTime) {
//return app('json')->fail('委托周期截止5天内可申请结束委托');
}
$res = Db::name('community')->where('community_id', $id)->where('is_del', 0)->update(['entrust_finish' => 3]);
if (!$res) {
return app('json')->fail('申请操作失败');
}
return app('json')->success('申请操作成功');
}
/**
* 结束商品委托
* @return mixed
*/
public function finishEntrust($id)
{
$communityInfo = Db::name('community')->where('community_id', $id)->where('mer_status', 1)->where('entrust_finish', 3)->where('is_del', 0)->find();
if (!$communityInfo) {
return app('json')->fail('当前状态委托商品不存在');
}
if ($this->request->uid() != $communityInfo['uid']) {
return app('json')->fail('当前商户无结束此委托商品权限');
}
$status = $this->request->param('status');
if (!$status) {
return app('json')->fail('请设置状态');
}
// 同意
if ($status == 1) {
$entrustInfo = Db::name('entrust')->where('community_id', $id)->where('is_del', 0)->find();
//委托周期截止5天内可申请结束委托
$limitTime = strtotime($communityInfo['entrust_start_date']) + ($entrustInfo['entrust_day'] ?? 0) * 86400 - 5 * 86400;
if (time() < $limitTime) {
//return app('json')->fail('委托周期截止5天内可申请结束委托');
}
$res = Db::name('community')->where('community_id', $id)->where('is_del', 0)->update(['entrust_finish_refusal' => '', 'entrust_finish' => 1]);
if (!$res) {
return app('json')->fail('结束操作失败');
}
}
// 拒绝
if ($status == 2) {
$refusal = $this->request->param('refusal', '');
$res = Db::name('community')->where('community_id', $id)->where('is_del', 0)->update(['entrust_finish_refusal' => $refusal, 'entrust_finish' => 2]);
if (!$res) {
return app('json')->fail('结束操作失败');
}
}
return app('json')->success('结束操作成功');
}
}