ProductController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. <?php
  2. /**
  3. * User: admin
  4. * Date Time: 2021/1/15 20:36
  5. */
  6. namespace ghs\controllers;
  7. use biz\admin\classes\AdminRoleClass;
  8. use bizGhs\item\classes\ItemClassClass;
  9. use bizGhs\merchant\classes\ShopClass;
  10. use bizGhs\product\classes\ProductClass;
  11. use bizGhs\product\services\ProductService;
  12. use common\components\stringUtil;
  13. use common\components\util;
  14. use Yii;
  15. class ProductController extends BaseController
  16. {
  17. public $guestAccess = ['index'];
  18. //开单时获取所有的分类花材信息
  19. //库存预警
  20. public function actionIndex()
  21. {
  22. $py = Yii::$app->request->get('py', '');
  23. $type = Yii::$app->request->get('type', '');
  24. //默认只显示上架的商品
  25. $status = Yii::$app->request->get('status', 1);
  26. //获取所有当前供应商的分类 (全部、常用)
  27. //根据分类组装分类下的花材信息
  28. $classIds = ItemClassClass::getGhsItemClassAll($this->sjId);
  29. $where['sjId'] = $this->sjId;
  30. if ($py) {
  31. $pyName = strtolower($py);
  32. $where['py'] = $pyName;
  33. }
  34. $shopId = $this->shopId;
  35. //兼容客户根据门店下单
  36. if ($this->lookShopId) {
  37. $shopId = $this->lookShopId;
  38. }
  39. //库存预警
  40. if ($type == 'waring') {
  41. //shopId 改为可以前端传,默认是当前shopId linqh 2021.1.26
  42. $shopId = Yii::$app->request->get('shopId', 0);
  43. if (!$shopId) {
  44. $shopId = $this->shopId;
  45. }
  46. }
  47. $where['shopId'] = $shopId;
  48. if (!empty($status)) {
  49. $where['status'] = $status;
  50. }
  51. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
  52. $itemInfoData = ProductClass::groupProductInfo($itemInfoData);
  53. //常用的itemIds
  54. $oftenItemIds = [];
  55. //$oftenItemIds = ProductClass::getOftenItemIds($shopId);
  56. //获取供应商下所有花材ID (itemIds)
  57. $itemIdsInfo = ProductClass::getGhsItemIds($this->sjId);
  58. //dd($itemInfoData);
  59. //组装数据: [{classId:'','className:'',child:[{itemInfoData}]}]
  60. $data = ProductService::assembleData($classIds, $itemIdsInfo, $itemInfoData, $oftenItemIds, $type);
  61. util::success($data);
  62. }
  63. //当前门店所有花材
  64. public function actionList()
  65. {
  66. $where = [];
  67. $where['sjId'] = $this->sjId;
  68. $classId = Yii::$app->request->get('classId', 0);
  69. if (!empty($classId)) {
  70. $where['classId'] = $classId;
  71. }
  72. $name = Yii::$app->request->get('name', '');
  73. if (empty($name)) {
  74. $py = Yii::$app->request->get('py', '');
  75. $name = !empty($py) ? $py : '';
  76. }
  77. if (!empty($name)) {
  78. if (stringUtil::isLetter($name)) {
  79. $where['py'] = ['like', $name];
  80. } else {
  81. $where['name'] = ['like', $name];
  82. }
  83. }
  84. $status = Yii::$app->request->get('status', 0);
  85. if (!empty($status)) {
  86. $where['status'] = $status;
  87. }
  88. $shopId = Yii::$app->request->get('shopId', 0);
  89. if (!$shopId) {
  90. $shopId = $this->shopId;
  91. }
  92. $where['shopId'] = $shopId;
  93. $productIds = Yii::$app->request->get('ids', '');
  94. if (!empty($productIds)) {
  95. $productIdArr = explode(',', $productIds);
  96. if (!empty($productIdArr)) {
  97. $where['id'] = ['in', $productIdArr];
  98. }
  99. }
  100. $type = Yii::$app->request->get('type', '');
  101. $showPage = Yii::$app->request->get('showPage', 0);
  102. if ($showPage) {
  103. if ($type == 'waring') {
  104. Yii::$app->params['pageSize'] = 1000;
  105. $respond = ProductClass::getList("*", $where, "stock asc");
  106. } else {
  107. $respond = ProductClass::getList("*", $where, "inTurn desc");
  108. }
  109. $respond['list'] = ProductClass::groupProductInfo($respond['list']);
  110. $respond['list'] = ProductClass::groupClassName($this->sjId, $respond['list']);
  111. util::success($respond);
  112. }
  113. if ($type == 'waring') {
  114. //库存预警 按库存从小到大排序
  115. $data = ProductClass::getAllByCondition($where, ['stock' => SORT_ASC, 'inTurn' => SORT_DESC], "*");
  116. } else {
  117. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  118. }
  119. $data = ProductClass::groupProductInfo($data);
  120. $data = ProductClass::groupClassName($this->sjId, $data);
  121. if ($type == 'waring') {
  122. //库存预警
  123. $newData = [];
  124. foreach ($data as $v) {
  125. if ($v['stock'] <= $v['stockWarning']) {
  126. $newData[] = $v;
  127. }
  128. }
  129. util::success(['list' => $newData]);
  130. }
  131. util::success(['list' => $data]);
  132. }
  133. //批量改价
  134. public function actionBatchChangePrice()
  135. {
  136. //花材价格只能在首店修改,并同步到分店
  137. $shop = $this->shop;
  138. $default = $shop->default ?? 0;
  139. if ($default != 1) {
  140. util::fail('请在首店修改');
  141. }
  142. $shopAdmin = $this->shopAdmin;
  143. $roleId = $shopAdmin['roleId'] ?? 0;
  144. $role = AdminRoleClass::getById($roleId);
  145. $roleName = $role['roleName'] ?? '';
  146. if ($roleName == '员工') {
  147. util::fail('没有权限操作哦');
  148. }
  149. $data = Yii::$app->request->post('data', '');
  150. if (empty($data)) {
  151. util::fail('没有修改');
  152. }
  153. $arr = json_decode($data, true);
  154. if (empty($arr)) {
  155. util::fail('没有修改!');
  156. }
  157. $connection = Yii::$app->db;
  158. $transaction = $connection->beginTransaction();
  159. try {
  160. $ids = array_column($arr, 'id');
  161. $ids = array_unique(array_filter($ids));
  162. if (empty($ids)) {
  163. util::fail('请选择花材');
  164. }
  165. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  166. foreach ($arr as $product) {
  167. if (isset($product['price']) == false) {
  168. util::fail('没有价格');
  169. }
  170. if (isset($product['id']) == false) {
  171. util::fail('没有花材');
  172. }
  173. $price = $product['price'];
  174. $productId = $product['id'];
  175. $product = $productList[$productId] ?? [];
  176. if (empty($product)) {
  177. util::fail('没有找到花材');
  178. }
  179. if (isset($product->shopId) == false || $product->shopId != $this->shopId) {
  180. util::fail('没有权限访问');
  181. }
  182. if (empty($price)) {
  183. //恢复自动调价
  184. $price = bcadd($product->cost, $product->addPrice, 2);
  185. ProductClass::changePrice($product, $price, ProductClass::PRICE_LABEL_AUTO);
  186. } else {
  187. //改价
  188. ProductClass::changePrice($product, $price, ProductClass::PRICE_LABEL_CHANGE);
  189. }
  190. }
  191. $transaction->commit();
  192. util::complete('修改成功');
  193. } catch (\Exception $e) {
  194. $transaction->rollBack();
  195. Yii::info("修改失败原因:" . $e->getMessage());
  196. util::fail('修改失败');
  197. }
  198. }
  199. //修改加价
  200. public function actionChangeAddPrice()
  201. {
  202. $productId = Yii::$app->request->post('productId', 0);
  203. $addPrice = Yii::$app->request->post('addPrice', '');
  204. if ($addPrice < 0) {
  205. util::fail("加价不能小于0");
  206. }
  207. $productData = ProductClass::getProductData($productId, $this->shopId);
  208. if (!$productData) {
  209. util::fail("花材不存在");
  210. }
  211. ProductClass::changeAddPrice($productId, $addPrice);
  212. util::complete();
  213. }
  214. //批量修改product 排序,成本价,加价
  215. public function actionBatchUpdate()
  216. {
  217. $shop = $this->shop;
  218. $default = $shop->default ?? 0;
  219. if ($default != 1) {
  220. util::fail('请在首店修改');
  221. }
  222. $shopAdmin = $this->shopAdmin;
  223. $roleId = $shopAdmin['roleId'] ?? 0;
  224. $role = AdminRoleClass::getById($roleId);
  225. $roleName = $role['roleName'] ?? '';
  226. if ($roleName == '员工') {
  227. util::fail('没有权限操作哦');
  228. }
  229. $data = Yii::$app->request->post('data', ''); //data = [{id:1,inTurn:10,addPrice:2,cost:10}]
  230. $type = Yii::$app->request->post('type', '');
  231. if (empty($data) || empty($type)) {
  232. util::fail("参数错误");
  233. }
  234. if (in_array($type, ['inTurn', 'addPrice', 'cost', 'weight', 'ratio', 'ratio', 'price', 'stockWarning', 'shelfLife'])) {
  235. $data = json_decode($data, true);
  236. } else {
  237. util::fail('非法操作');
  238. }
  239. foreach ($data as $v) {
  240. if ($type == 'status') {
  241. $val = $v[$type] ?? '';
  242. if (!in_array($val, [1, 2])) {
  243. util::fail("参数错误");
  244. }
  245. }
  246. }
  247. foreach ($data as $v) {
  248. $id = $v['id'];
  249. $val = $v[$type] ?? '';
  250. //shish 20210703
  251. if ($type == 'ratio') {
  252. if (preg_match("/^[1-9][0-9]*$/", $val) == false || $val <= 0) {
  253. util::fail('单位比请填写整数,并且要大于0');
  254. }
  255. }
  256. //修改售价 shish 20210703
  257. if ($type == 'price') {
  258. $product = ProductClass::getLockById($id);
  259. if (empty($product)) {
  260. continue;
  261. }
  262. $currentItemId = $product->itemId ?? 0;
  263. if (empty($currentItemId)) {
  264. continue;
  265. }
  266. //留空恢复自动调价
  267. //所有门店同步调整
  268. if (empty($val)) {
  269. $currentPrice = bcadd($product->cost, $product->addPrice, 2);
  270. $upData = ['priceLabel' => ProductClass::PRICE_LABEL_AUTO, 'price' => $currentPrice];
  271. } else {
  272. $upData = ['priceLabel' => ProductClass::PRICE_LABEL_CHANGE, 'price' => $val];
  273. }
  274. $shopAll = ShopClass::getAllShop($this->sjId);
  275. if (!empty($shopAll)) {
  276. foreach ($shopAll as $currentShop) {
  277. $currentShopId = $currentShop['id'] ?? 0;
  278. if (empty($currentShopId)) {
  279. continue;
  280. }
  281. ProductClass::updateByCondition(['shopId' => $currentShopId, 'itemId' => $currentItemId], $upData);
  282. }
  283. }
  284. continue;
  285. }
  286. $where = [];
  287. $where['id'] = $id;
  288. $where['shopId'] = $this->shopId;
  289. $where['sjId'] = $this->sjId;
  290. if (empty($val)) {
  291. util::fail("参数错误!");
  292. }
  293. $data = [
  294. $type => $val
  295. ];
  296. ProductClass::updateByCondition($where, $data);
  297. ProductClass::autoPriceById($id);
  298. //2021.7.6 linqh 如果是首店,则更新全部分店
  299. ProductClass::updateSyncProduct($id, $this->shopId, $data);
  300. }
  301. util::complete();
  302. }
  303. public function actionUpdate()
  304. {
  305. $post = Yii::$app->request->post();
  306. $shop = $this->shop;
  307. $default = $shop->default ?? 0;
  308. if ($default != 1) {
  309. //除了上下架其它只能在首店修改
  310. if (isset($post['status']) == false) {
  311. util::fail('请在首店修改');
  312. }
  313. }
  314. $shopAdmin = $this->shopAdmin;
  315. $roleId = $shopAdmin['roleId'] ?? 0;
  316. $role = AdminRoleClass::getById($roleId);
  317. $roleName = $role['roleName'] ?? '';
  318. if ($roleName == '员工') {
  319. util::fail('没有权限操作哦');
  320. }
  321. $post['adminId'] = $this->adminId;
  322. ProductClass::updateProduct($post);
  323. util::complete();
  324. }
  325. //上下架状态控制 shish 20210713
  326. public function actionStatusUpdate()
  327. {
  328. $post = Yii::$app->request->post();
  329. $shopAdmin = $this->shopAdmin;
  330. $roleId = $shopAdmin['roleId'] ?? 0;
  331. $role = AdminRoleClass::getById($roleId);
  332. $roleName = $role['roleName'] ?? '';
  333. if ($roleName == '员工') {
  334. util::fail('没有权限操作哦');
  335. }
  336. $id = $post['id'] ?? 0;
  337. $status = $post['status'] ?? 1;
  338. $product = ProductClass::getById($id, true);
  339. if ($product->shopId != $this->shopId) {
  340. util::fail('没有权限操作');
  341. }
  342. $product->status = $status;
  343. $product->save();
  344. util::complete();
  345. }
  346. //详情 ruidian 2021.5.3
  347. public function actionDetail()
  348. {
  349. $id = Yii::$app->request->get('id', 0);
  350. $respond = ProductClass::getItemInfo($id);
  351. util::success($respond);
  352. }
  353. //新增花材 2021.6.21
  354. public function actionAdd()
  355. {
  356. $post = Yii::$app->request->post();
  357. $shop = $this->shop;
  358. $default = $shop->default ?? 0;
  359. if ($default != 1) {
  360. util::fail('请在首店添加');
  361. }
  362. $shopAdmin = $this->shopAdmin;
  363. $roleId = $shopAdmin['roleId'] ?? 0;
  364. $role = AdminRoleClass::getById($roleId);
  365. $roleName = $role['roleName'] ?? '';
  366. if ($roleName == '员工') {
  367. util::fail('没有权限操作哦');
  368. }
  369. $post['sjId'] = $this->sjId;
  370. $post['adminId'] = $this->adminId;
  371. $post['shopId'] = $this->shopId;
  372. ProductClass::addProduct($post);
  373. util::complete('添加成功');
  374. }
  375. //获取拼音缩写 shish 20210707
  376. public function actionPy()
  377. {
  378. $name = Yii::$app->request->get('name');
  379. $py = stringUtil::py($name);
  380. util::success(['py' => $py]);
  381. }
  382. //复制花材
  383. public function actionCopy()
  384. {
  385. $id = Yii::$app->request->get('id');
  386. $productData = ProductClass::getById($id);
  387. if (!$productData) {
  388. util::fail("花材不存在");
  389. }
  390. if ($productData['shopId'] != $this->shopId) {
  391. util::fail("无效花材");
  392. }
  393. //去除无用的字段
  394. unset($productData['stock']); //库存去掉
  395. unset($productData['viewNum']);
  396. unset($productData['actualSold']);
  397. unset($productData['itemId']);
  398. unset($productData['id']);
  399. unset($productData['delStatus']);
  400. $productData['copy'] = 1; //复制标识
  401. ProductClass::addProduct($productData);
  402. util::complete();
  403. }
  404. }