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
<?php
 
namespace app\api\model\plus\release;
 
use app\common\model\plus\release\Project as ProjectModel;
use app\common\model\plus\release\ReleaseProjectImage as ReleaseProjectImageModel;
use app\common\model\plus\release\Tag as TagModel;
use app\common\model\plus\release\ReleaseProjectTag as ReleaseProjectTagModel;
use app\common\model\user\User as UserModel;
use app\common\model\user\PointsLog as PointsLogModel;
 
 
/**
 * 模型
 */
class SupplyProject extends ProjectModel
{
    /**
     * 获取提现明细
     */
    public function getList($user_id, $postdata)
    {
        $model = $this;
        if(!empty($postdata["keyword"])){
            $model = $model->where('name', 'like', '%'.$postdata["keyword"].'%');
        }
        return $model->where('is_delete', '=', 0)
            ->where('project_type', '=', 1)
            ->where('user_id', '=', $user_id)
            ->order(['create_time' => 'desc'])
            ->paginate($postdata);
    }
    /**
     * 新增记录
     */
    public function add($postdata,$supply,$pay_price)
    {
        $data = json_decode($postdata["formData"],true);
        if (empty($data['name'])) {
                $this->error = '请输入标题';
                return false;
         }
        if (empty($data['category_id'])) {
                $this->error = '请选择分类';
                return false;
         }
        if (empty($data['price'])) {
                $this->error = '请输入您的价格';
                return false;
        }
       if (empty($data['content'])) {
                $this->error = '请输入您的详细描述';
                return false;
        }
        
        $save_data =[
            'user_id'=>$supply["user_id"],
            'name'=>$data["name"],
            'category_id'=>$data["category_id"],
            'price'=>$data["price"],
            'content'=>$data["content"],
            'product_content'=>$data["product_content"],
            'product_case'=>$data["product_case"],
            'detail'=>$data["detail"],
            'project_type'=>1,
            'is_show'=>$data["is_show"],
            'app_id'=>self::$app_id,
 
        ];
        if(!empty($data["finish_time"]) && $data["finish_time"] != '请选择日期'){
            $save_data["finish_time"] = strtotime($data["finish_time"]);
        }else{
            $save_data["finish_time"] = '';
        }
 
          return $this->transaction(function () use ($data, $save_data,$pay_price) {
            // 记录内容
            $this->save($save_data);
            // 记录图片
            $this->saveAllImages($this['project_id'],$data);
            // 记录标签
            $this->saveTag($this['project_id'],$data);
 
             //减少连盟币
            if($pay_price>0){
                (new UserModel())->where('user_id', '=', $save_data['user_id'])->dec('points',$pay_price)->update();
                PointsLogModel::add([
                    'user_id' => $save_data['user_id'],
                    'value' => -$pay_price,
                    'describe' => "发布供应消耗连盟币",
                    'app_id' => $save_data['app_id'],
                ]);
            }
            
            return $this['project_id'];
        });
    }
 
    /**
     * 更新记录
     */
    public function edit($postdata)
    {
        $data = json_decode($postdata["formData"],true);
        if (empty($data['name'])) {
                $this->error = '请输入标题';
                return false;
         }
        if (empty($data['category_id'])) {
                $this->error = '请选择分类';
                return false;
         }
        if (empty($data['price'])) {
                $this->error = '请输入您的价格';
                return false;
        }
       if (empty($data['content'])) {
                $this->error = '请输入您的详细描述';
                return false;
        }
         $save_data =[
            'name'=>$data["name"],
            'category_id'=>$data["category_id"],
            'price'=>$data["price"],
            'content'=>$data["content"],
            'product_content'=>$data["product_content"],
            'product_case'=>$data["product_case"],
            'detail'=>$data["detail"],
            'is_show'=>$data["is_show"],
            'status'=>0,
        ];
        if(!empty($data["finish_time"]) && $data["finish_time"] != '请选择日期'){
            $save_data["finish_time"] = strtotime($data["finish_time"]);
        }else{
            $save_data["finish_time"] = '';
        }
 
        return $this->transaction(function () use ($data, $save_data) {
            // 记录内容
            $this->where("project_id","=",$data["project_id"])->save($save_data);
            // 记录图片
            $this->saveAllImages($data['project_id'],$data);
            // 记录标签
            $this->saveTag($data['project_id'],$data);
            
            return true;
        });
    }
     /**
     * 记录图片
     */
    private function saveAllImages($id,$formData)
    {
        (new ReleaseProjectImageModel())->where("project_id","=",$id)->delete();
        // 生成图片数据
        if(!empty($formData['image_list'])){
            $imageData = [];
            foreach ($formData['image_list'] as $imageId) {
                $imageData[] = [
                    'project_id' => $id,
                    'image_id' => !empty($imageId['image_id']) ? $imageId['image_id'] : $imageId['file_id'],
                    'app_id' => self::$app_id
                ];
            }
            $model = new ReleaseProjectImageModel;
            return !empty($imageData) && $model->saveAll($imageData);
        }
        return true;
 
    }
 
    /**
     * 记录标签
     */
    private function saveTag($id,$formData)
    {
        (new ReleaseProjectTagModel())->where("project_id","=",$id)->delete();
        // 生成数据
        if(!empty($formData['tag_list'])){
            $tagData = [];
            foreach ($formData['tag_list'] as $val) {
                $tagData[] = [
                    'project_id' => $id,
                    'tag_id' => $val,
                    'app_id' => self::$app_id
                ];
            }
            $model = new ReleaseProjectTagModel;
            return !empty($tagData) && $model->saveAll($tagData);
        }
        return true;
 
    }
 
    /**
     * 软删除
     */
    public function setDelete($id)
    {
        return $this->where("project_id","=",$id)->save(['is_delete' => 1]);
    }
 
}