Spec.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\store\controller\goods;
  13. use app\store\controller\Controller;
  14. use app\store\model\Spec as SpecModel;
  15. use app\store\model\SpecValue as SpecValueModel;
  16. /**
  17. * 商品规格控制器
  18. * Class Spec
  19. * @package app\store\controller
  20. */
  21. class Spec extends Controller
  22. {
  23. /* @var SpecModel $SpecModel */
  24. private $SpecModel;
  25. /* @var SpecValueModel $SpecModel */
  26. private $SpecValueModel;
  27. /**
  28. * 构造方法
  29. * @throws \app\common\exception\BaseException
  30. * @throws \think\db\exception\DataNotFoundException
  31. * @throws \think\db\exception\ModelNotFoundException
  32. */
  33. public function _initialize()
  34. {
  35. parent::_initialize();
  36. $this->SpecModel = new SpecModel;
  37. $this->SpecValueModel = new SpecValueModel;
  38. }
  39. /**
  40. * 添加规则组
  41. * @param $spec_name
  42. * @param $spec_value
  43. * @return array
  44. */
  45. public function addSpec($spec_name, $spec_value)
  46. {
  47. // 判断规格组是否存在
  48. if (!$specId = $this->SpecModel->getSpecIdByName($spec_name)) {
  49. // 新增规格组and规则值
  50. if ($this->SpecModel->add($spec_name)
  51. && $this->SpecValueModel->add($this->SpecModel['spec_id'], $spec_value))
  52. return $this->renderSuccess('', '', [
  53. 'spec_id' => (int)$this->SpecModel['spec_id'],
  54. 'spec_value_id' => (int)$this->SpecValueModel['spec_value_id'],
  55. ]);
  56. return $this->renderError();
  57. }
  58. // 判断规格值是否存在
  59. if ($specValueId = $this->SpecValueModel->getSpecValueIdByName($specId, $spec_value)) {
  60. return $this->renderSuccess('', '', [
  61. 'spec_id' => (int)$specId,
  62. 'spec_value_id' => (int)$specValueId,
  63. ]);
  64. }
  65. // 添加规则值
  66. if ($this->SpecValueModel->add($specId, $spec_value))
  67. return $this->renderSuccess('', '', [
  68. 'spec_id' => (int)$specId,
  69. 'spec_value_id' => (int)$this->SpecValueModel['spec_value_id'],
  70. ]);
  71. return $this->renderError();
  72. }
  73. /**
  74. * 添加规格值
  75. * @param $spec_id
  76. * @param $spec_value
  77. * @return array
  78. */
  79. public function addSpecValue($spec_id, $spec_value)
  80. {
  81. // 判断规格值是否存在
  82. if ($specValueId = $this->SpecValueModel->getSpecValueIdByName($spec_id, $spec_value)) {
  83. return $this->renderSuccess('', '', [
  84. 'spec_value_id' => (int)$specValueId,
  85. ]);
  86. }
  87. // 添加规则值
  88. if ($this->SpecValueModel->add($spec_id, $spec_value))
  89. return $this->renderSuccess('', '', [
  90. 'spec_value_id' => (int)$this->SpecValueModel['spec_value_id'],
  91. ]);
  92. return $this->renderError();
  93. }
  94. }