StoreBranchProductAttrValueDao.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\dao\product\branch;
  12. use app\dao\BaseDao;
  13. use app\model\product\branch\StoreBranchProductAttrValue;
  14. /**
  15. * Class StoreBranchProductAttrValueDao
  16. * @package app\dao\product\branch
  17. */
  18. class StoreBranchProductAttrValueDao extends BaseDao
  19. {
  20. /**
  21. * @return string
  22. */
  23. protected function setModel(): string
  24. {
  25. return StoreBranchProductAttrValue::class;
  26. }
  27. /**
  28. * 获取属性库存
  29. * @param string $unique
  30. * @param int $storeId
  31. * @return int|mixed
  32. */
  33. public function uniqueByStock(string $unique, int $storeId)
  34. {
  35. return $this->search(['unique' => $unique, 'store_id' => $storeId])->value('stock') ?: 0;
  36. }
  37. /**
  38. * 根据条件获取规格数据列表
  39. * @param array $where
  40. * @return array
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\DbException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. */
  45. public function getProductAttrValue(array $where)
  46. {
  47. return $this->search($where)->order('id asc')->select()->toArray();
  48. }
  49. /**
  50. * 根据规格信息获取商品库存
  51. * @param array $ids
  52. * @return array|\think\Model|null
  53. */
  54. public function getProductStockByValues(array $productIds, int $storeId)
  55. {
  56. try {
  57. return $this->getModel()->where([['product_id', 'in', $productIds], ['store_id', '=', $storeId], ['type', '=', 0]])
  58. ->field('`product_id`, SUM(`stock`) AS `stock`')->group("product_id")->select()->toArray();
  59. } catch (\Exception $e) {
  60. \think\facade\Log::error([$e->getFile() .'__' . $e->getLine() .'__' . $e->getMessage(),'file' => 'Exception']);
  61. }
  62. }
  63. /**
  64. * 保存数据
  65. * @param array $data
  66. * @return mixed|\think\Collection
  67. * @throws \Exception
  68. */
  69. public function saveAll(array $data)
  70. {
  71. return $this->getModel()->saveAll($data);
  72. }
  73. }