Construction.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\model;
  3. use think\Model;
  4. class Construction extends Model
  5. {
  6. public function style()
  7. {
  8. return $this->hasOne(Decostyle::class, 'id', 'style_id')->visible(['name']);
  9. }
  10. public function designer()
  11. {
  12. return $this->belongsTo(Employee::class, 'designer_id')->visible(['name']);
  13. }
  14. public function community()
  15. {
  16. return $this->hasOne(Community::class, 'id', 'community_id')->visible(['name']);
  17. }
  18. public function housetype()
  19. {
  20. return $this->morphTo('housetype', [
  21. 'building_housetype' => 'app\model\BuildingHousetype',
  22. 'housetype' => 'app\model\Housetype'
  23. ]);
  24. }
  25. public function shareLog()
  26. {
  27. return $this->hasMany(ShareLog::class, 'data_id', 'id');
  28. }
  29. public function record()
  30. {
  31. return $this->hasMany(ConstructionRecord::class, 'construction_id', 'id')->visible(['name']);
  32. }
  33. public function getCoverAttr($value)
  34. {
  35. if (empty($value)) return $value;
  36. $ali_oss_bindurl = config('app.ali_oss_bindurl');
  37. $url = 'https://' . $ali_oss_bindurl . '/' . $value;
  38. return $url;
  39. }
  40. public function setCoverAttr($value)
  41. {
  42. $domain = config('app.ali_oss_bindurl');
  43. $search = 'https://' . $domain . '/';
  44. return str_replace($search, '', $value);
  45. }
  46. public function getCoverShareImgAttr($value)
  47. {
  48. if (empty($value)) return $value;
  49. $ali_oss_bindurl = config('app.ali_oss_bindurl');
  50. $url = 'https://' . $ali_oss_bindurl . '/' . $value;
  51. return $url;
  52. }
  53. public function setCoverShareImgAttr($value)
  54. {
  55. $domain = config('app.ali_oss_bindurl');
  56. $search = 'https://' . $domain . '/';
  57. return str_replace($search, '', $value);
  58. }
  59. public function employee()
  60. {
  61. return $this->belongsTo(Employee::class, 'employee_id', 'id');
  62. }
  63. public static function onBeforeUpdate($case)
  64. {
  65. $old = static::find($case->id);
  66. if ($old->housetype_type == 'building_housetype' && ($case->housetype_type == 'housetype' || $old->housetype_id != $case->housetype_id)) {
  67. $find = BuildingHousetype::where(['id' => $old->housetype_id])->find();
  68. BuildingConstruction::where([
  69. 'building_id' => $find->building_id,
  70. 'construction_id' => $old->id,
  71. 'root_id' => $old->root_id
  72. ])->delete();
  73. if (!empty($find->constructionid)) {
  74. $cutid = explode(',', $find->constructionid);trace($cutid);
  75. do{
  76. $k = array_search($old->id, $cutid);
  77. if ($k !== false) {
  78. unset($cutid[$k]);
  79. }
  80. } while ($k !== false);trace($cutid);
  81. $find->constructionid = implode(',', $cutid);
  82. $find->save();
  83. }
  84. }
  85. }
  86. public static function onAfterWrite($case)
  87. {
  88. if ($case->housetype_type == 'building_housetype') {
  89. $find = BuildingHousetype::where(['id' => $case->housetype_id])->find();
  90. BuildingConstruction::create([
  91. 'building_id' => $find->building_id,
  92. 'construction_id' => $case->id,
  93. 'root_id' => $case->root_id
  94. ]);
  95. $cutid = empty($find->constructionid)?[]: explode(',', $find->constructionid);
  96. $cutid[] = $case->id;
  97. $find->constructionid = implode(',', $cutid);
  98. $find->save();
  99. }
  100. }
  101. }