123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace app\model;
- use think\Model;
- class MaterialCase extends Model
- {
- /**
- * 图片拼接
- */
- public function getCoverImgAttr($value)
- {
- if (empty($value)) return $value;
- $ali_oss_bindurl = config('app.ali_oss_bindurl');
- $url = 'https://' . $ali_oss_bindurl . '/' . $value;
- return $url;
- }
- public function setCoverImgAttr($value)
- {
- $domain = config('app.ali_oss_bindurl');
- $search = 'https://' . $domain . '/';
- return str_replace($search, '', $value);
- }
- public function getVideoCaseAttr($value)
- {
- if (empty($value)) return $value;
- $ali_oss_bindurl = config('app.ali_oss_bindurl');
- $url = 'https://' . $ali_oss_bindurl . '/' . $value;
- return $url;
- }
- public function getQrcodeAttr($value)
- {
- if (empty($value)) return $value;
- $ali_oss_bindurl = config('app.ali_oss_bindurl');
- $url = 'https://' . $ali_oss_bindurl . '/' . $value;
- return $url;
- }
- public function getMoneyAttr($value)
- {
- if (empty($value)) return '--';
- $flval = floatval($value);
- if ($flval == 0) return '暂无报价';
- return $flval . '万元';
- }
- public function getSquareAttr($value)
- {
- return floatval($value);
- }
- /**
- * 关联用户
- */
- public function community()
- {
- return $this->belongsTo(Community::class, 'community_id');
- }
- public function designer()
- {
- return $this->belongsTo(Employee::class, 'designer_id', 'id');
- }
- public function decostyle()
- {
- return $this->belongsTo(Decostyle::class, 'style_id');
- }
- public function housetype()
- {
- return $this->morphTo('housetype', [
- 'building_housetype' => 'app\model\BuildingHousetype',
- 'housetype' => 'app\model\Housetype'
- ]);
- }
- 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)) {
- BuildingMaterialCase::where([
- 'root_id'=>$old->root_id,
- 'housetype_id'=>$old->housetype_id,
- 'material_case_id'=>$old->id
- ])->delete();
- }
- }
- public static function onAfterWrite($case)
- {
- if ($case->housetype_type == 'building_housetype') {
- $building_id = BuildingHousetype::where(['id' => $case->housetype_id])->value('building_id');
- BuildingMaterialCase::create([
- 'building_id' => $building_id,
- 'housetype_id' => $case->housetype_id,
- 'material_case_id' => $case->id,
- 'root_id' => $case->root_id
- ]);
- }
- }
- }
|