Goods.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace app\admin\model\shopro\goods;
  3. use think\Model;
  4. use traits\model\SoftDelete;
  5. use fast\Tree;
  6. use app\admin\model\shopro\activity\Activity;
  7. class Goods extends Model
  8. {
  9. use SoftDelete;
  10. // 表名
  11. protected $name = 'shopro_goods';
  12. // 自动写入时间戳字段
  13. protected $autoWriteTimestamp = 'int';
  14. // 定义时间戳字段名
  15. protected $createTime = 'createtime';
  16. protected $updateTime = 'updatetime';
  17. protected $deleteTime = 'deletetime';
  18. // 追加属性
  19. protected $append = [
  20. 'type_text',
  21. 'status_text',
  22. 'dispatch_type_text',
  23. 'activity_type_arr',
  24. 'activity_type_text_arr',
  25. 'app_type_text',
  26. 'service_ids_arr',
  27. 'dispatch_type_arr',
  28. 'dispatch_ids_arr',
  29. 'images_arr'
  30. ];
  31. public function getTypeList()
  32. {
  33. return ['normal' => __('Type normal'), 'virtual' => __('Type virtual')];
  34. }
  35. public function getStatusList()
  36. {
  37. return ['up' => __('Status up'), 'hidden' => __('Status hidden'), 'down' => __('Status down')];
  38. }
  39. public function getDispatchTypeList()
  40. {
  41. return ['express' => __('Dispatch_type express'), 'selfetch' => __('Dispatch_type selfetch'), 'store' => __('Dispatch_type store'), 'autosend' => __('Dispatch_type autosend')];
  42. }
  43. public function getTypeTextAttr($value, $data)
  44. {
  45. $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
  46. $list = $this->getTypeList();
  47. return isset($list[$value]) ? $list[$value] : '';
  48. }
  49. public function getStatusTextAttr($value, $data)
  50. {
  51. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  52. $list = $this->getStatusList();
  53. return isset($list[$value]) ? $list[$value] : '';
  54. }
  55. public function getDispatchTypeArrAttr($value, $data)
  56. {
  57. $value = isset($data['dispatch_type']) ? $data['dispatch_type'] : '';
  58. $valueArr = explode(',', $value);
  59. return $valueArr;
  60. }
  61. public function getDispatchTypeTextAttr($value, $data)
  62. {
  63. $value = $value ? $value : (isset($data['dispatch_type']) ? $data['dispatch_type'] : '');
  64. $valueArr = explode(',', $value);
  65. $list = $this->getDispatchTypeList();
  66. return implode(',', array_intersect_key($list, array_flip($valueArr)));
  67. }
  68. public function getActivityTypeArrAttr($value, $data) {
  69. $activity_types = $value ? $value : (isset($data['activity_type']) ? $data['activity_type'] : '');
  70. $activityTypes = array_values(array_filter(explode(',', $activity_types)));
  71. return $activityTypes;
  72. }
  73. public function getActivityTypeTextArrAttr($value, $data)
  74. {
  75. $activityTypes = $this->activity_type_arr;
  76. $list = \app\admin\model\shopro\activity\Activity::getTypeList();
  77. $activityTypeTextArr = [];
  78. foreach ($activityTypes as $key => $activity_type) {
  79. if (isset($list[$activity_type])) {
  80. $activityTypeTextArr[$activity_type] = $list[$activity_type];
  81. }
  82. }
  83. return $activityTypeTextArr;
  84. }
  85. public function getAppTypeTextAttr($value, $data)
  86. {
  87. return $value = (isset($data['app_type']) && $data['app_type'] == 'score') ? '积分' : '';
  88. }
  89. public function getCategoryIdsArrAttr($value, $data)
  90. {
  91. $arr = $data['category_ids'] ? explode(',', $data['category_ids']) : [];
  92. $category_ids_arr = [];
  93. if ($arr) {
  94. $tree = Tree::instance();
  95. $tree->init(collection(\app\admin\model\shopro\Category::order('weigh desc,id desc')->select())->toArray(), 'pid');
  96. foreach ($arr as $key => $id) {
  97. $category_ids_arr[] = $tree->getParentsIds($id, true);
  98. }
  99. }
  100. return $category_ids_arr;
  101. }
  102. public function getServiceIdsArrAttr($value, $data)
  103. {
  104. return (isset($data['service_ids']) && $data['service_ids']) ? array_values(array_filter(array_map("intval", explode(',', $data['service_ids'])))) : [];
  105. }
  106. public function getDispatchIdsArrAttr($value, $data)
  107. {
  108. return (isset($data['dispatch_ids']) && $data['dispatch_ids']) ? array_values(array_filter(array_map("intval", explode(',', $data['dispatch_ids'])))) : [];
  109. }
  110. /**
  111. * 编辑的时候手动调用
  112. */
  113. public function getDispatchGroupIdsArrAttr($value, $data)
  114. {
  115. $dispatch_ids_arr = $this->dispatch_ids_arr;
  116. $dispatchs = \app\admin\model\shopro\dispatch\Dispatch::where('id', 'in', $dispatch_ids_arr)->select();
  117. $group = [];
  118. foreach ($dispatchs as $key => $dispatch) {
  119. $group[$dispatch['type']] = $dispatch['id'];
  120. }
  121. return $group;
  122. }
  123. public function getImagesArrAttr($value, $data)
  124. {
  125. $imagesArray = [];
  126. if (!empty($data['images'])) {
  127. $imagesArray = explode(',', $data['images']);
  128. return $imagesArray;
  129. }
  130. return $imagesArray;
  131. }
  132. public function getParamsArrAttr($value, $data)
  133. {
  134. return (isset($data['params']) && $data['params']) ? json_decode($data['params'], true) : [];
  135. }
  136. protected function setDispatchTypeAttr($value)
  137. {
  138. return is_array($value) ? implode(',', $value) : $value;
  139. }
  140. public function scoreGoodsSkuPrice()
  141. {
  142. return $this->hasMany(\app\admin\model\shopro\app\ScoreSkuPrice::class, 'goods_id', 'id')
  143. ->where('status', 'up')->order('id', 'asc');
  144. }
  145. }