ProductController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. $respond = ProductClass::getList("*", $where, "stock asc");
  105. } else {
  106. $respond = ProductClass::getList("*", $where, "inTurn desc");
  107. }
  108. $respond['list'] = ProductClass::groupProductInfo($respond['list']);
  109. $respond['list'] = ProductClass::groupClassName($this->sjId, $respond['list']);
  110. util::success($respond);
  111. }
  112. if ($type == 'waring') {
  113. //库存预警 按库存从小到大排序
  114. $data = ProductClass::getAllByCondition($where, ['stock' => SORT_ASC, 'inTurn' => SORT_DESC], "*");
  115. } else {
  116. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  117. }
  118. $data = ProductClass::groupProductInfo($data);
  119. $data = ProductClass::groupClassName($this->sjId, $data);
  120. if ($type == 'waring') {
  121. //库存预警
  122. $newData = [];
  123. foreach ($data as $v) {
  124. if ($v['stock'] <= $v['stockWarning']) {
  125. $newData[] = $v;
  126. }
  127. }
  128. util::success(['list' => $newData]);
  129. }
  130. util::success(['list' => $data]);
  131. }
  132. //批量改价
  133. public function actionBatchChangePrice()
  134. {
  135. //花材价格只能在首店修改,并同步到分店
  136. $shop = $this->shop;
  137. $default = $shop->default ?? 0;
  138. if ($default != 1) {
  139. util::fail('请在首店修改');
  140. }
  141. $shopAdmin = $this->shopAdmin;
  142. $roleId = $shopAdmin['roleId'] ?? 0;
  143. $role = AdminRoleClass::getById($roleId);
  144. $roleName = $role['roleName'] ?? '';
  145. if ($roleName == '员工') {
  146. util::fail('没有权限操作哦');
  147. }
  148. $data = Yii::$app->request->post('data', '');
  149. if (empty($data)) {
  150. util::fail('没有修改');
  151. }
  152. $arr = json_decode($data, true);
  153. if (empty($arr)) {
  154. util::fail('没有修改!');
  155. }
  156. $connection = Yii::$app->db;
  157. $transaction = $connection->beginTransaction();
  158. try {
  159. $ids = array_column($arr, 'id');
  160. $ids = array_unique(array_filter($ids));
  161. if (empty($ids)) {
  162. util::fail('请选择花材');
  163. }
  164. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  165. foreach ($arr as $product) {
  166. if (isset($product['price']) == false) {
  167. util::fail('没有价格');
  168. }
  169. if (isset($product['id']) == false) {
  170. util::fail('没有花材');
  171. }
  172. $price = $product['price'];
  173. $productId = $product['id'];
  174. $product = $productList[$productId] ?? [];
  175. if (empty($product)) {
  176. util::fail('没有找到花材');
  177. }
  178. if (isset($product->shopId) == false || $product->shopId != $this->shopId) {
  179. util::fail('没有权限访问');
  180. }
  181. if (empty($price)) {
  182. //恢复自动调价
  183. $price = bcadd($product->cost, $product->addPrice, 2);
  184. ProductClass::changePrice($product, $price, ProductClass::PRICE_LABEL_AUTO);
  185. } else {
  186. //改价
  187. ProductClass::changePrice($product, $price, ProductClass::PRICE_LABEL_CHANGE);
  188. }
  189. }
  190. $transaction->commit();
  191. util::complete('修改成功');
  192. } catch (\Exception $e) {
  193. $transaction->rollBack();
  194. Yii::info("修改失败原因:" . $e->getMessage());
  195. util::fail('修改失败');
  196. }
  197. }
  198. //修改加价
  199. public function actionChangeAddPrice()
  200. {
  201. $productId = Yii::$app->request->post('productId', 0);
  202. $addPrice = Yii::$app->request->post('addPrice', '');
  203. if ($addPrice < 0) {
  204. util::fail("加价不能小于0");
  205. }
  206. $productData = ProductClass::getProductData($productId, $this->shopId);
  207. if (!$productData) {
  208. util::fail("花材不存在");
  209. }
  210. ProductClass::changeAddPrice($productId, $addPrice);
  211. util::complete();
  212. }
  213. //批量修改product 排序,成本价,加价
  214. public function actionBatchUpdate()
  215. {
  216. $shop = $this->shop;
  217. $default = $shop->default ?? 0;
  218. if ($default != 1) {
  219. util::fail('请在首店修改');
  220. }
  221. $shopAdmin = $this->shopAdmin;
  222. $roleId = $shopAdmin['roleId'] ?? 0;
  223. $role = AdminRoleClass::getById($roleId);
  224. $roleName = $role['roleName'] ?? '';
  225. if ($roleName == '员工') {
  226. util::fail('没有权限操作哦');
  227. }
  228. $data = Yii::$app->request->post('data', ''); //data = [{id:1,inTurn:10,addPrice:2,cost:10}]
  229. $type = Yii::$app->request->post('type', '');
  230. if (empty($data) || empty($type)) {
  231. util::fail("参数错误");
  232. }
  233. if (in_array($type, ['inTurn', 'addPrice', 'cost', 'weight', 'ratio', 'ratio', 'price', 'stockWarning', 'shelfLife'])) {
  234. $data = json_decode($data, true);
  235. } else {
  236. util::fail('非法操作');
  237. }
  238. foreach ($data as $v) {
  239. if ($type == 'status') {
  240. $val = $v[$type] ?? '';
  241. if (!in_array($val, [1, 2])) {
  242. util::fail("参数错误");
  243. }
  244. }
  245. }
  246. foreach ($data as $v) {
  247. $id = $v['id'];
  248. $val = $v[$type] ?? '';
  249. //shish 20210703
  250. if ($type == 'ratio') {
  251. if (preg_match("/^[1-9][0-9]*$/", $val) == false || $val <= 0) {
  252. util::fail('单位比请填写整数,并且要大于0');
  253. }
  254. }
  255. //修改售价 shish 20210703
  256. if ($type == 'price') {
  257. $product = ProductClass::getLockById($id);
  258. if (empty($product)) {
  259. continue;
  260. }
  261. $currentItemId = $product->itemId ?? 0;
  262. if (empty($currentItemId)) {
  263. continue;
  264. }
  265. //留空恢复自动调价
  266. //所有门店同步调整
  267. if (empty($val)) {
  268. $currentPrice = bcadd($product->cost, $product->addPrice, 2);
  269. $upData = ['priceLabel' => ProductClass::PRICE_LABEL_AUTO, 'price' => $currentPrice];
  270. } else {
  271. $upData = ['priceLabel' => ProductClass::PRICE_LABEL_CHANGE, 'price' => $val];
  272. }
  273. $shopAll = ShopClass::getAllShop($this->sjId);
  274. if (!empty($shopAll)) {
  275. foreach ($shopAll as $currentShop) {
  276. $currentShopId = $currentShop['id'] ?? 0;
  277. if (empty($currentShopId)) {
  278. continue;
  279. }
  280. ProductClass::updateByCondition(['shopId' => $currentShopId, 'itemId' => $currentItemId], $upData);
  281. }
  282. }
  283. continue;
  284. }
  285. $where = [];
  286. $where['id'] = $id;
  287. $where['shopId'] = $this->shopId;
  288. $where['sjId'] = $this->sjId;
  289. if (empty($val)) {
  290. util::fail("参数错误!");
  291. }
  292. $data = [
  293. $type => $val
  294. ];
  295. ProductClass::updateByCondition($where, $data);
  296. ProductClass::autoPriceById($id);
  297. //2021.7.6 linqh 如果是首店,则更新全部分店
  298. ProductClass::updateSyncProduct($id, $this->shopId, $data);
  299. }
  300. util::complete();
  301. }
  302. public function actionUpdate()
  303. {
  304. $post = Yii::$app->request->post();
  305. $shop = $this->shop;
  306. $default = $shop->default ?? 0;
  307. if ($default != 1) {
  308. //除了上下架其它只能在首店修改
  309. if (isset($post['status']) == false) {
  310. util::fail('请在首店修改');
  311. }
  312. }
  313. $shopAdmin = $this->shopAdmin;
  314. $roleId = $shopAdmin['roleId'] ?? 0;
  315. $role = AdminRoleClass::getById($roleId);
  316. $roleName = $role['roleName'] ?? '';
  317. if ($roleName == '员工') {
  318. util::fail('没有权限操作哦');
  319. }
  320. $post['adminId'] = $this->adminId;
  321. ProductClass::updateProduct($post);
  322. util::complete();
  323. }
  324. //上下架状态控制 shish 20210713
  325. public function actionStatusUpdate()
  326. {
  327. $post = Yii::$app->request->post();
  328. $shopAdmin = $this->shopAdmin;
  329. $roleId = $shopAdmin['roleId'] ?? 0;
  330. $role = AdminRoleClass::getById($roleId);
  331. $roleName = $role['roleName'] ?? '';
  332. if ($roleName == '员工') {
  333. util::fail('没有权限操作哦');
  334. }
  335. $id = $post['id'] ?? 0;
  336. $status = $post['status'] ?? 1;
  337. $product = ProductClass::getById($id, true);
  338. if ($product->shopId != $this->shopId) {
  339. util::fail('没有权限操作');
  340. }
  341. $product->status = $status;
  342. $product->save();
  343. util::complete();
  344. }
  345. //详情 ruidian 2021.5.3
  346. public function actionDetail()
  347. {
  348. $id = Yii::$app->request->get('id', 0);
  349. $respond = ProductClass::getItemInfo($id);
  350. util::success($respond);
  351. }
  352. //新增花材 2021.6.21
  353. public function actionAdd()
  354. {
  355. $post = Yii::$app->request->post();
  356. $shop = $this->shop;
  357. $default = $shop->default ?? 0;
  358. if ($default != 1) {
  359. util::fail('请在首店添加');
  360. }
  361. $shopAdmin = $this->shopAdmin;
  362. $roleId = $shopAdmin['roleId'] ?? 0;
  363. $role = AdminRoleClass::getById($roleId);
  364. $roleName = $role['roleName'] ?? '';
  365. if ($roleName == '员工') {
  366. util::fail('没有权限操作哦');
  367. }
  368. $post['sjId'] = $this->sjId;
  369. $post['adminId'] = $this->adminId;
  370. $post['shopId'] = $this->shopId;
  371. ProductClass::addProduct($post);
  372. util::complete('添加成功');
  373. }
  374. //获取拼音缩写 shish 20210707
  375. public function actionPy()
  376. {
  377. $name = Yii::$app->request->get('name');
  378. $py = stringUtil::py($name);
  379. util::success(['py' => $py]);
  380. }
  381. //复制花材
  382. public function actionCopy()
  383. {
  384. $id = Yii::$app->request->get('id');
  385. $productData = ProductClass::getById($id);
  386. if (!$productData) {
  387. util::fail("花材不存在");
  388. }
  389. if ($productData['shopId'] != $this->shopId) {
  390. util::fail("无效花材");
  391. }
  392. //去除无用的字段
  393. unset($productData['stock']); //库存去掉
  394. unset($productData['viewNum']);
  395. unset($productData['actualSold']);
  396. unset($productData['itemId']);
  397. unset($productData['id']);
  398. unset($productData['delStatus']);
  399. $productData['copy'] = 1; //复制标识
  400. ProductClass::addProduct($productData);
  401. util::complete();
  402. }
  403. }