123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- namespace app\model;
- use think\Model;
- class Construction extends Model
- {
- public function style()
- {
- return $this->hasOne(Decostyle::class, 'id', 'style_id')->visible(['name']);
- }
- public function designer()
- {
- return $this->belongsTo(Employee::class, 'designer_id')->visible(['name']);
- }
- public function community()
- {
- return $this->hasOne(Community::class, 'id', 'community_id')->visible(['name']);
- }
- public function housetype()
- {
- return $this->morphTo('housetype', [
- 'building_housetype' => 'app\model\BuildingHousetype',
- 'housetype' => 'app\model\Housetype'
- ]);
- }
- public function shareLog()
- {
- return $this->hasMany(ShareLog::class, 'data_id', 'id');
- }
- public function record()
- {
- return $this->hasMany(ConstructionRecord::class, 'construction_id', 'id')->visible(['name']);
- }
- public function getCoverAttr($value)
- {
- if (empty($value)) return $value;
- $ali_oss_bindurl = config('app.ali_oss_bindurl');
- $url = 'https://' . $ali_oss_bindurl . '/' . $value;
- return $url;
- }
- public function setCoverAttr($value)
- {
- $domain = config('app.ali_oss_bindurl');
- $search = 'https://' . $domain . '/';
- return str_replace($search, '', $value);
- }
- public function getCoverShareImgAttr($value)
- {
- if (empty($value)) return $value;
- $ali_oss_bindurl = config('app.ali_oss_bindurl');
- $url = 'https://' . $ali_oss_bindurl . '/' . $value;
- return $url;
- }
- public function setCoverShareImgAttr($value)
- {
- $domain = config('app.ali_oss_bindurl');
- $search = 'https://' . $domain . '/';
- return str_replace($search, '', $value);
- }
- public function employee()
- {
- return $this->belongsTo(Employee::class, 'employee_id', 'id');
- }
- public static function onBeforeUpdate($case)
- {
- $old = static::find($case->id);
- if ($old->housetype_type == 'building_housetype' && ($case->housetype_type == 'housetype' || $old->housetype_id != $case->housetype_id)) {
- $find = BuildingHousetype::where(['id' => $old->housetype_id])->find();
- BuildingConstruction::where([
- 'building_id' => $find->building_id,
- 'construction_id' => $old->id,
- 'root_id' => $old->root_id
- ])->delete();
- if (!empty($find->constructionid)) {
- $cutid = explode(',', $find->constructionid);trace($cutid);
- do{
- $k = array_search($old->id, $cutid);
- if ($k !== false) {
- unset($cutid[$k]);
- }
- } while ($k !== false);trace($cutid);
- $find->constructionid = implode(',', $cutid);
- $find->save();
- }
- }
- }
- public static function onAfterWrite($case)
- {
- if ($case->housetype_type == 'building_housetype') {
- $find = BuildingHousetype::where(['id' => $case->housetype_id])->find();
- BuildingConstruction::create([
- 'building_id' => $find->building_id,
- 'construction_id' => $case->id,
- 'root_id' => $case->root_id
- ]);
- $cutid = empty($find->constructionid)?[]: explode(',', $find->constructionid);
- $cutid[] = $case->id;
- $find->constructionid = implode(',', $cutid);
- $find->save();
- }
- }
- }
|