ProductController.php 15 KB

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