CpItemController.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\cp\classes\CpItemClass;
  4. use bizGhs\Cp\models\CpClass;
  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. $jhId = $get['jhId'] ?? 0;
  16. $mainId = $this->mainId ?? 0;
  17. $where = [];
  18. $where['mainId'] = $mainId;
  19. if (!empty($jhId)) {
  20. $where['jhId'] = $jhId;
  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. $item->delete();
  38. util::complete('删除成功');
  39. }
  40. //添加关系 ssh 20240115
  41. public function actionAdd()
  42. {
  43. $get = Yii::$app->request->get();
  44. $cpId = $get['cpId'] ?? 0;
  45. $productId = $get['productId'] ?? 0;
  46. $cp = CpClass::getById($cpId, true);
  47. if (empty($cp)) {
  48. util::fail('没有找到产品');
  49. }
  50. if ($cp->mainId != $this->mainId) {
  51. util::fail('不是你的产品');
  52. }
  53. $product = ProductClass::getById($productId, true);
  54. if (empty($product)) {
  55. util::fail('没有找到花材');
  56. }
  57. if ($product->mainId != $this->mainId) {
  58. util::fail('不是你的花材');
  59. }
  60. $relate = CpItemClass::getByCondition(['cpId' => $cpId, 'productId' => $productId], true);
  61. if (!empty($relate)) {
  62. util::fail('已经添加了');
  63. }
  64. $productName = $product->name ?? '';
  65. $py = stringUtil::py($productName);
  66. $data = ['cpId' => $cpId, 'productId' => $productId, 'name' => $productName, 'py' => $py, 'mainId' => $this->mainId];
  67. $respond = CpItemClass::add($data, true);
  68. if (empty($respond)) {
  69. util::fail('添加失败');
  70. }
  71. util::complete('添加成功');
  72. }
  73. }