StoreBargain.php 24 KB

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