MerchantDao.php 8.4 KB

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