Spec.php 3.0 KB

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