Order.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Exception;
  6. class Order extends Api
  7. {
  8. protected $noNeedLogin = [];
  9. protected $noNeedRight = '*';
  10. protected $model = null;
  11. public function _initialize()
  12. {
  13. parent::_initialize();
  14. $this->model = Db::name('order');
  15. }
  16. /**
  17. * 列表
  18. * @return void
  19. */
  20. public function getList(){
  21. try {
  22. $status = input('status',0);//状态:1=待支付,2=待处理,3=已完成,4=已取消
  23. $o = 'order';
  24. $st = 'servicetype';
  25. $p = 'package';
  26. $field = $o.'.id,check_code,orderno,server_time,server_info,server_images,'.$o.'.status,user_mobile,'.$o.'.user_car_number,'.
  27. $o.'.createtime,cancel_time,cancel_reason,pay_fee,appen_fee,finish_time,'.$st.'.title as `service_title`,paytype,'.$p.'.title as `package_title`';
  28. $where[$o.'.user_id'] = $this->auth->id;
  29. $where[$o.'.company_id'] = $this->auth->company_id;
  30. if (!empty($status)) {
  31. $where[$o.'.status'] = $status;
  32. }
  33. $result = $this->model->alias($o)->field($field)
  34. ->join($st,$st.'.id = '.$o.'.servicetype_id','LEFT')
  35. ->join($p,$p.'.id = '.$o.'.package_id','LEFT')
  36. ->where($where)->order($o.'.createtime desc')->autopage()->select();
  37. if (!empty($result)) {
  38. $statusArr = [2=>'待处理',3=>'已完成',4=>'已取消'];
  39. $paytypeArr = [1=>'线下',2=>'余额',3=>'微信'];
  40. $timeArr = ['createtime','cancel_time','finish_time','server_time'];
  41. foreach ($result as $key => &$value) {
  42. foreach ($timeArr as $k => $v) {
  43. $value[$v] = !empty($value[$v]) ? date('Y年m月d日 H:i:s', $value[$v]) : '';
  44. }
  45. $value['total_amounts'] = bcadd($value['pay_fee'],$value['appen_fee'],2);
  46. $value['status_text'] = isset($statusArr[$value['status']]) ? $statusArr[$value['status']] : '';
  47. $value['paytype_text'] = isset($paytypeArr[$value['paytype']]) ? $paytypeArr[$value['paytype']] : '';
  48. }
  49. $result = list_domain_image($result,['server_images']);
  50. }
  51. $this->success('获取成功', $result);
  52. } catch (Exception $e) {
  53. $this->error($e->getMessage());
  54. }
  55. }
  56. /**
  57. * 套餐列表
  58. * @return void
  59. */
  60. public function getPackageList(){
  61. try {
  62. $status = input('status',0);//状态:1=待使用,2=已核销
  63. $o = 'order';
  64. $st= 'servicetype';
  65. $p = 'package';
  66. $field = $o.'.id,check_code,server_info,'.$o.'.servicetype_id,'.$st.'.title as `service_title`,'.$p.'.title as `package_title`,'.
  67. $p.'.images as `package_images`,hexiao_time';
  68. $where[$o.'.user_id'] = $this->auth->id;
  69. $where[$o.'.company_id'] = $this->auth->company_id;
  70. $where[$o.'.ordertype'] = 3;//类型:1=预约下单,2=在线下单,3=套餐订单
  71. $where[$o.'.status'] = ['in',[1,2,3]];//状态:2=待处理,3=已完成,4=已取消
  72. $where[$p.'.type'] = 1;//类型:1=普通,2=卡券
  73. if (!empty($status)) {
  74. if ($status == 1) {
  75. $where[$o.'.hexiao_time'] = 0;
  76. }
  77. if ($status == 2) {
  78. $where[$o.'.hexiao_time'] = ['gt',0];
  79. }
  80. }
  81. $result = $this->model->alias($o)->field($field)
  82. ->join($st,$st.'.id = '.$o.'.servicetype_id','LEFT')
  83. ->join($p,$p.'.id = '.$o.'.package_id','LEFT')
  84. ->where($where)->order($o.'.createtime desc')->autopage()->select();
  85. if (!empty($result)) {
  86. foreach ($result as $key => &$value) {
  87. $value['is_check'] = !empty($value['hexiao_time']) ? 1 : 0;
  88. }
  89. $result = list_domain_image($result,['package_images']);
  90. }
  91. $this->success('获取成功', $result);
  92. } catch (Exception $e) {
  93. $this->error($e->getMessage());
  94. }
  95. }
  96. /**
  97. * 详情
  98. * @return void
  99. */
  100. public function getInfo(){
  101. try {
  102. $id = input('id',0);
  103. $payOrderId = input('pay_order_id',0);
  104. if (!empty($payOrderId)) {
  105. $orderWhere['pay_order_id'] = $payOrderId;
  106. $order = Db::name('order')->field('id')->where($orderWhere)->find();
  107. $id = isset($order['id']) ? $order['id'] : 0;
  108. }
  109. $o = 'order';
  110. $st = 'servicetype';
  111. $p = 'package';
  112. $where[$o.'.user_id'] = $this->auth->id;
  113. $where[$o.'.id'] = $id;
  114. $field = $o.'.id,check_code,orderno,server_time,server_info,server_images,'.$o.'.status,user_mobile,'.$o.'.user_car_number,'.
  115. $o.'.createtime,cancel_time,cancel_reason,pay_fee,appen_fee,finish_time,'.$st.'.title as `service_title`,paytype,'.$p.'.title as `package_title`';
  116. $result = $this->model->alias($o)->field($field)
  117. ->join($st,$st.'.id = '.$o.'.servicetype_id','LEFT')
  118. ->join($p,$p.'.id = '.$o.'.package_id','LEFT')
  119. ->where($where)->find();
  120. if (!empty($result)) {
  121. $statusArr = [2=>'待处理',3=>'已完成',4=>'已取消'];
  122. $paytypeArr = [1=>'线下',2=>'余额',3=>'微信'];
  123. $timeArr = ['createtime','cancel_time','finish_time','server_time'];
  124. foreach ($timeArr as $k => $v) {
  125. $result[$v] = !empty($result[$v]) ? date('Y年m月d日 H:i:s', $result[$v]) : '';
  126. }
  127. $result['total_amounts'] = bcadd($result['pay_fee'],$result['appen_fee'],2);
  128. $result['status_text'] = isset($statusArr[$result['status']]) ? $statusArr[$result['status']] : '';
  129. $result['paytype_text'] = isset($paytypeArr[$result['paytype']]) ? $paytypeArr[$result['paytype']] : '';
  130. $orderAppenWhere['order_id'] = $id;
  131. $orderAppen = Db::name('order_appen')->where($orderAppenWhere)->select();
  132. $appenList = [];
  133. if (!empty($orderAppen)) {
  134. foreach ($orderAppen as $key => $value) {
  135. $appenList[] = [
  136. 'name' => $value['name'],
  137. 'price' => $value['price'],
  138. ];
  139. }
  140. }
  141. $result['appen_list'] = $appenList;
  142. $result = info_domain_image($result,['server_images']);
  143. }
  144. $this->success('获取成功', $result);
  145. } catch (Exception $e) {
  146. $this->error($e->getMessage());
  147. }
  148. }
  149. /**
  150. * 核销码
  151. * @return void
  152. */
  153. public function writeOff()
  154. {
  155. try {
  156. $id = $this->request->param('id',0);
  157. $payOrderId = input('pay_order_id',0);
  158. if (!empty($payOrderId)) {
  159. $orderWhere['pay_order_id'] = $payOrderId;
  160. $order = Db::name('order')->field('id')->where($orderWhere)->find();
  161. $id = isset($order['id']) ? $order['id'] : 0;
  162. }
  163. $companyId = $this->auth->company_id;
  164. $userId = $this->auth->id;
  165. $where['id'] = $id;
  166. $where['company_id'] = $companyId;
  167. $where['user_id'] = $userId;
  168. $modelData = $this->model->where($where)->find();
  169. if (empty($modelData)) {
  170. throw new Exception('未找到相关信息');
  171. }
  172. $text = 'order_'.$modelData['check_code'];
  173. $logo = '';
  174. $filRoute = '/uploads/temp/';
  175. $saveDir = ROOT_PATH.'public'.DS.'uploads'.DS.'temp'.DS;
  176. $fileStr = md5($text);
  177. $localpng = $saveDir.$fileStr.'.png';
  178. //验证存在直接返回
  179. $userCouponsUrl = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$filRoute.$fileStr.'.png';
  180. if (!file_exists($localpng)) {
  181. build_qrcode($text, $logo, $saveDir,$fileStr);
  182. }
  183. $result = [
  184. 'url' => $userCouponsUrl,
  185. 'check_code' => $modelData['check_code'],
  186. ];
  187. $this->success('获取成功',$result);
  188. } catch (Exception $e) {
  189. $this->error($e->getMessage());
  190. }
  191. }
  192. /**
  193. * 保养列表
  194. * @return void
  195. */
  196. public function upkeepList()
  197. {
  198. try {
  199. $serviceTypeId = input('servicetype_id',0);
  200. $carId = input('car_id',0);
  201. $carNumber = input('car_number','');
  202. $o = 'order';
  203. $st = 'servicetype';
  204. $p = 'package';
  205. if (!empty($serviceTypeId)) {
  206. $where[$o.'.servicetype_id'] = $serviceTypeId;
  207. }
  208. if (!empty($carNumber)) {
  209. $where[$o.'.user_car_number'] = $carNumber;
  210. } else {
  211. if (!empty($carId)) {
  212. $where[$o.'.user_car_id'] = $carId;
  213. }
  214. }
  215. $where[$o.'.user_id'] = $this->auth->id;
  216. $where[$o.'.status'] = ['in',[2,3]];//状态:2=待处理,3=已完成,4=已取消
  217. $where[$st.'.is_upkeep'] = 1;//是否保养:1=是,0=否
  218. $field = $o.'.id,'.$o.'.servicetype_id,server_info,finish_time,next_date,next_carlicheng,pay_fee,appen_fee,'.$st.'.title as `service_title`,'.$p.'.title as `package_title`';
  219. $result = $this->model->alias($o)->field($field)
  220. ->join($st,$st.'.id = '.$o.'.servicetype_id','LEFT')
  221. ->join($p,$p.'.id = '.$o.'.package_id','LEFT')
  222. ->where($where)->order($o.'.finish_time desc')->autopage()->select();
  223. if (!empty($result)) {
  224. $timeArr = ['finish_time'];
  225. foreach ($result as $key => &$value) {
  226. foreach ($timeArr as $k => $v) {
  227. $value[$v] = !empty($value[$v]) ? date('Y-m-d', $value[$v]) : '';
  228. }
  229. $value['total_amounts'] = bcadd($value['pay_fee'],$value['appen_fee'],2);
  230. }
  231. }
  232. $this->success('获取成功',$result);
  233. } catch (Exception $e) {
  234. $this->error($e->getMessage());
  235. }
  236. }
  237. }