StoreActivityProduct.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. <?php
  2. /**
  3. * Created by PhpStorm
  4. * Author: 向往那片天空
  5. * Date: 2020/6/8
  6. * Time: 16:21
  7. * 微信/QQ: 250023777
  8. * 格言: 抓住中心,宁精勿杂,宁专勿多
  9. */
  10. namespace app\admin\controller\ump;
  11. use app\admin\controller\AuthController;
  12. use app\admin\model\store\StoreCategory;
  13. use app\admin\model\store\StoreDescription;
  14. use app\admin\model\store\StoreProductAttr;
  15. use app\admin\model\store\StoreProductAttrResult;
  16. use app\admin\model\store\StoreProductAttrValue;
  17. use app\admin\model\system\ShippingTemplates;
  18. use app\Request;
  19. use crmeb\services\FormBuilder as Form;
  20. use crmeb\services\JsonService;
  21. use crmeb\services\UtilService;
  22. use app\admin\model\ump\StoreActivity as StoreActivityModel;
  23. use think\facade\Route as Url;
  24. use app\admin\model\ump\StoreActivityProduct as StoreActivityProductModel;
  25. /**
  26. * 活动商品控制器
  27. * Class StoreActivityProduct
  28. * @package app\admin\controller\ump
  29. */
  30. class StoreActivityProduct extends AuthController
  31. {
  32. public function index($aid)
  33. {
  34. $actName = StoreActivityModel::where("id={$aid}")->value('title');
  35. $this->assign('countCombination', StoreActivityProductModel::where("aid={$aid} and is_del=0")->count());
  36. $this->assign("aid", $aid);
  37. $this->assign("actName", $actName);
  38. return $this->fetch();
  39. }
  40. public function save_excel()
  41. {
  42. $where = UtilService::getMore([
  43. ['is_show', ''],
  44. ['title', ''],
  45. ]);
  46. StoreActivityProductModel::SaveExcel($where);
  47. }
  48. /**
  49. * 异步获取活动数据
  50. */
  51. public function get_combination_list()
  52. {
  53. $where = UtilService::getMore([
  54. 'aid',
  55. ['page', 1],
  56. ['limit', 20],
  57. ['export', 0],
  58. ['is_show', ''],
  59. ['is_host', ''],
  60. ['store_name', '']
  61. ]);
  62. $combinationList = StoreActivityProductModel::systemPage($where);
  63. if (is_object($combinationList['list'])) $combinationList['list'] = $combinationList['list']->toArray();
  64. $data = $combinationList['list']['data'];
  65. $count = sizeof($data);
  66. return JsonService::successlayui(['data' => $data, 'count' => $count]);
  67. }
  68. public function combination($id = 0)
  69. {
  70. if (!$id) return $this->failed('数据不存在');
  71. $product = ProductModel::get($id);
  72. if (!$product) return JsonService::fail('数据不存在!');
  73. $f = [];
  74. $f[] = Form::hidden('product_id', $id);
  75. $f[] = Form::input('title', '活动名称', $product->getData('store_name'));
  76. $f[] = Form::input('info', '活动简介', $product->getData('store_info'))->type('textarea');
  77. $f[] = Form::input('unit_name', '单位', $product->getData('unit_name'))->placeholder('个、位');
  78. $f[] = Form::select('temp_id', '砍价运费模板', (string)$product->getData('temp_id'))->setOptions(function () {
  79. $list = ShippingTemplates::getList(['page' => 1, 'limit' => 20]);
  80. $menus = [];
  81. foreach ($list['data'] as $menu) {
  82. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  83. }
  84. return $menus;
  85. })->filterable(1)->col(12);
  86. $f[] = Form::dateTimeRange('section_time', '活动时间');
  87. $f[] = Form::frameImageOne('image', '商品主图片(305*305px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), $product->getData('image'))->icon('image')->width('100%')->height('500px');
  88. $f[] = Form::frameImages('images', '商品轮播图(640*640px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'images')), json_decode($product->getData('slider_image')))->maxLength(5)->icon('images')->width('100%')->height('500px');
  89. $f[] = Form::number('effective_time', '活动时效(单位 小时)', 24)->placeholder('请输入活动有效时间,单位:小时');
  90. $f[] = Form::number('price', '活动价')->min(0)->col(12);
  91. $f[] = Form::number('people', '活动人数')->min(2)->col(12);
  92. $f[] = Form::number('stock', '库存', $product->getData('stock'))->min(0)->precision(0)->col(12);
  93. $f[] = Form::number('sales', '销量', $product->getData('sales'))->min(0)->precision(0)->col(12);
  94. $f[] = Form::number('sort', '排序')->col(12);
  95. $f[] = Form::number('weight', '重量', 0)->min(0)->col(12);
  96. $f[] = Form::number('volume', '体积', 0)->min(0)->col(12);
  97. $f[] = Form::radio('is_host', '热门推荐', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]])->col(12);
  98. $f[] = Form::radio('is_show', '活动状态', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]])->col(12);
  99. $form = Form::make_post_form('添加用户通知', $f, Url::buildUrl('save'));
  100. $this->assign(compact('form'));
  101. return $this->fetch('public/form-builder');
  102. }
  103. /**
  104. * 显示创建资源表单页.
  105. *
  106. * @return \think\Response
  107. */
  108. public function create(Request $request)
  109. {
  110. $aid = $request->param('aid');
  111. $f = [];
  112. $f[] = Form::frameImageOne('product', '选择商品', Url::buildUrl('productList', array('fodder' => 'product')))->icon('plus')->width('100%')->height('500px');
  113. $f[] = Form::hidden('product_id', '');
  114. $f[] = Form::input('title', '活动商品名称');
  115. // $f[] = Form::input('info', '活动简介')->type('textarea');
  116. $f[] = Form::input('unit_name', '单位')->placeholder('个、位');
  117. $f[] = Form::select('temp_id', '秒杀运费模板')->setOptions(function () {
  118. $list = ShippingTemplates::getList(['page' => 1, 'limit' => 20]);
  119. $menus = [];
  120. foreach ($list['data'] as $menu) {
  121. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  122. }
  123. return $menus;
  124. })->filterable(1)->col(12);
  125. // $f[] = Form::dateTimeRange('section_time', '活动时间');
  126. $f[] = Form::frameImageOne('image', '商品主图片(305*305px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')))->icon('image')->width('100%')->height('500px');
  127. $f[] = Form::frameImages('images', '商品轮播图(640*640px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'images')))->maxLength(5)->icon('images')->width('100%')->height('500px');
  128. // $f[] = Form::number('effective_time', '活动时效', '24')->placeholder('请输入活动订单有效时间,单位:小时')->col(12);
  129. // $f[] = Form::number('people', '活动人数', 2)->min(2)->col(12);
  130. // $f[] = Form::number('num', '单次购买商品个数', 1)->min(1)->col(12);
  131. $f[] = Form::number('sort', '排序')->col(12);
  132. $f[] = Form::radio('is_host', '热门推荐', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]])->col(12);
  133. $form = Form::make_post_form('添加用户通知', $f, Url::buildUrl('save', compact('aid')));
  134. $this->assign(compact('form'));
  135. return $this->fetch('public/form-builder');
  136. }
  137. /**
  138. * 保存新建的资源
  139. * @param int $id
  140. */
  141. public function save($id = 0, $aid = 0)
  142. {
  143. $data = UtilService::postMore([
  144. 'product_id',
  145. 'title',
  146. // 'info',
  147. ['unit_name', '个'],
  148. ['image', ''],
  149. ['images', []],
  150. // ['section_time', []],
  151. // ['effective_time', 0],
  152. ['postage', 0],
  153. ['price', 0],
  154. // ['people', 0],
  155. ['sort', 0],
  156. // ['stock', 0],
  157. // ['sales', 0],
  158. ['is_show', 0],
  159. ['is_host', 0],
  160. ['is_postage', 0],
  161. ['temp_id', ''],
  162. ['weight', ''],
  163. ['volume', ''],
  164. ['num', 1],
  165. ]);
  166. $data['description'] = StoreDescription::getDescription($data['product_id']);
  167. if (!$data['title']) return JsonService::fail('请输入活动名称');
  168. // if (!$data['info']) return JsonService::fail('请输入活动简介');
  169. if (!$data['image']) return JsonService::fail('请上传商品图片');
  170. if (count($data['images']) < 1) return JsonService::fail('请上传商品轮播图');
  171. // if ($data['effective_time'] == 0 || $data['effective_time'] < 0) return JsonService::fail('请输入活动有效时间');
  172. // if ($data['people'] == '' || $data['people'] < 1) return JsonService::fail('请输入活动人数');
  173. // if (count($data['section_time']) < 1) return JsonService::fail('请选择活动时间');
  174. $data['images'] = json_encode($data['images']);
  175. // $data['start_time'] = strtotime($data['section_time'][0]);
  176. // $data['stop_time'] = strtotime($data['section_time'][1]);
  177. // unset($data['section_time']);
  178. if ($id) {
  179. unset($data['description']);
  180. $product = StoreActivityProductModel::get($id);
  181. if (!$product) return JsonService::fail('数据不存在!');
  182. $data['product_id'] = $product['product_id'];
  183. StoreActivityProductModel::edit($data, $id);
  184. return JsonService::successful('编辑成功!');
  185. } else {
  186. $data['add_time'] = time();
  187. $data['aid'] = $aid;
  188. $res = StoreActivityProductModel::create($data);
  189. $description['product_id'] = $res['id'];
  190. $description['description'] = htmlspecialchars_decode($data['description']);
  191. $description['type'] = 4;
  192. StoreDescription::create($description);
  193. return JsonService::successful('添加活动成功!');
  194. }
  195. }
  196. /**
  197. * 显示编辑资源表单页.
  198. *
  199. * @param int $id
  200. * @return \think\Response
  201. */
  202. public function edit($id)
  203. {
  204. if (!$id) return $this->failed('数据不存在');
  205. $product = StoreActivityProductModel::get($id);
  206. if (!$product) return JsonService::fail('数据不存在!');
  207. $f = [];
  208. $f[] = Form::input('product_id', '产品ID', $product->getData('product_id'))->disabled(true);
  209. $f[] = Form::input('title', '活动商品名称', $product->getData('title'));
  210. // $f[] = Form::input('info', '活动简介', $product->getData('info'))->type('textarea');
  211. $f[] = Form::input('unit_name', '单位', $product->getData('unit_name'))->placeholder('个、位');
  212. $f[] = Form::select('temp_id', '砍价运费模板', (string)$product->getData('temp_id'))->setOptions(function () {
  213. $list = ShippingTemplates::getList(['page' => 1, 'limit' => 20]);
  214. $menus = [];
  215. foreach ($list['data'] as $menu) {
  216. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  217. }
  218. return $menus;
  219. })->filterable(1)->col(12);
  220. // $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')));
  221. $f[] = Form::frameImageOne('image', '商品主图片(305*305px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), $product->getData('image'))->icon('image')->width('100%')->height('500px');
  222. $f[] = Form::frameImages('images', '商品轮播图(640*640px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'images')), json_decode($product->getData('images')))->maxLength(5)->icon('images')->width('100%')->height('500px');
  223. // $f[] = Form::number('effective_time', '活动时效(单位 小时)', $product->getData('effective_time'))->placeholder('请输入活动订单有效时间,单位:小时')->col(12);
  224. $f[] = Form::hidden('price', $product->getData('price'));
  225. // $f[] = Form::number('people', '活动人数', $product->getData('people'))->min(2)->col(12);
  226. // $f[] = Form::number('num', '单次购买商品个数', $product->getData('num'))->min(1)->col(12);
  227. // $f[] = Form::hidden('stock', $product->getData('stock'));
  228. // $f[] = Form::hidden('sales', $product->getData('sales'));
  229. $f[] = Form::number('sort', '排序', $product->getData('sort'))->col(12);
  230. $f[] = Form::radio('is_host', '热门推荐', $product->getData('is_host'))->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]])->col(12);
  231. $f[] = Form::hidden('is_show', $product->getData('is_show'));
  232. $form = Form::make_post_form('添加用户通知', $f, Url::buildUrl('save', compact('id')));
  233. $this->assign(compact('form'));
  234. return $this->fetch('public/form-builder');
  235. }
  236. /**
  237. * 删除指定资源
  238. *
  239. * @param int $id
  240. * @return \think\Response
  241. */
  242. public function delete($id)
  243. {
  244. if (!$id) return $this->failed('数据不存在');
  245. $product = StoreActivityProductModel::get($id);
  246. if (!$product) return JsonService::fail('数据不存在!');
  247. if ($product['is_del']) return JsonService::fail('已删除!');
  248. $data['is_del'] = 1;
  249. if (!StoreActivityProductModel::edit($data, $id))
  250. return JsonService::fail(StoreActivityProductModel::getErrorInfo('删除失败,请稍候再试!'));
  251. else
  252. return JsonService::successful('删除成功!');
  253. }
  254. /**
  255. * 属性页面
  256. * @param $id
  257. * @return mixed|void
  258. */
  259. public function attr($id)
  260. {
  261. if (!$id) return $this->failed('数据不存在!');
  262. $result = StoreCombinationAttrResult::getResult($id);
  263. $image = StoreActivityProductModel::where('id', $id)->value('image');
  264. $this->assign(compact('id', 'result', 'product', 'image'));
  265. return $this->fetch();
  266. }
  267. /**
  268. * 活动属性选择页面
  269. * @param $id
  270. * @return string|void
  271. * @throws \think\db\exception\DataNotFoundException
  272. * @throws \think\db\exception\DbException
  273. * @throws \think\db\exception\ModelNotFoundException
  274. */
  275. public function attr_list($id)
  276. {
  277. if (!$id) return $this->failed('数据不存在!');
  278. $combinationInfo = StoreActivityProductModel::where('id', $id)->find();
  279. $combinationResult = StoreProductAttrResult::where('product_id', $id)->where('type', 4)->value('result');
  280. $productResult = StoreProductAttrResult::where('product_id', $combinationInfo['product_id'])->where('type', 0)->value('result');
  281. if ($productResult) {
  282. $attr = json_decode($productResult, true)['attr'];
  283. // Log::write($attr,'error');
  284. $productAttr = $this->get_attr($attr, $combinationInfo['product_id'], 0);
  285. $combinationAttr = $this->get_attr($attr, $id, 4);
  286. foreach ($productAttr as $pk => $pv) {
  287. foreach ($combinationAttr as $sv) {
  288. if ($pv['detail'] == $sv['detail']) {
  289. $productAttr[$pk] = $sv;
  290. }
  291. }
  292. }
  293. } else {
  294. if ($combinationResult) {
  295. $attr = json_decode($combinationResult, true)['attr'];
  296. $productAttr = $this->get_attr($attr, $id, 4);
  297. } else {
  298. $attr[0]['value'] = '默认';
  299. $attr[0]['detailValue'] = '';
  300. $attr[0]['attrHidden'] = '';
  301. $attr[0]['detail'][0] = '默认';
  302. $productAttr[0]['value1'] = '默认';
  303. $productAttr[0]['detail'] = json_encode(['默认' => '默认']);
  304. $productAttr[0]['pic'] = $combinationInfo['image'];
  305. $productAttr[0]['price'] = $combinationInfo['price'];
  306. $productAttr[0]['cost'] = $combinationInfo['cost'];
  307. $productAttr[0]['ot_price'] = $combinationInfo['ot_price'];
  308. $productAttr[0]['stock'] = $combinationInfo['stock'];
  309. $productAttr[0]['quota'] = 0;
  310. $productAttr[0]['bar_code'] = $combinationInfo['bar_code'];
  311. $productAttr[0]['weight'] = 0;
  312. $productAttr[0]['volume'] = 0;
  313. $productAttr[0]['brokerage'] = 0;
  314. $productAttr[0]['brokerage_two'] = 0;
  315. $productAttr[0]['check'] = 0;
  316. }
  317. }
  318. $attrs['attr'] = $attr;
  319. $attrs['value'] = $productAttr;
  320. $this->assign('attr', $attrs);
  321. $this->assign('id', $id);
  322. return $this->fetch();
  323. }
  324. /**
  325. * 活动属性添加
  326. * @throws \think\db\exception\DataNotFoundException
  327. * @throws \think\db\exception\DbException
  328. * @throws \think\db\exception\ModelNotFoundException
  329. */
  330. public function save_attr()
  331. {
  332. $data = UtilService::postMore([
  333. ['attr', []],
  334. ['ids', []],
  335. ['id', 0],
  336. ]);
  337. if (!$data['id']) return JsonService::fail('数据不存在!');
  338. if (!$data['ids']) return JsonService::fail('你没有选择任何规格!');
  339. $combinationId = StoreActivityProductModel::where('id', $data['id'])->value('product_id');
  340. //$attr = json_decode(StoreProductAttrResult::where('product_id', $combinationId)->where('type',3)->value('result'), true)['attr'];
  341. $attr = StoreProductAttrResult::where('product_id', $combinationId)->where('type', 0)->value('result');
  342. foreach ($data['attr'] as $k => $v) {
  343. if (in_array($k, $data['ids'])) {
  344. $v['detail'] = json_decode(htmlspecialchars_decode($v['detail']), true);
  345. $detail[$k] = $v;
  346. }
  347. }
  348. if (min(array_column($detail, 'quota')) == 0) return JsonService::fail('限购不能为0');
  349. $price = min(array_column($detail, 'price'));
  350. $quota = array_sum(array_column($detail, 'quota'));
  351. $stock = array_sum(array_column($detail, 'stock'));
  352. if (!$attr) {
  353. $attr[0]['value'] = '规格';
  354. $attr[0]['detailValue'] = '';
  355. $attr[0]['attrHidden'] = '';
  356. $attr[0]['detail'][0] = '默认';
  357. } else {
  358. $attr = json_decode($attr, true)['attr'];
  359. }
  360. $res = StoreProductAttr::createProductAttr($attr, $detail, $data['id'], 4);
  361. if ($res === false) {
  362. return JsonService::fail(StoreProductAttr::getErrorInfo('err'), [$attr, $detail]);
  363. }
  364. StoreActivityProductModel::where('id', $data['id'])->update(['stock' => $stock, 'quota' => $quota, 'quota_show' => $quota, 'price' => $price]);
  365. return JsonService::successful('添加成功!', [$attr, $detail]);
  366. }
  367. /**
  368. * 生成属性
  369. * @param int $id
  370. */
  371. public function is_format_attr($id = 0)
  372. {
  373. if (!$id) return JsonService::fail('商品不存在');
  374. list($attr, $detail) = UtilService::postMore([
  375. ['items', []],
  376. ['attrs', []]
  377. ], $this->request, true);
  378. $product = StoreActivityProductModel::get($id);
  379. if (!$product) return JsonService::fail('商品不存在');
  380. $attrFormat = attr_format($attr)[1];
  381. if (count($detail)) {
  382. foreach ($attrFormat as $k => $v) {
  383. foreach ($detail as $kk => $vv) {
  384. if ($v['detail'] == $vv['detail']) {
  385. $attrFormat[$k]['price'] = $vv['price'];
  386. $attrFormat[$k]['sales'] = $vv['sales'];
  387. $attrFormat[$k]['pic'] = $vv['pic'];
  388. $attrFormat[$k]['check'] = false;
  389. break;
  390. } else {
  391. $attrFormat[$k]['price'] = '';
  392. $attrFormat[$k]['sales'] = '';
  393. $attrFormat[$k]['pic'] = $product['image'];
  394. $attrFormat[$k]['check'] = true;
  395. }
  396. }
  397. }
  398. } else {
  399. foreach ($attrFormat as $k => $v) {
  400. $attrFormat[$k]['price'] = $product['price'];
  401. $attrFormat[$k]['sales'] = $product['stock'];
  402. $attrFormat[$k]['pic'] = $product['image'];
  403. $attrFormat[$k]['check'] = false;
  404. }
  405. }
  406. return JsonService::successful($attrFormat);
  407. }
  408. /**
  409. * 添加 修改属性
  410. * @param $id
  411. */
  412. public function set_attr($id)
  413. {
  414. if (!$id) return $this->failed('商品不存在!');
  415. list($attr, $detail) = UtilService::postMore([
  416. ['items', []],
  417. ['attrs', []]
  418. ], $this->request, true);
  419. $res = StoreCombinationAttr::createProductAttr($attr, $detail, $id);
  420. if ($res)
  421. return $this->successful('编辑属性成功!');
  422. else
  423. return $this->failed(StoreCombinationAttr::getErrorInfo());
  424. }
  425. /**
  426. * 清除属性
  427. * @param $id
  428. */
  429. public function clear_attr($id)
  430. {
  431. if (!$id) return $this->failed('商品不存在!');
  432. if (false !== StoreCombinationAttr::clearProductAttr($id) && false !== StoreCombinationAttrResult::clearResult($id))
  433. return $this->successful('清空商品属性成功!');
  434. else
  435. return $this->failed(StoreCombinationAttr::getErrorInfo('清空商品属性失败!'));
  436. }
  437. public function edit_content($id)
  438. {
  439. if (!$id) return $this->failed('数据不存在');
  440. $product = StoreActivityProductModel::get($id);
  441. if (!$product) return JsonService::fail('数据不存在!');
  442. $this->assign([
  443. 'content' => htmlspecialchars_decode(StoreDescription::getDescription($id, 4)),
  444. 'field' => 'description',
  445. 'action' => Url::buildUrl('change_field', ['id' => $id, 'field' => 'description'])
  446. ]);
  447. return $this->fetch('public/edit_content');
  448. }
  449. public function change_field($id)
  450. {
  451. if (!$id) return $this->failed('数据不存在');
  452. $combination = StoreActivityProductModel::get($id);
  453. if (!$combination) return JsonService::fail('数据不存在!');
  454. $data['description'] = request()->post('description');
  455. StoreDescription::saveDescription($data['description'], $id, 4);
  456. $res = StoreActivityProductModel::edit($data, $id);
  457. if ($res)
  458. return JsonService::successful('添加成功');
  459. else
  460. return JsonService::fail('添加失败');
  461. }
  462. /**活动列表
  463. * @return mixed
  464. */
  465. public function combina_list()
  466. {
  467. $where = UtilService::getMore([
  468. ['status', ''],
  469. ['data', ''],
  470. ], $this->request);
  471. $this->assign('where', $where);
  472. $this->assign(StorePink::systemPage($where));
  473. return $this->fetch();
  474. }
  475. /**活动人列表
  476. * @return mixed
  477. */
  478. public function order_pink($id)
  479. {
  480. if (!$id) return $this->failed('数据不存在');
  481. $StorePink = StorePink::getPinkUserOne($id);
  482. if (!$StorePink) return $this->failed('数据不存在!');
  483. $list = StorePink::getPinkMember($id);
  484. $list[] = $StorePink;
  485. $this->assign('list', $list);
  486. return $this->fetch();
  487. }
  488. /**
  489. * 修改活动状态
  490. * @param $status
  491. * @param int $idd
  492. */
  493. public function set_combination_status($status, $id = 0)
  494. {
  495. if (!$id) return JsonService::fail('参数错误');
  496. $res = StoreProductAttrValue::where('product_id', $id)->where('type', 4)->find();
  497. if (!$res) return JsonService::fail('请先配置规格');
  498. $res = StoreActivityProductModel::edit(['is_show' => $status], $id);
  499. if ($res) return JsonService::successful('修改成功');
  500. else return JsonService::fail('修改失败');
  501. }
  502. /**
  503. * 添加活动获取商品列表
  504. * @return string
  505. * @throws \Exception
  506. */
  507. public function productList()
  508. {
  509. $cate = StoreCategory::getTierList(null, 1);
  510. $this->assign('cate', $cate);
  511. return $this->fetch();
  512. }
  513. /**
  514. * 获取活动商品规格
  515. * @param $attr
  516. * @param $id
  517. * @param $type
  518. * @return array
  519. */
  520. public function get_attr($attr, $id, $type)
  521. {
  522. $value = attr_format($attr)[1];
  523. $valueNew = [];
  524. $count = 0;
  525. foreach ($value as $key => $item) {
  526. $detail = $item['detail'];
  527. sort($item['detail'], SORT_STRING);
  528. $suk = implode(',', $item['detail']);
  529. $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');
  530. if (count($sukValue)) {
  531. foreach (array_values($detail) as $k => $v) {
  532. $valueNew[$count]['value' . ($k + 1)] = $v;
  533. }
  534. // Log::write(json_encode($sukValue), 'error');
  535. $valueNew[$count]['detail'] = json_encode($detail);
  536. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  537. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  538. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  539. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  540. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  541. $valueNew[$count]['quota'] = $sukValue[$suk]['quota'] ? intval($sukValue[$suk]['quota']) : 0;
  542. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  543. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ?? 0;
  544. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ?? 0;
  545. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ?? 0;
  546. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ?? 0;
  547. $valueNew[$count]['check'] = $type != 0 ? 1 : 0;
  548. $count++;
  549. }
  550. }
  551. return $valueNew;
  552. }
  553. public function del(Request $request, $id)
  554. {
  555. //todo 判断活动下面是否有商品
  556. StoreActivityProductModel::update([
  557. 'id' => $id,
  558. 'is_del' => 1
  559. ]);
  560. return JsonService::success("删除成功");
  561. }
  562. public function set_show(Request $request)
  563. {
  564. $param = UtilService::getMore([
  565. 'id',
  566. 'is_show'
  567. ], $request);
  568. if (!$param['id']) return JsonService::fail('参数错误');
  569. $res = StoreProductAttrValue::where('product_id', $param['id'])->where('type', 4)->find();
  570. if (!$res) return JsonService::fail('请先配置规格');
  571. StoreActivityProductModel::update([
  572. 'id' => $param['id'],
  573. 'is_show' => $param['is_show']
  574. ]);
  575. if ($param['is_show']) { //启用商品后,改商品所属活动也改为启用状态
  576. $aid = StoreActivityProductModel::where("id={$param['id']}")->value('aid');
  577. StoreActivityModel::where('id', $aid)->update([
  578. 'is_show' => 1,
  579. ]);
  580. }
  581. JsonService::success("修改成功");
  582. }
  583. }