StoreProductRelation.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\wap\model\store;
  12. use basic\ModelBasic;
  13. use behavior\wap\StoreProductBehavior;
  14. use service\HookService;
  15. use traits\ModelTrait;
  16. /**商品点赞和收藏表
  17. * Class StoreProductRelation
  18. * @package app\wap\model\store
  19. */
  20. class StoreProductRelation extends ModelBasic
  21. {
  22. use ModelTrait;
  23. protected $insert = ['add_time'];
  24. protected function setAddTimeAttr($value)
  25. {
  26. return time();
  27. }
  28. public static function productRelation($productId, $uid, $relationType, $category = 'product')
  29. {
  30. if (!$productId) return self::setErrorInfo('产品不存在!');
  31. $relationType = strtolower($relationType);
  32. $category = strtolower($category);
  33. $data = ['uid' => $uid, 'product_id' => $productId, 'type' => $relationType, 'category' => $category];
  34. if (self::be($data)) return true;
  35. $data = self::set($data);
  36. HookService::afterListen('store_' . $category . '_' . $relationType, $productId, $uid, false, StoreProductBehavior::class);
  37. return $data;
  38. }
  39. public static function unProductRelation($productId, $uid, $relationType, $category = 'product')
  40. {
  41. if (!$productId) return self::setErrorInfo('产品不存在!');
  42. $relationType = strtolower($relationType);
  43. $category = strtolower($category);
  44. self::where(['uid' => $uid, 'product_id' => $productId, 'type' => $relationType, 'category' => $category])->delete();
  45. HookService::afterListen('store_' . $category . '_un_' . $relationType, $productId, $uid, false, StoreProductBehavior::class);
  46. return true;
  47. }
  48. public static function productRelationNum($productId, $relationType, $category = 'product')
  49. {
  50. $relationType = strtolower($relationType);
  51. $category = strtolower($category);
  52. return self::where('type', $relationType)->where('product_id', $productId)->where('category', $category)->count();
  53. }
  54. public static function isProductRelation($product_id, $uid, $relationType, $category = 'product')
  55. {
  56. $type = strtolower($relationType);
  57. $category = strtolower($category);
  58. return self::be(compact('product_id', 'uid', 'type', 'category'));
  59. }
  60. public static function collection_list($uid, $page, $limit)
  61. {
  62. $list = self::where(['a.uid' => $uid, 'a.category' => 'integral', 'a.type' => 'collect'])->alias('a')
  63. ->join('integral_product i', 'a.product_id=i.id', 'left')->field(['i.image', 'i.integral', 'i.integral_name', 'i.id'])
  64. ->page((int)$page, (int)$limit)->select();
  65. $page++;
  66. return compact('list', 'page');
  67. }
  68. }