MovieOrderDao.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020/6/1
  7. *
  8. *
  9. */
  10. namespace app\common\dao\movie\order;
  11. use app\common\dao\BaseDao;
  12. use app\common\model\movie\order\MovieOrder;
  13. use app\common\model\user\User;
  14. use think\facade\Db;
  15. /**
  16. * Class MovieOrderDao
  17. * @package app\common\dao\movie\order
  18. * @author xaboy
  19. * @day 2020/6/8
  20. */
  21. class MovieOrderDao extends BaseDao
  22. {
  23. /**
  24. * @return string
  25. * @author xaboy
  26. * @day 2020/6/8
  27. */
  28. protected function getModel(): string
  29. {
  30. return MovieOrder::class;
  31. }
  32. /**
  33. * @param array $where
  34. * @param int $sysDel
  35. * @return \think\db\BaseQuery
  36. * @author xaboyCRMEB
  37. * @day 2020/6/16
  38. */
  39. public function search(array $where, $sysDel = 0)
  40. {
  41. return MovieOrder::getDB()
  42. ->when(($sysDel !== null), function ($query) use ($sysDel) {
  43. $query->where('is_system_del', $sysDel);
  44. })
  45. ->when(isset($where['pay_type']) && $where['pay_type'] != -1 && $where['pay_type'] >= 0, function ($query) use ($where) {
  46. $query->where('pay_type', $where['pay_type']);
  47. })
  48. ->when(isset($where['order_type']) && $where['order_type'] && $where['order_type'] >= 0, function ($query) use ($where) {
  49. $query->where('order_type', $where['order_type']);
  50. })
  51. ->when(isset($where['distribution_mode']) && $where['distribution_mode'] && $where['distribution_mode'] >= 0, function ($query) use ($where) {
  52. $query->where('distribution_mode', $where['distribution_mode']);
  53. })
  54. ->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  55. if ($where['status'] == -2)
  56. $query->where('paid', 1);
  57. else
  58. $query->where('status', $where['status']);
  59. })
  60. ->when(isset($where['uid']) && $where['uid'] !== '', function ($query) use ($where) {
  61. $query->where('uid', $where['uid']);
  62. })
  63. ->when(isset($where['take_order']), function ($query) use ($where) {
  64. $query->where('order_type', 1)->where('status', '>=', 2);
  65. })
  66. ->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  67. $query->where('mer_id', $where['mer_id']);
  68. })
  69. ->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  70. getModelTime($query, $where['date']);
  71. })
  72. ->when(isset($where['verify_date']) && $where['verify_date'] !== '', function ($query) use ($where) {
  73. getModelTime($query, $where['verify_date']);
  74. })
  75. ->when(isset($where['order_sn']) && $where['order_sn'] !== '', function ($query) use ($where) {
  76. $query->where('order_sn', 'like', '%' . $where['order_sn'] . '%');
  77. })
  78. ->when(isset($where['paid']) && $where['paid'] !== '', function ($query) use ($where) {
  79. $query->where('paid', $where['paid']);
  80. })
  81. ->when(isset($where['is_del']) && $where['is_del'] !== '', function ($query) use ($where) {
  82. $query->where('is_del', $where['is_del']);
  83. })
  84. ->when(isset($where['service_id']) && $where['service_id'] !== '', function ($query) use ($where) {
  85. $query->where('service_id', $where['service_id']);
  86. })
  87. ->when(isset($where['username']) && $where['username'] !== '', function ($query) use ($where) {
  88. $uid = User::where('nickname', 'like', "%{$where['username']}%")
  89. ->whereOr('phone', 'like', "%{$where['username']}%")
  90. ->column('uid');
  91. $query->where('uid', 'in', $uid);
  92. })
  93. ->when(isset($where['gzc']) && $where['gzc'] !== '', function ($query) use ($where) {
  94. $query->where('gzc', $where['gzc']);
  95. })
  96. ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
  97. $query->where(function ($query) use ($where) {
  98. $query->where('real_name', 'like', "%" . $where['keywords'] . "%")
  99. ->whereOr('user_phone', 'like', "%{$where['keywords']}%")
  100. ->whereOr('order_sn', 'like', '%' . $where['keywords'] . '%')
  101. ->whereOr('verify_code', 'like', '%' . $where['keywords'] . '%');
  102. });
  103. })
  104. ->when(isset($where['reconciliation_type']) && $where['reconciliation_type'] !== '', function ($query) use ($where) {
  105. $query->when($where['reconciliation_type'], function ($query) use ($where) {
  106. $query->where('reconciliation_id', '<>', 0);
  107. }, function ($query) use ($where) {
  108. $query->where('reconciliation_id', 0);
  109. });
  110. })->order('create_time DESC');
  111. }
  112. /**
  113. * @param array $where
  114. * @param int $sysDel
  115. * @return \think\db\BaseQuery
  116. * @author xaboyCRMEB
  117. * @day 2020/6/16
  118. */
  119. public function merSearch(array $where, $sysDel = 0)
  120. {
  121. return MovieOrder::getDB()
  122. ->alias('so')
  123. ->leftJoin('user_bill ub', 'ub.order_sn=so.order_sn')
  124. ->when(($sysDel !== null), function ($query) use ($sysDel) {
  125. $query->where('so.is_system_del', $sysDel);
  126. })
  127. ->when(isset($where['pay_type']) && $where['pay_type'] != -1 && $where['pay_type'] >= 0, function ($query) use ($where) {
  128. $query->where('so.pay_type', $where['pay_type']);
  129. })
  130. ->when(isset($where['order_type']) && $where['order_type'] && $where['order_type'] >= 0, function ($query) use ($where) {
  131. $query->where('so.order_type', $where['order_type']);
  132. })
  133. ->when(isset($where['distribution_mode']) && $where['distribution_mode'] && $where['distribution_mode'] >= 0, function ($query) use ($where) {
  134. $query->where('so.distribution_mode', $where['distribution_mode']);
  135. })
  136. ->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  137. if ($where['status'] == -2)
  138. $query->where('so.paid', 1);
  139. else
  140. $query->where('so.status', $where['status']);
  141. })
  142. ->when(isset($where['uid']) && $where['uid'] !== '', function ($query) use ($where) {
  143. $query->where('so.uid', $where['uid']);
  144. })
  145. ->when(isset($where['take_order']), function ($query) use ($where) {
  146. $query->where('so.order_type', 1)->where('status', '>=', 2);
  147. })
  148. ->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  149. $query->where('so.mer_id', $where['mer_id']);
  150. })
  151. ->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  152. getModelTime($query, $where['date']);
  153. })
  154. ->when(isset($where['verify_date']) && $where['verify_date'] !== '', function ($query) use ($where) {
  155. getModelTime($query, $where['verify_date']);
  156. })
  157. ->when(isset($where['order_sn']) && $where['order_sn'] !== '', function ($query) use ($where) {
  158. $query->where('so.order_sn', 'like', '%' . $where['order_sn'] . '%');
  159. })
  160. ->when(isset($where['paid']) && $where['paid'] !== '', function ($query) use ($where) {
  161. $query->where('so.paid', $where['paid']);
  162. })
  163. ->when(isset($where['is_del']) && $where['is_del'] !== '', function ($query) use ($where) {
  164. $query->where('so.is_del', $where['is_del']);
  165. })
  166. ->when(isset($where['service_id']) && $where['service_id'] !== '', function ($query) use ($where) {
  167. $query->where('so.service_id', $where['service_id']);
  168. })
  169. ->when(isset($where['username']) && $where['username'] !== '', function ($query) use ($where) {
  170. $uid = User::where('so.nickname', 'like', "%{$where['username']}%")
  171. ->whereOr('so.phone', 'like', "%{$where['username']}%")
  172. ->column('so.uid');
  173. $query->where('so.uid', 'in', $uid);
  174. })
  175. ->when(isset($where['gzc']) && $where['gzc'] !== '', function ($query) use ($where) {
  176. $query->where('so.gzc', $where['gzc']);
  177. })
  178. ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
  179. $query->where(function ($query) use ($where) {
  180. $query->where('so.real_name', 'like', "%" . $where['keywords'] . "%")
  181. ->whereOr('so.user_phone', 'like', "%{$where['keywords']}%")
  182. ->whereOr('so.order_sn', 'like', '%' . $where['keywords'] . '%')
  183. ->whereOr('so.verify_code', 'like', '%' . $where['keywords'] . '%');
  184. });
  185. })
  186. ->when(isset($where['reconciliation_type']) && $where['reconciliation_type'] !== '', function ($query) use ($where) {
  187. $query->when($where['reconciliation_type'], function ($query) use ($where) {
  188. $query->where('so.reconciliation_id', '<>', 0);
  189. }, function ($query) use ($where) {
  190. $query->where('so.reconciliation_id', 0);
  191. });
  192. })->order('so.create_time DESC');
  193. }
  194. /**
  195. * @param $id
  196. * @param $uid
  197. * @return array|\think\Model|null
  198. * @throws \think\db\exception\DataNotFoundException
  199. * @throws \think\db\exception\DbException
  200. * @throws \think\db\exception\ModelNotFoundException
  201. * @author xaboy
  202. * @day 2020/6/11
  203. */
  204. public function userOrder($id, $uid)
  205. {
  206. return MovieOrder::getDB()->where('order_id', $id)->where('uid', $uid)->where('is_del', 0)->where('paid', 1)->where('is_system_del', 0)->find();
  207. }
  208. /**
  209. * @param array $where
  210. * @param $ids
  211. * @return \think\db\BaseQuery
  212. * @author xaboy
  213. * @day 2020/6/26
  214. */
  215. public function usersOrderQuery(array $where, $ids)
  216. {
  217. return MovieOrder::getDB()->whereIn('uid', $ids)->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  218. getModelTime($query, $where['date'], 'pay_time');
  219. })->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  220. $query->where('order_id|order_sn', $where['keyword']);
  221. })->where('paid', 1)->order('pay_time DESC');
  222. }
  223. /**
  224. * @param $field
  225. * @param $value
  226. * @param int|null $except
  227. * @return bool
  228. * @author xaboy
  229. * @day 2020/6/11
  230. */
  231. public function fieldExists($field, $value, ?int $except = null): bool
  232. {
  233. return ($this->getModel()::getDB())->when($except, function ($query) use ($field, $except) {
  234. $query->where($field, '<>', $except);
  235. })->where($field, $value)->count() > 0;
  236. }
  237. public function orderUserNum($date, $paid = null, $merId = null)
  238. {
  239. return MovieOrder::getDB()->when($paid, function ($query, $paid) {
  240. $query->where('paid', $paid);
  241. })->when($merId, function ($query, $merId) {
  242. $query->where('mer_id', $merId);
  243. })->when($date, function ($query, $date) {
  244. getModelTime($query, $date, 'pay_time');
  245. })->group('uid')->count();
  246. }
  247. public function orderUserGroup($date, $paid = null, $merId = null)
  248. {
  249. return MovieOrder::getDB()->when($paid, function ($query, $paid) {
  250. $query->where('paid', $paid);
  251. })->when($merId, function ($query, $merId) {
  252. $query->where('mer_id', $merId);
  253. })->when($date, function ($query, $date) {
  254. getModelTime($query, $date, 'pay_time');
  255. })->group('uid')->field(Db::raw('uid,sum(pay_price) as pay_price'))->select();
  256. }
  257. public function orderPrice($date, $paid = null, $merId = null)
  258. {
  259. return MovieOrder::getDB()->when($paid, function ($query, $paid) {
  260. $query->where('paid', $paid);
  261. })->when($merId, function ($query, $merId) {
  262. $query->where('mer_id', $merId);
  263. })->when($date, function ($query, $date) {
  264. getModelTime($query, $date, 'pay_time');
  265. })->sum('pay_price');
  266. }
  267. public function orderGroupNum($date, $merId = null)
  268. {
  269. return MovieOrder::getDB()->field(Db::raw('sum(pay_price) as pay_price,count(*) as total,count(distinct uid) as user,from_unixtime(unix_timestamp(pay_time),\'%m-%d\') as `day`'))
  270. ->where('paid', 1)->when($date, function ($query, $date) {
  271. getModelTime($query, $date, 'pay_time');
  272. })->when($merId, function ($query, $merId) {
  273. $query->where('mer_id', $merId);
  274. })->order('day ASC')->group('day')->select();
  275. }
  276. public function orderGroupNumPage($where, $page, $limit, $merId = null)
  277. {
  278. return MovieOrder::getDB()->when(isset($where['dateRange']), function ($query) use ($where) {
  279. getModelTime($query, date('Y/m/d H:i:s', $where['dateRange']['start']) . '-' . date('Y/m/d H:i:s', $where['dateRange']['stop']), 'pay_time');
  280. })->field(Db::raw('sum(pay_price) as pay_price,count(*) as total,count(distinct uid) as user,from_unixtime(unix_timestamp(pay_time),\'%m-%d\') as `day`'))
  281. ->where('paid', 1)->when($merId, function ($query, $merId) {
  282. $query->where('mer_id', $merId);
  283. })->order('day DESC')->page($page, $limit)->group('day')->select();
  284. }
  285. }