Goods.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\common\model;
  20. use app\common\logic\GoodsActivityLogic;
  21. use app\common\model\search\SearchGoods;
  22. use app\common\service\FileService;
  23. use think\model\concern\SoftDelete;
  24. use app\common\enum\{DistributionOrderGoodsEnum, GoodsCommentEnum, GoodsEnum, YesNoEnum};
  25. /**
  26. * 商品模型
  27. * Class Goods
  28. * @package app\common\model
  29. */
  30. class Goods extends BaseModel
  31. {
  32. use SoftDelete;
  33. use SearchGoods;
  34. protected $deleteTime = 'delete_time';
  35. public function unit()
  36. {
  37. return $this->hasOne(GoodsUnit::class, 'id', 'unit_id');
  38. }
  39. /**
  40. * @notes 去除分享海报域名
  41. * @param $value
  42. * @return mixed|string
  43. * @author 张无忌
  44. * @date 2021/9/10 11:04
  45. */
  46. public function setPosterAttr($value)
  47. {
  48. return trim($value) ? FileService::setFileUrl($value) : '';
  49. }
  50. /**
  51. * @notes 去掉视频域名
  52. * @param $value
  53. * @return mixed|string
  54. * @author 张无忌
  55. * @date 2021/9/10 11:04
  56. */
  57. public function setVideoAttr($value)
  58. {
  59. return trim($value) ? FileService::setFileUrl($value) : '';
  60. }
  61. /**
  62. * @notes 获取视频域名
  63. * @param $value
  64. * @return mixed|string
  65. * @author cjhao
  66. * @date 2022/3/28 14:35
  67. */
  68. public function getVideoAttr($value)
  69. {
  70. return trim($value) ? FileService::getFileUrl($value) : '';
  71. }
  72. /**
  73. * @notes 去掉视频封面域名
  74. * @param $value
  75. * @return mixed|string
  76. * @author 张无忌
  77. * @date 2021/9/10 11:04
  78. */
  79. public function setVideoCoverAttr($value)
  80. {
  81. return trim($value) ? FileService::setFileUrl($value) : '';
  82. }
  83. /**
  84. * @notes 获取视频封面域名
  85. * @param $value
  86. * @return string
  87. * @author cjhao
  88. * @date 2022/3/28 14:38
  89. */
  90. public function getVideoCoverAttr($value)
  91. {
  92. return trim($value) ? FileService::getFileUrl($value) : '';
  93. }
  94. /**
  95. * @notes 获取海报图片
  96. * @param $value
  97. * @param $data
  98. * @return string
  99. * @author cjhao
  100. * @date 2022/3/28 14:27
  101. */
  102. public function getPosterAttr($value, $data)
  103. {
  104. return trim($value) ? FileService::getFileUrl($value) : '';
  105. }
  106. /**
  107. * @notes 关联轮播图模型
  108. * @return \think\model\relation\HasMany
  109. * @author cjhao
  110. * @date 2021/8/16 17:59
  111. */
  112. public function imageList()
  113. {
  114. return $this->hasMany(GoodsImage::class, 'goods_id');
  115. }
  116. /**
  117. * @notes 关联商品分类模型
  118. * @return \think\model\relation\HasMany
  119. * @author cjhao
  120. * @date 2021/8/16 17:59
  121. */
  122. public function goodsCategoryIndex()
  123. {
  124. return $this->hasMany(GoodsCategoryIndex::class, 'goods_id');
  125. }
  126. /**
  127. * @notes 关联商品分类模型 使用as goods_id的情况
  128. * @return \think\model\relation\HasMany
  129. * @author lbzy
  130. * @datetime 2023-11-06 15:44:37
  131. */
  132. public function goodsCategoryIndex2()
  133. {
  134. return $this->hasMany(GoodsCategoryIndex::class, 'goods_id', 'goods_id');
  135. }
  136. function deliveryTemplate()
  137. {
  138. return $this->hasOne(GoodsDeliveryTemplate::class, 'id', 'delivery_template_id')
  139. ->field([ 'id', 'name', 'type', 'content', 'content1' ]);
  140. }
  141. /**
  142. * @notes 关联规格项模型
  143. * @return \think\model\relation\HasMany
  144. * @author cjhao
  145. * @date 2021/8/16 18:00
  146. */
  147. public function specValue()
  148. {
  149. return $this->hasMany(GoodsSpec::class, 'goods_id');
  150. }
  151. /**
  152. * @notes 关联规格值模型
  153. * @return \think\model\relation\HasMany
  154. * @author cjhao
  155. * @date 2021/8/16 18:00
  156. */
  157. public function specValueSpec()
  158. {
  159. return $this->hasMany(GoodsSpecValue::class, 'goods_id');
  160. }
  161. /**
  162. * @notes 关联规格信息模型
  163. * @return \think\model\relation\HasMany
  164. * @author cjhao
  165. * @date 2021/8/16 18:00
  166. */
  167. public function specValueList()
  168. {
  169. return $this->hasMany(GoodsItem::class, 'goods_id');
  170. }
  171. /**
  172. * @notes 关联评论模型
  173. * @return \think\model\relation\HasMany
  174. * @author cjhao
  175. * @date 2021/8/16 18:00
  176. */
  177. public function goodsComment()
  178. {
  179. return $this->hasMany(GoodsComment::class, 'goods_id');
  180. }
  181. /**
  182. * @notes 处理商品图片
  183. * @param $value
  184. * @param $data
  185. * @return array
  186. * @author cjhao
  187. * @date 2021/8/16 18:01
  188. */
  189. public function getGoodsImageAttr($value, $data)
  190. {
  191. $goodsImage = array_column($this->imageList->toArray(), 'uri');
  192. array_unshift($goodsImage, FileService::getFileUrl($data['image']));
  193. return $goodsImage;
  194. }
  195. /**
  196. * @notes 处理分类
  197. * @param $value
  198. * @param $data
  199. * @return array
  200. * @author cjhao
  201. * @date 2021/8/16 18:01
  202. */
  203. public function getCategoryIdAttr($value, $data)
  204. {
  205. $goods_category_index = $this->goodsCategoryIndex->toArray();
  206. return array_column($goods_category_index, 'category_id');
  207. }
  208. /**
  209. * @notes 分销状态获取器
  210. * @param $value
  211. * @return int
  212. * @author Tab
  213. * @date 2021/7/23 16:23
  214. */
  215. public function getIsDistributionAttr($value,$data)
  216. {
  217. $distributionGoods = DistributionGoods::where('goods_id', $data['id'])->findOrEmpty();
  218. if ($distributionGoods->isEmpty()) {
  219. return YesNoEnum::NO;
  220. }
  221. return $distributionGoods->is_distribution;
  222. }
  223. /**
  224. * @notes 会员折扣状态获取器
  225. * @param $value
  226. * @return int
  227. * @author ljj
  228. * @date 2023/3/28 3:59 下午
  229. */
  230. public function getIsDiscountAttr($value,$data)
  231. {
  232. $discountGoods = DiscountGoods::where('goods_id', $data['id'])->findOrEmpty();
  233. if ($discountGoods->isEmpty()) {
  234. return YesNoEnum::NO;
  235. }
  236. return $discountGoods->is_discount;
  237. }
  238. /**
  239. * @notes 该商品累计已返佣金
  240. * @param $value
  241. * @return float
  242. * @author Tab
  243. * @date 2021/7/23 14:17
  244. */
  245. public function getCommissionAttr($value)
  246. {
  247. $where = [
  248. 'status' => DistributionOrderGoodsEnum::RETURNED,
  249. 'goods_id' => $value
  250. ];
  251. return DistributionOrderGoods::where($where)->sum('earnings');
  252. }
  253. /**
  254. * @notes 最小值获取器
  255. * @param $value
  256. * @return int|mixed|string
  257. * @author Tab
  258. * @date 2021/9/18 11:09
  259. */
  260. public function getMinPriceAttr($value)
  261. {
  262. return clearZero($value);
  263. }
  264. /**
  265. * @notes 最大值获取器
  266. * @param $value
  267. * @return int|mixed|string
  268. * @author Tab
  269. * @date 2021/9/18 11:10
  270. */
  271. public function getMaxPriceAttr($value)
  272. {
  273. return clearZero($value);
  274. }
  275. /**
  276. * @notes 价格获取器
  277. * @param $value
  278. * @param $data
  279. * @author Tab
  280. * @datetime 2022/1/17 17:14
  281. */
  282. public function getPriceTextAttr($value, $data)
  283. {
  284. if ($data['spec_type'] == 1) {
  285. // 单规格
  286. return '¥ ' . clear_zero($data['min_price']);
  287. } else {
  288. // 多规格
  289. return '¥ ' . clear_zero($data['min_price']) . ' ~ ' . clear_zero($data['max_price']);
  290. }
  291. }
  292. /**
  293. * @notes 状态获取器
  294. * @param $value
  295. * @param $data
  296. * @return array|mixed|string
  297. * @author Tab
  298. * @datetime 2022/1/17 17:24
  299. */
  300. public function getStatusTextAttr($value, $data)
  301. {
  302. return GoodsEnum::getStatusDesc($data['status']);
  303. }
  304. /**
  305. * @notes 分类获取器
  306. * @param $value
  307. * @param $data
  308. * @author Tab
  309. * @datetime 2022/1/17 17:25
  310. */
  311. public function getCategoryTextAttr($value, $data)
  312. {
  313. $field = ['gci.goods_id, gci.category_id, gc.name'];
  314. $lists = GoodsCategoryIndex::alias('gci')
  315. ->leftJoin('goods_category gc', 'gc.id = gci.category_id')
  316. ->field($field)
  317. ->where('gci.goods_id', $data['id'])
  318. ->select()
  319. ->toArray();
  320. $categoryText = '';
  321. foreach ($lists as $item) {
  322. $categoryText .= $item['name'] . '/';
  323. }
  324. return trim($categoryText, '/');
  325. }
  326. /**
  327. * @notes 评论条数获取器(审核通过的)
  328. * @param $value
  329. * @param $data
  330. * @author Tab
  331. * @datetime 2022/1/17 17:32
  332. */
  333. public function getCommentTextAttr($value, $data)
  334. {
  335. $count = GoodsComment::where([
  336. 'goods_id' => $data['id'],
  337. 'status' => GoodsCommentEnum::APPROVED
  338. ])->count();
  339. return $count;
  340. }
  341. /**
  342. * @notes service_guarantee_ids 服务保障id列表
  343. * @return array
  344. * @author lbzy
  345. * @datetime 2023-08-22 17:13:35
  346. */
  347. function getServiceGuaranteeIdsAttr($fieldValue, $data)
  348. {
  349. return $fieldValue ? (array) json_decode($fieldValue, true) : [];
  350. }
  351. /**
  352. * @notes service_guarantee_ids 服务保障id列表
  353. * @return string
  354. * @author lbzy
  355. * @datetime 2023-08-22 17:13:35
  356. */
  357. function setServiceGuaranteeIdsAttr($fieldValue, $data)
  358. {
  359. return json_encode(is_array($fieldValue) ? array_values($fieldValue) : [], JSON_UNESCAPED_UNICODE);
  360. }
  361. }