Product.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace app\api\model\plus\points;
  3. use app\common\exception\BaseException;
  4. use app\common\model\plus\point\Product as PointProductModel;
  5. use app\api\model\product\Product as ProductModel;
  6. /**
  7. * 积分商城模型
  8. */
  9. class Product extends PointProductModel
  10. {
  11. /*
  12. * 获取列表
  13. */
  14. public function getList($params)
  15. {
  16. // 获取列表数据
  17. $list = $this->with(['product.image.file','sku'])
  18. ->where('is_delete','=', 0)
  19. ->order(['sort' => 'asc', 'create_time' => 'asc'])
  20. ->paginate($params);
  21. foreach ($list as $key => $val) {
  22. $list[$key]['product_image'] = $val['product']['image'][0]['file_path'];
  23. }
  24. return $list;
  25. }
  26. /**
  27. * 获取积分任务的商品列表(用于订单结算)
  28. */
  29. public static function getPointsProduct($params)
  30. {
  31. // 积分商品详情
  32. $points = self::detail($params['point_product_id'],['sku']);
  33. if (empty($points) || $points['status'] == 20 || $points['is_delete'] == 1) {
  34. throw new BaseException(['msg' => '积分兑换商品不存在或已结束']);
  35. }
  36. // 积分商品详情
  37. $product = ProductModel::detail($points['product_id']);
  38. // 积分商品sku信息
  39. $point_sku = null;
  40. if($product['spec_type'] == 10){
  41. $point_sku = $points['sku'][0];
  42. }else{
  43. //多规格
  44. foreach ($points['sku'] as $sku){
  45. if($sku['point_product_sku_id'] == $params['point_product_sku_id']){
  46. $point_sku = $sku;
  47. break;
  48. }
  49. }
  50. }
  51. if ($point_sku == null) {
  52. throw new BaseException(['msg' => '积分兑换商品规格不存在']);
  53. }
  54. // 商品sku信息
  55. $product['product_sku'] = ProductModel::getProductSku($product, $params['product_sku_id']);
  56. $product['point_sku'] = $point_sku;
  57. // 商品列表
  58. $productList = [$product->hidden(['category', 'content', 'image', 'sku'])];
  59. // 只会有一个商品
  60. foreach ($productList as &$item) {
  61. // 商品单价
  62. $item['product_price'] = $point_sku['point_money'];
  63. // 商品购买数量
  64. $item['total_num'] = $params['product_num'];
  65. $item['spec_sku_id'] = $item['product_sku']['spec_sku_id'];
  66. // 商品购买总金额
  67. $item['total_price'] = $point_sku['point_money'] * $item['total_num'];
  68. $item['points_num'] = $point_sku['point_num'] * $item['total_num'];
  69. $item['point_product_sku_id'] = $point_sku['point_product_sku_id'];
  70. $item['product_sku_id'] = $params['product_sku_id'];
  71. $item['product_source_id'] = $point_sku['point_product_id'];
  72. $item['sku_source_id'] = $point_sku['point_product_sku_id'];
  73. // 积分商品最大兑换数
  74. $item['point_product'] = [
  75. 'limit_num' => $points['limit_num']
  76. ];
  77. // 积分抵扣金额
  78. $item['points_money'] = ($item['product_sku']['product_price'] - $point_sku['point_money']) * $params['product_num'];
  79. }
  80. return $productList;
  81. }
  82. /**
  83. * 积分商品详情
  84. */
  85. public function getPointDetail($point_product_id)
  86. {
  87. $result = self::detail($point_product_id, ['product.image.file','sku.productSku.image']);
  88. if (!empty($result)) {
  89. $point_arr = array_column($result->toArray()['sku'], 'assemble_price');
  90. $product_arr = array_column($result->toArray()['sku'], 'product_price');
  91. sort($point_arr);
  92. sort($product_arr);
  93. $result['point_price'] = current($point_arr);
  94. $result['line_price'] = current($product_arr);
  95. if (count($point_arr) > 1) {
  96. $res['point_high_price'] = end($point_arr);
  97. $res['line_high_price'] = end($product_arr);
  98. }
  99. }
  100. return $result;
  101. }
  102. }