RuleAddedComponentController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace app\admin\controller\member;
  3. use app\admin\validate\member\RuleAddedValidate;
  4. use app\admin\validate\member\RulePricingValidate;
  5. use app\controller\Curd;
  6. use app\model\Coupon;
  7. use app\model\GoodsSku;
  8. use app\model\RuleAdded;
  9. use app\model\RuleAddedComponent;
  10. use app\model\RulePricing;
  11. use app\model\SysSerial;
  12. use support\exception\BusinessException;
  13. use support\Request;
  14. use support\Response;
  15. class RuleAddedComponentController extends Curd
  16. {
  17. public function __construct()
  18. {
  19. $this->model = new RuleAddedComponent();
  20. // $this->validate = true;
  21. // $this->validateClass = new RuleAddedValidate();
  22. }
  23. public function select(Request $request): Response
  24. {
  25. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  26. $order = $request->get('order', 'desc');
  27. $field = $field ?? 'rule_added_component_addtimes';
  28. $query = $this->doSelect($where, $field, $order);
  29. return $this->doFormat($query, $format, $limit);
  30. }
  31. protected function afterQuery($items)
  32. {
  33. foreach ($items as &$item) {
  34. $goods = [];
  35. $goodsIds = [];
  36. if (!empty($item->rule_added_component_goods_json)) {
  37. $goodsJson = json_decode($item->rule_added_component_goods_json, true);
  38. foreach ($goodsJson as $key => $item2) {
  39. $goodsInfo = [];
  40. if (isset($item2['goods_id'])) {
  41. $goodsInfo['goods_name'] = $item2['goods_name'];
  42. $sku = GoodsSku::where('goods_sku_id', $item2['goods_sku_id'])->where('goods_sku_status','ON')->select('goods_sku_id', 'goods_sku_sales_price', 'goods_sku_specs_json')->first();
  43. if (!empty($sku->goods_sku_specs_json)) {
  44. $specsJson = json_decode($sku->goods_sku_specs_json, true);
  45. $skuTitle = '';
  46. foreach ($specsJson as $item3) {
  47. if (is_array($item3)) {
  48. $item3 = implode(',', $item3);
  49. }
  50. $skuTitle .= $item3 . '-';
  51. }
  52. $goodsInfo['sku_title'] = rtrim($skuTitle, '-');
  53. $goodsJson[$key]['sku'][] = [
  54. 'goods_sku_id' => strval($sku->goods_sku_id),
  55. 'goods_sku_sales_price' => $sku->goods_sku_sales_price,
  56. 'goods_sku_title' => $goodsInfo['sku_title']
  57. ];
  58. }
  59. }
  60. if (isset($item2['coupon_id'])) {
  61. $coupon = Coupon::where('coupon_id', $item2['coupon_id'])->select('coupon_name')->first();
  62. $goods[] = ['goods_name' => $coupon->coupon_name];
  63. }
  64. if (!empty($goodsInfo)) {
  65. $goods[] = $goodsInfo;
  66. }
  67. if (isset($item2['goods_id'])) {
  68. $goodsIds[] = $item2['goods_id'];
  69. } elseif (isset($item2['coupon_id'])) {
  70. $goodsIds[] = $item2['coupon_id'];
  71. }
  72. }
  73. $item->rule_added_component_goods_json = $goodsJson;
  74. }
  75. // $item->rule_added_component_json = [];
  76. if (!empty($item->rule_added_component_json)){
  77. $item->rule_added_component_json = json_decode($item->rule_added_component_json);
  78. }else{
  79. $item->rule_added_component_json = [];
  80. }
  81. $item->rule_added_component_goods_nbr = intval($item->rule_added_component_goods_nbr);
  82. $item->goodsIds = $goodsIds;
  83. $item->goods = $goods;
  84. }
  85. return $items;
  86. }
  87. // protected function updateInput(Request $request): array
  88. // {
  89. // $primary_key = $this->model->getKeyName();
  90. // $id = $request->post($primary_key);
  91. // $data = $this->inputFilter($request->post());
  92. // if (!empty($data['rule_pricing_goods_json'])) {
  93. // $data['rule_pricing_goods_json'] = json_encode(explode(',', $data['rule_pricing_goods_json']));
  94. // } else {
  95. // $data['rule_pricing_goods_json'] = '[]';
  96. // }
  97. // $model = $this->model->find($id);
  98. // if (!$model) {
  99. // throw new BusinessException('记录不存在', 2);
  100. // }
  101. // unset($data[$primary_key]);
  102. // return [$id, $data];
  103. // }
  104. }