StoreBargain.php 24 KB

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