StoreProductAttrValueDao.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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\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. * @param array $where
  30. * @param string $field
  31. * @param int $page
  32. * @param int $limit
  33. * @return array
  34. * @throws \think\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\DbException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. */
  38. public function getList(array $where, string $field = '*', int $page = 0, $limit = 0)
  39. {
  40. return $this->search($where)->field($field)->when($page && $limit, function ($query) use ($page, $limit) {
  41. $query->page($page, $limit);
  42. })->order('id asc')->select()->toArray();
  43. }
  44. /**
  45. * 减库存
  46. * @param array $where
  47. * @param int $num
  48. * @param string $stock
  49. * @param string $sales
  50. * @return bool|mixed
  51. */
  52. public function decStockIncSales(array $where, int $num, string $stock = 'stock', string $sales = 'sales')
  53. {
  54. $isQuota = false;
  55. if (isset($where['type']) && $where['type']) {
  56. $isQuota = true;
  57. if (count($where) == 2) {
  58. unset($where['type']);
  59. }
  60. }
  61. $field = $isQuota ? 'stock,quota' : 'stock';
  62. $product = $this->getModel()->where($where)->field($field)->find();
  63. if ($product) {
  64. return $this->getModel()->where($where)->when($isQuota, function ($query) use ($num) {
  65. $query->dec('quota', $num);
  66. })->dec($stock, $num)->dec('sum_stock', $num)->inc($sales, $num)->update();
  67. }
  68. return true;
  69. // return $this->getModel()->where($where)->dec($stock, $num)->inc($sales, $num)->dec('sum_stock', $num)->update();
  70. }
  71. /**
  72. * 加库存
  73. * @param array $where
  74. * @param int $num
  75. * @param string $stock
  76. * @param string $sales
  77. * @return bool|mixed
  78. */
  79. public function incStockDecSales(array $where, int $num, string $stock = 'stock', string $sales = 'sales')
  80. {
  81. $isQuota = false;
  82. if (isset($where['type']) && $where['type']) {
  83. $isQuota = true;
  84. if (count($where) == 2) {
  85. unset($where['type']);
  86. }
  87. }
  88. $salesOne = $this->getModel()->where($where)->value($sales);
  89. if ($salesOne) {
  90. $salesNum = $num;
  91. if ($num > $salesOne) {
  92. $salesNum = $salesOne;
  93. }
  94. return $this->getModel()->where($where)->when($isQuota, function ($query) use ($num) {
  95. $query->inc('quota', $num);
  96. })->inc($stock, $num)->inc('sum_stock', $num)->dec($sales, $salesNum)->update();
  97. }
  98. return true;
  99. // $salesOne = $this->getModel()->where($where)->value($sales);
  100. // if ($salesOne) {
  101. // $salesNum = $num;
  102. // if ($num > $salesOne) {
  103. // $salesNum = $salesOne;
  104. // }
  105. // return $this->getModel()->where($where)->inc($stock, $num)->inc('sum_stock', $num)->dec($sales, $salesNum)->update();
  106. // };
  107. // return true;
  108. }
  109. /**
  110. * 根据条件获取规格value
  111. * @param array $where
  112. * @param string $field
  113. * @param string $key
  114. * @param bool $search
  115. * @return array
  116. */
  117. public function getColumn(array $where, string $field = '*', string $key = 'suk', bool $search = false)
  118. {
  119. if ($search) {
  120. return $this->search($where)
  121. ->when(isset($where['store_id']) && $where['store_id'], function ($query) use ($where) {
  122. $query->with(['storeBranch' => function ($querys) use ($where) {
  123. $querys->where(['store_id' => $where['store_id'], 'product_id' => $where['product_id']]);
  124. }]);
  125. })
  126. ->column($field, $key);
  127. } else {
  128. return $this->getModel()::where($where)
  129. ->when(isset($where['product_id']) && $where['product_id'], function ($query) use ($where, $field) {
  130. if (is_array($where['product_id'])) {
  131. $query->whereIn('product_id', $where['product_id']);
  132. } else {
  133. $query->where('product_id', $where['product_id']);
  134. }
  135. })
  136. ->column($field, $key);
  137. }
  138. }
  139. /**
  140. * 根据条件删除规格value
  141. * @param int $id
  142. * @param int $type
  143. * @param array $suk
  144. * @return bool
  145. */
  146. public function del(int $id, int $type, array $suk = [])
  147. {
  148. return $this->search(['product_id' => $id, 'type' => $type, 'suk' => $suk])->delete();
  149. }
  150. /**
  151. * 保存数据
  152. * @param array $data
  153. * @return mixed|\think\Collection
  154. * @throws \Exception
  155. */
  156. public function saveAll(array $data)
  157. {
  158. return $this->getModel()->saveAll($data);
  159. }
  160. /**
  161. * 根据条件获取规格数据列表
  162. * @param array $where
  163. * @return array
  164. * @throws \think\db\exception\DataNotFoundException
  165. * @throws \think\db\exception\DbException
  166. * @throws \think\db\exception\ModelNotFoundException
  167. */
  168. public function getProductAttrValue(array $where)
  169. {
  170. return $this->search($where)->order('id asc')->select()->toArray();
  171. }
  172. /**
  173. * 获取属性列表
  174. * @return mixed
  175. */
  176. public function attrValue()
  177. {
  178. return $this->search()->field('product_id,sum(sales * price) as val')->with(['product'])->group('product_id')->limit(20)->select()->toArray();
  179. }
  180. /**
  181. * 获取属性库存
  182. * @param string $unique
  183. * @return int
  184. */
  185. public function uniqueByStock(string $unique)
  186. {
  187. return $this->search(['unique' => $unique])->value('stock') ?: 0;
  188. }
  189. /**
  190. * 减库存加销量减限购
  191. * @param array $where
  192. * @param int $num
  193. * @return mixed
  194. */
  195. public function decStockIncSalesDecQuota(array $where, int $num)
  196. {
  197. return $this->getModel()->where($where)->dec('stock', $num)->dec('quota', $num)->inc('sales', $num)->update();
  198. }
  199. /**
  200. * 根据unique获取一条规格数据
  201. * @param string $unique
  202. * @param int $type
  203. * @param string $field
  204. * @param array $with
  205. * @return array|\think\Model|null
  206. * @throws \think\db\exception\DataNotFoundException
  207. * @throws \think\db\exception\DbException
  208. * @throws \think\db\exception\ModelNotFoundException
  209. */
  210. public function uniqueByField(string $unique, int $type = 0, string $field = '*', array $with = [])
  211. {
  212. return $this->search(['unique' => $unique, 'type' => $type])->field($field)->with($with)->find();
  213. }
  214. /**
  215. * 根据商品id获取对应规格库存
  216. * @param int $pid
  217. * @param int $type
  218. * @return float
  219. */
  220. public function pidBuStock(int $pid, $type = 0)
  221. {
  222. return $this->getModel()->where(['product_id' => $pid, 'type' => $type])->sum('stock');
  223. }
  224. /**
  225. * 获取门店规格
  226. * @param array $where
  227. * @return array
  228. * @throws \think\db\exception\DataNotFoundException
  229. * @throws \think\db\exception\DbException
  230. * @throws \think\db\exception\ModelNotFoundException
  231. */
  232. public function storeBranchAttr(array $where)
  233. {
  234. return $this->search($where)
  235. ->when(isset($where['store_id']) && $where['store_id'], function ($query) use ($where) {
  236. $query->with(['storeBranch' => function ($querys) use ($where) {
  237. $querys->where('store_id', $where['store_id']);
  238. }]);
  239. })->select()->toArray();
  240. }
  241. /**
  242. * 根据条形码获取一条商品规格信息
  243. * @param string $bar_code
  244. * @return array|\think\Model|null
  245. * @throws \think\db\exception\DataNotFoundException
  246. * @throws \think\db\exception\DbException
  247. * @throws \think\db\exception\ModelNotFoundException
  248. */
  249. public function getAttrByBarCode(string $bar_code)
  250. {
  251. return $this->getModel()->where('bar_code', $bar_code)->order('id desc')->find();
  252. }
  253. /**
  254. * 根据规格信息获取商品库存
  255. * @param array $ids
  256. * @return array|\think\Model|null
  257. */
  258. public function getProductStockByValues(array $ids)
  259. {
  260. return $this->getModel()->whereIn('product_id', $ids)->where('type', 0)
  261. ->field('`product_id` AS `id`, SUM(`stock`) AS `stock`')->group("product_id")->select()->toArray();
  262. }
  263. /**
  264. * 分组查询
  265. * @param string $file
  266. * @param string $group_id
  267. * @param array $where
  268. * @param string $having
  269. * @return mixed
  270. */
  271. public function getGroupData(string $file, string $group_id, array $where, string $having = '')
  272. {
  273. return $this->getModel()->when($where, function ($query) use ($where) {
  274. $query->where($where);
  275. })->field($file)->group($group_id)->when($having, function ($query) use ($having) {
  276. $query->having($having);
  277. })->select();
  278. }
  279. /**
  280. * 库存警戒查询
  281. * @author 等风来
  282. * @email 136327134@qq.com
  283. * @date 2023/4/18
  284. * @param array $where
  285. * @return int
  286. * @throws \think\db\exception\DbException
  287. */
  288. public function getPolice(array $where)
  289. {
  290. return $this->getModel()->when($where, function ($query) use ($where) {
  291. $query->where($where);
  292. })->count();
  293. }
  294. }