MerchantDao.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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\system\merchant;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\system\merchant\Merchant;
  14. use crmeb\services\VicWordService;
  15. use think\db\BaseQuery;
  16. use think\db\exception\DataNotFoundException;
  17. use think\db\exception\DbException;
  18. use think\db\exception\ModelNotFoundException;
  19. use think\facade\Db;
  20. use think\Model;
  21. class MerchantDao extends BaseDao
  22. {
  23. /**
  24. * @return string
  25. * @author xaboy
  26. * @day 2020-04-16
  27. */
  28. protected function getModel(): string
  29. {
  30. return Merchant::class;
  31. }
  32. /**
  33. * @param array $where
  34. * @return BaseQuery
  35. * @author xaboy
  36. * @day 2020-04-16
  37. */
  38. public function search(array $where, $is_del = 0)
  39. {
  40. $query = Merchant::getDB()
  41. ->when($is_del !== null, function ($query) use ($is_del) {
  42. $query->where('is_del', $is_del);
  43. })
  44. ->when(isset($where['is_trader']) && $where['is_trader'] !== '', function ($query) use ($where) {
  45. $query->where('is_trader', $where['is_trader']);
  46. })
  47. ->when(isset($where['is_best']) && $where['is_best'] !== '', function ($query) use ($where) {
  48. $query->where('is_best', $where['is_best']);
  49. })
  50. ->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  51. getModelTime($query, $where['date']);
  52. })
  53. ->when(isset($where['mer_state']) && $where['mer_state'] !== '', function ($query) use ($where) {
  54. $query->where('mer_state', $where['mer_state']);
  55. })
  56. ->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  57. $query->where('mer_id', $where['mer_id']);
  58. })
  59. ->when(isset($where['category_id']) && $where['category_id'] !== '', function ($query) use ($where) {
  60. is_array($where['category_id']) ? $query->whereIn('category_id', $where['category_id']) : $query->where('category_id', $where['category_id']);
  61. });
  62. if (isset($where['keyword']) && $where['keyword']) {
  63. if (is_numeric($where['keyword'])) {
  64. $query->whereLike('mer_name|mer_keyword|mer_phone', "%{$where['keyword']}%");
  65. } else {
  66. $word = app()->make(VicWordService::class)->getWord($where['keyword']);
  67. $query->where(function ($query) use ($word, $where) {
  68. foreach ($word as $item) {
  69. if(mb_strlen($item) > 1) {
  70. $query->whereOr('mer_name', 'LIKE', "%$item%");
  71. }
  72. }
  73. $query->whereOr('mer_name|mer_keyword', 'LIKE', "%{$where['keyword']}%");
  74. });
  75. }
  76. }
  77. if (isset($where['status']) && $where['status'] !== '')
  78. $query->where('status', $where['status']);
  79. $order = $where['order'] ?? '';
  80. $query->when($order, function ($query) use ($where, $order) {
  81. if ($order == 'rate') {
  82. $query->order('is_best DESC, product_score DESC,service_score DESC,postage_score DESC');
  83. } else if ($order == 'location' && isset($where['location']['long'], $where['location']['lat'])) {
  84. $lng = (float)$where['location']['long'];
  85. $lat = (float)$where['location']['lat'];
  86. $query->whereNotNull('lat')->whereNotNull('long')
  87. ->order(Db::raw("(2 * 6378.137 * ASIN(
  88. SQRT(
  89. POW( SIN( PI( ) * ( $lng- `long` ) / 360 ), 2 ) + COS( PI( ) * $lat / 180 ) * COS( `lat` * PI( ) / 180 ) * POW( SIN( PI( ) * ( $lat- `lat` ) / 360 ), 2 )
  90. )
  91. )
  92. ) ASC "));
  93. } else {
  94. $query->order('is_best DESC, sales DESC,sort DESC');
  95. }
  96. }, function ($query) use ($order) {
  97. $query->order('is_best DESC, sort DESC,sales DESC');
  98. });
  99. return $query;
  100. }
  101. /**
  102. * @param int $id
  103. * @return array|Model|null
  104. * @throws DataNotFoundException
  105. * @throws DbException
  106. * @throws ModelNotFoundException
  107. * @author xaboy
  108. * @day 2020-04-17
  109. */
  110. public function get($id)
  111. {
  112. return Merchant::getInstance()->where('is_del', 0)->find($id);
  113. }
  114. /**
  115. * @param $id
  116. * @author Qinii
  117. */
  118. public function apiGetOne($id)
  119. {
  120. return Merchant::getInstance()->where(['is_del' => 0, 'status' => 1, 'mer_state' => 1])->find($id);
  121. }
  122. /**
  123. * @param int $merId
  124. * @author Qinii
  125. */
  126. public function incCareCount(int $merId)
  127. {
  128. ($this->getModel()::getDB())->where($this->getPk(), $merId)->inc('care_count', 1)->update();
  129. }
  130. /**
  131. * @param int $merId
  132. * @param int $inc
  133. * @author xaboy
  134. * @day 2020/9/25
  135. */
  136. public function incSales($merId, $inc)
  137. {
  138. ($this->getModel()::getDB())->where($this->getPk(), $merId)->inc('sales', $inc)->update();
  139. }
  140. /**
  141. * @param int $merId
  142. * @author Qinii
  143. */
  144. public function decCareCount(int $merId)
  145. {
  146. ($this->getModel()::getDB())->where($this->getPk(), $merId)->where('care_count', '>', 0)->dec('care_count', 1)->update();
  147. }
  148. public function dateMerchantNum($date)
  149. {
  150. return Merchant::getDB()->where('is_del', 0)->when($date, function ($query, $date) {
  151. getModelTime($query, $date);
  152. })->count();
  153. }
  154. /**
  155. * TODO 获取复制商品次数
  156. * @param int $merId
  157. * @return mixed
  158. * @author Qinii
  159. * @day 2020-08-06
  160. */
  161. public function getCopyNum(int $merId)
  162. {
  163. return Merchant::getDB()->where('mer_id', $merId)->value('copy_product_num');
  164. }
  165. /**
  166. * TODO 变更复制次数
  167. * @param int $merId
  168. * @param int $num 正负数
  169. * @return mixed
  170. * @author Qinii
  171. * @day 2020-08-06
  172. */
  173. public function changeCopyNum(int $merId, int $num)
  174. {
  175. return $this->getModel()::where('mer_id', $merId)->inc('copy_product_num', $num)->update();
  176. }
  177. /**
  178. * @param $field
  179. * @param $value
  180. * @param int|null $except
  181. * @return bool
  182. * @author xaboy
  183. * @day 2020-03-30
  184. */
  185. public function fieldExists($field, $value, ?int $except = null): bool
  186. {
  187. $query = ($this->getModel())::getDB()->where($field, $value);
  188. if (!is_null($except)) $query->where($this->getPk(), '<>', $except);
  189. return $query->where('is_del', 0)->count() > 0;
  190. }
  191. public function names(array $ids)
  192. {
  193. return Merchant::getDB()->whereIn('mer_id',$ids)->column('mer_name');
  194. }
  195. /**
  196. * TODO 增加商户余额
  197. * @param int $merId
  198. * @param float $num
  199. * @author Qinii
  200. * @day 3/19/21
  201. */
  202. public function addMoney(int $merId,float $num)
  203. {
  204. $merchant = $this->getModel()::getDB()->where('mer_id', $merId)->find();
  205. $mer_money = bcadd($merchant['mer_money'], $num, 2);
  206. $merchant->mer_money = $mer_money;
  207. $merchant->save();
  208. }
  209. /**
  210. * TODO 减少商户余额
  211. * @param int $merId
  212. * @param float $num
  213. * @author Qinii
  214. * @day 3/19/21
  215. */
  216. public function subMoney(int $merId,float $num)
  217. {
  218. $merchant = $this->getModel()::getDB()->where('mer_id', $merId)->find();
  219. $mer_money = bcsub($merchant['mer_money'], $num, 2);
  220. $merchant->mer_money = $mer_money;
  221. $merchant->save();
  222. }
  223. }