StoreProductAttrResultServices.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\product\sku;
  12. use app\dao\product\sku\StoreProductAttrResultDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. /**
  16. * Class StoreProductAttrResultService
  17. * @package app\services\product\sku
  18. * @mixin StoreProductAttrResultDao
  19. */
  20. class StoreProductAttrResultServices extends BaseServices
  21. {
  22. /**
  23. * StoreProductAttrResultServices constructor.
  24. * @param StoreProductAttrResultDao $dao
  25. */
  26. public function __construct(StoreProductAttrResultDao $dao)
  27. {
  28. $this->dao = $dao;
  29. }
  30. /**
  31. * 获取属性规格
  32. * @param array $where
  33. * @return mixed
  34. */
  35. public function getResult(array $where)
  36. {
  37. return json_decode($this->dao->value($where, 'result'), true);
  38. }
  39. /**
  40. * 删除属性
  41. * @param int $id
  42. * @param int $type
  43. * @return bool
  44. */
  45. public function del(int $id, int $type)
  46. {
  47. return $this->dao->del($id, $type);
  48. }
  49. /**
  50. * 设置属性
  51. * @param array $data
  52. * @param int $id
  53. * @param int $type
  54. * @return bool
  55. * @throws \think\db\exception\DataNotFoundException
  56. * @throws \think\db\exception\DbException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. */
  59. public function setResult(array $data, int $id, int $type)
  60. {
  61. $result = $this->dao->get(['product_id' => $id, 'type' => $type]);
  62. if ($result) {
  63. $res = $this->dao->update($result['id'], ['result' => json_encode($data), 'change_time' => time()]);
  64. } else {
  65. $res = $this->dao->save(['product_id' => $id, 'result' => json_encode($data), 'change_time' => time(), 'type' => $type]);
  66. }
  67. if (!$res) throw new AdminException('规格保存失败');
  68. return true;
  69. }
  70. }