chengkun
2025-08-06 b54e02d98e42ae73071e3c01e59f12671d13d06a
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
<?php
 
namespace app\admin\controller;
 
use think\Exception;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
use app\supplier\validate\ThirdPartyStock;
use app\supplier\validate\ThirdPartyStockAddress;
 
class ThirdPartyStockTemp extends Common {
    
    /**
     * 新建仓库首页
     * @return string
     */
    public function index() {
        ////////
        return View::fetch();
    }
    
    /**
     * 审核仓库
     * @return array|int[]
     */
    public function reviewStock() {
        // 开始数据库事务
        Db::startTrans();
        try {
            if (!Request::isPost()) {
                throw new Exception(lang('request_method_incorrect'));
            }
            $id = input('post.id');
            if (is_numeric($id)) {
                $status = input('post.status');
                //更新数据
                $info = Db::name('third_party_stock')->field('id,status')->where('id', $id)->find();
                if (!$info) {
                    return $this->errorResponse(lang('request_method_incorrect'));
                }
                
                if ($status == 1) {
                    $count0 = Db::name('third_party_stock_address')->where('third_party_stock_id', $id)->where('status', 0)->count();
                    if ($count0 > 0) {
                        return $this->errorResponse('请先审核仓库地址');
                    }
                    $count1 = Db::name('third_party_stock_address')->where('third_party_stock_id', $id)->where('status', 1)->count();
                    if ($count1 == 0) {
                        return $this->errorResponse('至少有一个仓库地址通过审核');
                    }
                    Db::name('third_party_stock')->where('id', $id)->update(['status' => 1, 'need_review' => 1]);
                    $message = '操作成功,审核通过';
                } else {
                    Db::name('third_party_stock_address')->where('third_party_stock_id', $id)->where('status', 0)->save(['status' => 2]);
                    Db::name('third_party_stock')->where('id', $id)->update(['status' => 2, 'need_review' => 1]);
                    $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 array|int[]
     */
    public function reviewAddress() {
        // 开始数据库事务
        Db::startTrans();
        try {
            if (!Request::isPost()) {
                throw new Exception(lang('request_method_incorrect'));
            }
            $id = input('post.id');
            if (is_numeric($id)) {
                $status = input('post.status');
                $result = [
                    'stock_status' => 0,
                    'status'       => 0,
                    'need_review'  => 0,
                ];
                //更新数据
                $info = Db::name('third_party_stock_address')->field('id,status,third_party_stock_id')->where('id', $id)->find();
                if (!$info) {
                    return $this->errorResponse(lang('request_method_incorrect'));
                }
                if ($status == 1) {
                    Db::name('third_party_stock_address')->where('id', $id)->update(['status' => 1]);
                    $result['status'] = 1;
                    $result['message'] = '操作成功,审核通过';
                } else {
                    Db::name('third_party_stock_address')->where('id', $id)->update(['status' => 2]);
                    $result['status'] = 2;
                    $result['message'] = '操作成功,审核不通过';
                }
                
                $count0 = Db::name('third_party_stock_address')->where('third_party_stock_id', $info['third_party_stock_id'])->where('status', 0)->count();
                $count1 = Db::name('third_party_stock_address')->where('third_party_stock_id', $info['third_party_stock_id'])->where('status', 1)->count();
                if ($count0 == 0) {
                    //仓库地址全部审核
                    if ($count1 == 0) {
                        //仓库不通过审核
                        Db::name('third_party_stock')->where('id', $info['third_party_stock_id'])->update(['need_review' => 1, 'status' => 2]);
                        $result['need_review'] = 1;
                        $result['stock_status'] = 2;
                    } else {
                        //仓库通过审核
                        Db::name('third_party_stock')->where('id', $info['third_party_stock_id'])->update(['need_review' => 1, 'status' => 1]);
                        $result['need_review'] = 1;
                        $result['stock_status'] = 1;
                    }
                }
                // 提交事务
                Db::commit();
                return $this->successResponse($result);
            } else {
                return $this->errorResponse(lang('request_method_incorrect'));
            }
        } catch (Exception $exc) {
            // 回滚事务
            Db::rollback();
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    /**
     * 获取仓库列表
     */
    public function stockList() {
        if (Request::isPost()) {
            $kw = input('kw');
            
            if (!empty($kw)) {
                $condition[] = array('third_party_stock_name|docking_code', 'like', "%$kw%");
            }
//            $condition[]= array('need_review', '=', 2);
            $p = input('p', 1);
            $_GET['p'] = $p;
            $other['page_size'] = input('page_size', 20);
            $order = 'need_review desc,edit_time asc';
            $count = Db::name('third_party_stock')->where($condition)->count();
            $list = Db::name('third_party_stock')->field("id,stock_type,docking_system,third_party_stock_name,docking_code,status,need_review,edit_time")->where($condition)
                ->withAttr('edit_time', function ($value, $data) {
                    return date('Y-m-d H:i:s', $value);
                })
                ->order($order)->page($p, $other['page_size'])->select()->toArray();
            $other['count'] = $count;
            if (!$list) {
                $list = [];
            }
            $d['code'] = 200;
            $d['list'] = $list;
            $d['other'] = $other;
            return $d;
        }
    }
    
    /**
     * 获取仓库列表
     */
    public function stockAddressList() {
        if (Request::isPost()) {
            $third_party_stock_id = input('third_party_stock_id');
            
            $condition['a.third_party_stock_id'] = $third_party_stock_id;
            $order = 'a.id asc';
            $list = Db::name('third_party_stock_address')
                ->alias('a')
                ->join('web_stock_country b', 'a.stock_country_id=b.id')
                ->field("a.id,a.receive_man,a.tel,a.stock_address,a.stock_city,a.stock_province,a.stock_country_id,a.stock_post_code,a.suit_range,b.country,a.status")->where($condition)
                ->withAttr('suit_range', function ($value, $data) {
                    return $value ? array_map('intval', explode(',', $value)) : [];
                })
                ->order($order)->select()->toArray();
            
            if (!$list) {
                $result['code'] = 400;
                return $result;
            } else {
                $result['code'] = 200;
                $result['list'] = $list;
                return $result;
            }
        }
    }
    
    
}