CpItemController.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\cp\classes\CpClass;
  4. use bizGhs\cp\classes\CpItemClass;
  5. use bizGhs\product\classes\ProductClass;
  6. use common\components\stringUtil;
  7. use common\components\util;
  8. use Yii;
  9. class CpItemController extends BaseController
  10. {
  11. //根据集合id取花材,带分页 ssh 20231228
  12. public function actionList()
  13. {
  14. $get = Yii::$app->request->get();
  15. $cpId = $get['cpId'] ?? 0;
  16. $mainId = $this->mainId ?? 0;
  17. $where = [];
  18. $where['mainId'] = $mainId;
  19. if (!empty($cpId)) {
  20. $where['cpId'] = $cpId;
  21. }
  22. $respond = CpItemClass::getItemList($where);
  23. util::success($respond);
  24. }
  25. //删除关系 ssh 20240115
  26. public function actionDel()
  27. {
  28. $get = Yii::$app->request->get();
  29. $id = $get['id'] ?? 0;
  30. $item = CpItemClass::getById($id, true);
  31. if (empty($item)) {
  32. util::fail('没有找到');
  33. }
  34. if ($item->mainId != $this->mainId) {
  35. util::fail('不是你的产品');
  36. }
  37. $productId = $item->productId ?? 0;
  38. $count = CpItemClass::getCount(['productId' => $productId]);
  39. if ($count <= 1) {
  40. util::fail('最少要保留一个');
  41. }
  42. $item->delete();
  43. util::complete('删除成功');
  44. }
  45. //添加关系 ssh 20240115
  46. public function actionAdd()
  47. {
  48. $get = Yii::$app->request->get();
  49. $cpId = $get['cpId'] ?? 0;
  50. $productId = $get['productId'] ?? 0;
  51. $cp = CpClass::getById($cpId, true);
  52. if (empty($cp)) {
  53. util::fail('没有找到产品');
  54. }
  55. if ($cp->mainId != $this->mainId) {
  56. util::fail('不是你的产品');
  57. }
  58. $product = ProductClass::getById($productId, true);
  59. if (empty($product)) {
  60. util::fail('没有找到花材');
  61. }
  62. if ($product->mainId != $this->mainId) {
  63. util::fail('不是你的花材');
  64. }
  65. $relate = CpItemClass::getByCondition(['cpId' => $cpId, 'productId' => $productId], true);
  66. if (!empty($relate)) {
  67. util::fail('已经添加了');
  68. }
  69. $productName = $product->name ?? '';
  70. $py = stringUtil::py($productName);
  71. $data = ['cpId' => $cpId, 'productId' => $productId, 'name' => $productName, 'py' => $py, 'mainId' => $this->mainId];
  72. $respond = CpItemClass::add($data, true);
  73. if (empty($respond)) {
  74. util::fail('添加失败');
  75. }
  76. util::complete('添加成功');
  77. }
  78. }