StoreProductAttrValueDao.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\dao\product\sku;
  12. use app\dao\BaseDao;
  13. use app\model\product\sku\StoreProductAttrValue;
  14. /**
  15. * Class StoreProductAttrValueDao
  16. * @package app\dao\product\sku
  17. */
  18. class StoreProductAttrValueDao extends BaseDao
  19. {
  20. /**
  21. * 设置模型
  22. * @return string
  23. */
  24. protected function setModel(): string
  25. {
  26. return StoreProductAttrValue::class;
  27. }
  28. /**
  29. * 根据条件获取规格value
  30. * @param array $where
  31. * @param string $field
  32. * @param string $key
  33. * @return array
  34. */
  35. public function getColumn(array $where, string $field = '*', string $key = 'suk')
  36. {
  37. return $this->search($where)->column($field, $key);
  38. }
  39. /**
  40. * 根据条件删除规格value
  41. * @param int $id
  42. * @param int $type
  43. * @return bool
  44. * @throws \Exception
  45. */
  46. public function del(int $id, int $type)
  47. {
  48. return $this->search(['product_id' => $id, 'type' => $type])->delete();
  49. }
  50. /**
  51. * 保存数据
  52. * @param array $data
  53. * @return mixed|\think\Collection
  54. * @throws \Exception
  55. */
  56. public function saveAll(array $data)
  57. {
  58. return $this->getModel()->saveAll($data);
  59. }
  60. /**
  61. * 根据条件获取规格数据列表
  62. * @param array $where
  63. * @return array
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\DbException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. */
  68. public function getProductAttrValue(array $where)
  69. {
  70. return $this->search($where)->order('id asc')->select()->toArray();
  71. }
  72. /**获取属性列表
  73. * @return mixed
  74. */
  75. public function attrValue()
  76. {
  77. return $this->search()->field('product_id,sum(sales * price) as val')->with(['product'])->group('product_id')->limit(20)->select()->toArray();
  78. }
  79. /**获取属性库存
  80. * @param string $unique
  81. * @return int
  82. */
  83. public function uniqueByStock(string $unique)
  84. {
  85. return $this->search(['unique' => $unique])->value('stock') ?: 0;
  86. }
  87. /**
  88. * 减库存加销量减限购
  89. * @param array $where
  90. * @param int $num
  91. * @return mixed
  92. */
  93. public function decStockIncSalesDecQuota(array $where, int $num)
  94. {
  95. return $this->getModel()->where($where)->dec('stock', $num)->dec('quota', $num)->inc('sales', $num)->update();
  96. }
  97. /**
  98. * 根据unique获取一条规格数据(积分商城)
  99. * @param string $unique
  100. * @param string $field
  101. * @return array|\think\Model|null
  102. * @throws \think\db\exception\DataNotFoundException
  103. * @throws \think\db\exception\DbException
  104. * @throws \think\db\exception\ModelNotFoundException
  105. */
  106. public function uniqueByField(string $unique, string $field = '*')
  107. {
  108. return $this->search(['unique' => $unique, 'type' => 4])->field($field)->with(['storeIntegral'])->find();
  109. }
  110. }