hy-farm/muchang/backend/controllers/DealController.php
2026-05-29 19:54:56 +08:00

125 lines
3.4 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: zn
* Date: 2017/6/20
* Time: 14:47
*/
namespace app\controllers;
use app\utils\InfoUtil;
use common\models\Deal;
use common\services\DealService;
use common\services\ResultStatusServices;
use common\utils\EnumUtil;
use Yii;
use yii\data\Pagination;
use yii\web\Controller;
/**
* 交易
* Class DealController
* @package app\controllers
*/
class DealController extends Controller
{
/**
* 详情
*/
public function actionDealList()
{
$type = (int)Yii::$app->request->get('type');
$page = Yii::$app->request->get('page');
if (empty($page)) $page = 1;
$page--;
$pageSize = 20;
$offset = $page * $pageSize;
$where = ['del' => EnumUtil::DELETE_NOT];
if ($type == 1) $where['complete'] = EnumUtil::COMPETE;
else if ($type == 2) $where['complete'] = EnumUtil::NOT_COMPETE;
$find = Deal::find()->where($where)->orderBy(['updated_at' => SORT_DESC]);
$totalCount = $find->count();
$dbList = $find->offset($offset)->limit($pageSize)->all();
$list = [];
foreach ($dbList as $item) {
$itemData = $item->genClientData();
$itemData['dealData'] = $itemData['data']->toReadString();
array_push($list, $itemData);
}
$pages = new Pagination(['totalCount' => $totalCount, 'pageSize' => $pageSize]);
$returnData['totalCount'] = $totalCount;
$returnData['list'] = $list;
$returnData['pages'] = $pages;
return $this->render('deal_list', $returnData);
}
public function actionDealGet()
{
$id = Yii::$app->request->get('id');
if (!isset($id)) return InfoUtil::Info($this, 'ID错误');
$item = Deal::findById($id);
if (empty($item)) return InfoUtil::Info($this, 'ID错误');
return $this->render('deal_edit', ['item' => $item]);
}
/**
* 编辑交易
*/
public function actionDealEdit()
{
$id = Yii::$app->request->get('id');
$type = Yii::$app->request->get('type');
if (!isset($id)) return InfoUtil::Info($this, 'ID错误');
if (!isset($type) || ($type != 1 && $type != 2)) return InfoUtil::Info($this, '操作类型错误');
$deal = Deal::findByID($id);
if (empty($deal)) return InfoUtil::Info($this, 'ID错误');
if ($deal->complete == EnumUtil::CONFIRM) return InfoUtil::Info($this, '完成交易不能操作');
//修改数据
$dbTrans = Yii::$app->db->beginTransaction();
try {
$rs = ResultStatusServices::RS_OPERATION_SUCCESS;
if ($type == 1) {
//完成操作
$rs = DealService::dealBackendCompete($deal->id);
} else if ($type == 2) {
//取消操作
$rs = DealService::dealBackendCancel($deal->id);
} else {
return InfoUtil::Info($this, '操作类型错误');
}
if ($rs != ResultStatusServices::RS_OPERATION_SUCCESS)
return InfoUtil::Info($this, '操作失败:%d' . $rs);
$success = true;
} finally {
if (isset($success))
$dbTrans->commit();
else {
$dbTrans->rollBack();
Yii::$app->redis->clearDirtyKey();//清除脏缓存数据
}
}
return InfoUtil::Info($this, '操作成功');
}
}