Order.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\store\controller;
  15. use app\api\controller\Alipay;
  16. use EasyWeChat\Factory;
  17. use library\Controller;
  18. use think\Db;
  19. use app\api\controller\Area;
  20. use app\api\controller\Crontab;
  21. use function GuzzleHttp\Psr7\_caseless_remove;
  22. /**
  23. * 订单记录管理
  24. * Class Order
  25. * @package app\store\controller
  26. */
  27. class Order extends Controller
  28. {
  29. /**
  30. * 绑定数据表
  31. * @var string
  32. */
  33. protected $table = 'store_order';
  34. /**
  35. * 订单记录管理
  36. * @auth true
  37. * @menu true
  38. * @throws \think\Exception
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. * @throws \think\exception\DbException
  42. * @throws \think\exception\PDOException
  43. */
  44. public function index()
  45. {
  46. $this->root = $this->request->root(true);
  47. $this->title = '订单记录管理';
  48. $array = $this->statistical_order_info();
  49. $this->assign('a', $array);
  50. $this->byWhere(1)->field('a.*,b.name,b.headimg')->order('a.id desc')->page();
  51. }
  52. protected function statistical_order_info()
  53. {
  54. $array = array();
  55. $array['all_order'] = $this->byWhere(2)->count();
  56. $array['all_price'] = $this->byWhere(2)->where('a.status', '>',0)->sum('a.price_total');
  57. $array['weixin_all_price'] = $this->byWhere(2)->where('a.status', '>',0)->where('a.pay_type', '1')->sum('a.price_total');
  58. $array['zfb_all_price'] = $this->byWhere(2)->where('a.status', '>',0)->where('a.pay_type', '2')->sum('a.price_total');
  59. $array['engineer_id'] = session('user.engineer_id');
  60. return $array;
  61. }
  62. /**
  63. * 搜索条件
  64. * @return \library\helper\QueryHelper
  65. */
  66. protected function byWhere($type)
  67. {
  68. if ($type == 1) {
  69. $query = $this->_query($this->table);
  70. } elseif ($type == 2) {
  71. $query = Db::name($this->table);
  72. }
  73. $query = $query->alias('a')->join('store_member b', 'a.user_id=b.id');
  74. /*$engineer_id = session('user.engineer_id');
  75. if($engineer_id > 0){
  76. $query->where('a.worker_id = '.$engineer_id.' or a.status = 1');
  77. }*/
  78. if (isset($_GET['order_no']) && $_GET['order_no']) {
  79. $query->where('a.order_no', 'like','%'.$_GET['order_no'].'%');
  80. }
  81. if (isset($_GET['status']) && $_GET['status'] != '') {
  82. $query->where('a.status', $_GET['status']);
  83. }
  84. if (isset($_GET['is_self_build']) && $_GET['is_self_build'] != '') {
  85. $query->where('a.is_self_build', $_GET['is_self_build']);
  86. }
  87. if (isset($_GET['pay_status']) && $_GET['pay_status'] != '') {
  88. $query->where('a.pay_status', $_GET['pay_status']);
  89. }
  90. if (isset($_GET['pay_type']) && $_GET['pay_type']) {
  91. $query->where('a.pay_type', $_GET['pay_type']);
  92. }
  93. if (isset($_GET['create_at']) && $_GET['create_at']) {
  94. $time = explode(' - ', $_GET['create_at']);
  95. $start_date_time = $time[0] . ' 00:00:00';
  96. $end_date_time = $time[1] . ' 23:59:59';
  97. $query->whereBetweenTime('a.create_at', $start_date_time, $end_date_time);
  98. }
  99. if (isset($_GET['pay_at']) && $_GET['pay_at']) {
  100. $time = explode(' - ', $_GET['pay_at']);
  101. $start_date_time = $time[0] . ' 00:00:00';
  102. $end_date_time = $time[1] . ' 23:59:59';
  103. $query->whereBetweenTime('a.pay_at', $start_date_time, $end_date_time);
  104. }
  105. if (isset($_GET['user_info']) && $_GET['user_info']) {
  106. $query->where('b.name|b.phone', '=', $_GET['user_info'] );
  107. }
  108. if (isset($_GET['worker_info']) && $_GET['worker_info']) {
  109. $worker_id = Db::name('store_engineer')->where('name|phone', '=', $_GET['worker_info'] )->value('id');
  110. $query->where('worker_id',$worker_id);
  111. }
  112. return $query;
  113. }
  114. /**
  115. * 订单列表处理
  116. * @param array $data
  117. * @throws \think\db\exception\DataNotFoundException
  118. * @throws \think\db\exception\ModelNotFoundException
  119. * @throws \think\exception\DbException
  120. */
  121. protected function _index_page_filter(array &$data)
  122. {
  123. $mids = array_unique(array_merge(array_column($data, 'user_id'), array_column($data, 'user_id')));
  124. $memberList = Db::name('StoreMember')->whereIn('id', $mids)->select();
  125. $wids = array_unique(array_merge(array_column($data, 'worker_id'), array_column($data, 'worker_id')));
  126. $workerList = Db::name('store_engineer')->whereIn('id', $wids)->select();
  127. foreach ($data as &$vo) {
  128. list($vo['member'], $vo['worker']) = [[],[]];
  129. foreach ($memberList as $member) if ($member['id'] === $vo['user_id']) {
  130. $vo['member'] = $member;
  131. }
  132. foreach ($workerList as $worker) if ($worker['id'] === $vo['worker_id']) {
  133. $vo['worker'] = $worker;
  134. }
  135. $vo['serve_title'] = Db::name('store_goods')->where('id',$vo['goods_id'])->value('title');
  136. }
  137. }
  138. /**
  139. * 添加订单
  140. * @auth true
  141. * @throws \think\Exception
  142. * @throws \think\db\exception\DataNotFoundException
  143. * @throws \think\db\exception\ModelNotFoundException
  144. * @throws \think\exception\DbException
  145. * @throws \think\exception\PDOException
  146. */
  147. public function add()
  148. {
  149. $this->title = '添加订单';
  150. //服务类目
  151. $this->cate_arr = Db::name('store_goods')->field('id,title')->where('status',1)->where('is_deleted',0)->order(['sort'=>'desc','id'=>'desc'])->select();
  152. //服务类型
  153. $serve_type_arr = array('1'=>' 技术咨询','2'=>'预约检测','3'=>'加急检测','4'=>'夜间检测');
  154. $this->serve_type_arr = $serve_type_arr;
  155. $this->member_list = Db::name('store_member')->field('id,name,phone')->where('status',1)->where('is_deleted',0)->select();
  156. $this->_form($this->table, 'form');
  157. }
  158. /**
  159. * 表单数据处理
  160. * @param array $data
  161. * @throws \think\Exception
  162. * @throws \think\db\exception\DataNotFoundException
  163. * @throws \think\db\exception\ModelNotFoundException
  164. * @throws \think\exception\DbException
  165. * @throws \think\exception\PDOException
  166. */
  167. protected function _form_filter(array &$data)
  168. {
  169. if ($this->request->isGet()) {
  170. //接单人员信息
  171. if(isset($data['worker_id']) && $data['worker_id']){
  172. $data['worker_info'] = Db::name('store_engineer')->field('name,phone')->where('id',$data['worker_id'])->find();
  173. }
  174. $this->data = $data;
  175. }else{
  176. $serve_type_arr = array('1'=>' 技术咨询','2'=>'预约检测','3'=>'加急检测','4'=>'夜间检测');
  177. $data['worker_id'] = session('user.engineer_id');
  178. $data['price_amount'] = $data['price_total'];
  179. $data['is_self_build'] = 1;
  180. $data['serve_type'] = $serve_type_arr[$data['serve_type_id']];
  181. $data['order_no'] = get_order_sn();
  182. $data['pay_no'] = get_order_sn();
  183. }
  184. }
  185. /**
  186. * 订单详情
  187. * @auth true
  188. * @throws \think\Exception
  189. * @throws \think\db\exception\DataNotFoundException
  190. * @throws \think\db\exception\ModelNotFoundException
  191. * @throws \think\exception\DbException
  192. * @throws \think\exception\PDOException
  193. */
  194. public function order_detail()
  195. {
  196. $this->_form($this->table);
  197. }
  198. /**
  199. * 订单记录
  200. * @auth true
  201. * @throws \think\Exception
  202. * @throws \think\db\exception\DataNotFoundException
  203. * @throws \think\db\exception\ModelNotFoundException
  204. * @throws \think\exception\DbException
  205. * @throws \think\exception\PDOException
  206. */
  207. public function order_status()
  208. {
  209. $id = $this->app->request->get('id');
  210. $this->assign('id', $id);
  211. $post = $this->app->request->post();
  212. if (isset($post['id']) && $post['id']) {
  213. Db::name($this->table)->where('id', $post['id'])->update(['remark' => $post['remark']]);
  214. $this->success('编辑成功!');
  215. } else {
  216. $this->_form($this->table);
  217. }
  218. }
  219. /**
  220. * 接单
  221. * @auth true
  222. * @throws \think\Exception
  223. * @throws \think\exception\PDOException
  224. */
  225. public function receive()
  226. {
  227. $this->_save($this->table, ['worker_id' => session('user.engineer_id'),'status'=>2,'start_at'=>date('Y-m-d H:i:s')]);
  228. }
  229. /**
  230. * 指派订单
  231. * @auth true
  232. * @throws \think\Exception
  233. * @throws \think\db\exception\DataNotFoundException
  234. * @throws \think\db\exception\ModelNotFoundException
  235. * @throws \think\exception\DbException
  236. * @throws \think\exception\PDOException
  237. */
  238. public function send_order()
  239. {
  240. $id = $this->app->request->get('id');
  241. $this->assign('id', $id);
  242. $post = $this->app->request->post();
  243. if (isset($post['id']) && $post['id']) {
  244. $worker_id = $post['worker_id'];
  245. $order_info = Db::name('store_order')->where('id',$id)->find();
  246. Db::name('store_order')->where('id',$id)->update(['worker_id' => $worker_id,'status'=>$post['status'],'price_total'=>$post['price_total'],'price_amount'=>$post['price_total'] + $order_info['coupon_amount'],'remark'=>$post['remark']]);
  247. if($worker_id != $order_info['worker_id']){
  248. //添加指派记录
  249. $order_no = Db::name('store_order')->where('id',$id)->value('order_no');
  250. $send_log_data = array(
  251. 'order_id' => $id,
  252. 'order_no' => $order_no,
  253. 'worker_id' => session('user.engineer_id'),
  254. 'send_worker_id' => $worker_id
  255. );
  256. Db::name('store_send_log')->insert($send_log_data);
  257. //发送邮箱
  258. $engineer_mail = Db::name('store_engineer')->where('mail is not null')->where('id',$worker_id)->where('is_deleted',0)->value('mail');
  259. if($engineer_mail){
  260. mail_push($engineer_mail,$id);
  261. }
  262. }
  263. $this->success('操作成功!');
  264. } else {
  265. /*$engineer_id = session('user.engineer_id');*/
  266. $store_worker = Db::name('store_engineer')->field('id,name,phone')->where('is_deleted',0)->select();
  267. if($store_worker){
  268. $this->store_worker = $store_worker;
  269. $this->_form($this->table, 'send_order');
  270. }else{
  271. $this->error('暂没有合适的工程师');
  272. }
  273. }
  274. }
  275. }