RuleAddedController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\Goods;
  8. use app\model\GoodsSku;
  9. use app\model\RuleAdded;
  10. use app\model\RuleAddedComponent;
  11. use app\model\RulePricing;
  12. use app\model\SysSerial;
  13. use support\exception\BusinessException;
  14. use support\Request;
  15. use support\Response;
  16. class RuleAddedController extends Curd
  17. {
  18. public function __construct()
  19. {
  20. $this->model = new RuleAdded();
  21. $this->validate = true;
  22. $this->validateClass = new RuleAddedValidate();
  23. }
  24. public function select(Request $request): Response
  25. {
  26. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  27. $order = $request->get('order', 'desc');
  28. $field = $field ?? 'rule_added_addtimes';
  29. $query = $this->doSelect($where, $field, $order);
  30. return $this->doFormat($query, $format, $limit);
  31. }
  32. public function selectList(Request $request)
  33. {
  34. $data = $this->model->where('rule_added_status', 'ACTIVED')
  35. ->select('rule_added_id', 'rule_added_name')
  36. ->get()
  37. ->toArray();
  38. return json_success('', $data);
  39. }
  40. public function info(Request $request): Response
  41. {
  42. $id = $request->get('rule_added_id');
  43. $added = RuleAdded::find($id);
  44. if (!$added) {
  45. return json_fail('数据不存在');
  46. }
  47. $components = RuleAddedComponent::where('join_component_rule_added_id', $id)
  48. ->get()
  49. ->toArray();
  50. foreach ($components as &$component) {
  51. $goods = [];
  52. if (!empty($component['rule_added_component_goods_json'])) {
  53. $goodsJson = json_decode($component['rule_added_component_goods_json'], true);
  54. foreach ($goodsJson as $item) {
  55. $goodsInfo = [];
  56. if (isset($item['goods_id'])) {
  57. $goodsInfo['goods_name'] = $item['goods_name'];
  58. // $goods = Goods::where('goods_id',$item['goods_id'])->select('goods_name');
  59. $sku = GoodsSku::where('goods_sku_id', $item['goods_sku_id'])->where('goods_sku_status','ON')->select('goods_sku_specs_json')->first();
  60. if (!empty($sku->goods_sku_specs_json)) {
  61. $specsJson = json_decode($sku->goods_sku_specs_json, true);
  62. $skuTitle = '';
  63. foreach ($specsJson as $item) {
  64. if (is_array($item)) {
  65. $item = implode(',', $item);
  66. }
  67. $skuTitle .= $item . '-';
  68. }
  69. $goodsInfo['sku_title'] = rtrim($skuTitle, '-');
  70. }
  71. }
  72. if (isset($item['coupon_id'])){
  73. $coupon = Coupon::where('coupon_id',$item['coupon_id'])->select('coupon_name')->first();
  74. $goods[] = ['goods_name'=>$coupon->coupon_name];
  75. }
  76. if (!empty($goodsInfo)) {
  77. $goods[] = $goodsInfo;
  78. }
  79. }
  80. }
  81. $component['goods'] = $goods;
  82. $component['rule_added_component_goods_nbr'] = intval($component['rule_added_component_goods_nbr']);
  83. }
  84. return json_success('', $components);
  85. }
  86. protected function insertInput(Request $request): array
  87. {
  88. $data = $this->inputFilter($request->post());
  89. $data['rule_added_id'] = "RA" . str_pad(SysSerial::getSerial(), 16, '0', STR_PAD_LEFT) . random_string(6, 'up');
  90. return $data;
  91. }
  92. protected function updateInput(Request $request): array
  93. {
  94. $primary_key = $this->model->getKeyName();
  95. $id = $request->post($primary_key);
  96. $data = $this->inputFilter($request->post());
  97. // if (!empty($data['rule_pricing_goods_json'])) {
  98. // $data['rule_pricing_goods_json'] = json_encode(explode(',', $data['rule_pricing_goods_json']));
  99. // } else {
  100. // $data['rule_pricing_goods_json'] = '[]';
  101. // }
  102. $model = $this->model->find($id);
  103. if (!$model) {
  104. throw new BusinessException('记录不存在', 2);
  105. }
  106. unset($data[$primary_key]);
  107. return [$id, $data];
  108. }
  109. }