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