StoreProduct.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. <?php
  2. namespace app\admin\controller\store;
  3. use app\admin\controller\AuthController;
  4. use app\models\store\SystemStockBill;
  5. use app\admin\model\store\{
  6. StoreDescription,
  7. StoreProductAttrValue,
  8. StoreProductAttr,
  9. StoreProductAttrResult,
  10. StoreProductCate,
  11. StoreProductRelation,
  12. StoreCategory as CategoryModel,
  13. StoreProduct as ProductModel
  14. };
  15. use app\admin\model\ump\StoreBargain;
  16. use app\admin\model\ump\StoreCombination;
  17. use app\admin\model\ump\StoreSeckill;
  18. use crmeb\services\{
  19. JsonService, UtilService as Util, JsonService as Json, FormBuilder as Form
  20. };
  21. use crmeb\traits\CurdControllerTrait;
  22. use think\facade\Route as Url;
  23. use app\admin\model\system\{
  24. SystemAttachment, ShippingTemplates
  25. };
  26. /**
  27. * 产品管理
  28. * Class StoreProduct
  29. * @package app\admin\controller\store
  30. */
  31. class StoreProduct extends AuthController
  32. {
  33. use CurdControllerTrait;
  34. protected $bindModel = ProductModel::class;
  35. /**
  36. * 显示资源列表
  37. *
  38. * @return \think\Response
  39. */
  40. public function index()
  41. {
  42. $type = $this->request->param('type', 1);
  43. //获取分类
  44. $this->assign('cate', CategoryModel::getTierList(null, 1));
  45. //出售中产品
  46. $onsale = ProductModel::where('is_del', 0)->where('is_consumer', 0)->where('is_show', 1)->count();
  47. //待上架产品
  48. $forsale = ProductModel::where('is_del', 0)->where('is_consumer', 0)->where('is_show', 0)->count();
  49. //仓库中产品
  50. $warehouse = ProductModel::where('is_del', 0)->where('is_consumer', 0)->count();
  51. //已经售馨产品
  52. $outofstock = ProductModel::getModelObject(['type' => 4])->where('is_consumer', 0)->count();
  53. //警戒库存
  54. $policeforce = ProductModel::getModelObject(['type' => 5])->where('is_consumer', 0)->count();
  55. //回收站
  56. $recycle = ProductModel::where('is_del', 1)->where('is_consumer', 0)->count();
  57. $this->assign(compact('type', 'onsale', 'forsale', 'warehouse', 'outofstock', 'policeforce', 'recycle'));
  58. return $this->fetch();
  59. }
  60. public function consumer()
  61. {
  62. $type = $this->request->param('type', 1);
  63. //获取分类
  64. $this->assign('cate', CategoryModel::getTierList(null, 1, 1));
  65. //出售中产品
  66. $onsale = ProductModel::where('is_del', 0)->where('is_show', 1)->where('is_consumer', 1)->count();
  67. //待上架产品
  68. $forsale = ProductModel::where('is_del', 0)->where('is_show', 0)->where('is_consumer', 1)->count();
  69. //仓库中产品
  70. $warehouse = ProductModel::where('is_del', 0)->where('is_consumer', 1)->count();
  71. //已经售馨产品
  72. $outofstock = ProductModel::getModelObject(['type' => 4])->where('is_consumer', 1)->count();
  73. //警戒库存
  74. $policeforce = ProductModel::getModelObject(['type' => 5])->where('is_consumer', 1)->count();
  75. //回收站
  76. $recycle = ProductModel::where('is_del', 1)->where('is_consumer', 1)->count();
  77. $this->assign(compact('type', 'onsale', 'forsale', 'warehouse', 'outofstock', 'policeforce', 'recycle'));
  78. return $this->fetch();
  79. }
  80. /**
  81. * 异步查找产品
  82. *
  83. * @return json
  84. */
  85. public function product_ist()
  86. {
  87. $where = Util::getMore([
  88. ['page', 1],
  89. ['limit', 20],
  90. ['store_name', ''],
  91. ['cate_id', ''],
  92. ['excel', 0],
  93. ['order', ''],
  94. [['type', 'd'], $this->request->param('type/d')],
  95. ['is_consumer', 0]
  96. ]);
  97. return Json::successlayui(ProductModel::ProductList($where));
  98. }
  99. /**
  100. * 设置单个产品上架|下架
  101. *
  102. * @return json
  103. */
  104. public function set_show($is_show = '', $id = '')
  105. {
  106. ($is_show == '' || $id == '') && Json::fail('缺少参数');
  107. $res = ProductModel::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  108. if ($res) {
  109. return Json::successful($is_show == 1 ? '上架成功' : '下架成功');
  110. } else {
  111. return Json::fail($is_show == 1 ? '上架失败' : '下架失败');
  112. }
  113. }
  114. /**
  115. * 快速编辑
  116. *
  117. * @return json
  118. */
  119. public function set_product($field = '', $id = '', $value = '')
  120. {
  121. $field == '' || $id == '' || $value == '' && Json::fail('缺少参数');
  122. if (ProductModel::where(['id' => $id])->update([$field => $value]))
  123. return Json::successful('保存成功');
  124. else
  125. return Json::fail('保存失败');
  126. }
  127. /**
  128. * 设置批量产品上架
  129. *
  130. * @return json
  131. */
  132. public function product_show()
  133. {
  134. $post = Util::postMore([
  135. ['ids', []]
  136. ]);
  137. if (empty($post['ids'])) {
  138. return Json::fail('请选择需要上架的产品');
  139. } else {
  140. $res = ProductModel::where('id', 'in', $post['ids'])->update(['is_show' => 1]);
  141. if ($res)
  142. return Json::successful('上架成功');
  143. else
  144. return Json::fail('上架失败');
  145. }
  146. }
  147. /**
  148. * 显示创建资源表单页.
  149. *
  150. * @return \think\Response
  151. */
  152. public function create($id = 0, $consumer = 0)
  153. {
  154. $this->assign('id', (int)$id);
  155. $this->assign('consumer', (int)$consumer);
  156. return $this->fetch();
  157. }
  158. /**
  159. * 获取规则属性模板
  160. * @throws \think\db\exception\DataNotFoundException
  161. * @throws \think\db\exception\DbException
  162. * @throws \think\db\exception\ModelNotFoundException
  163. */
  164. public function get_rule()
  165. {
  166. return Json::successful(\app\models\store\StoreProductRule::field(['rule_name', 'rule_value'])->select()->each(function ($item) {
  167. $item['rule_value'] = json_decode($item['rule_value'], true);
  168. })->toArray());
  169. }
  170. /**
  171. * 获取产品详细信息
  172. * @param int $id
  173. * @throws \think\db\exception\DataNotFoundException
  174. * @throws \think\db\exception\DbException
  175. * @throws \think\db\exception\ModelNotFoundException
  176. */
  177. public function get_product_info($id = 0, $type = 0)
  178. {
  179. $list = CategoryModel::getTierList(null, 1, $type);
  180. $menus = [];
  181. foreach ($list as $menu) {
  182. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['cate_name'], 'disabled' => $menu['pid'] == 0 ? 0 : 1];//,'disabled'=>$menu['pid']== 0];
  183. }
  184. $data['tempList'] = ShippingTemplates::order('sort', 'desc')->field(['id', 'name'])->select()->toArray();
  185. $data['cateList'] = $menus;
  186. $data['productInfo'] = [];
  187. if ($id) {
  188. $productInfo = ProductModel::get($id);
  189. if (!$productInfo) {
  190. return Json::fail('修改的产品不存在');
  191. }
  192. $productInfo['cate_id'] = explode(',', $productInfo['cate_id']);
  193. $productInfo['description'] = htmlspecialchars_decode(StoreDescription::getDescription($id));
  194. $productInfo['slider_image'] = is_string($productInfo['slider_image']) ? json_decode($productInfo['slider_image'], true) : [];
  195. if ($productInfo['spec_type'] == 1) {
  196. $result = StoreProductAttrResult::getResult($id, 0);
  197. foreach ($result['value'] as $k => $v) {
  198. $num = 1;
  199. foreach ($v['detail'] as $dv) {
  200. $result['value'][$k]['value' . $num] = $dv;
  201. $num++;
  202. }
  203. }
  204. $productInfo['items'] = $result['attr'];
  205. $productInfo['attrs'] = $result['value'];
  206. $productInfo['attr'] = ['pic' => '', 'price' => 0, 'cost' => 0, 'ot_price' => 0, 'stock' => 0, 'bar_code' => '', 'weight' => 0, 'volume' => 0, 'brokerage' => 0, 'brokerage_two' => 0];
  207. } else {
  208. $result = StoreProductAttrValue::where('product_id', $id)->where('type', 0)->find();
  209. if ($result) {
  210. $single = $result->toArray();
  211. } else {
  212. $single = [];
  213. }
  214. $productInfo['items'] = [];
  215. $productInfo['attrs'] = [];
  216. $productInfo['attr'] = [
  217. 'pic' => $single['image'] ?? '',
  218. 'price' => $single['price'] ?? 0,
  219. 'cost' => $single['cost'] ?? 0,
  220. 'ot_price' => $single['ot_price'] ?? 0,
  221. 'stock' => $single['stock'] ?? 0,
  222. 'bar_code' => $single['bar_code'] ?? '',
  223. 'weight' => $single['weight'] ?? 0,
  224. 'volume' => $single['volume'] ?? 0,
  225. 'brokerage' => $single['brokerage'] ?? 0,
  226. 'brokerage_two' => $single['brokerage_two'] ?? 0,
  227. ];
  228. }
  229. if ($productInfo['activity']) {
  230. $activity = explode(',', $productInfo['activity']);
  231. foreach ($activity as $k => $v) {
  232. if ($v == 1) {
  233. $activity[$k] = '秒杀';
  234. } elseif ($v == 2) {
  235. $activity[$k] = '砍价';
  236. } elseif ($v == 3) {
  237. $activity[$k] = '拼团';
  238. }
  239. }
  240. $productInfo['activity'] = $activity;
  241. } else {
  242. $productInfo['activity'] = ['秒杀', '砍价', '拼团'];
  243. }
  244. $data['productInfo'] = $productInfo;
  245. }
  246. return JsonService::successful($data);
  247. }
  248. /**
  249. * 保存新建的资源
  250. *
  251. *
  252. */
  253. public function save($id)
  254. {
  255. $data = Util::postMore([
  256. ['cate_id', []],
  257. 'store_name',
  258. 'store_info',
  259. 'keyword',
  260. ['unit_name', '件'],
  261. ['image', []],
  262. ['slider_image', []],
  263. ['postage', 0],
  264. ['is_sub', 0],
  265. ['sort', 0],
  266. ['sales', 0],
  267. ['ficti', 100],
  268. ['give_integral', 0],
  269. ['max_use_integral', 0],
  270. ['is_show', 0],
  271. ['temp_id', 0],
  272. ['is_hot', 0],
  273. ['is_benefit', 0],
  274. ['is_suit', 0],
  275. ['is_award', 0],
  276. ['is_best', 0],
  277. ['is_new', 0],
  278. ['mer_use', 0],
  279. ['is_postage', 0],
  280. ['is_good', 0],
  281. ['description', ''],
  282. ['spec_type', 0],
  283. ['video_link', ''],
  284. ['items', []],
  285. ['attrs', []],
  286. ['activity', []],
  287. ['is_consumer', 0],
  288. ['bar_code', ''],
  289. ]);
  290. foreach ($data['activity'] as $k => $v) {
  291. if ($v == '秒杀') {
  292. $data['activity'][$k] = 1;
  293. } elseif ($v == '砍价') {
  294. $data['activity'][$k] = 2;
  295. } else {
  296. $data['activity'][$k] = 3;
  297. }
  298. }
  299. $data['activity'] = implode(',', $data['activity']);
  300. $detail = $data['attrs'];
  301. $data['price'] = min(array_column($detail, 'price'));
  302. $data['ot_price'] = min(array_column($detail, 'ot_price'));
  303. $data['cost'] = min(array_column($detail, 'cost'));
  304. $attr = $data['items'];
  305. unset($data['items'], $data['video'], $data['attrs']);
  306. if (count($data['cate_id']) < 1) return Json::fail('请选择产品分类');
  307. $cate_id = $data['cate_id'];
  308. $data['cate_id'] = implode(',', $data['cate_id']);
  309. if (!$data['store_name']) return Json::fail('请输入产品名称');
  310. if (count($data['image']) < 1) return Json::fail('请上传产品图片');
  311. if (count($data['slider_image']) < 1) return Json::fail('请上传产品轮播图');
  312. $data['image'] = $data['image'][0];
  313. $data['slider_image'] = json_encode($data['slider_image']);
  314. $data['stock'] = array_sum(array_column($detail, 'stock'));
  315. ProductModel::beginTrans();
  316. foreach ($detail as &$item) {
  317. if (($item['brokerage'] + $item['brokerage_two']) > $item['price']) {
  318. return Json::fail('一二级返佣相加不能大于商品售价');
  319. }
  320. }
  321. if ($id) {
  322. unset($data['sales']);
  323. ProductModel::edit($data, $id);
  324. $description = $data['description'];
  325. unset($data['description']);
  326. StoreDescription::saveDescription($description, $id);
  327. StoreProductCate::where('product_id', $id)->delete();
  328. $cateData = [];
  329. foreach ($cate_id as $cid) {
  330. $cateData[] = ['product_id' => $id, 'cate_id' => $cid, 'add_time' => time()];
  331. }
  332. StoreProductCate::insertAll($cateData);
  333. if ($data['spec_type'] == 0) {
  334. $attr = [
  335. [
  336. 'value' => '规格',
  337. 'detailValue' => '',
  338. 'attrHidden' => '',
  339. 'detail' => ['默认']
  340. ]
  341. ];
  342. $detail[0]['value1'] = '规格';
  343. $detail[0]['detail'] = ['规格' => '默认'];
  344. }
  345. $attr_res = StoreProductAttr::createProductAttr($attr, $detail, $id);
  346. if ($attr_res) {
  347. ProductModel::commitTrans();
  348. return Json::success('修改成功!');
  349. } else {
  350. ProductModel::rollbackTrans();
  351. return Json::fail(StoreProductAttr::getErrorInfo());
  352. }
  353. } else {
  354. $data['add_time'] = time();
  355. $data['code_path'] = '';
  356. $res = ProductModel::create($data);
  357. $description = $data['description'];
  358. StoreDescription::saveDescription($description, $res['id']);
  359. $cateData = [];
  360. foreach ($cate_id as $cid) {
  361. $cateData[] = ['product_id' => $res['id'], 'cate_id' => $cid, 'add_time' => time()];
  362. }
  363. StoreProductCate::insertAll($cateData);
  364. if ($data['spec_type'] == 0) {
  365. $attr = [
  366. [
  367. 'value' => '规格',
  368. 'detailValue' => '',
  369. 'attrHidden' => '',
  370. 'detail' => ['默认']
  371. ]
  372. ];
  373. $detail[0]['value1'] = '规格';
  374. $detail[0]['detail'] = ['规格' => '默认'];
  375. }
  376. $attr_res = StoreProductAttr::createProductAttr($attr, $detail, $res['id']);
  377. if ($attr_res) {
  378. ProductModel::commitTrans();
  379. return Json::success('添加产品成功!');
  380. } else {
  381. ProductModel::rollbackTrans();
  382. return Json::fail(StoreProductAttr::getErrorInfo());
  383. }
  384. }
  385. }
  386. public function attr_save()
  387. {
  388. $where = Util::getMore([
  389. ['bar_code', 20],
  390. ['in_stock', 0],
  391. ]);
  392. if ($where['in_stock'] < 1) return app('json')->fail('补货数量不能少于1');
  393. $info = StoreProductAttrValue::alias('a')->join("StoreProduct b", "a.product_id=b.id", "left")->where('a.bar_code', $where['bar_code'])->field('a.price,a.unique,a.stock as in_stock,a.product_id,b.store_name,b.image,a.bar_code')->find();
  394. if (!$info) return app('json')->fail('商品条形码不存在');
  395. $info = $info->toarray();
  396. $info['in_stock'] = $where['in_stock'];
  397. $info['admin_id'] = $this->adminId;
  398. if (SystemStockBill::order_create($info)) {
  399. return Json::successful('入库成功');
  400. } else {
  401. return Json::fail('入库失败');
  402. }
  403. }
  404. public function bill()
  405. {
  406. return $this->fetch();
  407. }
  408. public function bill_lst()
  409. {
  410. $where = Util::getMore([
  411. ['key', ''],
  412. ['page', 0],
  413. ['limit', 0],
  414. ['status', -2],
  415. ]);
  416. return Json::successlayui(SystemStockBill::lst($where));
  417. }
  418. public function audit($id)
  419. {
  420. if (!$id) return $this->failed('数据不存在');
  421. $Bill = SystemStockBill::where('id', $id)->find();
  422. if (!$Bill) return Json::fail('数据不存在!');
  423. $f = array();
  424. $f[] = Form::radio('status', '状态', $Bill->getData('status'))->options([['value' => 1, 'label' => '通过'], ['value' => -1, 'label' => '拒绝']]);
  425. $f[] = Form::textarea('refuse_msg', '备注');
  426. $form = Form::make_post_form('入库审核', $f, Url::buildUrl('audit_save', array('id' => $id)), 2);
  427. $this->assign(compact('form'));
  428. return $this->fetch('public/form-builder');
  429. }
  430. public function batch_audit($id)
  431. {
  432. if (!$id) return $this->failed('数据不存在');
  433. $f = array();
  434. $f[] = Form::radio('status', '状态')->options([['value' => 1, 'label' => '通过'], ['value' => -1, 'label' => '拒绝']]);
  435. $f[] = Form::textarea('refuse_msg', '备注');
  436. $form = Form::make_post_form('入库审核', $f, Url::buildUrl('batch_audit_save', array('id' => $id)), 2);
  437. $this->assign(compact('form'));
  438. return $this->fetch('public/form-builder');
  439. }
  440. public function audit_save($id)
  441. {
  442. $where = Util::postMore([
  443. ['refuse_msg', ''],
  444. ['status', 1],
  445. ]);
  446. if (!SystemStockBill::be(['id' => $id, 'status' => 0])) return $this->failed('数据不存在或已经审核');
  447. $where['audit_time'] = time();
  448. $where['audit_admin'] = $this->adminId;
  449. SystemStockBill::edit($where, $id);
  450. if ($where['status'] == 1) {
  451. $info = SystemStockBill::find($id)->toArray();
  452. $rs = StoreProductAttrValue::in_Stock($info);
  453. }
  454. return Json::successful('审核成功');
  455. }
  456. /**
  457. * 批量审核
  458. */
  459. public function batch_audit_save()
  460. {
  461. $where = Util::postMore([
  462. ['refuse_msg', ''],
  463. ['status', 1],
  464. ]);
  465. $ids = explode(',', input('id'));
  466. if ($where['status'] == 1) {
  467. foreach ($ids as $v) {
  468. $info = SystemStockBill::where('id', $v)->where('status', 0)->find();
  469. if ($info) {
  470. $rs = StoreProductAttrValue::in_Stock($info);
  471. }
  472. }
  473. }
  474. $where['audit_time'] = time();
  475. $where['audit_admin'] = $this->adminId;
  476. SystemStockBill::where('id', 'in', join(",", $ids))->update($where);
  477. return Json::successful('批量审核成功');
  478. }
  479. public function edit_content($id)
  480. {
  481. if (!$id) return $this->failed('数据不存在');
  482. $product = ProductModel::get($id);
  483. if (!$product) return Json::fail('数据不存在!');
  484. $this->assign([
  485. 'content' => $product->description,
  486. 'field' => 'description',
  487. 'action' => Url::buildUrl('change_field', ['id' => $id, 'field' => 'description'])
  488. ]);
  489. return $this->fetch('public/edit_content');
  490. }
  491. /**
  492. * 显示编辑资源表单页.
  493. *
  494. * @param int $id
  495. * @return \think\Response
  496. */
  497. public function edit($id)
  498. {
  499. if (!$id) return $this->failed('数据不存在');
  500. $product = ProductModel::get($id);
  501. if (!$product) return Json::fail('数据不存在!');
  502. $field = [
  503. Form::select('cate_id', '产品分类', explode(',', $product->getData('cate_id')))->setOptions(function () {
  504. $list = CategoryModel::getTierList(null, 1);
  505. $menus = [];
  506. foreach ($list as $menu) {
  507. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['cate_name'], 'disabled' => $menu['pid'] == 0];//,'disabled'=>$menu['pid']== 0];
  508. }
  509. return $menus;
  510. })->filterable(1)->multiple(1),
  511. Form::input('store_name', '产品名称', $product->getData('store_name')),
  512. Form::input('store_info', '产品简介', $product->getData('store_info'))->type('textarea'),
  513. Form::input('keyword', '产品关键字', $product->getData('keyword'))->placeholder('多个用英文状态下的逗号隔开'),
  514. Form::input('unit_name', '产品单位', $product->getData('unit_name'))->col(12),
  515. Form::input('bar_code', '产品条码', $product->getData('bar_code'))->col(12),
  516. Form::frameImageOne('image', '产品主图片(305*305px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), $product->getData('image'))->icon('image')->width('100%')->height('500px'),
  517. Form::frameImages('slider_image', '产品轮播图(640*640px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'slider_image')), json_decode($product->getData('slider_image'), 1) ?: [])->maxLength(5)->icon('images')->width('100%')->height('500px'),
  518. Form::number('price', '产品售价', $product->getData('price'))->min(0)->col(8),
  519. Form::number('ot_price', '产品市场价', $product->getData('ot_price'))->min(0)->col(8),
  520. Form::number('give_integral', '赠送积分', $product->getData('give_integral'))->min(0)->col(8),
  521. Form::number('postage', '邮费', $product->getData('postage'))->min(0)->col(8),
  522. Form::number('sales', '销量', $product->getData('sales'))->min(0)->precision(0)->col(8)->readonly(1),
  523. Form::number('ficti', '虚拟销量', $product->getData('ficti'))->min(0)->precision(0)->col(8),
  524. Form::number('stock', '库存', ProductModel::getStock($id) > 0 ? ProductModel::getStock($id) : $product->getData('stock'))->min(0)->precision(0)->col(8),
  525. Form::number('cost', '产品成本价', $product->getData('cost'))->min(0)->col(8),
  526. Form::number('sort', '排序', $product->getData('sort'))->col(8),
  527. Form::radio('is_show', '产品状态', $product->getData('is_show'))->options([['label' => '上架', 'value' => 1], ['label' => '下架', 'value' => 0]])->col(8),
  528. Form::radio('is_hot', '热卖单品', $product->getData('is_hot'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  529. Form::radio('is_benefit', '促销单品', $product->getData('is_benefit'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  530. Form::radio('is_best', '精品推荐', $product->getData('is_best'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  531. Form::radio('is_new', '首发新品', $product->getData('is_new'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  532. Form::radio('is_postage', '是否包邮', $product->getData('is_postage'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  533. Form::radio('is_good', '是否优品推荐', $product->getData('is_good'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  534. ];
  535. $form = Form::make_post_form('编辑产品', $field, Url::buildUrl('update', array('id' => $id)), 2);
  536. $this->assign(compact('form'));
  537. return $this->fetch('public/form-builder');
  538. }
  539. /**
  540. * 保存更新的资源
  541. *
  542. * @param $id
  543. */
  544. public function update($id)
  545. {
  546. $data = Util::postMore([
  547. ['cate_id', []],
  548. 'store_name',
  549. 'store_info',
  550. 'keyword',
  551. 'bar_code',
  552. ['unit_name', '件'],
  553. ['image', []],
  554. ['slider_image', []],
  555. ['postage', 0],
  556. ['ot_price', 0],
  557. ['price', 0],
  558. ['sort', 0],
  559. ['stock', 0],
  560. ['temp_id', 0],
  561. ['ficti', 100],
  562. ['give_integral', 0],
  563. ['is_show', 0],
  564. ['cost', 0],
  565. ['is_hot', 0],
  566. ['is_benefit', 0],
  567. ['is_best', 0],
  568. ['is_new', 0],
  569. ['mer_use', 0],
  570. ['is_postage', 0],
  571. ['is_good', 0],
  572. ]);
  573. if (count($data['cate_id']) < 1) return Json::fail('请选择产品分类');
  574. $cate_id = $data['cate_id'];
  575. $data['cate_id'] = implode(',', $data['cate_id']);
  576. if (!$data['store_name']) return Json::fail('请输入产品名称');
  577. if (count($data['image']) < 1) return Json::fail('请上传产品图片');
  578. if (count($data['slider_image']) < 1) return Json::fail('请上传产品轮播图');
  579. // if(count($data['slider_image'])>8) return Json::fail('轮播图最多5张图');
  580. if ($data['price'] == '' || $data['price'] < 0) return Json::fail('请输入产品售价');
  581. if ($data['ot_price'] == '' || $data['ot_price'] < 0) return Json::fail('请输入产品市场价');
  582. if ($data['stock'] == '' || $data['stock'] < 0) return Json::fail('请输入库存');
  583. $data['image'] = $data['image'][0];
  584. $data['slider_image'] = json_encode($data['slider_image']);
  585. ProductModel::edit($data, $id);
  586. StoreProductCate::where('product_id', $id)->delete();
  587. foreach ($cate_id as $cid) {
  588. StoreProductCate::insert(['product_id' => $id, 'cate_id' => $cid, 'add_time' => time()]);
  589. }
  590. return Json::successful('修改成功!');
  591. }
  592. public function attr($id)
  593. {
  594. if (!$id) return $this->failed('数据不存在!');
  595. // $result = StoreProductAttrResult::getResult($id);
  596. $result = StoreProductAttrValue::getStoreProductAttrResult($id);
  597. $image = ProductModel::where('id', $id)->value('image');
  598. $this->assign(compact('id', 'result', 'image'));
  599. return $this->fetch();
  600. }
  601. /**
  602. * 生成属性
  603. * @param int $id
  604. */
  605. public function is_format_attr($id = 0, $type = 0)
  606. {
  607. $data = Util::postMore([
  608. ['attrs', []],
  609. ['items', []]
  610. ]);
  611. $attr = $data['attrs'];
  612. $value = attr_format($attr)[1];
  613. $valueNew = [];
  614. $count = 0;
  615. foreach ($value as $key => $item) {
  616. $detail = $item['detail'];
  617. sort($item['detail'], SORT_STRING);
  618. $suk = implode(',', $item['detail']);
  619. $types = 1;
  620. if ($id) {
  621. $sukValue = StoreProductAttrValue::where('product_id', $id)->where('type', 0)->where('suk', $suk)->column('bar_code,cost,price,ot_price,stock,image as pic,weight,volume,brokerage,brokerage_two', 'suk');
  622. if (!count($sukValue)) {
  623. if ($type == 0) $types = 0; //编辑商品时,将没有规格的数据不生成默认值
  624. $sukValue[$suk]['pic'] = '';
  625. $sukValue[$suk]['price'] = 0;
  626. $sukValue[$suk]['cost'] = 0;
  627. $sukValue[$suk]['ot_price'] = 0;
  628. $sukValue[$suk]['stock'] = 0;
  629. $sukValue[$suk]['bar_code'] = '';
  630. $sukValue[$suk]['weight'] = 0;
  631. $sukValue[$suk]['volume'] = 0;
  632. $sukValue[$suk]['brokerage'] = 0;
  633. $sukValue[$suk]['brokerage_two'] = 0;
  634. }
  635. } else {
  636. $sukValue[$suk]['pic'] = '';
  637. $sukValue[$suk]['price'] = 0;
  638. $sukValue[$suk]['cost'] = 0;
  639. $sukValue[$suk]['ot_price'] = 0;
  640. $sukValue[$suk]['stock'] = 0;
  641. $sukValue[$suk]['bar_code'] = '';
  642. $sukValue[$suk]['weight'] = 0;
  643. $sukValue[$suk]['volume'] = 0;
  644. $sukValue[$suk]['brokerage'] = 0;
  645. $sukValue[$suk]['brokerage_two'] = 0;
  646. }
  647. if ($types) { //编辑商品时,将没有规格的数据不生成默认值
  648. foreach (array_keys($detail) as $k => $title) {
  649. $header[$k]['title'] = $title;
  650. $header[$k]['align'] = 'center';
  651. $header[$k]['minWidth'] = 130;
  652. }
  653. foreach (array_values($detail) as $k => $v) {
  654. $valueNew[$count]['value' . ($k + 1)] = $v;
  655. $header[$k]['key'] = 'value' . ($k + 1);
  656. }
  657. $valueNew[$count]['detail'] = $detail;
  658. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  659. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  660. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  661. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  662. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  663. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  664. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ?? 0;
  665. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ?? 0;
  666. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ?? 0;
  667. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ?? 0;
  668. $count++;
  669. }
  670. }
  671. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 80];
  672. $header[] = ['title' => '售价', 'slot' => 'price', 'align' => 'center', 'minWidth' => 120];
  673. $header[] = ['title' => '成本价', 'slot' => 'cost', 'align' => 'center', 'minWidth' => 140];
  674. $header[] = ['title' => '原价', 'slot' => 'ot_price', 'align' => 'center', 'minWidth' => 140];
  675. $header[] = ['title' => '库存', 'slot' => 'stock', 'align' => 'center', 'minWidth' => 140];
  676. $header[] = ['title' => '产品编号', 'slot' => 'bar_code', 'align' => 'center', 'minWidth' => 140];
  677. $header[] = ['title' => '重量(KG)', 'slot' => 'weight', 'align' => 'center', 'minWidth' => 140];
  678. $header[] = ['title' => '体积(m³)', 'slot' => 'volume', 'align' => 'center', 'minWidth' => 140];
  679. $header[] = ['title' => '操作', 'slot' => 'action', 'align' => 'center', 'minWidth' => 70];
  680. $info = ['attr' => $attr, 'value' => $valueNew, 'header' => $header];
  681. return Json::successful($info);
  682. }
  683. public function set_attr($id)
  684. {
  685. if (!$id) return $this->failed('产品不存在!');
  686. list($attr, $detail) = Util::postMore([
  687. ['items', []],
  688. ['attrs', []]
  689. ], null, true);
  690. $res = StoreProductAttr::createProductAttr($attr, $detail, $id);
  691. if ($res)
  692. return $this->successful('编辑属性成功!');
  693. else
  694. return $this->failed(StoreProductAttr::getErrorInfo());
  695. }
  696. public function clear_attr($id)
  697. {
  698. if (!$id) return $this->failed('产品不存在!');
  699. if (false !== StoreProductAttr::clearProductAttr($id) && false !== StoreProductAttrResult::clearResult($id))
  700. return $this->successful('清空产品属性成功!');
  701. else
  702. return $this->failed(StoreProductAttr::getErrorInfo('清空产品属性失败!'));
  703. }
  704. /**
  705. * 删除指定资源
  706. *
  707. * @param int $id
  708. * @return \think\Response
  709. */
  710. public function delete($id)
  711. {
  712. if (!$id) return $this->failed('数据不存在');
  713. if (!ProductModel::be(['id' => $id])) return $this->failed('产品数据不存在');
  714. if (ProductModel::be(['id' => $id, 'is_del' => 1])) {
  715. $data['is_del'] = 0;
  716. if (!ProductModel::edit($data, $id))
  717. return Json::fail(ProductModel::getErrorInfo('恢复失败,请稍候再试!'));
  718. else
  719. return Json::successful('成功恢复产品!');
  720. } else {
  721. $res1 = StoreSeckill::where('product_id', $id)->where('is_del', 0)->find();
  722. $res2 = StoreBargain::where('product_id', $id)->where('is_del', 0)->find();
  723. $res3 = StoreCombination::where('product_id', $id)->where('is_del', 0)->find();
  724. if ($res1 || $res2 || $res3) {
  725. return Json::fail(ProductModel::getErrorInfo('该商品已参加活动,无法删除!'));
  726. } else {
  727. $data['is_del'] = 1;
  728. if (!ProductModel::edit($data, $id))
  729. return Json::fail(ProductModel::getErrorInfo('删除失败,请稍候再试!'));
  730. else
  731. return Json::successful('成功移到回收站!');
  732. }
  733. }
  734. }
  735. /**
  736. * 点赞
  737. * @param $id
  738. * @return mixed|\think\response\Json|void
  739. */
  740. public function collect($id)
  741. {
  742. if (!$id) return $this->failed('数据不存在');
  743. $product = ProductModel::get($id);
  744. if (!$product) return Json::fail('数据不存在!');
  745. $this->assign(StoreProductRelation::getCollect($id));
  746. return $this->fetch();
  747. }
  748. /**
  749. * 收藏
  750. * @param $id
  751. * @return mixed|\think\response\Json|void
  752. */
  753. public function like($id)
  754. {
  755. if (!$id) return $this->failed('数据不存在');
  756. $product = ProductModel::get($id);
  757. if (!$product) return Json::fail('数据不存在!');
  758. $this->assign(StoreProductRelation::getLike($id));
  759. return $this->fetch();
  760. }
  761. /**
  762. * 修改产品价格
  763. */
  764. public function edit_product_price()
  765. {
  766. $data = Util::postMore([
  767. ['id', 0],
  768. ['price', 0],
  769. ]);
  770. if (!$data['id']) return Json::fail('参数错误');
  771. $res = ProductModel::edit(['price' => $data['price']], $data['id']);
  772. if ($res) return Json::successful('修改成功');
  773. else return Json::fail('修改失败');
  774. }
  775. /**
  776. * 修改产品库存
  777. *
  778. */
  779. public function edit_product_stock()
  780. {
  781. $data = Util::postMore([
  782. ['id', 0],
  783. ['stock', 0],
  784. ]);
  785. if (!$data['id']) return Json::fail('参数错误');
  786. $res = ProductModel::edit(['stock' => $data['stock']], $data['id']);
  787. if ($res) return Json::successful('修改成功');
  788. else return Json::fail('修改失败');
  789. }
  790. /**
  791. * 检测商品是否开活动
  792. * @param $id
  793. * @throws \think\db\exception\DataNotFoundException
  794. * @throws \think\db\exception\DbException
  795. * @throws \think\db\exception\ModelNotFoundException
  796. */
  797. public function check_activity($id)
  798. {
  799. if ($id != 0) {
  800. $res1 = StoreSeckill::where('product_id', $id)->where('is_del', 0)->find();
  801. $res2 = StoreBargain::where('product_id', $id)->where('is_del', 0)->find();
  802. $res3 = StoreCombination::where('product_id', $id)->where('is_del', 0)->find();
  803. if ($res1 || $res2 || $res3) {
  804. return Json::successful('该商品有活动开启,无法删除属性');
  805. } else {
  806. return Json::fail('删除成功');
  807. }
  808. } else {
  809. return Json::fail('没有参数ID');
  810. }
  811. }
  812. public function product_br()
  813. {
  814. $list = StoreProductAttrValue::where('bar_code', '<', '99999')->where('volume', '>', '0')->column('product_id,bar_code,price,volume');
  815. $str = "";
  816. foreach ($list as $v) {
  817. $title = ProductModel::where('id', $v['product_id'])->value('store_name');
  818. $str .= sprintf("PLU\t%d\t%s\t\t%s\t%s\t1\t1\t999\t\t\r\n", $v['volume'], $v['bar_code'], $title, bcadd($v['price'], 0, 2));
  819. }
  820. $str = $this->charsetToGBK($str);
  821. header("Content-type: text/plain; charset=gbk");
  822. header("Accept-Ranges: bytes");
  823. header("Content-Disposition: attachment; filename=NewFile.txt");
  824. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  825. header("Pragma: no-cache");
  826. header("Expires: 0");
  827. exit($str);
  828. }
  829. /**
  830. * 将非GBK字符集的编码转为GBK
  831. *
  832. * @param mixed $mixed 源数据
  833. *
  834. * @return mixed GBK格式数据
  835. */
  836. function charsetToGBK($mixed)
  837. {
  838. if (is_array($mixed)) {
  839. foreach ($mixed as $k => $v) {
  840. if (is_array($v)) {
  841. $mixed[$k] = charsetToGBK($v);
  842. } else {
  843. $encode = mb_detect_encoding($v, array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5'));
  844. if ($encode == 'UTF-8') {
  845. $mixed[$k] = iconv('UTF-8', 'GBK', $v);
  846. }
  847. }
  848. }
  849. } else {
  850. $encode = mb_detect_encoding($mixed, array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5'));
  851. //var_dump($encode);
  852. if ($encode == 'UTF-8') {
  853. $mixed = iconv('UTF-8', 'GBK', $mixed);
  854. }
  855. }
  856. return $mixed;
  857. }
  858. }