StoreCombination.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\model\activity\combination;
  12. use app\model\product\product\StoreDescription;
  13. use app\model\product\product\StoreProduct;
  14. use crmeb\traits\ModelTrait;
  15. use crmeb\basic\BaseModel;
  16. use think\Model;
  17. /**
  18. * 拼团商品Model
  19. * Class StoreCombination
  20. * @package app\model\activity\combination
  21. */
  22. class StoreCombination extends BaseModel
  23. {
  24. use ModelTrait;
  25. /**
  26. * 数据表主键
  27. * @var string
  28. */
  29. protected $pk = 'id';
  30. /**
  31. * 模型名称
  32. * @var string
  33. */
  34. protected $name = 'store_combination';
  35. /**
  36. * 配送信息
  37. * @param $value
  38. * @return false|string
  39. */
  40. protected function setDeliveryTypeAttr($value)
  41. {
  42. if ($value) {
  43. return is_array($value) ? implode(',', $value) : $value;
  44. }
  45. return '';
  46. }
  47. /**
  48. * 配送信息
  49. * @param $value
  50. * @param $data
  51. * @return mixed
  52. */
  53. protected function getDeliveryTypeAttr($value)
  54. {
  55. if ($value) {
  56. return is_string($value) ? explode(',', $value) : $value;
  57. }
  58. return [];
  59. }
  60. /**
  61. * 配送信息
  62. * @param $value
  63. * @param $data
  64. * @return mixed
  65. */
  66. protected function getCustomFormAttr($value)
  67. {
  68. if ($value) {
  69. return is_string($value) ? json_decode($value, true) : $value;
  70. }
  71. return [];
  72. }
  73. /**
  74. * @param $value
  75. * @return array|mixed
  76. */
  77. public function getStoreLabelIdAttr($value)
  78. {
  79. if ($value) {
  80. return is_string($value) ? array_map('intval', array_filter(explode(',', $value))) : $value;
  81. }
  82. return [];
  83. }
  84. /**
  85. * 商品标签
  86. * @param $value
  87. * @return array|mixed
  88. */
  89. protected function getLabelIdAttr($value)
  90. {
  91. if ($value) {
  92. return is_string($value) ? explode(',', $value) : $value;
  93. }
  94. return [];
  95. }
  96. /**
  97. * 商品保障服务
  98. * @param $value
  99. * @return array|mixed
  100. */
  101. protected function getEnsureIdAttr($value)
  102. {
  103. if ($value) {
  104. return is_string($value) ? explode(',', $value) : $value;
  105. }
  106. return [];
  107. }
  108. /**
  109. * 参数信息
  110. * @param $value
  111. * @return array|mixed
  112. */
  113. protected function getSpecsAttr($value)
  114. {
  115. if ($value) {
  116. return is_string($value) ? json_decode($value, true) : $value;
  117. }
  118. return [];
  119. }
  120. /**
  121. * 适用门店信息
  122. * @param $value
  123. * @return string
  124. */
  125. protected function setApplicableStoreIdAttr($value)
  126. {
  127. if ($value) {
  128. return is_array($value) ? implode(',', $value) : $value;
  129. }
  130. return '';
  131. }
  132. /**
  133. * 适用门店信息
  134. * @param $value
  135. * @return array|false|string[]
  136. */
  137. protected function getApplicableStoreIdAttr($value)
  138. {
  139. if ($value) {
  140. return is_string($value) ? array_map('intval', array_filter(explode(',', $value))) : $value;
  141. }
  142. return [];
  143. }
  144. /**
  145. * 一对一获取原价
  146. * @return \think\model\relation\HasOne
  147. */
  148. public function getPrice()
  149. {
  150. return $this->hasOne(StoreProduct::class, 'id', 'product_id')->field(['id', 'ot_price', 'price'])->bind(['ot_price', 'product_price' => 'price']);
  151. }
  152. /**
  153. * 一对一关联
  154. * 商品关联商品商品详情
  155. * @return \think\model\relation\HasOne
  156. */
  157. public function total()
  158. {
  159. return $this->hasOne(StoreProduct::class, 'id', 'product_id')->where('is_show', 1)->where('is_del', 0)->field(['SUM(sales+ficti) as total', 'id', 'price'])->bind([
  160. 'total' => 'total', 'product_price' => 'price'
  161. ]);
  162. }
  163. /**
  164. * 一对一关联
  165. * 商品关联商品商品详情
  166. * @return \think\model\relation\HasOne
  167. */
  168. public function descriptions()
  169. {
  170. return $this->hasOne(StoreDescription::class, 'product_id', 'id')->where('type', 3)->bind(['description']);
  171. }
  172. /**
  173. * 添加时间获取器
  174. * @param $value
  175. * @return false|string
  176. */
  177. protected function getAddTimeAttr($value)
  178. {
  179. if ($value) return date('Y-m-d H:i:s', (int)$value);
  180. return '';
  181. }
  182. /**
  183. * 轮播图获取器
  184. * @param $value
  185. * @return mixed
  186. */
  187. public function getImagesAttr($value)
  188. {
  189. return json_decode($value, true) ?? [];
  190. }
  191. /**
  192. * 拼团商品名称搜索器
  193. * @param Model $query
  194. * @param $value
  195. * @param $data
  196. */
  197. public function searchStoreNameAttr($query, $value, $data)
  198. {
  199. if ($value) $query->where('title|id', 'like', '%' . $value . '%');
  200. }
  201. /**
  202. * 是否推荐搜索器
  203. * @param Model $query
  204. * @param $value
  205. * @param $data
  206. */
  207. public function searchIsHostAttr($query, $value, $data)
  208. {
  209. $query->where('is_host', $value ?? 1);
  210. }
  211. /**
  212. * 状态搜索器
  213. * @param Model $query
  214. * @param $value
  215. * @param $data
  216. */
  217. public function searchIsShowAttr($query, $value, $data)
  218. {
  219. if ($value != '') $query->where('is_show', $value ?: 0);
  220. }
  221. /**
  222. * 是否删除搜索器
  223. * @param Model $query
  224. * @param $value
  225. * @param $data
  226. */
  227. public function searchIsDelAttr($query, $value, $data)
  228. {
  229. $query->where('is_del', $value ?? 0);
  230. }
  231. /**
  232. * 商品ID搜索器
  233. * @param Model $query
  234. * @param $value
  235. * @param $data
  236. */
  237. public function searchProductIdAttr($query, $value, $data)
  238. {
  239. if ($value) {
  240. if (is_array($value)) {
  241. $query->whereIn('product_id', $value);
  242. } else {
  243. $query->where('product_id', $value);
  244. }
  245. }
  246. }
  247. /**
  248. * 系统表单搜索器
  249. * @param Model $query
  250. * @param $value
  251. */
  252. public function searchSystemFormIdAttr($query, $value)
  253. {
  254. if (is_array($value)) {
  255. if ($value) $query->whereIn('system_form_id', $value);
  256. } else {
  257. if ($value !== '') $query->where('system_form_id', $value);
  258. }
  259. }
  260. /**
  261. * 适用门店类型搜索器
  262. * @param Model $query
  263. * @param $value
  264. */
  265. public function searchApplicableTypeAttr($query, $value)
  266. {
  267. if (is_array($value)) {
  268. if ($value) $query->whereIn('applicable_type', $value);
  269. } else {
  270. if ($value !== '') $query->where('applicable_type', $value);
  271. }
  272. }
  273. }