176 lines
6.1 KiB
PHP
176 lines
6.1 KiB
PHP
<?php
|
||
|
||
namespace app\admin\controller\user4s;
|
||
|
||
use app\common\controller\Backend;
|
||
use app\common\model\user4s\User;
|
||
use app\common\model\user4s\Log;
|
||
use app\common\model\user4s\Goods;
|
||
use Exception;
|
||
use think\Db;
|
||
use think\exception\PDOException;
|
||
use think\exception\ValidateException;
|
||
|
||
/**
|
||
* 兑换订单
|
||
*
|
||
* @icon fa fa-circle-o
|
||
*/
|
||
class Goodlog extends Backend
|
||
{
|
||
|
||
/**
|
||
* Goodlog模型对象
|
||
* @var \app\common\model\user4s\Goodlog
|
||
*/
|
||
protected $model = null;
|
||
protected $searchFields = 'id,description,user_name';
|
||
|
||
public function _initialize()
|
||
{
|
||
parent::_initialize();
|
||
$this->model = new \app\common\model\user4s\Goodlog;
|
||
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
||
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
|
||
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
|
||
*/
|
||
|
||
|
||
/**
|
||
* 查看
|
||
*/
|
||
public function index()
|
||
{
|
||
//当前是否为关联查询
|
||
$this->relationSearch = true;
|
||
//设置过滤方法
|
||
$this->request->filter(['strip_tags', 'trim']);
|
||
if ($this->request->isAjax()) {
|
||
//如果发送的来源是Selectpage,则转发到Selectpage
|
||
if ($this->request->request('keyField')) {
|
||
return $this->selectpage();
|
||
}
|
||
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
||
|
||
$list = $this->model
|
||
->with(['user','admin'])
|
||
->where($where)
|
||
->order($sort, $order)
|
||
->paginate($limit);
|
||
|
||
foreach ($list as $row) {
|
||
|
||
|
||
}
|
||
|
||
$result = array("total" => $list->total(), "rows" => $list->items());
|
||
|
||
return json($result);
|
||
}
|
||
return $this->view->fetch();
|
||
}
|
||
// public function add()
|
||
// {
|
||
// if (!$this->request->isPost()) {
|
||
// $user4s_id = $this->request->get("user4s_id/d");
|
||
// if ($user4s_id > 0) {
|
||
// $user4s=$this->user_model->get($user4s_id);
|
||
// if(isset($user4s->id)){
|
||
// $this->view->assign("user4s", $user4s);
|
||
// }
|
||
// }
|
||
// }
|
||
// if ($this->request->isPost()) {
|
||
// $params = $this->request->post("row/a");
|
||
// }
|
||
// return parent::add();
|
||
// }
|
||
public function add()
|
||
{
|
||
if (!$this->request->isPost()) {
|
||
$user4s_id = $this->request->get("user4s_id/d");
|
||
if ($user4s_id > 0) {
|
||
$user4s=$this->user_model->get($user4s_id);
|
||
if(isset($user4s->id)){
|
||
$this->view->assign("user4s", $user4s);
|
||
}
|
||
}
|
||
}
|
||
if ($this->request->isPost()) {
|
||
$params = $this->request->post("row/a");
|
||
if ($params) {
|
||
//写兑换记录
|
||
$this->user = new User();
|
||
$this->log = new Log();
|
||
$user = $this->user->get($params['user4s_id']);
|
||
$good = Goods::get($params['good_id']);
|
||
$num = intval($params['good_num']);
|
||
if(!$user){
|
||
$this->error('用户不存在');
|
||
}
|
||
if(!$good){
|
||
$this->error('商品不存在');
|
||
}
|
||
if(!$good){
|
||
$this->error('商品不存在');
|
||
}
|
||
if($good->inventory < $num){
|
||
$this->error('商品库存不足,当前只有'.$good->inventory.'件');
|
||
}
|
||
$need_price = $good->price * $num;
|
||
$need_integral = $good->integral * $num;
|
||
if($user->integral < $need_integral){
|
||
$this->error('用户积分不足,用户只有'.$good->integral);
|
||
}
|
||
if($user->balance < $need_price){
|
||
$this->error('用户余额不足,用户只有'.$good->balance.'元');
|
||
}
|
||
$this->user->startTrans();
|
||
try {
|
||
if($need_integral > 0 ){
|
||
$this->user->where('id', $user->id)->setDec('integral', $need_integral);
|
||
}
|
||
if($need_price > 0 ){
|
||
$this->user->where('id', $user->id)->setDec('balance', $need_price);
|
||
}
|
||
$good->where('id', $good->id)->setDec('inventory', $num);
|
||
$good->where('id', $good->id)->setInc('sales', $num);
|
||
$logok = $this->log->addLog($user->id,$this->auth->id,$need_price,$need_integral, '兑换商品'.$good->title.' x '.$num.'件');
|
||
if(!$logok){
|
||
$this->user->rollback();
|
||
$this->error('兑换失败');
|
||
}
|
||
$params['admin_id'] = $this->auth->id;
|
||
$params['good_name'] = $good->title;
|
||
$params['user_name'] = $user->name;
|
||
$params['integral'] = $need_integral;
|
||
$params['price'] = $need_price;
|
||
|
||
$result = $this->model->allowField(true)->save($params);
|
||
if($result !== false && $logok){
|
||
$this->user->commit();
|
||
$this->success('兑换成功');
|
||
}else{
|
||
$this->user->rollback();
|
||
$this->error('兑换失败');
|
||
}
|
||
} catch (ValidateException $e) {
|
||
$this->user->rollback();
|
||
$this->error($e->getMessage());
|
||
} catch (PDOException $e) {
|
||
$this->user->rollback();
|
||
$this->error($e->getMessage());
|
||
}
|
||
}
|
||
$this->error(__('Parameter %s can not be empty', ''));
|
||
}
|
||
return $this->view->fetch();
|
||
}
|
||
}
|