ProductAttrValueDao.php 7.7 KB

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