GoodsLogic.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * GoodsLogic.php UTF-8
  4. * 商品逻辑处理
  5. *
  6. * @date : 2018/5/7 22:49
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\logic\shop;
  13. use huo\model\common\CommonModel;
  14. use huo\model\shop\GoodsModel;
  15. use huolib\status\ShopStatus;
  16. class GoodsLogic extends CommonModel {
  17. protected $base_field
  18. = [
  19. 'id' => 'goods_id',
  20. 'goods_name' => 'goods_name',
  21. 'is_real' => 'is_real',
  22. 'store_cnt' => 'total_cnt',
  23. 'remain_cnt' => 'remain_cnt',
  24. 'mem_times' => 'mem_times',
  25. 'market_price' => 'market_price',
  26. 'goods_intro' => 'goods_intro',
  27. 'initial' => 'initial',
  28. 'original_img' => 'original_img',
  29. 'integral' => 'integral',
  30. 'gain_integral' => 'gain_integral',
  31. 'object_type' => 'object_name',
  32. 'object_id' => 'object_id',
  33. 'flag' => 'flag',
  34. ];
  35. /**
  36. * 获取商品列表
  37. *
  38. * @param array $where
  39. * @param string $page
  40. * @param string $order
  41. *
  42. * @param array $field
  43. * @param bool $_base
  44. *
  45. * @return int| array
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\ModelNotFoundException
  48. * @throws \think\exception\DbException
  49. */
  50. public function getList($where = [], $page = '1,10', $order = '', $field = [], $_base = true) {
  51. $_goods_model = new GoodsModel();
  52. $_count = $_goods_model->useGlobalScope($_base)->where($where)->count();
  53. if (empty($_count)) {
  54. return ShopStatus::GOODS_CNT_ZERO;
  55. }
  56. $_field = $this->base_field;
  57. $_field = array_merge($_field, $field);
  58. $_order = $this->orderFilter($order);
  59. $_goodss = $_goods_model
  60. ->useGlobalScope($_base)
  61. ->field($_field)
  62. ->where($where)
  63. ->order($_order)
  64. ->page($page)
  65. ->select();
  66. if (is_object($_goodss)) {
  67. $_goodss = $_goodss->toArray();
  68. }
  69. if (empty($_goodss)) {
  70. $_list = null;
  71. $_rdata['count'] = $_count;
  72. $_rdata['list'] = $_list;
  73. return $_rdata;
  74. }
  75. $_list = [];
  76. foreach ($_goodss as $_k => $_v) {
  77. $_data = [];
  78. $_data['goods_id'] = $_v['goods_id'];
  79. $_data['goods_name'] = $_v['goods_name'];
  80. $_data['is_real'] = $_v['is_real'];
  81. $_data['total_cnt'] = $_v['total_cnt'];
  82. $_data['remain_cnt'] = $_v['remain_cnt'];
  83. $_data['market_price'] = $_v['market_price'];
  84. $_data['goods_intro'] = $_v['goods_intro'];
  85. $_data['initial'] = $_v['initial'];
  86. $_data['original_img'] = $_v['original_img'];
  87. $_data['integral'] = $_v['integral'];
  88. $_data['gain_integral'] = $_v['gain_integral'];
  89. $_data['object_name'] = $_v['object_name'];
  90. $_data['object_id'] = $_v['object_id'];
  91. $_data['flag'] = $_v['flag'];
  92. if (isset($_v['is_on_sale'])) {
  93. $_data['is_on_sale'] = $_v['is_on_sale'];
  94. }
  95. if (isset($_v['on_time'])) {
  96. $_data['on_time'] = $_v['on_time'];
  97. }
  98. if (isset($_v['update_time'])) {
  99. $_data['update_time'] = $_v['update_time'];
  100. }
  101. if (isset($_v['list_order'])) {
  102. $_data['list_order'] = $_v['list_order'];
  103. }
  104. $_list[] = $_data;
  105. }
  106. $_rdata['count'] = $_count;
  107. $_rdata['list'] = $_list;
  108. return $_rdata;
  109. }
  110. /**
  111. * 通过商品ID 查询商品信息
  112. *
  113. * @param $goods_id
  114. *
  115. * @return array|bool|false
  116. */
  117. public function getInfoByGoodsId($goods_id) {
  118. if (empty($goods_id)) {
  119. return false;
  120. }
  121. $_map['id'] = $goods_id;
  122. $_field = $this->base_field;
  123. $_field['goods_content'] = 'goods_content';
  124. $_goods_data = (new GoodsModel())->useGlobalScope(false)->field($_field)->where($_map)->find();
  125. if (is_object($_goods_data)) {
  126. $_goods_data = $_goods_data->toArray();
  127. }
  128. return $_goods_data;
  129. }
  130. }