| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755 |
- <?php
- namespace app\api\controller\v1;
- use app\api\controller\ApiController;
- use app\common\validate\IDMustBePositiveInt;
- use Lettered\Support\Exceptions\EvidentException;
- use app\http\IResponse;
- use EasyWeChat\Factory;
- use think\App;
- use think\Db;
- class Goods extends ApiController
- {
-
- /**
- * 获取用户红包信息
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/9/16 13:44
- *
- * @return \think\response\Json
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getUnrebate()
- {
- // 红包
- $rebate = model('common/GoodsOrder')
- ->where([
- 'is_pin' => 1,
- 'pin_rebate_rec' => 0,
- 'status' => 6
- ])
- ->where(['user_id' => $this->auth->user()['id']])
- ->where('pin_rebate_expired','<>', 0)
- // ->orderRaw('rand()')
- ->field('id,is_pin,pin_rebate,pin_rebate_expired,pin_rebate_rec')
- ->select();
- return $this->ApiJson(0, '红包信息', $rebate);
- }
-
- /**
- * 首页分类
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/9/15 17:44
- *
- * @return \think\response\Json
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getCategory()
- {
- // 拼团产品
- $collages = model('common/GoodsCategory')
- // ->orderRaw('rand()')
- ->field('id,name')
- ->select();
- return $this->ApiJson(0,'获取产品分类成功', $collages);
- }
- /**
- * 首页搜索
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/7/6 17:44
- *
- * @return \think\response\Json
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getGoodsBySearch()
- {
- $param = $this->request->param();
- $limit = 10;
- // 数据校验
- $valid = $this->validate($param, [
- 'page' => 'require',
- ]);
- // 错误
- if (true !== $valid){
- return $this->ApiJson(-1,$valid);
- }
- $where = [];
- //组合搜索
- !empty($param['keywords']) && $where[]
- = ['name|content', 'like', '%' . $param['keywords'] . '%'];
-
- // 拼团产品
- $collages = model('Goods')
- ->where($where)
- ->where('stock','>=',5)
- ->limit((($param['page'] - 1) * $limit) . "," . $limit)
- ->select();
- return $this->ApiJson(0,'获取产品成功', $collages);
- }
-
-
- /**
- * 首页拼团产品
- *
- * @url /goods/collage
- *
- * @return \think\response\Json
- * @throws \think\exception\DbException
- */
- public function getCollageByHome()
- {
- $param = $this->request->param();
- $where = [];
- $limit = 10;
- // 数据校验
- $valid = $this->validate($param, [
- 'page' => 'require',
- ]);
- // 错误
- if (true !== $valid){
- return $this->ApiJson(-1,$valid);
- }
- !empty($param['cateId']) && $where[]
- = ['category_id', 'eq', $param['cateId']];
-
- // 拼团产品
- $collages = model('Goods')
- ->where($where)
- ->where([ 'is_pin' => 1, 'status' => 1])
- // ->orderRaw('rand()')
- ->limit((($param['page'] - 1) * $limit) . "," . $limit)
- ->select();
- return $this->ApiJson(0,'获取产品成功', $collages);
- }
- /**
- * 获取商户的产品列表
- *
- * @url /goods/by_seller/:id
- * @param integer $id 商户ID
- *
- * @return \think\response\Json
- * @throws \Lettered\Support\Exceptions\EvidentException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getGoodsBySellerId($id)
- {
- (new IDMustBePositiveInt())->valid();
- // 分页
- $page = $this->request->param('page', 1);
- $goods = model('Goods')
- ->where(['seller_id' => $id])
- ->limit((($page - 1) * 10) . "," . 10)
- // ->order(['xx' => 'xx'])
- ->select();
- if($goods->isEmpty()){
- //
- return $this->ApiJson(0,$page > 1 ? '无更多产品' :'此商户暂无商品!');
- }
- return $this->ApiJson(0, '', $goods);
- }
- /**
- * 获取产品详情
- *
- * @url /goods/:id
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/7/3 15:07
- *
- * @param $id
- * @return \think\response\Json
- * @throws \Lettered\Support\Exceptions\EvidentException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getGoodsByID($id)
- {
- (new IDMustBePositiveInt())->valid();
- $goods = model('common/Goods')
- ->with(['seller'])
- ->find($id);
- if (!$goods){
- return $this->ApiJson(-1, '不存在商品编号');
- }
- return $this->ApiJson(0, '获取商品信息成功', $goods);
- }
- /**
- * 取得商品规格
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/6/29 11:45
- *
- * @param $id
- * @return \think\response\Json
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getGoodsSkuByID($id)
- {
- // 获取规格
- $skus = model('common/GoodsSku')
- ->where(['goods_id' => $id])
- ->select();
- $spec = [];
- $values = [];
- $ret = [];
- // 取出这个产品的SPEC SPEC_VALUE
- foreach ($skus as $sku) {
- $ret['sku'][] = [
- 'ids' => dejson($sku['spec_param']),
- 'price' => $sku['price'],
- 'pin_price' => $sku['pin_price'],
- 'stock' => $sku['stock'],
- ];
- foreach (dejson($sku['spec_param']) as $sp) {
- foreach ($sp as $sid => $vid) {
- $spec[] = model('GoodsSpec')->where(['id' => $sid])->find();
- $values[] = model('GoodsSpecValue')->where(['spec_id' => $sid, 'id' => $vid])->find();
- }
- }
- }
- // 构造
- foreach (array_unique($spec) as $s => $k) {
- $sub = [];
- foreach (array_unique($values) as $i => $v) {
- if ($v['spec_id'] == $k['id']) {
- $sub[] = [
- 'id' => $v['id'],
- 'name' => $v['value_name']
- ];
- }
- }
- $ret['spec'][] = [
- 'id' => $k['id'],
- 'name' => $k['spec_name'],
- 'sub' => $sub
- ];
- }
- return $this->ApiJson(0, '获取商品信息成功', $ret);
- }
- /**
- * 获取商品正在进行拼单
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/7/8 15:30
- *
- * @param $id
- * @return \think\response\Json
- * @throws \Lettered\Support\Exceptions\EvidentException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getGroupOrderByID($id)
- {
- (new IDMustBePositiveInt())->valid();
- $orders = model('common/GoodsOrderGroup')->with(['user'])
- ->where(['goods_id' => $id,'status' => 0])
- ->whereTime('expired_at', '>=', time())
- ->select();
- return $this->ApiJson(0, '获取商品信息成功', $orders);
- }
- /**
- * 下订单
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/7/3 10:49
- *
- * @return \think\response\Json
- */
- public function placeOrder()
- {
- $param = $this->request->param();
- // 数据校验
- $valid = $this->validate($param, [
- 'addr|送货地址' => 'require',
- 'total_price|订单总价' => 'require',
- 'goods|产品列表' => 'require',
- 'area_id|区域信息' => 'require',
- ]);
- // 错误
- if (true !== $valid){
- return $this->ApiJson(-1,$valid);
- }
- // 地址解析
- $addr = dejson($param['addr']);
- // 事务处理
- Db::startTrans();
- try {
- $all_total_price = 0; // 总需支付
- $order_idx = ""; // 相关订单
- // 循环商家产品(商家单订单多产品)
- foreach (dejson($param['goods']) as $seller){
- // 创建商家订单
- $order = model('common/GoodsOrder')::create(
- [
- 'seller_id' => $seller['id'],
- 'addr_id' => $addr['id'],
- 'user_id' => $this->auth->user()['id'],
- // 订单号
- 'order_no' => get_order_no(),
- // 订单总价后续创建完更新
- ], true
- );
- $total_price = 0; // 订单总价
- $is_pin = 0; // 是否拼团
- $joinId = 0; // 拼单ID
- foreach ($seller['goods'] as $good) {
- // 单商品总价 数量* 单价
- $good['total_price'] = sprintf("%.2f", round($good['count'] * $good['price'], 2));
- // 1. 查询某个产品库存
- $sku = model('common/GoodsSku')->getBy(['goods_id' => $good['id'],'param' => enjson($good['spec'])]);
- // 2. 库存充足 创建 / 否则回滚
- if ($sku['stock'] <= 20)
- throw new EvidentException([
- 'errmsg' => '当前商品库存不足!'
- ]);
- // 创建商家订单产品
- model('common/GoodsOrderDetail')::create(
- [
- 'order_id' => $order['id'],
- 'goods_id' => $good['id'],
- 'title' => $good['name'] . " " . arr2str($good['spec'], " "),
- 'spec' => arr2str($good['spec'], ","),
- 'count' => $good['count'],
- 'price' => $good['price'],
- 'total_price' => $good['total_price']
- ], true
- );
- $total_price += $good['total_price'];
- // 3. 总库/单库存处理
- // todo 加不加锁?
- model('common/GoodsSku')->update([
- 'stock' => $sku['stock'] - $good['count'] // 减库存
- ], ['goods_id' => $good['id'], 'param' => enjson($good['spec'])
- ]);
- // 更新
- $goods = model('common/Goods')->findBy($good['id']); // 不知道这里还要不要
- $goods->updateBy($goods['id'], [
- 'stock' => $goods['stock'] - $good['count'], // 减库存
- 'sell_count' => $goods['sell_count'] + $good['count'] // 加销量
- ]);
-
- // 拼团
- if (isset($good['is_pin']) || isset($good['is_join'])) {
- // 参团先给group_id
- isset($good['is_join']) && $joinId = $good['joinId'];
- $is_pin = 1;
- }
- }
- // 总价
- $all_total_price += $total_price;
- $order_idx .= "," . $order['id'];
- // 更新订单总价/ 所属拼单iD
- model('common/GoodsOrder')->updateBy($order['id'], [
- 'group_id' => $joinId,
- 'area_id' => $param['area_id'],
- 'total_price' => $total_price,
- 'is_pin' => $is_pin
- ]);
- }
- // 创建对应支付记录
- $trade_no = get_order_no();
- model('common/OrderPaylog')->storeBy([
- 'out_trade_no' => $trade_no,
- 'total_price' => $all_total_price,
- 'order_idx' => ltrim($order_idx, ","), // 去逗号
- 'ascription' => 'goods' // 归属订单
- ]);
- Db::commit();
- // push_socket_data('goods',[
- // 'id' => $trade_no,
- // 'msg' => '有新的商品订单等待处理,点击前往!'
- // ]);
- return $this->ApiJson(0, '下单成功',$trade_no);
- }catch (\Exception $e){
- Db::rollback();
- p($e->getMessage());
- p($e->getTraceAsString(), 1);
- return $this->ApiJson(-1, '数据异常,请稍后重试' . $e->getMessage());
- }
- }
- /**
- * 获取用户订单列表
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/7/6 9:43
- *
- * @param $action
- * @return \think\response\Json
- * @throws \Lettered\Support\Exceptions\FailedException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getUserOrder($action)
- {
- $param = $this->request->param();
- $limit = 10;
- // 数据校验
- $valid = $this->validate($param, [
- 'page' => 'require',
- ]);
- // 错误
- if (true !== $valid) {
- return $this->ApiJson(-1, $valid);
- }
- //
- $orders = model('common/GoodsOrder')->with(['seller','detail'])
- ->where(['user_id' => $this->auth->user()['id']]);
- // 条件
- switch ($action) {
- case 'unpay':
- $orders->where(['status' => 1]);
- break;
- case 'ship':
- $orders->where(['status' => 2]);
- break;
- case 'wait':
- $orders->where(['status' => 3]);
- break;
- case 'check':
- $orders->where(['status' => 4]);
- break;
- }
- return $this->ApiJson(0, '获取成功',
- $orders->limit((($param['page'] - 1) * $limit) . "," . $limit)
- ->order(['created_at' => 'desc'])
- ->select());
- }
- /**
- * 获取订单详情
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/7/6 10:08
- *
- * @param $id
- * @return \think\response\Json
- * @throws \Lettered\Support\Exceptions\EvidentException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getOrderByID($id)
- {
- (new IDMustBePositiveInt())->valid();
- $order = model('common/GoodsOrder')
- ->with(['addr'])
- ->find($id);
- if (!$order){
- return $this->ApiJson(-1, '不存在订单编号');
- }
- // 查询订单产品
- $order['detail'] = model('common/GoodsOrderDetail')
- ->with(['goods'])
- ->where(['order_id' => $order['id']])
- ->select();
- // 查询订单产品
- $order['service'] = model('common/GoodsOrderService')
- ->where(['order_id' => $order['id']])
- ->find();
- return $this->ApiJson(0, '获取订单信息成功', $order);
- }
- /**
- * 申请订单售后
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/9/21 10:08
- *
- * @param $id
- * @return \think\response\Json
- * @throws \Lettered\Support\Exceptions\EvidentException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function applyOrderService($id)
- {
- // 获取产品信息
- (new IDMustBePositiveInt())->valid();
- $order = model('common/GoodsOrder')
- ->find($id);
- if(!$order)
- return $this->ApiJson( -1, '订单数据不存在');
-
- // 查找售后状态
- $service = model('common/GoodsOrderService')->getBy(['order_id' => $order['id']]);
- // 提交申请
- if($this->request->isPost()){
- // 接收参数
- $params = $this->request->param();
- // 参数校验
- $valid = $this->validate($params, [
- 'action|售后类型' => 'require',
- 'remarks|备注说明' => 'require',
- 'attach|附件照片' => 'require'
- ]);
- // 错误返回
- if(true !== $valid){
- return $this->ApiJson(-1, $valid);
- }
-
- if (!$service) {
- unset($params['id']);
- $params['order_id'] = $order['id'];
- $params['seller_id'] = $order['seller_id'];
- // 写入数据
- $ret = model('common/GoodsOrderService')::create($params, true);
- }else {
- $params['status'] = 1;
- // 更新数据
- unset($params['id']);
- // $ret = model('common/GoodsOrderService')->update($params,['id' => $service['id']]);
- $ret = model('common/GoodsOrderService')->updateBy($service['id'],$params);
- }
- // 消息
- if ($ret){
- // 订单售后状态
- model('common/GoodsOrder')->updateBy($order['id'],[
- 'in_service' => 1
- ]);
- // 发送订阅消息
- return $this->ApiJson(0,'提交成功,请等待商家审核!');
- }
- return $this->ApiJson(-1,'数据异常,请稍后重试!');
-
- }
- // 查询时间 多少天内在售后期 读后台配置 sys_config('order_service_expired_at','store')
-
-
- return $this->ApiJson(0, '获取信息成功', ['order' => $order,
- 'service' => $service]);
- }
- /**
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/7/15 9:56
- *
- * @param $id
- * @return \think\response\Json
- * @throws \Lettered\Support\Exceptions\EvidentException
- * @throws \think\Exception
- * @throws \think\exception\PDOException
- */
- public function cancelOrderByID($id)
- {
- (new IDMustBePositiveInt())->valid();
- Db::startTrans();
- try {
- // 查订单
- $order = model('common/GoodsOrder')->where(['id' => $id])->lock(true)->find();
- if ($order['status'] == 0){
- return $this->ApiJson(-1, "当前订单已关闭");
- }
- // 取消状态应该是 未支付,待发货
- $data = [ 'status' => 0 ];
- // 待发货取消需要退钱
- if ($order['status'] == 2){
- model('common/Users')->changeBalance(
- $order['user_id'],
- $order['pay_price'] ,
- "取消订单,退还支付金额【" . $order['pay_price'] .'】',
- true);
- }
- // 获取订单产品
- $order_detail = model('common/GoodsOrderDetail')->getAll(['order_id' => $order['id']]);
- foreach ($order_detail as $goods){
- // 单产品SKU库存回增
- model('common/GoodsSku')
- ->where(['param' => enjson(str2arr($goods['spec']))])
- ->setInc('stock', $goods['count']);
- // 产品总库存更新
- model('common/Goods')
- ->where(['id' => $goods['goods_id']])
- ->setInc('stock', $goods['count']);
- // 销量减
- model('common/Goods')
- ->where(['id' => $goods['goods_id']])
- ->setDec('sell_count', $goods['count']);
- }
- // 更新订单
- $order->updateBy($order['id'], $data);
- Db::commit();
- return $this->ApiJson(0, "订单取消成功");
- }catch (\Exception $e){
- Db::rollback();
- return $this->ApiJson(-1, "系统异常,请稍后再试");
- }
- }
- /**
- * 确认订单
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/7/25 17:35
- *
- * @param $id
- * @return \think\response\Json
- * @throws \Lettered\Support\Exceptions\EvidentException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function confirmOrderByID($id)
- {
- (new IDMustBePositiveInt())->valid();
- // 查订单
- $order = model('common/GoodsOrder')->where(['id' => $id])->find();
- if ($order['status'] == 0){
- return $this->ApiJson(-1, "当前订单已关闭");
- }
-
- // 查商品信息 取得商品进货价
- // 计算利润
- // 利润的百分几
- // 利润 count * ( total_price - instock )
- // 总利润
- $cgo_total = 0;
- // 子单明细
- $its = model('common/GoodsOrderDetail')->where(['order_id' => $order['id']])->select();
- foreach ($its as $itsv) {
- // 产品
- $goin = model('common/GoodsSku')->where(['goods_id' => $itsv['goods_id'],'param' => enjson(str2arr($itsv['spec'],','))])->value('instock_price');
- $cgo_total += sprintf("%.2f", round($itsv['total_price'] - $goin, 2));
- }
-
- // 奖励期权条件 -- 资产消费不得期权
- if ($order && $order['is_pin'] !== 1 && $order['pay_type'] !== 'property'){
- // 用户期权
- // order_property_reward
- $order_property_reward = sys_config('order_property_reward','store');
- // 计算 交易金额$order['total_price'] * order_property_reward //2020 12 09改为利润$cgo_total
- $reward = sprintf("%.3f", round(((float)$order_property_reward / 100) * $cgo_total, 3));
- app()->log(enjson([$order_property_reward,$reward]),'debug');
- if($reward > 0){
- // 更新
- model('common/Users')->changeProperty(
- $order['user_id'],
- $reward,
- '订单完成,奖励资产【' . $reward . "】",
- true
- );
- }
- }
-
-
- // 查找上级
- $parent = model('common/UsersInviteRelation')->getBy(['invite_id' => $order['user_id']]);
- // 上级推荐奖励
- if ($parent['form_id'] !== 0) {
-
- // 取得收益百分几
- $user_spread_reward_scale = sys_config('user_spread_reward_scale','user');
- // 计算收益
- $reward = sprintf("%.2f", round(((float)$user_spread_reward_scale / 100) * $cgo_total, 2));
- model('common/Users')->changeBalance(
- $parent['form_id'],
- $reward,
- "下级消费奖励,奖金【" . $reward .'】',
- true
- );
- }
-
- // 更新确认订单
- $order->updateBy($order['id'], [
- 'received_at' => time(),
- 'status' => 4 // 结束订单
- ]);
- return $this->ApiJson(0, "订单已确认");
- }
- /**
- * 拼团返现领取
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/7/14 15:18
- *
- * @return \think\response\Json
- * @throws \Lettered\Support\Exceptions\EvidentException
- * @throws \think\exception\PDOException
- */
- public function getRebateByID()
- {
- (new IDMustBePositiveInt())->valid();
- try {
- // 查订单
- $order = model('common/GoodsOrder')->getBy(['id' => input('id'),'is_pin' => 1]);
- if($order){
- // 是否领取了
- if ($order['pin_rebate_rec']) {
- return $this->ApiJson(-1, '本次返现已经领取,请勿重复领取');
- }
- // 写入记录
- model('common/Users')->changeBalance(
- $order['user_id'],
- $order['pin_rebate'],
- "参团失败,领取红包奖励金额【" . $order['pin_rebate'] . '】',
- true);
- // 更新订单
- $order->updateBy($order['id'], [
- 'pin_rebate_rec' => 1,
- 'status' => 4 // 完成订单
- ]);
- return $this->ApiJson(0, '领取红包成功!');
- }
- return $this->ApiJson(-1, '订单数据不存在!');
- }catch (\Exception $e){
- return $this->ApiJson(-1, '数据异常,请稍后再试!');
- }
- }
- }
|