123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- <?php
- namespace app\Admin\controller;
- use think\Db;
- use think\Exception;
- class Order extends Base
- {
- #public $post="";
- public function __construct()
- {
- parent::__construct();
- #$this->post=request()->post();
- }
- public function list()
- {
- $page = isset($this->post['page']) && $this->post['page'] != "" ? intval($this->post['page']) : 1;
- $size = isset($this->post['size']) && $this->post['size'] != "" ? intval($this->post['size']) : 10;
- $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
- $where = [];
- if ($status !== "") {
- $where['a.status'] = $status;
- }
- $type = isset($this->post['type']) && $this->post['type'] !== "" ? intval($this->post['type']) : "";
- if ($type !== "") {
- $where['a.order_type'] = $type;
- }
- $username = isset($this->post['username']) && $this->post['username'] !== "" ? trim($this->post['username']) : "";
- if ($username != "") {
- $where['e.username'] = ["like", "%{$username}%"];
- }
- $nickname = isset($this->post['nickname']) && $this->post['nickname'] !== "" ? trim($this->post['nickname']) : "";
- if ($nickname != "") {
- $where['g.nickname'] = ["like", "%{$nickname}%"];
- }
- $mobile = isset($this->post['mobile']) && $this->post['mobile'] !== "" ? trim($this->post['mobile']) : "";
- if ($mobile != "") {
- $where['g.mobile'] = ["like", "%{$mobile}%"];
- }
- $order_low = isset($this->post['order_low']) && $this->post['order_low'] !== "" ? $this->post['order_low'] : "";
- if ($order_low != "") {
- $order_low = date("Y-m-d H:i:s", strtotime($order_low . " 00:00:00"));
- } else {
- $order_low = "1970-01-01 00:00:00";
- }
- $order_up = isset($this->post['order_up']) && $this->post['order_up'] !== "" ? $this->post['order_up'] : "";
- if ($order_up != "") {
- $order_up = date("Y-m-d H:i:s", strtotime($order_up . " 23:59:59"));
- } else {
- $order_up = date("Y-m-d H:i:s");
- }
- $where['a.order_time'] = ["between", [$order_low, $order_up]];
- $count = Db::name("order a")->join("fc_order_post b", "a.order_sn=b.order_sn", "left")
- ->join("fc_addr c", "b.addrid = c.id", "left")
- ->join("fc_account e", "a.accountid = e.id", "left")
- ->join("fc_rela_account f", "a.accountid = f.accountid", "left")
- ->join("fc_account_info g", "f.account_info =g.id", "left")->where($where)->count();
- $total = ceil($count / $size);
- $page = $page >= $total ? $total : $page;
- $list = Db::name("order a")->where($where)->page($page, $size)->join("fc_order_post b", "a.order_sn=b.order_sn", "left")
- ->join("fc_addr c", "b.addrid = c.id", "left")
- ->join("fc_account e", "a.accountid = e.id", "left")
- ->join("fc_rela_account f", "a.accountid = f.accountid", "left")
- ->join("fc_account_info g", "f.account_info =g.id", "left")
- ->field("`a`.`id` AS `id`,
- `a`.`order_sn` AS `order_sn`,
- `a`.`accountid` AS `accountid`,
- `a`.`order_num` AS `order_num`,
- `a`.`status` AS `status`,
- `a`.`is_del` AS `is_del`,
- `a`.`delivery_time` AS `delivery_time`,
- `a`.`order_time` AS `order_time`,
- `b`.`addrid` AS `addrid`,
- `b`.`order_num` AS `border_num`,
- `b`.`post_code` AS `post_code`,
- `b`.`post_name` AS `post_name`,
- `b`.`status` AS `bstatus`,
- `c`.`addr` AS `addr`,
- `c`.`provice` AS `provice`,
- `c`.`city` AS `city`,
- `c`.`provice_name` AS `provice_name`,
- `c`.`city_name` AS `city_name`,
- `c`.`area` AS `area`,
- `c`.`area_name` AS `area_name`,
- `c`.`contector` AS `contector`,
- `c`.`mobile` AS `contector_mobile`,
- `e`.`username` AS `username`,
- `e`.`account_type` AS `account_type`,
- `g`.`nickname` AS `nickname`,
- `g`.`mobile` AS `mobile`,
- `a`.`unit_weight` AS `unit_weight`,
- `a`.`unit` AS `unit`
- ")
- ->order("order_time desc")
- ->select();
- // var_dump(Db::name("order a")->getLastSql());
- return app_show(0, "获取成功", ["list" => $list, "count" => $count]);
- }
- /**
- * @param ordersn
- * @param post_code
- * @param post_company
- */
- public function OrderDelivery()
- {
- $ordersn = isset($this->post['ordersn']) && $this->post['ordersn'] != "" ? trim($this->post['ordersn']) : "";
- if ($ordersn == "") {
- return error_show(1004, "参数ordersn 不能为空");
- }
- $orderinfo = Db::name("order")->where(["order_sn" => $ordersn])->find();
- if (empty($orderinfo)) {
- return error_show(1005, "订单不存在");
- }
- if ($orderinfo['is_del'] == 1) {
- return error_show(1005, "订单已删除");
- }
- if ($orderinfo['status'] != 1) {
- return error_show(1005, "订单状态有误");
- }
- $orderpost = Db::name("order_post")->where(["order_sn" => $ordersn, "is_del" => 0])->find();
- if (empty($orderpost)) {
- return error_show(1005, "订单地址信息不存在");
- }
- $post_code = isset($this->post['post_code']) && $this->post['post_code'] != "" ? trim($this->post['post_code']) : "";
- if ($post_code == "") {
- return error_show(1004, "参数post_code 不能为空");
- }
- // $post_company = isset($this->post['post_company']) && $this->post['post_company'] != "" ? trim($this->post['post_company']) : "";
- // if ($post_company == "") {
- // return error_show(1004, "参数post_company 不能为空");
- // }
- $express_code = isset($this->post['express_code']) && $this->post['express_code'] != "" ? trim($this->post['express_code']) : "";
- if ($express_code == "") {
- return error_show(1004, "参数 express_code 不能为空");
- }
- $express= Db::name("express")->where(["express_code"=>$express_code])->find();
- if($express==false){
- return error_show(1004, "参数物流公司信息未找到");
- }
- $orderpost['post_code'] = $post_code;
- $orderpost['post_name'] = isset($express['express_name']) ?$express['express_name']:"" ;
- $orderpost['updatetime'] = date("Y-m-d H:i:s");
- Db::startTrans();
- try {
- $psot = Db::name("order_post")->update($orderpost);
- if ($psot) {
- $orderinfo['status'] = 2;
- $orderinfo['delivery_time'] = date("Y-m-d H:i:s");
- $order = Db::name("order")->update($orderinfo);
- if ($order) {
- $express_data=[
- "post_code"=>$post_code,
- "post_name"=> isset($express['express_name']) ?$express['express_name']:"" ,
- "post_express"=>$express_code,
- "post_data"=>"",
- "status"=>1,
- "addtime"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $ins = Db::name("express_data")->insert($express_data);
- if($ins){
- write_log("订单{$ordersn}发货", $this->userinfo, "order", "edit", "0");
- Db::commit();
- return app_show(0, "订单物流信息新建成功");
- }
- }
- }
- Db::rollback();
- return error_show(1004, "物流信息新建失败");
- } catch (\Exception $e) {
- Db::rollback();
- return error_show(1004, $e->getMessage());
- }
- }
- public function uploud()
- {
- $sql = "SELECT
- a.order_sn '订单编号',
- a.order_num '订单数量',
- a.order_time '下单时间',
- '' as '商品名称',
- if(a.status=1,'待发货',if(a.status=2,'已发货','')) '发货状态',
- a.delivery_time '发货时间',
- b.post_code '快递编号',
- b.post_name '快递公司',
- k.username '账户名',
- CONCAT(c.provice_name,c.city_name,c.area_name,c.addr ) '收货地址',
- c.contector '收货人',
- c.mobile '收货电话',
- v.nickname '用户名',
- v.mobile '用户电话'
- FROM
- fc_order a
- LEFT JOIN fc_order_post b ON b.order_sn = a.order_sn
- LEFT JOIN fc_addr c ON c.id = b.addrid
- LEFT JOIN fc_account k ON k.id = a.accountid
- LEFT JOIN fc_rela_account n ON n.accountid = a.accountid
- LEFT JOIN fc_account_info v ON v.id = n.account_info
- where a.status=1 and a.is_del=0
- ";
- $list = Db::query($sql);
- if (empty($list)) {
- $list = [["未找到数据" => ""]];
- }
- $header = array_keys($list[0]);
- array_walk($list, function (&$v) {
- //根据账户名区分商品名称
- $flag = strtoupper(substr($v['账户名'], 6, 3));
- if ($flag == 'TKS') $v['商品名称'] = '悦享泰康2.5Kg*1';
- elseif ($flag = 'TKR') $v['商品名称'] = '红运稻2Kg*2';
- $v = array_values($v);
- });
- excelExport(date("Y-m-d") . "未发货订单", $header, $list);
- }
- public function express(){
- $order_sn = isset($this->post['order_sn'])&&$this->post['order_sn']!="" ? trim($this->post['order_sn']):"";
- if($order_sn==""){
- return error_show(1004,"参数order_sn 不能为空");
- }
- $datapost=Db::name("order_post")->where(["order_sn"=>$order_sn,"is_del"=>0])->find();
- if($datapost==false || $datapost['post_code']==""){
- return error_show(1004,"未找到快递信息");
- }
- $express = Db::name("express_data")->where(['post_code'=>$datapost['post_code']])->find();
- if($express==false){
- return error_show(1004,"暂无物流信息");
- }
- $express['post_content']=$express['post_data']!="" ? json_decode($express['post_data'],true):[];
- unset($express['post_data']);
- $status = express_status();
- $express['status_cn'] = $status[$express['status']];
- return app_show(0,"获取成功",$express);
- }
- //批量修改订单的物流信息
- public function updateOrderPostBatch()
- {
- // $list = $this->post['list'];
- $list = input('list/a');
- $order_sns = array_column($list, 'order_sn');//所有的订单号
- $post_names = array_column($list, 'post_name');//所有的物流公司
- //1.集中校验订单号
- //1.1数据库中存在的且未发货的订单号
- $order_list = Db::name('order')
- ->where(['status' => 1, 'is_del' => 0])
- ->whereIn('order_sn', $order_sns)
- ->column('id', 'order_sn');
- //1.2将查询的订单号和传入的订单号取差集,差集不为空说明有错误
- $undeliver = array_diff($order_sns, array_keys($order_list));
- if (!empty($undeliver)) return app_show(1005, '订单编号不存在或已发货', $undeliver);
- //2.集中校验快递公司
- $express_name_list = Db::name('express')
- ->where(['status' => 1])
- ->whereIn('express_name', $post_names)
- // ->column('id', 'express_name,express_code');
- ->column('id,express_name,express_code', 'express_name');
- $unexpress = array_diff($post_names, array_keys($express_name_list));
- if (!empty($unexpress)) return app_show(1006, '快递公司不存在或未启用', $unexpress);
- //3.处理数据
- $time = date('Y-m-d H:i:s');
- // 启动事务
- Db::startTrans();
- try {
- foreach ($list as $k => $v) {
- //3.1更新订单表
- Db::name('order')
- ->where('id', $order_list[$v['order_sn']])
- ->update([
- 'delivery_time' => empty($v['delivery_time']) ? $time : $v['delivery_time'],//发货时间
- 'status' => 2,//发货状态,2:已发货
- 'updatetime' => $time
- ]);
- //3.2更新订单发货表
- Db::name('order_post')->where('order_sn', $v['order_sn'])->update([
- 'post_code' => $v['post_code'],//快递编号
- 'post_name' => $v['post_name'],//快递公司
- 'updatetime' => $time
- ]);
- //3.3物流信息表(也不知道是不是叫这个名字)
- $express_data = [
- "post_code" => $v['post_code'],
- "post_name" => $v['post_name'],
- "post_express" => isset($express_name_list[$v['post_name']]['express_code']) ? $express_name_list[$v['post_name']]['express_code'] : '',
- "post_data" => "",
- "status" => 1,
- "addtime" => $time,
- "updatetime" => $time
- ];
- Db::name("express_data")->insert($express_data);
- //3.4记录日志
- write_log("订单{$v['order_sn']}发货", $this->userinfo, "order", "edit", "0");
- }
- // 提交
- Db::commit();
- return error_show(0, '批量修改成功');
- } catch (\Exception $e) {
- // 回滚
- Db::rollback();
- return error_show(1004, '执行失败,' . $e->getMessage().'|'.$e->getFile().'|'.$e->getLine());
- }
- }
- }
|