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
<?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\user\User as UserModel;
use app\common\model\user\PointsLog as PointsLogModel;
 
/**
 * 模型
 */
class DemandProject 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', '=', 0)
            ->where('user_id', '=', $user_id)
            ->order(['create_time' => 'desc'])
            ->paginate($postdata);
    }
    /**
     * 新增记录
     */
    public function add($postdata,$demand,$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'=>$demand["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"],
            'show_phone'=>$data["show_phone"],
            '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);
 
            //减少连盟币
            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"],
            'show_phone'=>$data["show_phone"],
            '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);
            
            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;
 
    }
 
    /**
     * 软删除
     */
    public function setDelete($id)
    {
        return $this->where("project_id","=",$id)->save(['is_delete' => 1]);
    }
 
}