1
0

MaterialCase.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace app\model;
  3. use think\Model;
  4. class MaterialCase extends Model
  5. {
  6. /**
  7. * 图片拼接
  8. */
  9. public function getCoverImgAttr($value)
  10. {
  11. if (empty($value)) return $value;
  12. $ali_oss_bindurl = config('app.ali_oss_bindurl');
  13. $url = 'https://' . $ali_oss_bindurl . '/' . $value;
  14. return $url;
  15. }
  16. public function setCoverImgAttr($value)
  17. {
  18. $domain = config('app.ali_oss_bindurl');
  19. $search = 'https://' . $domain . '/';
  20. return str_replace($search, '', $value);
  21. }
  22. public function getVideoCaseAttr($value)
  23. {
  24. if (empty($value)) return $value;
  25. $ali_oss_bindurl = config('app.ali_oss_bindurl');
  26. $url = 'https://' . $ali_oss_bindurl . '/' . $value;
  27. return $url;
  28. }
  29. public function getQrcodeAttr($value)
  30. {
  31. if (empty($value)) return $value;
  32. $ali_oss_bindurl = config('app.ali_oss_bindurl');
  33. $url = 'https://' . $ali_oss_bindurl . '/' . $value;
  34. return $url;
  35. }
  36. public function getMoneyAttr($value)
  37. {
  38. if (empty($value)) return '--';
  39. $flval = floatval($value);
  40. if ($flval == 0) return '暂无报价';
  41. return $flval . '万元';
  42. }
  43. public function getSquareAttr($value)
  44. {
  45. return floatval($value);
  46. }
  47. /**
  48. * 关联用户
  49. */
  50. public function community()
  51. {
  52. return $this->belongsTo(Community::class, 'community_id');
  53. }
  54. public function designer()
  55. {
  56. return $this->belongsTo(Employee::class, 'designer_id', 'id');
  57. }
  58. public function decostyle()
  59. {
  60. return $this->belongsTo(Decostyle::class, 'style_id');
  61. }
  62. public function housetype()
  63. {
  64. return $this->morphTo('housetype', [
  65. 'building_housetype' => 'app\model\BuildingHousetype',
  66. 'housetype' => 'app\model\Housetype'
  67. ]);
  68. }
  69. public function employee()
  70. {
  71. return $this->belongsTo(Employee::class, 'employee_id', 'id');
  72. }
  73. public static function onBeforeUpdate($case){
  74. $old = static::find($case->id);
  75. if($old->housetype_type == 'building_housetype' && ($case->housetype_type == 'housetype' || $old->housetype_id != $case->housetype_id)) {
  76. BuildingMaterialCase::where([
  77. 'root_id'=>$old->root_id,
  78. 'housetype_id'=>$old->housetype_id,
  79. 'material_case_id'=>$old->id
  80. ])->delete();
  81. }
  82. }
  83. public static function onAfterWrite($case)
  84. {
  85. if ($case->housetype_type == 'building_housetype') {
  86. $building_id = BuildingHousetype::where(['id' => $case->housetype_id])->value('building_id');
  87. BuildingMaterialCase::create([
  88. 'building_id' => $building_id,
  89. 'housetype_id' => $case->housetype_id,
  90. 'material_case_id' => $case->id,
  91. 'root_id' => $case->root_id
  92. ]);
  93. }
  94. }
  95. }