CpItemController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. $search = $get['search'] ?? '';
  17. $mainId = $this->mainId ?? 0;
  18. $where = [];
  19. $where['mainId'] = $mainId;
  20. if (!empty($cpId)) {
  21. $where['cpId'] = $cpId;
  22. }
  23. if (!empty($search)) {
  24. if (stringUtil::isLetter($search)) {
  25. $where['py'] = ['like', $search];
  26. } else {
  27. $where['name'] = ['like', $search];
  28. }
  29. }
  30. $respond = CpItemClass::getItemList($where);
  31. util::success($respond);
  32. }
  33. //删除关系 ssh 20240115
  34. public function actionDel()
  35. {
  36. $get = Yii::$app->request->get();
  37. $id = $get['id'] ?? 0;
  38. $item = CpItemClass::getById($id, true);
  39. if (empty($item)) {
  40. util::fail('没有找到');
  41. }
  42. if ($item->mainId != $this->mainId) {
  43. util::fail('不是你的产品');
  44. }
  45. $productId = $item->productId ?? 0;
  46. $count = CpItemClass::getCount(['productId' => $productId]);
  47. if ($count <= 1) {
  48. util::fail('最少要保留一个');
  49. }
  50. $item->delete();
  51. util::complete('删除成功');
  52. }
  53. //添加关系 ssh 20240115
  54. public function actionAdd()
  55. {
  56. $get = Yii::$app->request->get();
  57. $cpId = $get['cpId'] ?? 0;
  58. $productId = $get['productId'] ?? 0;
  59. $cp = CpClass::getById($cpId, true);
  60. if (empty($cp)) {
  61. util::fail('没有找到产品');
  62. }
  63. if ($cp->mainId != $this->mainId) {
  64. util::fail('不是你的产品');
  65. }
  66. $ptCpId = $cp->ptCpId ?? 0;
  67. $product = ProductClass::getById($productId, true);
  68. if (empty($product)) {
  69. util::fail('没有找到花材');
  70. }
  71. if ($product->mainId != $this->mainId) {
  72. util::fail('不是你的花材');
  73. }
  74. $ptItemId = $product->itemId ?? 0;
  75. $relate = CpItemClass::getByCondition(['cpId' => $cpId, 'productId' => $productId], true);
  76. if (!empty($relate)) {
  77. util::fail('已经添加了');
  78. }
  79. $productName = $product->name ?? '';
  80. $py = stringUtil::py($productName);
  81. $data = [
  82. 'cpId' => $cpId,
  83. 'ptCpId' => $ptCpId,
  84. 'ptItemId' => $ptItemId,
  85. 'productId' => $productId,
  86. 'name' => $productName,
  87. 'py' => $py,
  88. 'mainId' => $this->mainId
  89. ];
  90. $respond = CpItemClass::add($data, true);
  91. if (empty($respond)) {
  92. util::fail('添加失败');
  93. }
  94. util::complete('添加成功');
  95. }
  96. }