ProductAttrValueDao.php 7.1 KB

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