【类 型】:factor 锁定航线 和 处理 订单表 融合处理

【原  因】:采用事务处理 保证两表同步更新
【过  程】:用$rsDb->startTrans();  把之前的lockSite 和 questAss函数 融合到一起 采用事务处理
【影  响】:
This commit is contained in:
szdot 2024-07-29 00:21:49 +08:00
parent f1bb039101
commit 476c54f3b4

View File

@ -156,61 +156,6 @@ class PlaneController extends PublicController
echo json_encode(array('status' => 0, 'msg' => '暂无站点数据'));
}
}
/**
* @description: 飞机在站点上注册航线或注销航线
*/
public function lockSite()
{
//总管理员可接收任何shop_id非总管理员只可以调用自身shop_id否则会中断
$this->isPower();
if ($_REQUEST['id'] && $_REQUEST['shop_id']) {
$rsDb = D('receive_site');
$where['id'] = $_REQUEST['id'];
$where['shop_id'] = $_REQUEST['shop_id'];
// 检查站点是否绑定了航线
$rsCheck = $rsDb->where($where)->find();
if (!$rsCheck || $rsCheck['bind_route'] === null || $rsCheck['bind_route'] === '') {
exit(json_encode(array('status' => 0, 'msg' => '此站点未绑定航线!')));
}
// 获取传入的 runing 字段,并按逗号分割成数组,过滤掉空字符串和 null
$submittedRuning = $_REQUEST['runing'] ?? '';
$submittedRuningArray = array_filter(explode(',', $submittedRuning), function ($value) {
return trim($value) !== '';
});
// 构建查询条件,排除当前站点并且 id 不等于当前 id 的记录
$whereAll['shop_id'] = $_REQUEST['shop_id'];
$whereAll['id'] = array('neq', $_REQUEST['id']);
// 查询数据库中符合条件的所有记录
$otherRecords = $rsDb->where($whereAll)->select();
foreach ($otherRecords as $record) {
$existingRuning = $record['runing'] ?? '';
$existingRuningArray = array_filter(explode(',', $existingRuning), function ($value) {
return trim($value) !== '';
});
// 检查两个数组是否有重复元素
$intersection = array_intersect($submittedRuningArray, $existingRuningArray);
if (!empty($intersection)) {
exit(json_encode(array('status' => 0, 'msg' => '提交的航线与其他站点重复!')));
}
}
// 如果 $_REQUEST['runing'] 为空字符串 或者null将其设置为 null
$data['runing'] = trim($_REQUEST['runing']) === '' ? null : trim($_REQUEST['runing']);
if ($rsDb->where($where)->save($data)) {
exit(json_encode(array('status' => 1, 'msg' => '站点航线执行操作成功!')));
} else {
exit(json_encode(array('status' => 0, 'msg' => '站点航线执行操作失败!')));
}
} else {
exit(json_encode(array('status' => 0, 'msg' => '参数有误')));
}
}
/**
* @description: 创建新站点
*/
@ -573,6 +518,107 @@ class PlaneController extends PublicController
echo json_encode(array('status' => 0, 'msg' => '删除失败'));
}
}
/**
* @description: 飞机在站点上注册航线或注销航线 并处理对应的订单 ps:用处理事务 保证两表同步
*/
public function lockSite()
{
//总管理员可接收任何shop_id非总管理员只可以调用自身shop_id否则会中断
$this->isPower();
if ($_REQUEST['site_id'] && $_REQUEST['order_id'] && $_REQUEST['shop_id']) {
// 公用条件
$where['shop_id'] = $_REQUEST['shop_id'];
// 获取模型
$rsDb = D('receive_site');
$orderDb = D('order');
// 启动事务
$rsDb->startTrans();
try {
/*航线表操作*/
$where['id'] = $_REQUEST['site_id'];
// 检查站点是否绑定了航线
$rsCheck = $rsDb->where($where)->find();
if (!$rsCheck || $rsCheck['bind_route'] === null || $rsCheck['bind_route'] === '') {
throw new Exception('此站点未绑定航线!');
}
// 获取传入的 runing 字段,并按逗号分割成数组,过滤掉空字符串和 null
$submittedRuning = $_REQUEST['runing'] ?? '';
$submittedRuningArray = array_filter(explode(',', $submittedRuning), function ($value) {
return trim($value) !== '';
});
// 构建查询条件,排除当前站点并且 id 不等于当前 id 的记录
$whereAll['shop_id'] = $_REQUEST['shop_id'];
$whereAll['id'] = array('neq', $_REQUEST['id']);
// 查询数据库中符合条件的所有记录
$otherRecords = $rsDb->where($whereAll)->select();
foreach ($otherRecords as $record) {
$existingRuning = $record['runing'] ?? '';
$existingRuningArray = array_filter(explode(',', $existingRuning), function ($value) {
return trim($value) !== '';
});
// 检查两个数组是否有重复元素
$intersection = array_intersect($submittedRuningArray, $existingRuningArray);
if (!empty($intersection)) {
throw new Exception('提交的航线与其他站点重复!');
}
}
// 如果 $_REQUEST['runing'] 为空字符串 或者null将其设置为 null
$data['runing'] = trim($_REQUEST['runing']) === '' ? null : trim($_REQUEST['runing']);
if (!$rsDb->where($where)->save($data)) {
exit(json_encode(array('status' => 1, 'msg' => '站点航线执行操作成功!')));
throw new Exception('站点航线执行操作失败!');
}
/*订单表操作*/
//前端提交数据校验
$where['id'] = intval($_REQUEST['order_id']);
$field = array('main_status', 'openid');
$order = $orderDb->where($where)->field($field)->find();
//只处理主状态已付款的订单 其他状态跳出
if ($order['main_status'] != '已付款') {
throw new Exception('参数有误');
}
//操作数据库 只能操作 主状态 执行状态 退款状态 执行飞机
if ($_REQUEST['main_status']) {
$data['main_status'] = htmlspecialchars($_REQUEST['main_status']);
}
if ($_REQUEST['shipment_status']) {
$data['shipment_status'] = htmlspecialchars($_REQUEST['shipment_status']);
}
if ($_REQUEST['refund_status']) {
$data['refund_status'] = htmlspecialchars($_REQUEST['refund_status']);
}
if ($_REQUEST['by_plane_id']) {
$data['by_plane_id'] = ($_REQUEST['by_plane_id'] == 'null') ? null : intval($_REQUEST['by_plane_id']);
}
if ($orderDb->where($where)->save($data)) { //修改数据
$topicPrefix = makeTopicPrefix($order['openid']); //小程序端用户订阅主题的前缀 ps:订单对应的用户的openid算出来的
// 提醒小程序端 刷新订单列表(mqtt)
$this->publish('refreshOrderList/' . $topicPrefix, 1);
} else {
throw new Exception('订单修改失败');
}
/*提交事务*/
$rsDb->commit();
echo json_encode(array('status' => 1, 'msg' => '航线锁定和订单修改成功'));
} catch (Exception $e) {
/*回滚事务*/
$rsDb->rollback();
exit(json_encode(array('status' => 0, 'msg' => $e)));
}
} else {
exit(json_encode(array('status' => 0, 'msg' => '参数有误')));
}
}
/**
* @description: 只处理 主状态 已付款 的订单
*/