Spec.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace app\store\controller\goods;
  3. use app\store\controller\Controller;
  4. use app\store\model\Spec as SpecModel;
  5. use app\store\model\SpecValue as SpecValueModel;
  6. /**
  7. * 商品规格控制器
  8. * Class Spec
  9. * @package app\store\controller
  10. */
  11. class Spec extends Controller
  12. {
  13. /* @var SpecModel $SpecModel */
  14. private $SpecModel;
  15. /* @var SpecValueModel $SpecModel */
  16. private $SpecValueModel;
  17. /**
  18. * 构造方法
  19. * @throws \app\common\exception\BaseException
  20. * @throws \think\db\exception\DataNotFoundException
  21. * @throws \think\db\exception\ModelNotFoundException
  22. * @throws \think\exception\DbException
  23. */
  24. public function _initialize()
  25. {
  26. parent::_initialize();
  27. $this->SpecModel = new SpecModel;
  28. $this->SpecValueModel = new SpecValueModel;
  29. }
  30. /**
  31. * 添加规则组
  32. * @param $spec_name
  33. * @param $spec_value
  34. * @return array
  35. */
  36. public function addSpec($spec_name, $spec_value)
  37. {
  38. // 判断规格组是否存在
  39. if (!$specId = $this->SpecModel->getSpecIdByName($spec_name)) {
  40. // 新增规格组and规则值
  41. if ($this->SpecModel->add($spec_name)
  42. && $this->SpecValueModel->add($this->SpecModel['spec_id'], $spec_value))
  43. return $this->renderSuccess('', '', [
  44. 'spec_id' => (int)$this->SpecModel['spec_id'],
  45. 'spec_value_id' => (int)$this->SpecValueModel['spec_value_id'],
  46. ]);
  47. return $this->renderError();
  48. }
  49. // 判断规格值是否存在
  50. if ($specValueId = $this->SpecValueModel->getSpecValueIdByName($specId, $spec_value)) {
  51. return $this->renderSuccess('', '', [
  52. 'spec_id' => (int)$specId,
  53. 'spec_value_id' => (int)$specValueId,
  54. ]);
  55. }
  56. // 添加规则值
  57. if ($this->SpecValueModel->add($specId, $spec_value))
  58. return $this->renderSuccess('', '', [
  59. 'spec_id' => (int)$specId,
  60. 'spec_value_id' => (int)$this->SpecValueModel['spec_value_id'],
  61. ]);
  62. return $this->renderError();
  63. }
  64. /**
  65. * 添加规格值
  66. * @param $spec_id
  67. * @param $spec_value
  68. * @return array
  69. */
  70. public function addSpecValue($spec_id, $spec_value)
  71. {
  72. // 判断规格值是否存在
  73. if ($specValueId = $this->SpecValueModel->getSpecValueIdByName($spec_id, $spec_value)) {
  74. return $this->renderSuccess('', '', [
  75. 'spec_value_id' => (int)$specValueId,
  76. ]);
  77. }
  78. // 添加规则值
  79. if ($this->SpecValueModel->add($spec_id, $spec_value))
  80. return $this->renderSuccess('', '', [
  81. 'spec_value_id' => (int)$this->SpecValueModel['spec_value_id'],
  82. ]);
  83. return $this->renderError();
  84. }
  85. }