MerchantDao.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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. // 分组权限
  42. if (app('request')->hasMacro('regionAuthority') && $region = app('request')->regionAuthority()) {
  43. $query->whereIn('mer_id', $region);
  44. }
  45. $query->when($is_del !== null, function ($query) use ($is_del) {
  46. $query->where('is_del', $is_del);
  47. })
  48. ->when(isset($where['is_trader']) && $where['is_trader'] !== '', function ($query) use ($where) {
  49. $query->where('is_trader', $where['is_trader']);
  50. })
  51. ->when(isset($where['is_margin']) && $where['is_margin'] !== '', function ($query) use ($where) {
  52. $query->where('is_margin', $where['is_margin']);
  53. })
  54. //补缴
  55. ->when(isset($where['margin']) && $where['margin'] !== '', function ($query) use ($where) {
  56. if ($where['margin']) {
  57. $query->where('is_margin',$where['margin']);
  58. } else {
  59. $query->where(function($query){
  60. $query->where('is_margin',1)->whereOr(function($query){
  61. $query->where('is_margin',10)->whereRaw('ot_margin > margin');
  62. });
  63. });
  64. }
  65. })
  66. ->when(isset($where['is_best']) && $where['is_best'] !== '', function ($query) use ($where) {
  67. $query->where('is_best', $where['is_best']);
  68. })
  69. ->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  70. getModelTime($query, $where['date']);
  71. })
  72. ->when(isset($where['mer_state']) && $where['mer_state'] !== '', function ($query) use ($where) {
  73. $query->where('mer_state', $where['mer_state']);
  74. })
  75. ->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  76. $query->where('mer_id', $where['mer_id']);
  77. })
  78. ->when(isset($where['category_id']) && $where['category_id'] !== '', function ($query) use ($where) {
  79. $query->whereIn('category_id', is_array($where['category_id']) ? $where['category_id'] : explode(',', $where['category_id']));
  80. })
  81. ->when(isset($where['type_id']) && $where['type_id'] !== '', function ($query) use ($where) {
  82. $query->whereIn('type_id', is_array($where['type_id']) ? $where['type_id'] : explode(',', $where['type_id']));
  83. })
  84. ->when(isset($where['delivery_way']) && $where['delivery_way'] !== '', function ($query) use ($where) {
  85. $query->whereLike('delivery_way', "%{$where['delivery_way']}%");
  86. })
  87. ->when(isset($where['offline_switch']) && $where['offline_switch'] !== '', function ($query) use ($where) {
  88. $query->where('offline_switch', $where['offline_switch']);
  89. })
  90. ->when(isset($where['region_id']) && $where['region_id'] !== '', function ($query) use ($where) {
  91. $query->where('region_id', $where['region_id']);
  92. });
  93. if (isset($where['keyword']) && $where['keyword']) {
  94. if (is_numeric($where['keyword'])) {
  95. $query->whereLike('mer_name|mer_keyword|mer_phone|real_name', "%{$where['keyword']}%");
  96. } else {
  97. $word = app()->make(VicWordService::class)->getWord($where['keyword']);
  98. $query->where(function ($query) use ($word, $where) {
  99. foreach ($word as $item) {
  100. if(mb_strlen($item) > 1) {
  101. $query->whereOr('mer_name', 'LIKE', "%$item%");
  102. }
  103. }
  104. $query->whereOr('mer_name|mer_keyword|real_name', 'LIKE', "%{$where['keyword']}%");
  105. });
  106. }
  107. }
  108. if (isset($where['status']) && $where['status'] !== '')
  109. $query->where('status', $where['status']);
  110. $order = $where['order'] ?? '';
  111. $query->when($order, function ($query) use ($where, $order) {
  112. if ($order == 'rate') {
  113. $query->order('is_best DESC, product_score DESC,service_score DESC,postage_score DESC, sort DESC');
  114. } else if ($order == 'location' && isset($where['location']['long'], $where['location']['lat'])) {
  115. $lng = (float)$where['location']['long'];
  116. $lat = (float)$where['location']['lat'];
  117. $query->whereNotNull('lat')->whereNotNull('long')
  118. ->order(Db::raw("(2 * 6378.137 * ASIN(
  119. SQRT(
  120. POW( SIN( PI( ) * ( $lng- `long` ) / 360 ), 2 ) + COS( PI( ) * $lat / 180 ) * COS( `lat` * PI( ) / 180 ) * POW( SIN( PI( ) * ( $lat- `lat` ) / 360 ), 2 )
  121. )
  122. )
  123. ) ASC "));
  124. } else {
  125. $query->order('sort DESC,create_time DESC');
  126. }
  127. }, function ($query) use ($order) {
  128. $query->order('is_best DESC, sort DESC,sales DESC');
  129. });
  130. return $query;
  131. }
  132. /**
  133. * @param int $id
  134. * @return array|Model|null
  135. * @throws DataNotFoundException
  136. * @throws DbException
  137. * @throws ModelNotFoundException
  138. * @author xaboy
  139. * @day 2020-04-17
  140. */
  141. public function get($id)
  142. {
  143. return Merchant::getInstance()->where('is_del', 0)->find($id);
  144. }
  145. /**
  146. * @param $id
  147. * @author Qinii
  148. */
  149. public function apiGetOne($id)
  150. {
  151. return Merchant::getInstance()->where(['is_del' => 0, 'status' => 1, 'mer_state' => 1])->find($id);
  152. }
  153. /**
  154. * @param int $merId
  155. * @author Qinii
  156. */
  157. public function incCareCount(int $merId)
  158. {
  159. ($this->getModel()::getDB())->where($this->getPk(), $merId)->inc('care_count', 1)->update();
  160. }
  161. /**
  162. * @param int $merId
  163. * @param int $inc
  164. * @author xaboy
  165. * @day 2020/9/25
  166. */
  167. public function incSales($merId, $inc)
  168. {
  169. ($this->getModel()::getDB())->where($this->getPk(), $merId)->inc('sales', $inc)->update();
  170. }
  171. /**
  172. * @param int $merId
  173. * @author Qinii
  174. */
  175. public function decCareCount(array $merId)
  176. {
  177. ($this->getModel()::getDB())->whereIn($this->getPk(), $merId)->where('care_count', '>', 0)->dec('care_count', 1)->update();
  178. }
  179. public function dateMerchantNum($date)
  180. {
  181. return Merchant::getDB()->where('is_del', 0)->when($date, function ($query, $date) {
  182. getModelTime($query, $date);
  183. })->count();
  184. }
  185. /**
  186. * 获取复制商品次数
  187. * @param int $merId
  188. * @return mixed
  189. * @author Qinii
  190. * @day 2020-08-06
  191. */
  192. public function getCopyNum(int $merId)
  193. {
  194. return Merchant::getDB()->where('mer_id', $merId)->value('copy_product_num');
  195. }
  196. /**
  197. * 变更复制次数
  198. * @param int $merId
  199. * @param int $num 正负数
  200. * @return mixed
  201. * @author Qinii
  202. * @day 2020-08-06
  203. */
  204. public function changeCopyNum(int $merId, int $num)
  205. {
  206. return $this->getModel()::where('mer_id', $merId)->inc('copy_product_num', $num)->update();
  207. }
  208. /**
  209. * @param $field
  210. * @param $value
  211. * @param int|null $except
  212. * @return bool
  213. * @author xaboy
  214. * @day 2020-03-30
  215. */
  216. public function fieldExists($field, $value, ?int $except = null): bool
  217. {
  218. $query = ($this->getModel())::getDB()->where($field, $value);
  219. if (!is_null($except)) $query->where($this->getPk(), '<>', $except);
  220. return $query->where('is_del', 0)->count() > 0;
  221. }
  222. public function names(array $ids)
  223. {
  224. return Merchant::getDB()->whereIn('mer_id',$ids)->column('mer_name');
  225. }
  226. /**
  227. * 增加商户余额
  228. * @param int $merId
  229. * @param float $num
  230. * @author Qinii
  231. * @day 3/19/21
  232. */
  233. public function addMoney(int $merId, float $num)
  234. {
  235. $field = 'mer_money';
  236. $merchant = $this->getModel()::getDB()->where('mer_id', $merId)->find();
  237. if ($merchant) {
  238. $mer_money = bcadd($merchant[$field], $num, 2);
  239. $merchant[$field] = $mer_money;
  240. $merchant->save();
  241. }
  242. }
  243. /**
  244. * 减少商户余额
  245. * @param int $merId
  246. * @param float $num
  247. * @author Qinii
  248. * @day 3/19/21
  249. */
  250. public function subMoney(int $merId, float $num)
  251. {
  252. $field = 'mer_money';
  253. $merchant = $this->getModel()::getDB()->where('mer_id', $merId)->find();
  254. if ($merchant) {
  255. $mer_money = bcsub($merchant[$field], $num, 2);
  256. $merchant[$field] = $mer_money;
  257. $merchant->save();
  258. }
  259. }
  260. public function clearTypeId(int $typeId)
  261. {
  262. return Merchant::getDB()->where('type_id', $typeId)->update(['type_id' => 0]);
  263. }
  264. public function addFieldNum(int $merId, int $num, string $field)
  265. {
  266. if ($num < 0) $num = -$num;
  267. $merchant = $this->getModel()::getDB()->where('mer_id', $merId)->find();
  268. $number = $merchant[$field] + $num;
  269. $merchant[$field] = $number;
  270. $merchant->save();
  271. }
  272. public function sumFieldNum(int $merId, int $num, string $field)
  273. {
  274. if ($num < 0) $num = -$num;
  275. $merchant = $this->getModel()::getDB()->where('mer_id', $merId)->find();
  276. $number = $merchant[$field] - $num;
  277. $merchant[$field] = $number;
  278. $merchant->save();
  279. }
  280. public function merIdByImage($merIds)
  281. {
  282. return $this->getModel()::getDB()->whereIn('mer_id', $merIds)->column('mer_id,mer_avatar');
  283. }
  284. public function updateMargin($typeId, $margin, $is_margin)
  285. {
  286. return $this->getModel()::where('type_id',$typeId)->where('is_margin','in',[0,1])->update([
  287. 'is_margin' => $is_margin,
  288. 'margin' => $margin
  289. ]);
  290. }
  291. }