StoreBargain.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018/4/16 0016
  6. * Time: 10:39
  7. */
  8. namespace app\admin\controller\ump;
  9. use app\admin\controller\AuthController;
  10. use app\admin\model\store\{StoreCategory,
  11. StoreDescription,
  12. StoreProductAttr,
  13. StoreProductAttrResult,
  14. StoreProduct as ProductModel,
  15. StoreProductAttrValue
  16. };
  17. use crmeb\traits\CurdControllerTrait;
  18. use think\facade\Route as Url;
  19. use app\admin\model\system\{SystemAttachment, ShippingTemplates};
  20. use app\admin\model\ump\StoreBargain as StoreBargainModel;
  21. use crmeb\services\{UtilService as Util, FormBuilder as Form, JsonService as Json};
  22. //砍价
  23. class StoreBargain extends AuthController
  24. {
  25. use CurdControllerTrait;
  26. protected $bindModel = StoreBargainModel::class;
  27. /**
  28. * 显示资源列表
  29. *
  30. * @return \think\Response
  31. */
  32. public function index()
  33. {
  34. $where = Util::getMore([
  35. ['status', ''],
  36. ['store_name', ''],
  37. ['export', 0],
  38. ['data', ''],
  39. ], $this->request);
  40. $limitTimeList = [
  41. 'today' => implode(' - ', [date('Y/m/d'), date('Y/m/d', strtotime('+1 day'))]),
  42. 'week' => implode(' - ', [
  43. date('Y/m/d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600)),
  44. date('Y/m/d', (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600))
  45. ]),
  46. 'month' => implode(' - ', [date('Y/m') . '/01', date('Y/m') . '/' . date('t')]),
  47. 'quarter' => implode(' - ', [
  48. date('Y') . '/' . (ceil((date('n')) / 3) * 3 - 3 + 1) . '/01',
  49. date('Y') . '/' . (ceil((date('n')) / 3) * 3) . '/' . date('t', mktime(0, 0, 0, (ceil((date('n')) / 3) * 3), 1, date('Y')))
  50. ]),
  51. 'year' => implode(' - ', [
  52. date('Y') . '/01/01', date('Y/m/d', strtotime(date('Y') . '/01/01 + 1year -1 day'))
  53. ])
  54. ];
  55. $this->assign('where', $where);
  56. $this->assign('countBargain', StoreBargainModel::getCountBargain());
  57. $this->assign('limitTimeList', $limitTimeList);
  58. $this->assign(StoreBargainModel::systemPage($where));
  59. $this->assign('bargainId', StoreBargainModel::getBargainIdAll($where));
  60. return $this->fetch();
  61. }
  62. /**
  63. * 异步获取砍价数据
  64. */
  65. public function get_bargain_list()
  66. {
  67. $where = Util::getMore([
  68. ['page', 1],
  69. ['limit', 20],
  70. ['export', 0],
  71. ['store_name', ''],
  72. ['status', ''],
  73. ['data', '']
  74. ]);
  75. $bargainList = StoreBargainModel::systemPage($where);
  76. if (is_object($bargainList['list'])) $bargainList['list'] = $bargainList['list']->toArray();
  77. $data = $bargainList['list']['data'];
  78. foreach ($data as $k => $v) {
  79. $data[$k]['_stop_time'] = date('Y/m/d H:i:s', $v['stop_time']);
  80. }
  81. return Json::successlayui(['count' => $bargainList['list']['total'], 'data' => $data]);
  82. }
  83. /**
  84. * 添加砍价
  85. * @param int $id
  86. * @return \think\Response
  87. */
  88. public function create()
  89. {
  90. $f = [];
  91. $f[] = Form::frameImageOne('product', '选择商品', Url::buildUrl('productList', array('fodder' => 'product')))->icon('plus')->width('100%')->height('500px');
  92. $f[] = Form::hidden('product_id', '');
  93. $f[] = Form::input('title', '砍价活动名称');
  94. $f[] = Form::input('info', '砍价活动简介')->type('textarea');
  95. $f[] = Form::input('unit_name', '单位')->placeholder('个、位');
  96. $f[] = Form::select('temp_id', '砍价运费模板')->setOptions(function () {
  97. $list = ShippingTemplates::getList(['page' => 1, 'limit' => 20]);
  98. $menus = [];
  99. foreach ($list['data'] as $menu) {
  100. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  101. }
  102. return $menus;
  103. })->filterable(1)->col(12);
  104. $f[] = Form::dateTimeRange('section_time', '活动时间');//->format("yyyy-MM-dd HH:mm:ss");
  105. $f[] = Form::frameImageOne('image', '商品主图片(305*305px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')))->icon('image')->width('100%')->height('500px');
  106. $f[] = Form::frameImages('images', '商品轮播图(640*640px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'images')))->maxLength(5)->icon('images')->width('100%')->height('500px');
  107. $f[] = Form::number('bargain_max_price', '单次砍价的最大金额', 10)->min(0)->col(12);
  108. $f[] = Form::number('bargain_min_price', '单次砍价的最小金额', 0.01)->min(0)->col(12);
  109. $f[] = Form::number('sort', '排序')->col(12);
  110. $f[] = Form::number('give_integral', '赠送积分')->min(0)->col(12);
  111. $f[] = Form::radio('is_hot', '热门推荐', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]])->col(12);
  112. $form = Form::make_post_form('开启砍价活动', $f, Url::buildUrl('update'));
  113. $this->assign(compact('form'));
  114. return $this->fetch('public/form-builder');
  115. }
  116. /**
  117. * 显示编辑资源表单页.
  118. *
  119. * @param int $id
  120. * @return \think\Response
  121. */
  122. public function edit($id)
  123. {
  124. if (!$id) return $this->failed('数据不存在');
  125. $product = StoreBargainModel::get($id);
  126. if (!$product) return $this->failed('数据不存在!');
  127. $f = [];
  128. $f[] = Form::input('product_id', '产品ID', $product->getData('product_id'))->disabled(true);
  129. $f[] = Form::input('title', '砍价活动名称', $product->getData('title'));
  130. $f[] = Form::input('info', '砍价活动简介', $product->getData('info'))->type('textarea');
  131. $f[] = Form::input('unit_name', '单位', $product->getData('unit_name'))->placeholder('个、位');
  132. $f[] = Form::select('temp_id', '砍价运费模板', (string)$product->getData('temp_id'))->setOptions(function () {
  133. $list = ShippingTemplates::getList(['page' => 1, 'limit' => 20]);
  134. $menus = [];
  135. foreach ($list['data'] as $menu) {
  136. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  137. }
  138. return $menus;
  139. })->filterable(1)->col(12);
  140. $f[] = Form::dateTimeRange('section_time', '活动时间', date("Y-m-d H:i:s", $product->getData('start_time')), date("Y-m-d H:i:s", $product->getData('stop_time')));//->format("yyyy-MM-dd HH:mm:ss");
  141. $f[] = Form::frameImageOne('image', '商品主图片(305*305px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), $product->getData('image'))->icon('image')->width('100%')->height('500px');
  142. $f[] = Form::frameImages('images', '商品轮播图(640*640px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'images')), json_decode($product->getData('images'), 1))->maxLength(5)->icon('images')->width('100%')->height('500px');
  143. $f[] = Form::number('bargain_max_price', '单次砍价的最大金额', $product->getData('bargain_max_price'))->min(0)->col(12);
  144. $f[] = Form::number('bargain_min_price', '单次砍价的最小金额', $product->getData('bargain_min_price'))->min(0)->col(12);
  145. $f[] = Form::hidden('stock', $product->getData('stock'));
  146. $f[] = Form::hidden('sales', $product->getData('sales'));
  147. $f[] = Form::number('sort', '排序', $product->getData('sort'))->col(12);
  148. $f[] = Form::number('give_integral', '赠送积分', $product->getData('give_integral'))->min(0)->col(12);
  149. $f[] = Form::radio('is_hot', '热门推荐', $product->getData('is_hot'))->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]])->col(12);
  150. $f[] = Form::hidden('status', $product->getData('status'));
  151. $form = Form::make_post_form('添加用户通知', $f, Url::buildUrl('update', array('id' => $id)));
  152. $this->assign(compact('form'));
  153. return $this->fetch('public/form-builder');
  154. }
  155. /**
  156. * 保存更新的资源
  157. * @param string $id
  158. */
  159. public function update($id = '')
  160. {
  161. $data = Util::postMore([
  162. ['title', ''],
  163. ['info', ''],
  164. ['store_name', ''],
  165. ['unit_name', ''],
  166. ['section_time', []],
  167. ['image', ''],
  168. ['images', []],
  169. ['price', 0],
  170. ['min_price', 0],
  171. ['bargain_max_price', 0],
  172. ['bargain_min_price', 0],
  173. ['cost', 0],
  174. ['bargain_num', 1],
  175. ['stock', 0],
  176. ['sales', 0],
  177. ['sort', 0],
  178. ['num', 1],
  179. ['give_integral', 0],
  180. ['postage', 0],
  181. ['is_postage', 0],
  182. ['is_hot', 0],
  183. ['status', 0],
  184. ['product_id', 0],
  185. ['temp_id', ''],
  186. ['weight', ''],
  187. ['volume', ''],
  188. ]);
  189. $data['description'] = StoreDescription::getDescription($data['product_id']);
  190. $data['store_name'] = $data['title'];
  191. if ($data['title'] == '') return Json::fail('请输入砍价活动名称');
  192. if ($data['info'] == '') return Json::fail('请输入砍价活动简介');
  193. if ($data['store_name'] == '') return Json::fail('请输入砍价商品名称');
  194. if ($data['unit_name'] == '') return Json::fail('请输入商品单位');
  195. if (count($data['section_time']) < 1) return Json::fail('请选择活动时间');
  196. if (!$data['section_time'][0]) return Json::fail('请选择活动时间');
  197. if (!$data['section_time'][1]) return Json::fail('请选择活动时间');
  198. $data['start_time'] = strtotime($data['section_time'][0]);
  199. $data['stop_time'] = strtotime($data['section_time'][1]);
  200. unset($data['section_time']);
  201. if (!($data['image'])) return Json::fail('请选择推荐图');
  202. if (count($data['images']) < 1) return Json::fail('请选择轮播图');
  203. $data['images'] = json_encode($data['images']);
  204. if ($data['bargain_max_price'] == '' || $data['bargain_max_price'] < 0) return Json::fail('请输入用户单次砍价的最大金额');
  205. if ($data['bargain_min_price'] == '' || $data['bargain_min_price'] < 0) return Json::fail('请输入用户单次砍价的最小金额');
  206. if ($data['bargain_num'] == '' || $data['bargain_num'] < 0) return Json::fail('请输入用户单次砍价的次数');
  207. if ($data['num'] == '' || $data['num'] < 0) return Json::fail('请输入单次购买的砍价商品数量');
  208. unset($data['img']);
  209. if ($id) {
  210. $product = StoreBargainModel::get($id);
  211. if (!$product) return Json::fail('数据不存在!');
  212. unset($data['price'], $data['min_price']);
  213. $res = StoreBargainModel::edit($data, $id);
  214. if ($res) return Json::successful('修改成功');
  215. else return Json::fail('修改失败');
  216. } else {
  217. $data['add_time'] = time();
  218. $res = StoreBargainModel::create($data);
  219. $description['product_id'] = $res['id'];
  220. $description['description'] = htmlspecialchars_decode($data['description']);
  221. $description['type'] = 2;
  222. StoreDescription::create($description);
  223. if ($res) return Json::successful('添加成功');
  224. else return Json::fail('添加成功');
  225. }
  226. }
  227. /**
  228. * 删除指定资源
  229. *
  230. * @param int $id
  231. * @return \think\Response
  232. */
  233. public function delete($id)
  234. {
  235. if (!$id) return Json::fail('数据不存在');
  236. $product = StoreBargainModel::get($id);
  237. if (!$product) return Json::fail('数据不存在!');
  238. if ($product['is_del']) return Json::fail('已删除!');
  239. $data['is_del'] = 1;
  240. if (StoreBargainModel::edit($data, $id))
  241. return Json::successful('删除成功!');
  242. else
  243. return Json::fail(StoreBargainModel::getErrorInfo('删除失败,请稍候再试!'));
  244. }
  245. /**
  246. * 显示内容窗口
  247. * @param $id
  248. * @return mixed|\think\response\Json|void
  249. */
  250. public function edit_content($id)
  251. {
  252. if (!$id) return $this->failed('数据不存在');
  253. $seckill = StoreBargainModel::get($id);
  254. if (!$seckill) return $this->failed('数据不存在');
  255. $this->assign([
  256. 'content' => htmlspecialchars_decode(StoreDescription::getDescription($id, 2)),
  257. 'field' => 'description',
  258. 'action' => Url::buildUrl('change_field', ['id' => $id, 'field' => 'description'])
  259. ]);
  260. return $this->fetch('public/edit_content');
  261. }
  262. public function change_field($id, $field)
  263. {
  264. if (!$id) return $this->failed('数据不存在');
  265. $bargain = StoreBargainModel::get($id);
  266. if (!$bargain) return Json::fail('数据不存在!');
  267. if ($field == 'rule') {
  268. $data['rule'] = request()->post('rule');
  269. } else {
  270. $data['description'] = request()->post('description');
  271. StoreDescription::saveDescription($data['description'], $id, 2);
  272. }
  273. $res = StoreBargainModel::edit($data, $id);
  274. if ($res)
  275. return Json::successful('添加成功');
  276. else
  277. return Json::fail('添加失败');
  278. }
  279. public function edit_rule($id)
  280. {
  281. if (!$id) return $this->failed('数据不存在');
  282. $seckill = StoreBargainModel::get($id);
  283. if (!$seckill) return $this->failed('数据不存在');
  284. $this->assign([
  285. 'content' => htmlspecialchars_decode(StoreBargainModel::where('id', $id)->value('rule')),
  286. 'field' => 'rule',
  287. 'action' => Url::buildUrl('change_field', ['id' => $id, 'field' => 'rule'])
  288. ]);
  289. return $this->fetch('public/edit_content');
  290. }
  291. /**
  292. * 开启砍价商品
  293. * @param int $id
  294. * @return mixed|\think\response\Json|void
  295. */
  296. public function bargain($id = 0)
  297. {
  298. if (!$id) return $this->failed('数据不存在');
  299. $product = ProductModel::get($id);
  300. if (!$product) return Json::fail('数据不存在!');
  301. $f = [];
  302. $f[] = Form::input('title', '砍价活动名称');
  303. $f[] = Form::input('info', '砍价活动简介')->type('textarea');
  304. $f[] = Form::hidden('product_id', $product->getData('id'));
  305. $f[] = Form::input('store_name', '砍价商品名称', $product->getData('store_name'));
  306. $f[] = Form::input('unit_name', '单位', $product->getData('unit_name'))->placeholder('个、位');
  307. $f[] = Form::select('temp_id', '砍价运费模板', (string)$product->getData('temp_id'))->setOptions(function () {
  308. $list = ShippingTemplates::getList(['page' => 1, 'limit' => 20]);
  309. $menus = [];
  310. foreach ($list['data'] as $menu) {
  311. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  312. }
  313. return $menus;
  314. })->filterable(1)->col(12);
  315. $f[] = Form::dateTimeRange('section_time', '活动时间');//->format("yyyy-MM-dd HH:mm:ss");
  316. $f[] = Form::frameImageOne('image', '商品主图片(305*305px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), $product->getData('image'))->icon('image')->width('100%')->height('500px');
  317. $f[] = Form::frameImages('images', '商品轮播图(640*640px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'images')), json_decode($product->getData('slider_image'), 1))->maxLength(5)->icon('images')->width('100%')->height('500px');
  318. $f[] = Form::number('price', '砍价金额')->min(0)->col(12);
  319. $f[] = Form::number('min_price', '砍价最低金额', 0)->min(0)->col(12);
  320. $f[] = Form::number('bargain_max_price', '单次砍价的最大金额', 10)->min(0)->col(12);
  321. $f[] = Form::number('bargain_min_price', '单次砍价的最小金额', 0.01)->min(0)->precision(2)->col(12);
  322. $f[] = Form::number('cost', '成本价', $product->getData('cost'))->min(0)->col(12);
  323. $f[] = Form::number('bargain_num', '单次砍价的次数', 1)->min(0)->col(12);
  324. $f[] = Form::number('stock', '库存', $product->getData('stock'))->min(1)->col(12);
  325. $f[] = Form::number('sales', '销量', $product->getData('sales'))->min(0)->col(12);
  326. $f[] = Form::number('sort', '排序', $product->getData('sort'))->col(12);
  327. $f[] = Form::number('num', '单次购买的砍价商品数量', 1)->col(12);
  328. $f[] = Form::number('give_integral', '赠送积分', $product->getData('give_integral'))->min(0)->col(12);
  329. $f[] = Form::number('weight', '重量', 0)->min(0)->col(12);
  330. $f[] = Form::number('volume', '体积', 0)->min(0)->col(12);
  331. $f[] = Form::radio('is_hot', '热门推荐', $product->getData('is_hot'))->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]])->col(12);
  332. $f[] = Form::radio('status', '活动状态', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]])->col(12);
  333. $form = Form::make_post_form('开启砍价活动', $f, Url::buildUrl('update'));
  334. $this->assign(compact('form'));
  335. return $this->fetch('public/form-builder');
  336. }
  337. /**
  338. * 修改砍价状态
  339. * @param $status
  340. * @param int $id
  341. */
  342. public function set_bargain_status($status, $id = 0)
  343. {
  344. if (!$id) return Json::fail('参数错误');
  345. $res = StoreProductAttrValue::where('product_id', $id)->where('type', 2)->find();
  346. if (!$res) return Json::fail('请先配置规格');
  347. $res = StoreBargainModel::edit(['status' => $status], $id);
  348. if ($res) return Json::successful('修改成功');
  349. else return Json::fail('修改失败');
  350. }
  351. /**
  352. * 砍价属性选择页面
  353. * @param $id
  354. * @return string|void
  355. * @throws \think\db\exception\DataNotFoundException
  356. * @throws \think\db\exception\DbException
  357. * @throws \think\db\exception\ModelNotFoundException
  358. */
  359. public function attr_list($id)
  360. {
  361. if (!$id) return $this->failed('数据不存在!');
  362. $bargainInfo = StoreBargainModel::where('id', $id)->find();
  363. $bargainResult = StoreProductAttrResult::where('product_id', $id)->where('type', 2)->value('result');
  364. $productResult = StoreProductAttrResult::where('product_id', $bargainInfo['product_id'])->where('type', 0)->value('result');
  365. if ($productResult) {
  366. $attr = json_decode($productResult, true)['attr'];
  367. $productAttr = $this->get_attr($attr, $bargainInfo['product_id'], 0);
  368. $bargainAttr = $this->get_attr($attr, $id, 2);
  369. foreach ($productAttr as $pk => $pv) {
  370. foreach ($bargainAttr as $sv) {
  371. if ($pv['detail'] == $sv['detail']) {
  372. $productAttr[$pk] = $sv;
  373. }
  374. }
  375. }
  376. } else {
  377. if ($bargainResult) {
  378. $attr = json_decode($bargainResult, true)['attr'];
  379. $productAttr = $this->get_attr($attr, $id, 2);
  380. } else {
  381. $attr[0]['value'] = '默认';
  382. $attr[0]['detailValue'] = '';
  383. $attr[0]['attrHidden'] = '';
  384. $attr[0]['detail'][0] = '默认';
  385. $productAttr[0]['value1'] = '默认';
  386. $productAttr[0]['detail'] = json_encode(['默认' => '默认']);
  387. $productAttr[0]['pic'] = $bargainInfo['image'];
  388. $productAttr[0]['price'] = $bargainInfo['price'];
  389. $productAttr[0]['min_price'] = 0;
  390. $productAttr[0]['cost'] = $bargainInfo['cost'];
  391. $productAttr[0]['ot_price'] = $bargainInfo['ot_price'];
  392. $productAttr[0]['stock'] = $bargainInfo['stock'];
  393. $productAttr[0]['quota'] = 0;
  394. $productAttr[0]['bar_code'] = $bargainInfo['bar_code'];
  395. $productAttr[0]['weight'] = 0;
  396. $productAttr[0]['volume'] = 0;
  397. $productAttr[0]['brokerage'] = 0;
  398. $productAttr[0]['brokerage_two'] = 0;
  399. $productAttr[0]['check'] = 0;
  400. }
  401. }
  402. $attrs['attr'] = $attr;
  403. $attrs['value'] = $productAttr;
  404. $this->assign('attr', $attrs);
  405. $this->assign('id', $id);
  406. return $this->fetch();
  407. }
  408. /**
  409. * 砍价属性添加
  410. * @throws \think\db\exception\DataNotFoundException
  411. * @throws \think\db\exception\DbException
  412. * @throws \think\db\exception\ModelNotFoundException
  413. */
  414. public function save_attr()
  415. {
  416. $data = Util::postMore([
  417. ['attr', []],
  418. ['ids', []],
  419. ['id', 0],
  420. ]);
  421. if (!$data['id']) return Json::fail('数据不存在!');
  422. if (!$data['ids']) return Json::fail('你没有选择任何规格!');
  423. $productId = StoreBargainModel::where('id', $data['id'])->value('product_id');
  424. $attr = json_decode(StoreProductAttrResult::where('product_id', $productId)->where('type', 0)->value('result'), true)['attr'];
  425. foreach ($data['attr'] as $k => $v) {
  426. if (in_array($k, $data['ids'])) {
  427. $v['detail'] = json_decode(htmlspecialchars_decode($v['detail']), true);
  428. $min_price = $v['min_price'];
  429. unset($v['min_price']);
  430. $detail[$k] = $v;
  431. break;
  432. }
  433. }
  434. if (min(array_column($detail, 'quota')) == 0) return Json::fail('限购不能为0');
  435. $price = min(array_column($detail, 'price'));
  436. $quota = array_sum(array_column($detail, 'quota'));
  437. $stock = array_sum(array_column($detail, 'stock'));
  438. if (!$attr) {
  439. $attr[0]['value'] = '默认';
  440. $attr[0]['detailValue'] = '';
  441. $attr[0]['attrHidden'] = '';
  442. $attr[0]['detail'][0] = '默认';
  443. }
  444. StoreProductAttr::createProductAttr($attr, $detail, $data['id'], 2);
  445. StoreBargainModel::where('id', $data['id'])->update(['stock' => $stock, 'quota' => $quota, 'quota_show' => $quota, 'price' => $price, 'min_price' => $min_price]);
  446. return Json::successful('添加成功!');
  447. }
  448. /**
  449. * 添加砍价获取商品列表
  450. * @return string
  451. * @throws \Exception
  452. */
  453. public function productList()
  454. {
  455. $cate = StoreCategory::getTierList(null, 1);
  456. $this->assign('cate', $cate);
  457. return $this->fetch();
  458. }
  459. /**
  460. * 获取砍价商品规格
  461. * @param $attr
  462. * @param $id
  463. * @param $type
  464. * @return array
  465. */
  466. public function get_attr($attr, $id, $type)
  467. {
  468. $value = attr_format($attr)[1];
  469. $valueNew = [];
  470. $count = 0;
  471. $min_price = StoreBargainModel::where('id', $id)->value('min_price');
  472. foreach ($value as $key => $item) {
  473. $detail = $item['detail'];
  474. sort($item['detail'], SORT_STRING);
  475. $suk = implode(',', $item['detail']);
  476. $sukValue = StoreProductAttrValue::where('product_id', $id)->where('type', $type)->where('suk', $suk)->column('bar_code,cost,price,ot_price,stock,image as pic,weight,volume,brokerage,brokerage_two,quota', 'suk');
  477. if (count($sukValue)) {
  478. foreach (array_values($detail) as $k => $v) {
  479. $valueNew[$count]['value' . ($k + 1)] = $v;
  480. }
  481. $valueNew[$count]['detail'] = json_encode($detail);
  482. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  483. $valueNew[$count]['price'] = isset($sukValue[$suk]['price']) ? floatval($sukValue[$suk]['price']) : 0;
  484. $valueNew[$count]['min_price'] = $min_price;
  485. $valueNew[$count]['cost'] = isset($sukValue[$suk]['cost']) ? floatval($sukValue[$suk]['cost']) : 0;
  486. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  487. $valueNew[$count]['stock'] = isset($sukValue[$suk]['stock']) ? intval($sukValue[$suk]['stock']) : 0;
  488. $valueNew[$count]['quota'] = isset($sukValue[$suk]['quota']) ? intval($sukValue[$suk]['quota']) : 0;
  489. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  490. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ?? 0;
  491. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ?? 0;
  492. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ?? 0;
  493. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ?? 0;
  494. $valueNew[$count]['check'] = $type != 0 ? 1 : 0;
  495. $count++;
  496. }
  497. }
  498. return $valueNew;
  499. }
  500. }