Order.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. namespace app\Admin\controller;
  3. use think\Db;
  4. use think\Exception;
  5. class Order extends Base
  6. {
  7. #public $post="";
  8. public function __construct()
  9. {
  10. parent::__construct();
  11. #$this->post=request()->post();
  12. }
  13. public function list()
  14. {
  15. $page = isset($this->post['page']) && $this->post['page'] != "" ? intval($this->post['page']) : 1;
  16. $size = isset($this->post['size']) && $this->post['size'] != "" ? intval($this->post['size']) : 10;
  17. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  18. $where = [];
  19. if ($status !== "") {
  20. $where['a.status'] = $status;
  21. }
  22. $type = isset($this->post['type']) && $this->post['type'] !== "" ? intval($this->post['type']) : "";
  23. if ($type !== "") {
  24. $where['a.order_type'] = $type;
  25. }
  26. $username = isset($this->post['username']) && $this->post['username'] !== "" ? trim($this->post['username']) : "";
  27. if ($username != "") {
  28. $where['e.username'] = ["like", "%{$username}%"];
  29. }
  30. $nickname = isset($this->post['nickname']) && $this->post['nickname'] !== "" ? trim($this->post['nickname']) : "";
  31. if ($nickname != "") {
  32. $where['g.nickname'] = ["like", "%{$nickname}%"];
  33. }
  34. $mobile = isset($this->post['mobile']) && $this->post['mobile'] !== "" ? trim($this->post['mobile']) : "";
  35. if ($mobile != "") {
  36. $where['g.mobile'] = ["like", "%{$mobile}%"];
  37. }
  38. $order_low = isset($this->post['order_low']) && $this->post['order_low'] !== "" ? $this->post['order_low'] : "";
  39. if ($order_low != "") {
  40. $order_low = date("Y-m-d H:i:s", strtotime($order_low . " 00:00:00"));
  41. } else {
  42. $order_low = "1970-01-01 00:00:00";
  43. }
  44. $order_up = isset($this->post['order_up']) && $this->post['order_up'] !== "" ? $this->post['order_up'] : "";
  45. if ($order_up != "") {
  46. $order_up = date("Y-m-d H:i:s", strtotime($order_up . " 23:59:59"));
  47. } else {
  48. $order_up = date("Y-m-d H:i:s");
  49. }
  50. $where['a.order_time'] = ["between", [$order_low, $order_up]];
  51. $count = Db::name("order a")->join("fc_order_post b", "a.order_sn=b.order_sn", "left")
  52. ->join("fc_addr c", "b.addrid = c.id", "left")
  53. ->join("fc_account e", "a.accountid = e.id", "left")
  54. ->join("fc_rela_account f", "a.accountid = f.accountid", "left")
  55. ->join("fc_account_info g", "f.account_info =g.id", "left")->where($where)->count();
  56. $total = ceil($count / $size);
  57. $page = $page >= $total ? $total : $page;
  58. $list = Db::name("order a")->where($where)->page($page, $size)->join("fc_order_post b", "a.order_sn=b.order_sn", "left")
  59. ->join("fc_addr c", "b.addrid = c.id", "left")
  60. ->join("fc_account e", "a.accountid = e.id", "left")
  61. ->join("fc_rela_account f", "a.accountid = f.accountid", "left")
  62. ->join("fc_account_info g", "f.account_info =g.id", "left")
  63. ->field("`a`.`id` AS `id`,
  64. `a`.`order_sn` AS `order_sn`,
  65. `a`.`accountid` AS `accountid`,
  66. `a`.`order_num` AS `order_num`,
  67. `a`.`status` AS `status`,
  68. `a`.`is_del` AS `is_del`,
  69. `a`.`delivery_time` AS `delivery_time`,
  70. `a`.`order_time` AS `order_time`,
  71. `b`.`addrid` AS `addrid`,
  72. `b`.`order_num` AS `border_num`,
  73. `b`.`post_code` AS `post_code`,
  74. `b`.`post_name` AS `post_name`,
  75. `b`.`status` AS `bstatus`,
  76. `c`.`addr` AS `addr`,
  77. `c`.`provice` AS `provice`,
  78. `c`.`city` AS `city`,
  79. `c`.`provice_name` AS `provice_name`,
  80. `c`.`city_name` AS `city_name`,
  81. `c`.`area` AS `area`,
  82. `c`.`area_name` AS `area_name`,
  83. `c`.`contector` AS `contector`,
  84. `c`.`mobile` AS `contector_mobile`,
  85. `e`.`username` AS `username`,
  86. `e`.`account_type` AS `account_type`,
  87. `g`.`nickname` AS `nickname`,
  88. `g`.`mobile` AS `mobile`,
  89. `a`.`unit_weight` AS `unit_weight`,
  90. `a`.`unit` AS `unit`
  91. ")
  92. ->order("order_time desc")
  93. ->select();
  94. // var_dump(Db::name("order a")->getLastSql());
  95. return app_show(0, "获取成功", ["list" => $list, "count" => $count]);
  96. }
  97. /**
  98. * @param ordersn
  99. * @param post_code
  100. * @param post_company
  101. */
  102. public function OrderDelivery()
  103. {
  104. $ordersn = isset($this->post['ordersn']) && $this->post['ordersn'] != "" ? trim($this->post['ordersn']) : "";
  105. if ($ordersn == "") {
  106. return error_show(1004, "参数ordersn 不能为空");
  107. }
  108. $orderinfo = Db::name("order")->where(["order_sn" => $ordersn])->find();
  109. if (empty($orderinfo)) {
  110. return error_show(1005, "订单不存在");
  111. }
  112. if ($orderinfo['is_del'] == 1) {
  113. return error_show(1005, "订单已删除");
  114. }
  115. if ($orderinfo['status'] != 1) {
  116. return error_show(1005, "订单状态有误");
  117. }
  118. $orderpost = Db::name("order_post")->where(["order_sn" => $ordersn, "is_del" => 0])->find();
  119. if (empty($orderpost)) {
  120. return error_show(1005, "订单地址信息不存在");
  121. }
  122. $post_code = isset($this->post['post_code']) && $this->post['post_code'] != "" ? trim($this->post['post_code']) : "";
  123. if ($post_code == "") {
  124. return error_show(1004, "参数post_code 不能为空");
  125. }
  126. // $post_company = isset($this->post['post_company']) && $this->post['post_company'] != "" ? trim($this->post['post_company']) : "";
  127. // if ($post_company == "") {
  128. // return error_show(1004, "参数post_company 不能为空");
  129. // }
  130. $express_code = isset($this->post['express_code']) && $this->post['express_code'] != "" ? trim($this->post['express_code']) : "";
  131. if ($express_code == "") {
  132. return error_show(1004, "参数 express_code 不能为空");
  133. }
  134. $express= Db::name("express")->where(["express_code"=>$express_code])->find();
  135. if($express==false){
  136. return error_show(1004, "参数物流公司信息未找到");
  137. }
  138. $orderpost['post_code'] = $post_code;
  139. $orderpost['post_name'] = isset($express['express_name']) ?$express['express_name']:"" ;
  140. $orderpost['updatetime'] = date("Y-m-d H:i:s");
  141. Db::startTrans();
  142. try {
  143. $psot = Db::name("order_post")->update($orderpost);
  144. if ($psot) {
  145. $orderinfo['status'] = 2;
  146. $orderinfo['delivery_time'] = date("Y-m-d H:i:s");
  147. $order = Db::name("order")->update($orderinfo);
  148. if ($order) {
  149. $express_data=[
  150. "post_code"=>$post_code,
  151. "post_name"=> isset($express['express_name']) ?$express['express_name']:"" ,
  152. "post_express"=>$express_code,
  153. "post_data"=>"",
  154. "status"=>1,
  155. "addtime"=>date("Y-m-d H:i:s"),
  156. "updatetime"=>date("Y-m-d H:i:s")
  157. ];
  158. $ins = Db::name("express_data")->insert($express_data);
  159. if($ins){
  160. write_log("订单{$ordersn}发货", $this->userinfo, "order", "edit", "0");
  161. Db::commit();
  162. return app_show(0, "订单物流信息新建成功");
  163. }
  164. }
  165. }
  166. Db::rollback();
  167. return error_show(1004, "物流信息新建失败");
  168. } catch (\Exception $e) {
  169. Db::rollback();
  170. return error_show(1004, $e->getMessage());
  171. }
  172. }
  173. public function uploud()
  174. {
  175. $sql = "SELECT
  176. a.order_sn '订单编号',
  177. a.order_num '订单数量',
  178. a.order_time '下单时间',
  179. '' as '商品名称',
  180. if(a.status=1,'待发货',if(a.status=2,'已发货','')) '发货状态',
  181. a.delivery_time '发货时间',
  182. b.post_code '快递编号',
  183. b.post_name '快递公司',
  184. k.username '账户名',
  185. CONCAT(c.provice_name,c.city_name,c.area_name,c.addr ) '收货地址',
  186. c.contector '收货人',
  187. c.mobile '收货电话',
  188. v.nickname '用户名',
  189. v.mobile '用户电话'
  190. FROM
  191. fc_order a
  192. LEFT JOIN fc_order_post b ON b.order_sn = a.order_sn
  193. LEFT JOIN fc_addr c ON c.id = b.addrid
  194. LEFT JOIN fc_account k ON k.id = a.accountid
  195. LEFT JOIN fc_rela_account n ON n.accountid = a.accountid
  196. LEFT JOIN fc_account_info v ON v.id = n.account_info
  197. where a.status=1 and a.is_del=0
  198. ";
  199. $list = Db::query($sql);
  200. if (empty($list)) {
  201. $list = [["未找到数据" => ""]];
  202. }
  203. $header = array_keys($list[0]);
  204. array_walk($list, function (&$v) {
  205. //根据账户名区分商品名称
  206. $flag = strtoupper(substr($v['账户名'], 6, 3));
  207. if ($flag == 'TKS') $v['商品名称'] = '悦享泰康2.5Kg*1';
  208. elseif ($flag = 'TKR') $v['商品名称'] = '红运稻2Kg*2';
  209. $v = array_values($v);
  210. });
  211. excelExport(date("Y-m-d") . "未发货订单", $header, $list);
  212. }
  213. public function express(){
  214. $order_sn = isset($this->post['order_sn'])&&$this->post['order_sn']!="" ? trim($this->post['order_sn']):"";
  215. if($order_sn==""){
  216. return error_show(1004,"参数order_sn 不能为空");
  217. }
  218. $datapost=Db::name("order_post")->where(["order_sn"=>$order_sn,"is_del"=>0])->find();
  219. if($datapost==false || $datapost['post_code']==""){
  220. return error_show(1004,"未找到快递信息");
  221. }
  222. $express = Db::name("express_data")->where(['post_code'=>$datapost['post_code']])->find();
  223. if($express==false){
  224. return error_show(1004,"暂无物流信息");
  225. }
  226. $express['post_content']=$express['post_data']!="" ? json_decode($express['post_data'],true):[];
  227. unset($express['post_data']);
  228. $status = express_status();
  229. $express['status_cn'] = $status[$express['status']];
  230. return app_show(0,"获取成功",$express);
  231. }
  232. //批量修改订单的物流信息
  233. public function updateOrderPostBatch()
  234. {
  235. // $list = $this->post['list'];
  236. $list = input('list/a');
  237. $order_sns = array_column($list, 'order_sn');//所有的订单号
  238. $post_names = array_column($list, 'post_name');//所有的物流公司
  239. //1.集中校验订单号
  240. //1.1数据库中存在的且未发货的订单号
  241. $order_list = Db::name('order')
  242. ->where(['status' => 1, 'is_del' => 0])
  243. ->whereIn('order_sn', $order_sns)
  244. ->column('id', 'order_sn');
  245. //1.2将查询的订单号和传入的订单号取差集,差集不为空说明有错误
  246. $undeliver = array_diff($order_sns, array_keys($order_list));
  247. if (!empty($undeliver)) return app_show(1005, '订单编号不存在或已发货', $undeliver);
  248. //2.集中校验快递公司
  249. $express_name_list = Db::name('express')
  250. ->where(['status' => 1])
  251. ->whereIn('express_name', $post_names)
  252. // ->column('id', 'express_name,express_code');
  253. ->column('id,express_name,express_code', 'express_name');
  254. $unexpress = array_diff($post_names, array_keys($express_name_list));
  255. if (!empty($unexpress)) return app_show(1006, '快递公司不存在或未启用', $unexpress);
  256. //3.处理数据
  257. $time = date('Y-m-d H:i:s');
  258. // 启动事务
  259. Db::startTrans();
  260. try {
  261. foreach ($list as $k => $v) {
  262. //3.1更新订单表
  263. Db::name('order')
  264. ->where('id', $order_list[$v['order_sn']])
  265. ->update([
  266. 'delivery_time' => empty($v['delivery_time']) ? $time : $v['delivery_time'],//发货时间
  267. 'status' => 2,//发货状态,2:已发货
  268. 'updatetime' => $time
  269. ]);
  270. //3.2更新订单发货表
  271. Db::name('order_post')->where('order_sn', $v['order_sn'])->update([
  272. 'post_code' => $v['post_code'],//快递编号
  273. 'post_name' => $v['post_name'],//快递公司
  274. 'updatetime' => $time
  275. ]);
  276. //3.3物流信息表(也不知道是不是叫这个名字)
  277. $express_data = [
  278. "post_code" => $v['post_code'],
  279. "post_name" => $v['post_name'],
  280. "post_express" => isset($express_name_list[$v['post_name']]['express_code']) ? $express_name_list[$v['post_name']]['express_code'] : '',
  281. "post_data" => "",
  282. "status" => 1,
  283. "addtime" => $time,
  284. "updatetime" => $time
  285. ];
  286. Db::name("express_data")->insert($express_data);
  287. //3.4记录日志
  288. write_log("订单{$v['order_sn']}发货", $this->userinfo, "order", "edit", "0");
  289. }
  290. // 提交
  291. Db::commit();
  292. return error_show(0, '批量修改成功');
  293. } catch (\Exception $e) {
  294. // 回滚
  295. Db::rollback();
  296. return error_show(1004, '执行失败,' . $e->getMessage().'|'.$e->getFile().'|'.$e->getLine());
  297. }
  298. }
  299. }