chengkun
2025-09-05 4822304b63e1bd6327860af7f3db0133cecf167f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
<?php
 
namespace app\admin\controller;
 
use app\supplier\model\DistributionArea as DistributionAreaModel;
use think\Exception;
use think\facade\Config;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
use app\supplier\model\GoodsAnnex;
use app\supplier\model\GoodsSpec;
use app\supplier\model\GoodsAttribute;
use app\supplier\model\GoodsLabel;
use app\supplier\model\GoodsRetailPriceLimit;
 
//use app\supplier\model\Goods;
 
//商品零售价限制
 
class CancelPublishOrder extends Common {
    
    /**
     * 商品管理
     * @return string
     */
    public function index() {
        return View::fetch();
    }
    
    /**
     * 获取刊登列表
     * @return mixed
     */
    public function getPublishOrder() {
        if (!Request::isPost()) {
            return $this->errorResponse(lang('request_method_incorrect'));
        }
        try {
            $platform_goods_code = input('platform_goods_code');
            $supplier_goods_code = input('supplier_goods_code');
            $condition['poc.status'] = 0;//
            $p = input('p', 1);
            $_GET['p'] = $p;
            $other['page_size'] = input('page_size', 20);
            $order = 'id asc';
            $count = \app\supplier\model\PublishOrder::alias('po')
                ->alias('po')
                ->join('web_publish_order_cancel poc', 'po.id=poc.publish_order_id')
                ->where($condition)
                ->count();
            $list = \app\supplier\model\PublishOrder::alias('po')
                ->alias('po')
                ->join('web_publish_order_cancel poc', 'po.id=poc.publish_order_id')
                ->field("poc.id as cancel_id,po.id,supplier_goods_code,publish_order_code,first_cate_code,second_cate_code,three_cate_code,public_type,brand_code,poc.add_time,publish_status,update_time,goods_title_cn,(select url from web_publish_order_picture where publish_order_id=po.id order by id asc limit 1) as picture,poc.status,poc.remark,poc.reason_id")->where($condition)->order($order)->page($p, $other['page_size'])->select()->toArray();
            $other['count'] = $count;
            if ($list) {
                $categorylist = get_category();
                $categorylist = datalist($categorylist, 'cate_code', '', FALSE);
                foreach ($list as &$item) {
                    $item['first_cate_name'] = $categorylist[$item['first_cate_code']]['cate_name'];
                    $item['second_cate_name'] = $categorylist[$item['second_cate_code']]['cate_name'];
                    $item['three_cate_name'] = $categorylist[$item['three_cate_code']]['cate_name'];
                    $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
                }
                $result['list'] = $list;
                $result['other'] = $other;
                return $this->successResponse($result);
            } else {
                return $this->errorResponse(lang('no_data_found'));
            }
        } catch (\Exception $exc) {
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    /**
     * 取消刊登审核
     * @return array|int[]
     */
    public function reviewPublishOrder() {
        // 开始数据库事务
        Db::startTrans();
        try {
            if (!Request::isPost()) {
                throw new Exception(lang('request_method_incorrect'));
            }
            $publish_order_id = input('post.publish_order_id');
            $cancel_id = input('post.cancel_id');
            if (is_numeric($cancel_id)) {
                $status = input('post.status');
                //更新数据
                $info = Db::name('publish_order_cancel')->field('id,status')->where('id', $cancel_id)->find();
                if (!$info) {
                    return $this->errorResponse(lang('request_method_incorrect'));
                }
                if($info['status']==1){
                    return $this->errorResponse('该申请已审核通过,不能重复审核');
                }
                if ($status == 1) {
                    Db::name('publish_order_cancel')->where('id', $cancel_id)->update(['status' => 1]);
                    Db::name('publish_order')->where('id', $publish_order_id)->update(['publish_status' => 0, 'audit_status' => 1, 'review_comments' => '']);
                    $message = '操作成功,审核通过';
                } else {
                    $comments = trim(input('comments'));
                    if (!$comments) {
                        return $this->errorResponse('请填写审核不通过原因');
                    }
                    Db::name('publish_order_cancel')->where('id', $cancel_id)->update(['status' => 1, 'comments' => $comments]);
                    $message = '操作成功,审核不通过';
                }
                // 提交事务
                Db::commit();
                return $this->successResponse($message);
            } else {
                return $this->errorResponse(lang('request_method_incorrect'));
            }
        } catch (Exception $exc) {
            // 回滚事务
            Db::rollback();
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    /**
     * 创建商品
     * @return string
     */
    public function create() {
        ////////
        $id = input('id');
        if (!is_numeric($id)) {
            $this->redirect(url('/supplier/cancelPublishOrder/index')->build());
        }
        $cancel_id = input('cancel_id');
        $other['id'] = $id;
        $other['cancel_id'] = $cancel_id;
        View::assign('other', $other);
        return View::fetch();
    }
    
    public function getGoodsInfo() {
        if (!Request::isPost()) {
            return $this->errorResponse(lang('request_method_incorrect'));
        }
        $supplier_goods_code = input('supplier_goods_code');
        if (!is_numeric($supplier_goods_code)) {
            return $this->errorResponse(lang('invalid_id'));
        }
        try {
            $where = [
                'supplier_goods_code' => $supplier_goods_code,
            ];
            $info = Db::name('goods')->field('id,first_cate_code,second_cate_code,three_cate_code,brand_code')->where($where)->find();
            if ($info['three_cate_code']) {
                $info['cate_code'] = [$info['first_cate_code'], $info['second_cate_code'], $info['three_cate_code']];
            }
            $info['goods_purpose'] = json_decode($info['goods_purpose'], TRUE);
            //////获取描述标题数据/////////
            $where_other = [
                'goods_id' => $info['id'],
            ];
            $goods_desc = Db::name('goods_desc')->field('goods_title_en,goods_title_cn')->where($where_other)->find();
            //获取描述内容数据
            $goods_desc_content = Db::name('goods_desc_content')->field('goods_desc_en,goods_desc_cn')->where($where_other)->find();
            
            ////////////////
            $result['info'] = $info;
            $result['goods_desc'] = $goods_desc;
            $result['goods_desc_content'] = $goods_desc_content;
            return $this->successResponse($result);
        } catch (Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }
    
    /**
     * 获取商品分销信息
     * @return array|int[]
     */
    public function getGoodsDisInfo() {
        if (!Request::isPost()) {
            return $this->errorResponse(lang('request_method_incorrect'));
        }
        try {
            $supplier_goods_code = input('supplier_goods_code');
            
            
            $condition['g.istemp'] = 1;//编辑过的商品
            if ($supplier_goods_code) {
                $condition['g.supplier_goods_code'] = $supplier_goods_code;
            }
            
            $info = \app\supplier\model\Goods::alias('g')
                ->join('goods_desc gd', 'g.id=gd.goods_id')
                ->join('goods_desc_content gdc', 'g.id=gdc.goods_id')
                ->field("g.id,g.warehouse_mode,g.sales_form,g.supplier_goods_code,g.platform_goods_code,g.goods_purpose,g.add_time,g.status,g.publish_status,g.whether_dis,g.is_limit_dis_platform,gd.goods_title_cn,gd.goods_title_en,gd.goods_keyword_en,gd.goods_keyword_cn,g.first_cate_code,g.second_cate_code,g.three_cate_code,g.docking_way,g.price_method,g.freight_attr_code,g.is_tort,g.is_stop_buy,g.recommend_man,gdc.goods_desc_en,gdc.goods_desc_cn")->where($condition)->find();
            if ($info) {
                $categorylist = get_category();
                $categorylist = datalist($categorylist, 'cate_code', '', FALSE);
                $info['first_cate_name'] = $categorylist[$info['first_cate_code']]['cate_name'];
                $info['second_cate_name'] = $categorylist[$info['second_cate_code']]['cate_name'];
                $info['three_cate_name'] = $categorylist[$info['three_cate_code']]['cate_name'];
                $info['attr_name'] = Db::name('goods_freight_attr')->where('attr_code', $info['freight_attr_code'])->value('attr_name');
                $info['goods_keyword_en'] = $info['goods_keyword_en'] ? json_decode($info['goods_keyword_en'], TRUE) : [];
                $info['goods_keyword_cn'] = $info['goods_keyword_cn'] ? json_decode($info['goods_keyword_cn'], TRUE) : [];
                
                $info['goodsSpecInfo'] = Db::name('goods_spec')->field('*')->where('goods_id', $info['id'])->find();
                $goods_attribute_info = Db::name('goods_attribute')->field('*')
                    ->withAttr('material_main', function ($value, $data) {
                        return Db::name('goods_material')->where('id', $value)->value('material_name');
                    })
                    ->withAttr('material_one', function ($value, $data) {
                        return Db::name('goods_material')->where('id', $value)->value('material_name');
                    })
                    ->withAttr('material_two', function ($value, $data) {
                        return Db::name('goods_material')->where('id', $value)->value('material_name');
                    })
                    ->withAttr('material_three', function ($value, $data) {
                        if ($value) {
                            $value = json_decode($value, TRUE);
                            return Db::name('goods_material')->whereIn('id', $value)->column('material_name');
                        } else {
                            return [];
                        }
                    })
                    ->where('goods_id', $info['id'])->find();
                $info['goodsAttributeInfo'] = $goods_attribute_info;
                $info['goodsAttributExtendList'] = Db::name('goods_attribute_extend')->field('*')->where('goods_id', $info['id'])->select()->toArray();
                
                $info['goodsRetailPriceLimitList'] = GoodsRetailPriceLimit::field('*')->where('goods_id', $info['id'])
                    ->withAttr('sale_country_code', function ($value, $data) {
                        return Db::name('stock_country')->where('id', $value)->value("CONCAT(country_code,' ',country)");
                    })
                    ->withAttr('sales_platform', function ($value, $data) {
                        return Db::name('sales_platform')->where('id', $value)->value("platform_name");
                    })
                    ->order('id asc')->select()->toArray();///
                $info['goodsLabelList'] = GoodsLabel::field('label_type,goods_label')->where('goods_id', $info['id'])->order('id asc')->select()->toArray();///
                if ($info['is_limit_dis_platform'] == 1) {
                    $info['goodsLimitDisPlatformList'] = Db::name('goods_limit_dis_platform')
                        ->alias('a')
                        ->join('dis_platform b', 'a.dis_platform = b.id')
                        ->field('b.platform_name')->where('goods_id', $info['id'])->order('a.id asc')->select()->toArray();///
                } else {
                    $info['goodsLimitDisPlatformList'] = [];
                }
                $result['info'] = $info;
                ///分销区域//
                $disAreaList = Db::name('goods_distribution_area')
                    ->alias('gda')
                    ->join('distribution_area da', 'da.id=gda.distribution_area_id')
                    ->field("gda.one_shipment,gda.distribution_area_id")->where('gda.goods_id', $info['id'])->where('gda.whether_dis', 1)->withAttr('one_shipment', function ($value, $data) {
                        return $value == 1 ? '一件代发' : '';
                    })->withAttr('distribution_area_id', function ($value, $data) {
                        return $this->getDisArea($value);
                    })->select()->toArray();
                $result['disAreaList'] = $disAreaList;
                //分销库存
                $disStockList = Db::name('goods_distribution_area_stock')
                    ->alias('gdas')
                    ->join('third_party_stock tps', 'tps.id=gdas.third_party_stock_id')
                    ->field("gdas.distribution_area_id,gdas.supplier_stock_amount,gdas.platform_stock_amount,gdas.goods_distribution_area_id,tps.third_party_stock_name,tps.docking_code,tps.stock_type")->where('gdas.goods_id', $info['id'])->where('gdas.whether_dis', 1)
                    ->withAttr('distribution_area_id', function ($value, $data) {
                        return $this->getDisArea($value);
                    })->select()->toArray();
                $result['disStockList'] = $disStockList;
                //分销价格///
                $disPriceList = Db::name('goods_distribution_area_price')
                    ->alias('gdap')
                    ->join('goods_distribution_area gda', 'gda.id=gdap.goods_distribution_area_id')
                    ->join('logistics_product lp', 'lp.id=gdap.logistics_product_id')
                    ->field("gdap.distribution_area_id,gdap.goods_distribution_area_id,lp.product_name_cn,gdap.supply_price,gdap.dis_price,gdap.package_goods_amount,gda.take_effect_time")->where('gdap.goods_id', $info['id'])->where('gda.whether_dis', 1)
                    ->withAttr('distribution_area_id', function ($value, $data) {
                        return $this->getDisArea($value);
                    })
                    ->withAttr('take_effect_time', function ($value, $data) {
                        return date('Y-m-d H:i:s', $value);
                    })->select()->toArray();
                $result['disPriceList'] = $disPriceList;
                //退货地址列表//
                $disReturnGoodsList = Db::name('goods_distribution_area')
                    ->alias('gda')
                    ->join('third_party_stock tps', 'tps.id=gda.third_party_stock_id')
                    ->join('third_party_stock_address tpsa', 'tpsa.id=gda.third_party_stock_address_id')
                    ->field("gda.distribution_area_id,tps.third_party_stock_name,tpsa.stock_address,tpsa.stock_city,tpsa.stock_province,tpsa.stock_post_code,tpsa.stock_country_id,tpsa.receive_man,tpsa.tel")->where('gda.goods_id', $info['id'])->where('gda.whether_dis', 1)
                    ->withAttr('distribution_area_id', function ($value, $data) {
                        return $this->getDisArea($value);
                    })
                    ->withAttr('stock_country_id', function ($value, $data) {
                        return Db::name('stock_country')->where('id', $value)->value('country');
                    })
                    ->select()->toArray();
                $result['disReturnGoodsList'] = $disReturnGoodsList;
                
                return $this->successResponse($result);
            } else {
                return $this->errorResponse(lang('no_data_found'));
            }
        } catch (\Exception $exc) {
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    /**
     * 获取商品图片列表
     * @return array|int[]
     */
    public function getGoodsPictureList() {
        if (!Request::isPost()) {
            return $this->errorResponse(lang('request_method_incorrect'));
        }
        try {
            $goods_id = input('goods_id');
            $picture_type = input('picture_type');
            
            if (is_array($picture_type)) {
                $where[] = ['a.goods_id', '=', $goods_id];
                if (is_array($picture_type)) {
                    $where[] = ['b.type', 'in', $picture_type];
                }
                $list = Db::name('goods_picture')
                    ->alias('a')
                    ->join('goods_picture_type b', 'a.id=b.picture_id')
                    ->field('DISTINCT  a.id,a.url')->where($where)->order('id asc')->select()->toArray();
            } else {
                $where[] = ['goods_id', '=', $goods_id];
                $list = Db::name('goods_picture')->field('id,url')->where($where)->order('id asc')->select()->toArray();
            }
            
            if ($list) {
                $result['list'] = $list;
                return $this->successResponse($result);
            } else {
                return $this->errorResponse(lang('no_data_found'));
            }
        } catch (\Exception $exc) {
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    /**
     * 根据类型获取商品图片数量
     * @return void
     */
    public function getGoodsPictureCount() {
        if (!Request::isPost()) {
            return $this->errorResponse(lang('request_method_incorrect'));
        }
        try {
            $goods_id = input('goods_id');
            $goodsPictureCount['type0'] = Db::name('goods_picture')->where(['goods_id' => $goods_id])->count();
            $goodsPictureCount['type1'] = Db::name('goods_picture_type')->where(['goods_id' => $goods_id, 'type' => 1])->count();
            $goodsPictureCount['type2'] = Db::name('goods_picture_type')->where(['goods_id' => $goods_id, 'type' => 2])->count();
            $goodsPictureCount['type3'] = Db::name('goods_picture_type')->where(['goods_id' => $goods_id, 'type' => 3])->count();
            $goodsPictureCount['type4'] = Db::name('goods_picture_type')->where(['goods_id' => $goods_id, 'type' => 4])->count();
            $goodsPictureCount['type5'] = Db::name('goods_picture_type')->where(['goods_id' => $goods_id, 'type' => 5])->count();
            $goodsPictureCount['type6'] = Db::name('goods_picture_type')->where(['goods_id' => $goods_id, 'type' => 6])->count();
            $goodsPictureCount['type7'] = Db::name('goods_picture_type')->where(['goods_id' => $goods_id, 'type' => 7])->count();
            $result['goodsPictureCount'] = $goodsPictureCount;
            return $this->successResponse($result);
        } catch (\Exception $exc) {
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    /**
     * 获取分销区域
     */
    private function getDisArea($id) {
        return Db::name('distribution_area')
            ->alias('da')
            ->join('deliver_region dr', 'dr.deliver_region_id=da.deliver_region_id')
            ->field("da.dis_currency,dr.region_name,da.dis_model")
            ->where('da.id', $id)->find();
    }
    
    /**
     * 获取刊登信息
     * @return array|int[]
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPublishOrderInfo() {
        if (!Request::isPost()) {
            return $this->errorResponse(lang('request_method_incorrect'));
        }
        $id = input('id');
        $cancel_id = input('cancel_id');
        if (!is_numeric($id)) {
            return $this->errorResponse(lang('invalid_id'));
        }
        try {
            $where = [
                'id' => $id,
            ];
            $info = Db::name('publish_order')->field('*')->where($where)->find();
            if ($info['three_cate_code']) {
                $info['cate_code'] = [$info['first_cate_code'], $info['second_cate_code'], $info['three_cate_code']];
            }
            //////获取描述标题数据/////////
            $cancelInfo = Db::name('publish_order_cancel')->field('*')->where('id',$cancel_id)->find();
            ////////////////
            $result['info'] = $info;
            $result['cancelInfo'] = $cancelInfo;
            return $this->successResponse($result);
        } catch (Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }
    
    /**
     * 保存刊登信息
     * @return array
     */
    public function savePublishOrder() {
        // 开始数据库事务
        Db::startTrans();
        try {
            if (!Request::isPost()) {
                return $this->errorResponse(lang('request_method_incorrect'));
            }
            $data = Request::post();
            
            $id = $data['id'];
            // 验证ID的合法性
            if (!is_numeric($id) || $id <= 0) {
                return $this->errorResponse(lang('invalid_id'));
            }
            // 数据验证
            $validate = new \app\supplier\validate\PublishOrder();
            $check_result = $validate->check($data);
            if (!$check_result) {
                return $this->errorResponse($validate->getError());
            }
            /////////////////
            $img_count = Db::name('publish_order_picture')->where('publish_order_id', $id)->count();
            if ($img_count == 0) {
                return $this->errorResponse('请上传图片');
            }
            
            $data['update_time'] = time();
            $data['istemp'] = 1;
            $data['publish_status'] = 0;//未刊登
            $data['audit_status'] = 2;//提交待审核
            
            $where['id'] = $id;
            $update_result = Db::name('publish_order')->where($where)->withoutField('id,supplier_id,publish_order_code')->update($data);
            if ($update_result === FALSE) {
                // 回滚事务
                Db::rollback();
                return $this->errorResponse(lang('editing_failed'));
            }
            // 提交事务
            Db::commit();
            $result['url'] = url('/supplier/publishOrder/index')->build();
            $result['message'] = lang('successfully_saved');
            return $this->successResponse($result);
        } catch (Exception $exc) {
            // 回滚事务
            Db::rollback();
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    /**
     * 删除刊登信息
     * @return array
     */
    public function deletePublishOrder() {
        if (!Request::isPost()) {
            return $this->errorResponse(lang('request_method_incorrect'));
        }
        $id = input('id');
        if (!$id || !is_numeric($id)) {
            throw new Exception(lang('parameter_error'));
        }
        $condition['id'] = $id;
        $info = Db::name('publish_order')->field('id,publish_status,audit_status')->where($condition)->find();
        if (!$info) {
            return $this->errorResponse(lang('delete_failed'));
        } else {
            if ($info['publish_status'] == 1) {
                return $this->errorResponse('刊登单已发布,不能删除');
            }
        }
        // 启动事务
        Db::startTrans();
        try {
            $del_result = Db::name('publish_order')->where('id', $id)->delete();
            if ($del_result) {
                ////执行成功,提交事务
                Db::commit();
                return $this->successResponse(lang('delete_successful'));
            } else {
                Db::rollback();
                return $this->errorResponse(lang('delete_failed'));
            }
        } catch (\Exception $exc) {
            //// 回滚事务
            Db::rollback();
            return $this->errorResponse($exc->getMessage());
        }
        
    }
    
    
    /**
     * 上传商品图片
     * @return array|int[]
     */
    
    /**
     * 获取商品图片列表
     * @return array|int[]
     */
    public function getPublishOrderPictureList() {
        if (!Request::isPost()) {
            return $this->errorResponse(lang('request_method_incorrect'));
        }
        try {
            $publish_order_id = input('publish_order_id');
            
            $where[] = ['publish_order_id', '=', $publish_order_id];
            $list = Db::name('publish_order_picture')->field('id,url')->where($where)->order('id asc')->select()->toArray();
            
            if ($list) {
                $result['list'] = $list;
                return $this->successResponse($result);
            } else {
                return $this->errorResponse(lang('no_data_found'));
            }
        } catch (\Exception $exc) {
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    
    /**
     * 删除商品图片
     * @return array|int[]
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function deletePublishOrderPicture() {
        if (!Request::isPost()) {
            return $this->errorResponse(lang('request_method_incorrect'));
        }
        $id = input('id');
        if (!$id || !is_numeric($id)) {
            return $this->errorResponse(lang('parameter_error'));
        }
        $condition['id'] = $id;
        $info = Db::name('publish_order_picture')->field('id,publish_order_id,url')->where($condition)->find();
        if (!$info) {
            return $this->errorResponse(lang('delete_failed'));
        }
        // 启动事务
        Db::startTrans();
        try {
            $del_result = Db::name('publish_order_picture')->where('id', $id)->delete();
            if ($del_result) {
                ////执行成功,提交事务
                Db::commit();
                delpic($info['url']);
                return $this->successResponse(lang('delete_successful'));
            } else {
                Db::rollback();
                return $this->errorResponse(lang('delete_failed'));
            }
        } catch (\Exception $exc) {
            //// 回滚事务
            Db::rollback();
            return $this->errorResponse($exc->getMessage());
        }
        
    }
    
    
    /**
     * 获取分类列表
     * @return array
     */
    public function getCategoryList() {
        if (!Request::isPost()) {
            return $this->errorResponse(lang('request_method_incorrect'));
        }
        try {
            $result['list'] = $this->getchildrenids(0);
            return $this->successResponse($result);
        } catch (\Exception $exc) {
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    /**
     * 循环获取分类数据
     * @param $id
     * @return array|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function getchildrenids($id) {
        $where['father_code'] = $id;
        $result = Db::name('category')->field('id,cate_code as value,cate_name as label')->where($where)->cacheAlways(TRUE, 0, 'category')->order('id asc')->select()->toArray();
        if ($result) {
            foreach ($result as &$val) {
                $val['children'] = $this->getchildrenids($val['value']); ////
            }
            return $result;
        }
    }
    
    /**
     * 商品发布
     * @return array|int[]
     */
    public function goodsPublish() {
        // 开始数据库事务
        Db::startTrans();
        try {
            if (!Request::isPost()) {
                return $this->errorResponse(lang('request_method_incorrect'));
            }
            $publish_order_id = input('publish_order_id');
            $goods_info = Db::name('goods')->field('id')->where('id', $publish_order_id)->find();
            if ($goods_info) {
                $update_data = [
                    'publish_status' => 1,
                    'update_time'    => time(),
                ];
                Db::name('goods')->where('id', $publish_order_id)->update($update_data);
            } else {
                return $this->errorResponse(lang('goods_not_exist'));
            }
            // 提交事务
            Db::commit();
            return $this->successResponse(lang('goods_publish_success'));
        } catch (Exception $exc) {
            // 回滚事务
            Db::rollback();
            return $this->errorResponse($exc->getMessage());
        }
    }
}