Files
ctms-yii/ctms-api/controllers/gm/v1/OrderpreController.php
T
fm453 270b881437 ADD:添加后台管理端接口;
DEL:删除不必要的头部注释;
FIX:修正若干已知错误;
2025-07-13 19:40:10 +08:00

456 lines
14 KiB
PHP
Executable File

<?php
namespace api\controllers\gm\v1;
use Yii;
use yii\data\Pagination;
use addons\models\AcEmployee;
use addons\models\AcOrderPre;
use addons\models\AcStore;
class OrderpreController extends Common
{
public function beforeAction($action)
{
if (!$this->pid) {
$this->result('您正使用本系统内部接口,禁止非法链接使用!');
}
return parent::beforeAction($action);
}
public function actionIndex()
{
$apis = [
'list' => '搜索询单',
'detail' => '询单详情',
'get-status'=>'获取询价单状态字典',
'add' => '创建询单',
'edit'=>'编辑',
'cancel'=>'取消',
'delete'=>'删除',
'ajax'=>[
'show'=>'显示',
'hide'=>'隐藏'
]
];
$this->result('您正使用CMTS-GM系统询价单管理接口!', $apis, 200);
}
//订单列表
public function actionList()
{
$s = $this->search();
$res = $s['res'];
if (!$res) {
$this->result('没有查询到相应的数据!', [], 0);
}
$_res = $this->showOrders($res);
$data = $s['data'];
$return = [
'total' => $data->count(),
'orders' => $_res['orders'],
'employees' => $_res['employees'],
'stores' => $_res['stores'],
'page' => $this->page
];
$this->result('订单查询成功',$return,200);
}
public function actionDetail()
{
$order = $this->preUpdate();
$detail = $this->showOrder($order);
$this->result('查询成功!', $detail, 200);
}
public function actionGetStatus()
{
$status = Yii::$app->params['OrderPreStatus'];
$this->result('查询成功!', $status, 200);
}
public function actionCancel()
{
$order = $this->preUpdate();
$res = $this->userPrivilege($this->user_info,'orderPreEdit',$order);
if(!$res) $this->result('您没有操作权限!',[],403);
if ($order->status_code <= 0 || $order->status_code >= 99) {
$this->result('订单已取消或已经完成,不允许操作!',[],403);
}
$order->status_code = -1;
$order->save();
$this->result('已取消!', $order, 200);
}
/**
* 创建咨询单
* @mid [int] 平台租户身份
* @uid [int] 前端用户身份
*/
public function actionAdd()
{
//数据预检查、编排
$data = $this->preSave('add');
if(!$data) $this->result('数据预检查未通过,保存失败', $data, 100);
//保存资料
$model = new AcOrderPre();
foreach ($data as $key=>$val) {
$model->$key = $val;
}
$res = $model->save();
$msg = '数据保存失败!';
if(!$res) $this->result($msg, [], 100);
$msg = '数据保存成功!';
$return = [];
$return['id']= $model->attributes['id']; //获取插入后id;
$this->result($msg,$return, 200);
}
/**
* 更新咨询单
* @mid [int] 平台租户身份
* @uid [int] 前端用户身份
*/
public function actionEdit()
{
$detail = $this->preUpdate();
$res = $this->userPrivilege($this->user_info,'orderPreEdit',$detail);
if(!$res) $this->result('您没有操作权限!',[],403);
//数据预检查、编排
$data = $this->preSave('edit');
if(!$data) $this->result('数据预检查未通过,保存失败', $data, 100);
//保存资料
foreach ($data as $key=>$val) {
$detail->$key = $val;
}
$res = $detail->save();
$msg = '数据编辑失败!';
if(!$res) $this->result($msg, [], 100);
$msg = '数据编辑成功!';
$return = [];
$return['data']= $detail;
$this->result($msg,$return, 200);
}
//删除记录
public function actionDelete()
{
$detail = $this->preUpdate();
$res = $this->userPrivilege($this->user_info,'orderPreDelete',$detail);
if(!$res) $this->result('您没有操作权限!',[],403);
if ($detail->deleted != 0) {
$this->result('数据已被删除过,操作无效', [], 400);
}
$data = [];
$data['update_at'] = time();
$data['deleted'] = $detail->deleted + 1;
//保存资料
foreach ($data as $key=>$val) {
$detail->$key = $val;
}
$res = $detail->save();
$msg = '询单记录删除失败!';
if(!$res) $this->result($msg, [], 100);
$msg = '询单记录删除成功!';
$this->result($msg,[], 200);
}
public function actionAjax()
{
$detail = $this->preUpdate();
$get = Yii::$app->request->get();
$msg = '';
$errorCode = 0;
$_time = time();
$detail->update_at = $_time;
$res = true;
switch ($get['do']) {
case 'hide':
if ($detail->status_code != 0) {
$detail->status_code = 0;
$res = $detail->save();
}
if ($res) {
$msg = '记录隐藏标记成功';
$errorCode = 200;
} else {
$msg = '记录隐藏标记失败';
$errorCode = 0;
}
break;
default:
break;
}
$this->result($msg, [], $errorCode);
}
/*onlyMe,预置搜索条件,仅查看本人运单*/
private function search($onlyMe=false)
{
$pid = $this->pid;
$model = new AcOrderPre();
$where = [];
$where[] = 'and';
$where[] = ['=', 'pid', $pid];
$post = $this->postdata;
$get = yii::$app->request->get();
$search = isset($post['search']) ? $post['search'] : [];
if($onlyMe){
//预置搜索条件,只允许查询与自己完全相关的订单
$where[] = ['=', 'from_uid', $this->user_id];
$search['phone'] = $this->user_info->mobile;
}
if (isset($search['phone']) && !empty($search['phone'])) {
$search['phone'] = trim($search['phone']);
$where[] = ['=', 'from_mobile', $search['phone']];
}
if (isset($search['carno']) && !empty($search['carno'])) {
$search['carno'] = trim($search['carno']);
$where[] = ['LIKE', 'car_no', $search['carno']];
}
if (isset($search['city']) && !empty($search['city'])) {
$search['city'] = trim($search['city']);
$where[] = ['LIKE', 'aim_city', $search['city']];
}
$search['employee'] = isset($search['employee']) ? (int)$search['employee'] : 0;
if (!empty($search['employee'])) {
$where[] = ['=', 'employee_id', $search['employee']];
}
$search['store_id'] = isset($search['store_id']) ? (int)$search['store_id'] : 0;
if (empty($search['store_id'])) {
$search['store_id'] = isset($get['store_id']) ? (int)$get['store_id'] : 0;
}
if (!empty($search['store_id'])) {
$where[] = ['=', 'store_id', $search['store_id']];
}
$search['start_province'] = isset($search['start_province']) ? trim($search['start_province']) : '';
if (!empty($search['start_province'])) {
$where[] = ['=', 'start_province', $search['start_province']];
}
$search['start_city'] = isset($search['start_city']) ? trim($search['start_city']) : '';
if (!empty($search['start_city'])) {
$where[] = ['LIKE', 'start_city', $search['start_city']];
}
$search['aim_province'] = isset($search['aim_province']) ? trim($search['aim_province']) : '';
if (!empty($search['aim_province'])) {
$where[] = ['=', 'aim_province', $search['aim_province']];
}
$search['aim_city'] = isset($search['aim_city']) ? trim($search['aim_city']) : '';
if (!empty($search['aim_city'])) {
$where[] = ['LIKE', 'aim_city', $search['aim_city']];
}
$search['bind'] = isset($search['bind']) ? trim($search['bind']) : '';
if (!empty($search['bind'])) {
$where[] = ['LIKE', 'bind', $search['bind']];
}
if (isset($search['status_code']) && $search['status_code'] != 'all') {
$search['status_code'] = (int)$search['status_code'];
$where[] = ['=', 'status_code', $search['status_code']];
}
$where[] = ['=', 'deleted', 0];
$page = $this->page;
$pageSize = $this->pageSize;
$data = $model->find()->where($where);
$pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $pageSize]);
$_orderby = 'update_at DESC,date ASC,id DESC';
$res = $data->offset($pages->offset)->limit($pages->limit)->orderby($_orderby)->all();
return ['res' => $res, 'data' => $data];
}
/*
* 数据更新前的预检查,返回对应关联数据
* 必须确保get与post数据中均包含需更新的数据id且一致
* */
private function preUpdate()
{
$id = (int)Yii::$app->request->get('id');
if(!$id) $this->result('请求错误,未携带ID参数');
$post = $this->postdata;
if($post['id'] != $id) $this->result('传参id与请求数据不匹配',[],403);
$model = new AcOrderPre();
$res = $model->findOne($id);
if(!$res) $this->result('未查询到相应数据',[],404);
if($res->pid != $this->pid) $this->result('非本平台数据,不允许操作',[],401);
return $res;
}
/*
* 数据保存前的预检查(查重、参数校验等)
* 要保存的数据[],直接从post中取出
* @op,操作类型(add,edit……)
* 校验机制:
* 编辑数据时,必须用get方式传入参数id,并与post进来的数据id进行比对,只有一致时才能继续;
* 查重:禁止录入重复数据
* 返回:校验重组后的数据
* */
private function preSave($op)
{
$post = $this->postdata;
$data = [];
$_time = time();
//格式化数据
//目标键=》POST键
switch ($op) {
case 'add':
$data['from_uid'] = $this->user_id;
$data['status_code'] = 1; //订单处于创建待审核状态
$data['create_at'] = $_time;
$data['update_at'] = $data['create_at'];
break;
case 'edit':
$id = (int)Yii::$app->request->get('id');
$_id = (int)$post['id'];
if(!$id != $_id) $this->result('id参数不匹配,请检查');
$data['update_at'] = $_time;
break;
}
$data['pid'] = $this->pid;
$cols = ['start_province' => 'start_province', 'start_city' => 'start_city', 'aim_province' => 'aim_province', 'aim_city' => 'aim_city',
'car_title' => 'car_title', 'car_no' => 'car_no', 'price' => 'price',
'bind' => 'bind', 'receptionist' => "receptionist",
'from_name' => 'from_name', 'from_mobile' => 'from_mobile',
];
foreach ($cols as $col => $key) {
$data[$col] = isset($post[$key]) ? trim($post[$key]) : '';
}
$cols = ['car_value' => 'car_value', 'from_gender' => 'from_gender','from_mid'=>'from_mid','status_code'=>'status_code'];
foreach ($cols as $col => $key) {
$data[$col] = isset($post[$key]) ? (int)$post[$key] : 0;
}
$data['employee_id'] = isset($post['employee_id']) ? (int)$post['employee_id'] : 0;
$data['store_id'] = isset($post['store_id']) ? (int)$post['store_id'] : 0;
$cols = ['remark' => 'remark', 'car' => 'car'];
foreach ($cols as $col => $key) {
if (isset($post[$key]) && $post[$key] != '') {
$data[$col] = htmlspecialchars($post[$key]);
}
}
/*@date: 1649952000000*/
$data['date'] = isset($post['plan_date']) ? $post['plan_date'] / 1000 : strtotime('+7 day');
// 计划时间,默认7天后
return $data;
}
/*
订单数据显示
*/
private function showOrders($orderRes)
{
$status = Yii::$app->params['OrderPreStatus'];
$sids = $eids = [];
if ($orderRes) {
foreach ($orderRes as $r) {
$sids[$r->store_id] = $r->store_id;
$eids[$r->employee_id] = $r->employee_id;
}
}
$model = new AcStore();
$_stores = $model->find()->where(['in', 'id', $sids])->all();
$stores = [];
foreach ($_stores as $s) {
$stores[$s->id] = $s->toArray();
}
unset($s);
$model = new AcEmployee();
$employees = [];
$_employees = $model->find()->where(['in', 'id', $eids])->indexBy('id')->all();
foreach ($_employees as $s) {
$employees[$s->id] = $s->toArray();
}
unset($s);
$orders = [];
$unsets = ['pid', 'create_at', 'deleted', 'from_mid', 'car'];
foreach ($orderRes as $s) {
$s = $s->toArray();
foreach ($unsets as $us) {
unset($s[$us]);
}
$s['price'] = !empty($s['price']) ? $s['price'] : '尚未报价';
$weekdays = [0 => '日', 1 => '一', 2 => '二', 3 => '三', 4 => '四', 5 => '五', 6 => '六'];
$wk = date('w', $s['date']);
$s['date_plan'] = date('Y-m-d ', $s['date']) . '星期' . $weekdays[$wk];
$s['from_province'] = isset($s['start_province']) ? Yii::$app->params['regionAreas'][$s['start_province']] : '';
$s['to_province'] = isset($s['aim_province']) ? Yii::$app->params['regionAreas'][$s['aim_province']] : '';
$s['status'] = $status[$s['status_code']];
$orders[] = $s;
}
unset($s);
$data = ['orders'=>$orders,'employees'=>$employees,'stores'=>$stores];
return $data;
}
private function showOrder($order)
{
$detail = $order->toArray();
if ($detail['store_id']) {
$model = new AcStore();
$store = $model->findOne($detail['store_id']);
if ($store) {
$detail['store_title'] = $store->title;
}
}
if ($detail['employee_id']) {
$model = new AcEmployee();
$employee = $model->findOne($detail['employee_id']);
if ($employee) {
$detail['employee_title'] = $employee->name;
}
}
$sn = '00000000000'; //11位
$sn = substr($sn, 0, 11 - strlen($order->id));
$sn .= $order->id;
$detail['sn'] = $detail['sn'] ? $detail['sn'] : $sn;
$weekdays = [0 => '日', 1 => '一', 2 => '二', 3 => '三', 4 => '四', 5 => '五', 6 => '六'];
$wk = date('w', $detail['date']);
$detail['date_plan'] = date('Y-m-d 星期' . $weekdays[$wk], $detail['date']);
$detail['remark'] = !empty($detail['remark']) ? htmlspecialchars_decode($detail['remark']) : '';
$detail['car'] = !empty($detail['car']) ? htmlspecialchars_decode($detail['car']) : '';
$unsets = ['create_at', 'deleted', 'from_mid'];
foreach ($unsets as $us) {
unset($detail[$us]);
}
return $detail;
}
}