ProductAttrValueDao.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\dao\store\product;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\store\product\ProductAttrValue as model;
  14. use app\common\repositories\store\product\ProductRepository;
  15. use think\db\exception\DbException;
  16. use think\facade\Db;
  17. /**
  18. * Class ProductAttrValueDao
  19. * @package app\common\dao\store\product
  20. * @author xaboy
  21. * @day 2020/6/9
  22. */
  23. class ProductAttrValueDao extends BaseDao
  24. {
  25. /**
  26. * @return string
  27. * @author xaboy
  28. * @day 2020/6/9
  29. */
  30. protected function getModel(): string
  31. {
  32. return model::class;
  33. }
  34. /**
  35. * @Author:Qinii
  36. * @Date: 2020/5/9
  37. * @param int $productId
  38. * @return mixed
  39. */
  40. public function clearAttr(int $productId)
  41. {
  42. return ($this->getModel())::where('product_id', $productId)->delete();
  43. }
  44. /**
  45. * @Author:Qinii
  46. * @Date: 2020/5/9
  47. * @param int $merId
  48. * @param $field
  49. * @param $value
  50. * @param null $except
  51. * @return mixed
  52. */
  53. public function getFieldColumnt($key, $value, $field, $except = null)
  54. {
  55. return ($this->getModel()::getDB())->when($except, function ($query, $except) use ($field) {
  56. $query->where($field, '<>', $except);
  57. })->where($key, $value)->column($field);
  58. }
  59. /**
  60. * @Author:Qinii
  61. * @Date: 2020/5/11
  62. * @param $key
  63. * @param $value
  64. * @param $field
  65. * @param null $except
  66. * @return mixed
  67. */
  68. public function getFieldSum($key, $value, $field, $except = null)
  69. {
  70. return ($this->getModel()::getDB())->when($except, function ($query, $except) use ($field) {
  71. $query->where($field, '<>', $except);
  72. })->where($key, $value)->sum($field);
  73. }
  74. /**
  75. * @Author:Qinii
  76. * @Date: 2020/5/11
  77. * @param array $data
  78. * @return mixed
  79. */
  80. public function insert(array $data)
  81. {
  82. return ($this->getModel()::getDB())->insertAll($data);
  83. }
  84. /**
  85. * @Author:Qinii
  86. * @Date: 2020/5/11
  87. * @param int|null $merId
  88. * @param $field
  89. * @param $value
  90. * @param null $except
  91. * @return bool
  92. */
  93. public function merFieldExists(?int $merId, $field, $value, $except = null)
  94. {
  95. return ($this->getModel())::getDB()->when($except, function ($query, $except) use ($field) {
  96. $query->where($field, '<>', $except);
  97. })->when($merId, function ($query, $merId) {
  98. $query->where('mer_id', $merId);
  99. })->where($field, $value)->count() > 0;
  100. }
  101. /**
  102. * @param $id
  103. * @return mixed
  104. * @author xaboy
  105. * @day 2020/6/9
  106. */
  107. public function getSku($id)
  108. {
  109. return ($this->getModel())::where('product_id', $id);
  110. }
  111. /**
  112. * @param int|null $merId
  113. * @param $field
  114. * @param $value
  115. * @param null $except
  116. * @return mixed
  117. * @author xaboy
  118. * @day 2020/6/9
  119. */
  120. public function getFieldExists(?int $merId, $field, $value, $except = null)
  121. {
  122. return ($this->getModel())::getDB()->when($except, function ($query, $except) use ($field) {
  123. $query->where($field, '<>', $except);
  124. })->when($merId, function ($query, $merId) {
  125. $query->where('mer_id', $merId);
  126. })->where($field, $value);
  127. }
  128. /**
  129. * @param int $productId
  130. * @param string $unique
  131. * @param int $desc
  132. * @return int
  133. * @throws DbException
  134. * @author xaboy
  135. * @day 2020/6/8
  136. */
  137. public function descStock(int $productId, string $unique, int $desc)
  138. {
  139. return model::getDB()->where('product_id', $productId)->where('unique', $unique)->update([
  140. 'stock' => Db::raw('stock-' . $desc),
  141. 'sales' => Db::raw('sales+' . $desc)
  142. ]);
  143. }
  144. /**
  145. * @param int $productId
  146. * @param string $sku
  147. * @param int $desc
  148. * @return int
  149. * @throws DbException
  150. * @author xaboy
  151. * @day 2020/6/8
  152. */
  153. public function descSkuStock(int $productId, string $sku, int $desc)
  154. {
  155. return model::getDB()->where('product_id', $productId)->where('sku', $sku)->update([
  156. 'stock' => Db::raw('stock-' . $desc),
  157. 'sales' => Db::raw('sales+' . $desc)
  158. ]);
  159. }
  160. /**
  161. * @param int $productId
  162. * @param string $unique
  163. * @param int $inc
  164. * @throws DbException
  165. * @author xaboy
  166. * @day 2020/6/8
  167. */
  168. public function incStock(int $productId, string $unique, int $inc)
  169. {
  170. model::getDB()->where('product_id', $productId)->where('unique', $unique)->inc('stock', $inc)->update();
  171. model::getDB()->where('product_id', $productId)->where('unique', $unique)->where('sales', '>=', $inc)->dec('sales', $inc)->update();
  172. }
  173. /**
  174. * @param int $productId
  175. * @param string $sku
  176. * @param int $inc
  177. * @throws DbException
  178. * @author xaboy
  179. * @day 2020/6/8
  180. */
  181. public function incSkuStock(int $productId, string $sku, int $inc)
  182. {
  183. model::getDB()->where('product_id', $productId)->where('sku', $sku)->inc('stock', $inc)->update();
  184. model::getDB()->where('product_id', $productId)->where('sku', $sku)->where('sales', '>', $inc)->dec('sales', $inc)->update();
  185. }
  186. /**
  187. * @param int $productId
  188. * @param string $unique
  189. * @return bool
  190. * @author xaboy
  191. * @day 2020/6/9
  192. */
  193. public function attrExists(int $productId, string $unique): bool
  194. {
  195. return model::getDB()->where('product_id', $productId)->where('unique', $unique)->count() > 0;
  196. }
  197. /**
  198. * @param int $productId
  199. * @param string $sku
  200. * @return bool
  201. * @author xaboy
  202. * @day 2020/6/9
  203. */
  204. public function skuExists(int $productId, string $sku): bool
  205. {
  206. return model::getDB()->where('product_id', $productId)->where('sku', $sku)->count() > 0;
  207. }
  208. /**
  209. * TODO 商品佣金是否大于设置佣金比例
  210. * @param $productId
  211. * @return bool
  212. * @author Qinii
  213. * @day 2020-06-25
  214. */
  215. public function checkExtensionById($productId)
  216. {
  217. $extension_one_rate = systemConfig('extension_one_rate');
  218. $extension_two_rate = systemConfig('extension_two_rate');
  219. $count = ($this->getModel()::getDb())->where(function($query)use($productId,$extension_one_rate){
  220. $query->where('product_id',$productId)->whereRaw('price * '.$extension_one_rate.' > extension_one');
  221. })->whereOr(function($query)use($productId,$extension_two_rate){
  222. $query->where('product_id',$productId)->whereRaw('price * '.$extension_two_rate.' > extension_two');
  223. })->count();
  224. return $count ? false : true;
  225. }
  226. public function search(array $where)
  227. {
  228. $query = ($this->getModel()::getDb())
  229. ->when(isset($where['product_id']) && $where['product_id'] !== '',function($query)use($where){
  230. $query->where('product_id',$where['product_id']);
  231. })
  232. ->when(isset($where['sku']) && $where['sku'] !== '',function($query)use($where){
  233. $query->where('sku',$where['sku']);
  234. })
  235. ->when(isset($where['unique']) && $where['unique'] !== '',function($query)use($where){
  236. $query->where('unique',$where['unique']);
  237. });
  238. return $query;
  239. }
  240. public function updates(array $ids, array $data)
  241. {
  242. $this->getModel()::getDb()->whereIn('product_id',$ids)->update($data);
  243. }
  244. public function updatesExtension(array $ids, array $data)
  245. {
  246. app()->make(ProductRepository::class)->updates($ids,['extension_type' => 1]);
  247. $query = $this->getModel()::getDb()->where('product_id','in',$ids);
  248. $query->chunk(100, function($list) use($data){
  249. foreach ($list as $item) {
  250. $arr['extension_one'] = bcmul($item->price,$data['extension_one'],2);
  251. $arr['extension_two'] = bcmul($item->price,$data['extension_two'],2);
  252. $this->getModel()::getDb()->where('unique',$item->unique)->update($arr);
  253. }
  254. },'product_id');
  255. }
  256. }