admin/app/common/model/release/DemandProject.php
New file
@@ -0,0 +1,157 @@
<?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;
/**
 * 模型
 */
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)
    {
        $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"],
            '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) {
            // 记录内容
            $this->save($save_data);
            // 记录图片
            $this->saveAllImages($this['project_id'],$data);
            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"],
            '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]);
    }
}