quanwei
2025-10-29 76ed09d116f484b261d44219de300b79eb2013b3
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
<?php
 
namespace app\api\model\plus\ticket;
 
use app\common\exception\BaseException;
use app\common\model\plus\ticket\Ticket as TicketModel;
 
/**
 * 模型
 */
class Ticket extends TicketModel
{
    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'is_delete',
        'app_id',
        'update_time'
    ];
 
    /**
     * 详情
     */
    public static function detail($upload_id)
    {
        if (!$model = parent::detail($upload_id)) {
            throw new BaseException(['msg' => '信息不存在']);
        }
        return $model;
    }
 
    /**
     * 获取列表
     */
    public function getList($params,$user)
    {
        $model = $this;
        return $model ->with(['image.file'])
            ->where('user_id', '=', $user["user_id"])
            ->where('is_delete', '=', 0)
            ->order(['create_time' => 'desc'])
            ->paginate($params);
    }
    /**
     * 添加小票上传
     */
    public function addTicket($user,$params)
    {
        $formData=json_decode($params["formData"], true);
        $data = [
            'user_id' => $user["user_id"],
            'content' => $formData['content'],
            'app_id' => self::$app_id
        ];
        if (empty($formData["content"]) && empty($formData["image_list"])) {
            $this->error = '图片与备注不能留空';
            return false;
        }
        return $this->transaction(function () use ( $formData, $data) {
            // 记录备注内容
            $this->save($data);
            // 记录小票图片
            $this->saveAllImages($this['upload_id'],$formData);
            return true;
        });
    }
    /**
     * 记录小票图片
     */
    private function saveAllImages($upload_id,$formData)
    {
        // 生成图片数据
        if(!empty($formData['image_list'])){
            $imageData = [];
            foreach ($formData['image_list'] as $imageId) {
                $imageData[] = [
                    'upload_id' => $upload_id,
                    'image_id' => $imageId['file_id'],
                    'app_id' => self::$app_id
                ];
            }
            $model = new TicketUploadImage;
            return !empty($imageData) && $model->saveAll($imageData);
        }
        return true;
 
    }
 
}