UserBillDao.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 $uid
  182. * @return CommonEntity
  183. */
  184. public function getUserBillEntityByBillId($uid): CommonEntity
  185. {
  186. $data = [];
  187. try {
  188. $dataRes = UserBill::getDB()
  189. ->where('id', $uid)
  190. ->find();
  191. if (!empty($dataRes)) {
  192. $data = $dataRes->toArray();
  193. }
  194. } catch (\Exception $e) {
  195. }
  196. return UserBillEntity::newInstance($data);
  197. }
  198. /**
  199. * @param string $typeShop
  200. * @param array $orderIdList
  201. * @return UserBillEntity[]
  202. */
  203. public function getUserBillEntityListByOrderIdListAndPending(string $typeShop, array $orderIdList): array
  204. {
  205. $entityList = [];
  206. try {
  207. $dataList = UserBill::getDB()
  208. ->where('type_shop', '=', $typeShop)
  209. ->whereIn('order_id', $orderIdList)
  210. ->where('status', '=', UserBillEnum::STATUS['PENDING']['code'])
  211. ->select()
  212. ->toArray();
  213. if (!empty($dataList)) {
  214. $entityList = array_map(function ($data) {
  215. return UserBillEntity::newInstance($data);
  216. }, $dataList);
  217. }
  218. } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
  219. }
  220. return $entityList;
  221. }
  222. }