UserBillDao.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-05-07
  7. *
  8. *
  9. */
  10. namespace app\common\dao\user;
  11. use app\common\dao\BaseDao;
  12. use app\common\enum\user\UserBillEnum;
  13. use app\common\model\BaseModel;
  14. use app\common\model\user\UserBill;
  15. use app\entity\CommonEntity;
  16. use app\entity\data\user\UserBillEntity;
  17. use think\db\exception\DataNotFoundException;
  18. use think\db\exception\DbException;
  19. use think\db\exception\ModelNotFoundException;
  20. /**
  21. * Class UserBillDao
  22. * @package app\common\dao\user
  23. * @author xaboy
  24. * @day 2020/6/22
  25. */
  26. class UserBillDao extends BaseDao
  27. {
  28. /**
  29. * @return BaseModel
  30. * @author xaboy
  31. * @day 2020-03-30
  32. */
  33. protected function getModel(): string
  34. {
  35. return UserBill::class;
  36. }
  37. /**
  38. * @param array $where
  39. * @param $data
  40. * @return int
  41. * @throws \think\db\exception\DbException
  42. * @author xaboy
  43. * @day 2020/6/22
  44. */
  45. public function updateBill(array $where, $data)
  46. {
  47. return UserBill::getDB()->where($where)->limit(1)->update($data);
  48. }
  49. /**
  50. * @param $time
  51. * @return \think\Collection
  52. * @throws \think\db\exception\DataNotFoundException
  53. * @throws \think\db\exception\DbException
  54. * @throws \think\db\exception\ModelNotFoundException
  55. * @author xaboy
  56. * @day 2020/6/22
  57. */
  58. public function getTimeoutBrokerageBill($time)
  59. {
  60. return UserBill::getDB()->where('create_time', '<=', $time)->where('category', 'brokerage')
  61. ->whereIn('type', ['order_one', 'order_two'])->with('user')->where('status', 0)->select();
  62. }
  63. /**
  64. * @param $uid
  65. * @return float
  66. * @author xaboy
  67. * @day 2020/6/22
  68. */
  69. public function lockBrokerage($uid)
  70. {
  71. return UserBill::getDB()->where('category', 'brokerage')
  72. ->whereIn('type', ['order_one', 'order_two'])->where('uid', $uid)->where('status', 0)->sum('number');
  73. }
  74. /**
  75. * @param $uid
  76. * @return float
  77. * @author xaboy
  78. * @day 2020/6/22
  79. */
  80. public function totalBrokerage($uid)
  81. {
  82. return UserBill::getDB()->where('category', 'brokerage')
  83. ->whereIn('type', ['order_one', 'order_two'])->where('uid', $uid)->sum('number');
  84. }
  85. /**
  86. * @param $uid
  87. * @return float
  88. * @author xaboy
  89. * @day 2020/6/22
  90. */
  91. public function yesterdayBrokerage($uid)
  92. {
  93. return getModelTime(UserBill::getDB()->where('category', 'brokerage')
  94. ->whereIn('type', ['order_one', 'order_two'])->where('uid', $uid), 'yesterday')->sum('number');
  95. }
  96. /**
  97. * @param array $where
  98. * @return \think\db\BaseQuery
  99. * @author xaboy
  100. * @day 2020/6/22
  101. */
  102. public function search(array $where)
  103. {
  104. return UserBill::getDB()->when(isset($where['now_money']) && in_array($where['now_money'], [0, 1, 2]), function ($query) use ($where) {
  105. if ($where['now_money'] == 0)
  106. $query->where('category', 'now_money')->whereIn('type', ['pay_product', 'recharge']);
  107. else if ($where['now_money'] == 1)
  108. $query->where('category', 'now_money')->whereIn('type', 'pay_product');
  109. else if ($where['now_money'] == 2)
  110. $query->where('category', 'now_money')->whereIn('type', 'recharge');
  111. })->when(isset($where['uid']) && $where['uid'] !== '', function ($query) use ($where) {
  112. $query->where('uid', $where['uid']);
  113. })->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  114. $query->where('mer_id', $where['mer_id']);
  115. })->when(isset($where['pm']) && $where['pm'] !== '', function ($query) use ($where) {
  116. $query->where('pm', $where['pm']);
  117. })->when(isset($where['category']) && $where['category'] !== '', function ($query) use ($where) {
  118. $query->where('category', $where['category']);
  119. })->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  120. $query->where('status', $where['status']);
  121. })->when(isset($where['is_red_brokerage']) && $where['is_red_brokerage'] !== '', function ($query) use ($where) {
  122. $query->where('is_red_brokerage', $where['is_red_brokerage']);
  123. })->when(isset($where['commission_type']) && !empty($where['commission_type']), function ($query) use ($where) {
  124. $query->whereIn('commission_type', $where['commission_type']);
  125. })->when(isset($where['not_commission_type']) && !empty($where['not_commission_type']), function ($query) use ($where) {
  126. $query->whereNotIn('commission_type', $where['not_commission_type']);
  127. });
  128. }
  129. public function searchJoin(array $where)
  130. {
  131. return UserBill::getDB()
  132. ->alias('a')
  133. // ->join('User b', 'a.uid = b.uid')
  134. ->Join('merchant_intention c', 'a.mer_id = c.mer_id')
  135. ->Join('user_city d', 'c.city_id = d.city_id')
  136. ->where("a.status",1)
  137. // ->where('a.source', 1)
  138. ->where("a.type_shop",0)
  139. // ->where("a.mer_id",'>',0)
  140. ->field('a.bill_id,a.pm,a.title,a.number,a.balance,a.mark,a.create_time,a.status,a.uid,a.order_sn,a.source,a.mer_id,a.commission_type')
  141. ->field('c.mer_name')
  142. ->field('d.city_name')
  143. ->group("a.order_sn")
  144. ->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  145. $query->where('a.type', $where['type']);
  146. })
  147. ->when(isset($where['user_time']) && $where['user_time'] !== '', function ($query) use ($where) {
  148. getModelTime($query, $where['user_time'], 'a.create_time');
  149. })
  150. // ->when(isset($where['city_id']) && $where['city_id'], function ($query) use ($where) {
  151. // $query->where('c.city_id', $where['city_id']);
  152. // })
  153. ->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  154. $query->whereLike('c.mer_name', "%{$where['keyword']}%");
  155. });
  156. }
  157. public function searchJoins(array $where)
  158. {
  159. return UserBill::getDB()
  160. ->alias('a')
  161. ->join('User b', 'a.uid = b.uid')
  162. ->field('a.bill_id,a.pm,a.title,a.number,a.balance,a.mark,a.create_time,a.status,b.nickname,b.phone,a.uid,a.order_sn,a.source')
  163. ->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  164. $query->where('a.type', $where['type']);
  165. })->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  166. getModelTime($query, $where['date'], 'a.create_time');
  167. })->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  168. $query->whereLike('a.uid|b.nickname', "%{$where['keyword']}%");
  169. });
  170. }
  171. public function refundBrokerage($order_id, $uid)
  172. {
  173. return UserBill::getDB()->where('link_id', $order_id)->where('uid', $uid)
  174. ->where('category', 'brokerage')->whereIn('type', ['refund_two', 'refund_one'])->sum('number');
  175. }
  176. public function type()
  177. {
  178. return UserBill::getDB()->group('type')->column('title,type');
  179. }
  180. /**
  181. * @param array $billIdList
  182. * @return UserBillEntity[]
  183. */
  184. public function getUserBillEntityListByBillIdList(array $billIdList): array
  185. {
  186. $entityList = [];
  187. try {
  188. $dataList = UserBill::getDB()
  189. ->whereIn('bill_id', $billIdList)
  190. ->select()
  191. ->toArray();
  192. if (!empty($dataList)) {
  193. $entityList = array_map(function ($data) {
  194. return UserBillEntity::newInstance($data);
  195. }, $dataList);
  196. }
  197. } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
  198. }
  199. return $entityList;
  200. }
  201. /**
  202. * @param string $typeShop
  203. * @param array $orderIdList
  204. * @return UserBillEntity[]
  205. */
  206. public function getUserBillEntityListByOrderIdListAndPending(string $typeShop, array $orderIdList): array
  207. {
  208. $entityList = [];
  209. try {
  210. $dataList = UserBill::getDB()
  211. ->where('type_shop', '=', $typeShop)
  212. ->whereIn('link_id', $orderIdList)
  213. ->where('status', '=', UserBillEnum::STATUS['PENDING']['code'])
  214. ->select()
  215. ->toArray();
  216. if (!empty($dataList)) {
  217. $entityList = array_map(function ($data) {
  218. return UserBillEntity::newInstance($data);
  219. }, $dataList);
  220. }
  221. } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
  222. }
  223. return $entityList;
  224. }
  225. /**
  226. * @param string $typeShop
  227. * @param string $orderSn
  228. * @return UserBillEntity
  229. */
  230. public function getUserBillEntityByTypeShopAndOrderSn(string $typeShop, string $orderSn): UserBillEntity
  231. {
  232. $entity = UserBillEntity::newInstance();
  233. try {
  234. $dataRes = UserBill::getDB()
  235. ->where('type_shop', '=', $typeShop)
  236. ->where('order_sn', '=', $orderSn)
  237. ->find()
  238. ->toArray();
  239. if (!empty($dataRes)) {
  240. $entity = UserBillEntity::newInstance($dataRes);
  241. }
  242. } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
  243. }
  244. return $entity;
  245. }
  246. /**
  247. * 批量更新
  248. * @param $dataList
  249. * @return array
  250. */
  251. public function saveAll($dataList): array
  252. {
  253. try {
  254. return $this->getModel()::getInstance()->allowField(array_keys(reset($dataList)))->saveAll($dataList)->toArray();
  255. } catch (\Exception $e) {
  256. return [];
  257. }
  258. }
  259. /**
  260. * 根据order_id与uid查找养老金值(link_id,status=1, pm=1)
  261. */
  262. public function getYanglaoFromUidOrderId($orderId, $uid)
  263. {
  264. try {
  265. return $this::getModel()::getDB()
  266. ->where('link_id', '=', $orderId)
  267. ->where('status', '=', 1)
  268. ->where('pm', '=', 1)
  269. ->where('uid', '=', $uid)
  270. ->value('number');
  271. } catch (\Exception $e) {
  272. return [];
  273. }
  274. }
  275. /**
  276. * 根据order_id与uid查找佣金(commission_type=5)
  277. */
  278. public function getYljFromCommissionTypeUidOrderId($orderId, $uid)
  279. {
  280. try {
  281. return $this::getModel()::getDB()
  282. ->where('link_id', '=', $orderId)
  283. ->where('commission_type', '=', 5)
  284. ->where('uid', '=', $uid)
  285. ->value('number');
  286. } catch (\Exception $e) {
  287. return [];
  288. }
  289. }
  290. /**
  291. * 根据order_id与uid查找佣金(commission_type=3)
  292. */
  293. public function getYongjinFromCommissionTypeUidOrderId($orderId, $uid)
  294. {
  295. try {
  296. return $this::getModel()::getDB()
  297. ->where('link_id', '=', $orderId)
  298. ->where('commission_type', '=', 3)
  299. ->where('uid', '=', $uid)
  300. ->value('number');
  301. } catch (\Exception $e) {
  302. return [];
  303. }
  304. }
  305. /**
  306. * 根据order_id查找第二推荐佣金uid(commission_type=3,排除当前用户和一级推荐人)
  307. */
  308. public function getYongjinTwoUidFromOrderId($orderId, $uid, $spreadUid)
  309. {
  310. try {
  311. $value = $this::getModel()::getDB()
  312. ->where('link_id', '=', $orderId)
  313. ->where('commission_type', '=', 3)
  314. ->whereNotIn('uid', [$uid, $spreadUid])
  315. ->value('uid');
  316. return $value ?: 0;
  317. } catch (\Exception $e) {
  318. return 0;
  319. }
  320. }
  321. /**
  322. * @param $params
  323. * @return array
  324. * @throws DataNotFoundException
  325. * @throws DbException
  326. * @throws ModelNotFoundException
  327. * @author 史晨
  328. * @date 2026/2/11 14:45
  329. * 获取数据列表
  330. */
  331. public function getListData($params)
  332. {
  333. $query = $this::getModel()::getDB();
  334. if (isset($params['start']) && $params['start'] != '') {
  335. if (!is_numeric($params['start'])) {
  336. $params['start'] = strtotime($params['start']);
  337. }
  338. $query->where('take_time', '>=', $params['start']);
  339. }
  340. if (isset($params['end']) && $params['end'] != '') {
  341. if (!is_numeric($params['end'])) {
  342. $params['end'] = strtotime($params['end']);
  343. }
  344. $query->where('take_time', '<=', $params['end']);
  345. }
  346. if (isset($params['status']) && $params['status'] != '') {
  347. $query->where('status', '=', $params['status']);
  348. }
  349. if (isset($params['merId']) && $params['merId'] != '') {
  350. $query->whereIn('mer_id', $params['merId']);
  351. }
  352. if (isset($params['commissionType']) && $params['commissionType'] != '') {
  353. $query->whereIn('commission_type', $params['commissionType']);
  354. }
  355. return $query->select()->toArray();
  356. }
  357. }