GoodsActivityLogic.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\logic;
  20. use app\common\enum\ActivityEnum;
  21. use app\common\model\GoodsActivity;
  22. /**
  23. * 商品活动信息
  24. */
  25. class GoodsActivityLogic extends BaseLogic
  26. {
  27. /**
  28. * @notes 获取商品活动信息
  29. * @param $goodsIds
  30. * @param $type 活动类型
  31. * @return array
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\DbException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. * @author Tab
  36. * @date 2021/10/11 18:16
  37. */
  38. public static function activityInfo($goodsIds = [],$type = 0)
  39. {
  40. // 转数组
  41. if ($goodsIds && !is_array($goodsIds)) {
  42. $goodsIds = [$goodsIds];
  43. }
  44. $where = [];
  45. if($goodsIds){
  46. $where[] = ['goods_id','in',$goodsIds];
  47. }
  48. if($type){
  49. $where[] = ['activity_type','=',$type];
  50. }
  51. // 获取活动信息
  52. $lists = GoodsActivity::where($where)->select()->toArray();
  53. if (empty($lists)) {
  54. return [];
  55. }
  56. // 提取有参与活动的商品ids
  57. $goodsIds = array_column($lists, 'goods_id');
  58. $goodsIds = array_unique($goodsIds);
  59. // 生成初始化数据
  60. $data = [];
  61. foreach ($goodsIds as $goodsId) {
  62. $data[$goodsId] = [];
  63. foreach (ActivityEnum::TYPE as $type) {
  64. $data[$goodsId][$type]['type'] = $type;
  65. $data[$goodsId][$type]['activity_id'] = null;
  66. $data[$goodsId][$type]['item_id'] = [];
  67. }
  68. }
  69. // 填充活动信息
  70. foreach ($lists as $item) {
  71. $data[$item['goods_id']][$item['activity_type']]['activity_id'] = $item['activity_id'];
  72. $data[$item['goods_id']][$item['activity_type']]['item_id'][] = $item['item_id'];
  73. }
  74. // 去除没有活动信息的初始数据
  75. foreach($data as $key => $item) {
  76. foreach (ActivityEnum::TYPE as $type) {
  77. if (is_null($item[$type]['activity_id'])) {
  78. unset($data[$key][$type]);
  79. }
  80. }
  81. if (empty($data[$key])) {
  82. unset($data[$key]);
  83. }
  84. }
  85. return $data;
  86. }
  87. static function goodsActivityInfo($goods_id) : array
  88. {
  89. $info = GoodsActivity::where('goods_id', $goods_id)->append([ 'activity_text' ])->findOrEmpty()->toArray();
  90. return $info;
  91. }
  92. }