quanwei
2025-12-10 898043fc97d2ab8b793fd317a049b874ed207c6d
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\common\model\plus\business;
 
use app\common\model\BaseModel;
use app\common\model\settings\Region;
 
/**
 * 名片管理模型
 */
class Business extends BaseModel
{
    protected $name = 'business_card';
    /**
     * 追加字段
     * @var string[]
     */
    protected $append = ['region'];
 
    /**
     * 地区名称
     * @param $value
     * @param $data
     * @return array
     */
    public function getRegionAttr($value, $data)
    {
        return [
            'province' => Region::getNameById($data['province_id']),
            'city' => Region::getNameById($data['city_id']),
            'region' => $data['region_id'] == 0 ? '' : Region::getNameById($data['region_id']),
        ];
    }
 
    public function getSexAttr($value)
    {
        $data = [10 => '未知', 20 => '男', 30 => '女'];
        return $data[$value];
    }
 
    /**
     * 关联用户
     * @return \think\model\relation\HasOne
     */
    public function user()
    {
        $model = self::getCalledModule() ?: 'common';
        return $this->hasOne("app\\$model\\model\\user\\User", 'user_id', 'user_id');
    }
 
    /**
     * 头像
     * @return \think\model\relation\HasOne
     */
    public function image()
    {
        $model = self::getCalledModule() ?: 'common';
        return $this->hasOne("app\\$model\\model\\file\\UploadFile", 'file_id', 'file_id')->bind(['file_path']);
    }
 
    /**
     * logo
     * @return \think\model\relation\HasOne
     */
    public function logoImage()
    {
        $model = self::getCalledModule() ?: 'common';
        return $this->hasOne("app\\$model\\model\\file\\UploadFile", 'file_id', 'logo');
    }
 
    /**
     * 等级
     * @return \think\model\relation\HasOne
     */
    public function grade()
    {
        $model = self::getCalledModule() ?: 'common';
        return $this->hasOne("app\\$model\\model\\plus\\business\\Grade", 'grade_id', 'grade_id');
    }
    public function industry()
    {
        $model = self::getCalledModule() ?: 'common';
        return $this->hasOne("app\\$model\\model\\plus\\business\\Industry", 'industry_id', 'industry_id');
    }
 
    /**
     * 添加
     * @param $data
     * @return false|int
     */
    public function add($data)
    {
        $data['app_id'] = self::$app_id;
        $data['grade_id'] = (new Grade())->getDefaultGradeId();
        return $this->save($data);
    }
 
    public function getUnitAttr($name)
    {
        return json_decode($name);
    }
 
    public function getDutiesAttr($name)
    {
        return json_decode($name);
    }
 
    public function getAddressAttr($name)
    {
        return json_decode($name);
    }
 
    public function getPositionAttr($name)
    {
        return $name ? json_decode($name) : [];
    }
 
    /**
     * 获取名片列表
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList($param = [])
    {
        $paramr = array_merge(['listRow' => 15], $param);
        $where = [];
        !empty($paramr['name']) && $where['name'] = ['like', '%' . $paramr['name'] . '%'];
        !empty($paramr['search']) && $where['name|duties|unit'] = ['like', '%' . $paramr['search'] . '%'];
        !empty($paramr['user_id']) && $where['user_id'] = $paramr['user_id'];
        !empty($paramr['industry_id']) && $where['industry_id'] = $paramr['industry_id'];
        !empty($paramr['province_id']) && $where['province_id'] = $paramr['province_id'];
        !empty($paramr['city_id']) && $where['city_id'] = $paramr['city_id'];
        !empty($paramr['region_id']) && $where['region_id'] = $paramr['region_id'];
        // 检查置顶时间是否过期,如果过期则设置为非置顶
        $this->checkTopExpire();
        if (!empty($paramr['sort'])) {
            $order = ['is_top' => 'desc', 'top_time' => 'desc'];
            if ($paramr['sort'] == 'name') {
                $order['name'] = "asc";
            } else if ($paramr['sort'] == 'time') {
                $order['create_time'] = "asc";
            } else {
                $order['unit'] = "asc";
            }
        } else {
            // 按置顶状态和创建时间排序
            $order = ['is_top' => 'desc', 'top_time' => 'desc', 'create_time' => 'desc'];
        }
        return $this->with(['user', 'image', 'logoImage', 'grade'])->order($order)->where($where)->paginate($paramr);
    }
 
    /**
     * 检查并更新过期的置顶名片
     */
    private function checkTopExpire()
    {
        $this->where('is_top', 1)
            ->where('top_time', '<', time())
            ->update(['is_top' => 0]);
    }
 
    /**
     * 获取名片详情
     * @param $business_card_id
     * @return array|false|\think\Model|null
     * @throws \think\db\exception\DbException
     */
    public static function detail($business_card_id)
    {
        return (new self())->with(['user', 'image', 'logoImage', 'grade', 'industry'])->where(['business_card_id' => $business_card_id])->find();
    }
}