Files
2025-07-13 19:40:10 +08:00

414 lines
14 KiB
PHP
Executable File

<?php
namespace api\controllers\client\v1;
use addons\models\AcEmployee;
use addons\models\AcOrderPre;
use addons\models\AcStore;
use Yii;
use yii\data\Pagination;
class OrderpreController extends Common
{
public function beforeAction($action)
{
if (!$this->pid) {
$this->result('您正使用本系统内部接口,禁止非法链接使用!');
}
return parent::beforeAction($action);
}
public function actionIndex()
{
$apis = [
'list'=>'搜索询单',
'detail'=>'询单详情',
'create'=>'创建询单'
];
$this->result('您正使用CMTS-CLIENT系统询价单管理接口!', $apis, 200);
}
//订单列表
public function actionList()
{
$s = $this->search();
$res = $s['res'];
if (!$res) {
$this->result('没有查询到相应的数据!', [], 0);
}
$data = $s['data'];
$this->showOrder($res, $data);
}
private function search()
{
$pid = $this->pid;
$return = [];
$model = new AcOrderPre();
$where = $orwhere = [];
$where[]='and';
$where[] = ['=','pid',$pid];
$post = $this->postdata;
$search = isset($post['search']) ? $post['search'] : [];
//预置搜索条件,只允许查询与自己完全相关的订单
if(!$this->user_id) $this->result('用户未登陆,拒绝查询!', [], 0);
$where[] = ['=', 'from_uid', $this->user_id];
$search['phone'] = $this->user_info->mobile;
if(!$search['phone']) $this->result('没有用户手机号,查询失败!', [], 0);
$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['phone']) && !empty($search['phone'])) {
$search['phone'] = trim($search['phone']);
$where[] = ['=','from_mobile',$search['phone']];
}
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'] : '';
$EmployeeModel = new AcEmployee();
if (!empty($search['employee'])) {
$where[] = ['=','employee_id',$search['employee']];
$employee = $EmployeeModel->findOne($search['employee']);
$search['employee_title'] = isset($employee->name) ? $employee->name : '';
}
$search['store_id'] = isset($search['store_id']) ? (int)$search['store_id'] : '';
if (empty($search['sotre_id'])) {
$search['store_id'] = isset($get['store_id']) ? (int)$get['store_id'] : 0;
}
$StoreModel = new AcStore();
if (!empty($search['store_id'])) {
$where[] = ['=','store_id',$search['store_id']];
$store = $StoreModel->findOne($search['store_id']);
$search['store_title'] = isset($store->title) ? $store->title : '';
}
$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']];
} else {
unset($search['status_code']);
}
$deleted = 0;
$where[]=['=','deleted',$deleted];
$page = $this->page;
$pageSize = $this->pageSize;
$data = $model->find()->where($where)->andwhere($orwhere);
$pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => $pageSize]);
$pages->setPage($page-1, true); //设置分页的当前页面值
$_orderby = 'update_at DESC,id DESC';
$res = $data->offset($pages->offset)->limit($pages->limit)->orderby($_orderby)->all();
return ['res'=>$res,'data'=>$data];
}
private function showOrder($orderRes, $data)
{
$status = Yii::$app->params['OrderPreStatus'];
$cids = $sids = $eids = $mids = [];
if ($orderRes) {
foreach ($orderRes as $r) {
$sids[$r->store_id] = $r->store_id;
$eids[$r->employee_id] = $r->employee_id;
}
}
$where = [];
$where['pid'] = Yii::$app->session->get('pid');
$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);
$return = [];
$return['code'] = 200;
$return['msg'] = '订单查询成功!';
$return['data']= [
'total'=>$data->count(),
'orders' => $orders,
'employees'=>$employees,
'stores'=>$stores,
'page'=>$this->page
];
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
public function actionDetail()
{
$pid = $this->pid;
$status = Yii::$app->params['OrderStatus'];
$post = $this->postdata;
$id = isset($post['id']) ? $post['id'] : 0;
if ($id<=0) {
$this->result('查询参数错误!');
}
$AcOrderPre = new AcOrderPre();
$order = $AcOrderPre->findOne($id);
$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($id));
$sn .=$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]);
}
$this->result('查询成功!', $detail, 200);
}
public function actionCancel()
{
$pid = $this->pid;
$status = Yii::$app->params['OrderStatus'];
$post = $this->postdata;
$id = isset($post['id']) ? $post['id'] : 0;
if ($id<=0) {
$this->result('未指定订单!');
}
$AcOrderPre = new AcOrderPre();
$order = $AcOrderPre->findOne($id);
if ($order->from_uid != $this->user_id) {
$this->result('非本人订单,不允许操作!');
}
if ($order->status_code <= 0 || $order->status_code >= 99) {
$this->result('订单已取消或已经完成,不允许操作!');
}
$order->status_code = -1;
$order->save();
$this->result('已取消!', $order->toArray(), 200);
}
public function actionCreate()
{
/**
* 创建咨询单
* @mid [int] 平台租户身份
* @uid [int] 前端用户身份
*/
$get = Yii::$app->request->get();
$post = $this->postdata;
$pid = $this->pid;
//格式化数据 为空的项则不修改
$data['from_uid'] = $this->user_id;
//目标键=》POST键
$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) {
if (isset($post[$key]) && $post[$key] !='') {
$data[$col] = trim($post[$key]);
}
}
$cols = ['car_value'=>'car_value','from_gender'=>'from_gender'];
foreach ($cols as $col=>$key) {
$data[$col] = 0;
if (isset($post[$key]) && $post[$key] !='') {
$data[$col] = (int)$post[$key];
}
}
$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天后
$mid = isset($post['mid']) ? (int)$post['mid'] : 0 ;
$data['from_mid'] = $mid;
$data['status_code'] = 1; //订单处于创建待审核状态
$data['create_at'] = time();
$data['update_at'] = $data['create_at'];
$data['pid'] = $pid;
$data['employee_id'] = isset($post['employee_id']) ? (int)$post['employee_id'] : 0;
$data['store_id'] = isset($post['store_id']) ? (int)$post['store_id'] : 0;
//保存资料
$AcOrderPre = new AcOrderPre();
foreach ($data as $key=>$val) {
$AcOrderPre->$key = $val;
}
$res = $AcOrderPre->save();
$id = $AcOrderPre->attributes['id']; //获取插入后id
if ($res) {
$this->result('询单保存成功!', ['id' => $id], 200);
} else {
$this->result('询单保存失败!', [], 100);
}
}
/*
订单数据显示格式化
*/
public function actionEdit()
{
/**
* 更新咨询单
* @mid [int] 平台租户身份
* @uid [int] 前端用户身份
*/
$get = Yii::$app->request->get();
$post = $this->postdata;
$id = isset($post['id']) ? (int)$post['id'] : 0;
if (!$id) {
$this->result('未指定询价单!');
}
$model = new AcOrderPre();
$detail = $model->findOne($id);
if (!$detail) {
$this->result('询价单信息不存在!');
}
if ($detail->pid != $this->pid) {
$this->result('非法操作!');
}
if ($detail->from_uid != $this->user_id) {
$this->result('非法操作,请检查订单归属!');
}
//格式化数据 为空的项则不修改
//目标键=》POST键
$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',
'from_name'=>'from_name','from_mobile'=>'from_mobile',
];
foreach ($cols as $col=>$key) {
if (isset($post[$key]) && $post[$key] !='') {
$detail->$col = trim($post[$key]);
}
}
$cols = ['car_value'=>'car_value','from_gender'=>'from_gender'];
foreach ($cols as $col=>$key) {
if (isset($post[$key]) && $post[$key] !='') {
$detail->$col = (int)$post[$key];
}
}
$cols = ['remark'=>'remark'];
foreach ($cols as $col=>$key) {
if (isset($post[$key]) && $post[$key] !='') {
$detail->$col = htmlspecialchars($post[$key]);
}
}
$detail->date = isset($post['plan_date']) ? $post['plan_date']/1000 : $detail->date;
$detail->status_code = 1; //标记订单处于创建待审核状态
$detail->update_at = time();
//保存资料
$res = $detail->save();
if ($res) {
$this->result('询单保存成功!', ['id' => $id], 200);
} else {
$this->result('询单保存失败!', [], 100);
}
}
}