StoreProduct.php 37 KB

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