TestPaperOrder.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model\order;
  12. use traits\ModelTrait;
  13. use basic\ModelBasic;
  14. use app\admin\model\questions\TestPaper as TestPaperModel;
  15. use app\admin\model\user\User;
  16. use app\admin\model\user\UserBill;
  17. use service\PhpSpreadsheetService;
  18. /**
  19. * 试卷订单 model
  20. * Class TestPaperOrder
  21. * @package app\admin\model\order
  22. */
  23. class TestPaperOrder extends ModelBasic
  24. {
  25. use ModelTrait;
  26. public static function orderCount()
  27. {
  28. $data['wz'] = self::statusByWhere(0, new self(), '')->count();
  29. $data['wf'] = self::statusByWhere(1, new self(), '')->count();
  30. $data['tk'] = self::statusByWhere(-1, new self(), '')->count();
  31. $data['yt'] = self::statusByWhere(-2, new self(), '')->count();
  32. return $data;
  33. }
  34. public static function statusByWhere($status, $model = null, $alert = '')
  35. {
  36. if ($model == null) $model = new self;
  37. $model = $model->where($alert . 'is_system_del', 0);
  38. if ('' === $status)
  39. return $model;
  40. else if ($status == 0)//未支付
  41. return $model->where($alert . 'paid', 0)->where($alert . 'is_system_del', 0)->where($alert . 'status', 0)->where($alert . 'refund_status', 0);
  42. else if ($status == 1)//已支付
  43. return $model->where($alert . 'paid', 1)->where($alert . 'is_system_del', 0)->where($alert . 'status', 0)->where($alert . 'refund_status', 0);
  44. else if ($status == -1)//退款中
  45. return $model->where($alert . 'paid', 1)->where($alert . 'is_system_del', 0)->where($alert . 'refund_status', 1);
  46. else if ($status == -2)//已退款
  47. return $model->where($alert . 'paid', 1)->where($alert . 'is_system_del', 0)->where($alert . 'refund_status', 2);
  48. else
  49. return $model;
  50. }
  51. public static function getBadge($where)
  52. {
  53. $price = self::getOrderPrice($where);
  54. return [
  55. [
  56. 'name' => '订单数量',
  57. 'field' => '件',
  58. 'count' => $price['order_sum'],
  59. 'background_color' => 'layui-bg-blue',
  60. 'col' => 3
  61. ],
  62. [
  63. 'name' => '出售试卷',
  64. 'field' => '套',
  65. 'count' => $price['total_num'],
  66. 'background_color' => 'layui-bg-blue',
  67. 'col' => 3
  68. ],
  69. [
  70. 'name' => '订单金额',
  71. 'field' => '元',
  72. 'count' => $price['pay_price'],
  73. 'background_color' => 'layui-bg-blue',
  74. 'col' => 3
  75. ],
  76. [
  77. 'name' => '退款金额',
  78. 'field' => '元',
  79. 'count' => $price['refund_price'],
  80. 'background_color' => 'layui-bg-blue',
  81. 'col' => 3
  82. ],
  83. [
  84. 'name' => '微信支付金额',
  85. 'field' => '元',
  86. 'count' => $price['pay_price_wx'],
  87. 'background_color' => 'layui-bg-blue',
  88. 'col' => 3
  89. ],
  90. [
  91. 'name' => '余额支付金额',
  92. 'field' => '元',
  93. 'count' => $price['pay_price_yue'],
  94. 'background_color' => 'layui-bg-blue',
  95. 'col' => 3
  96. ],
  97. [
  98. 'name' => '支付宝支付金额',
  99. 'field' => '元',
  100. 'count' => $price['pay_price_zhifubao'],
  101. 'background_color' => 'layui-bg-blue',
  102. 'col' => 3
  103. ]
  104. ];
  105. }
  106. /**
  107. * 处理订单金额
  108. * @param $where
  109. * @return array
  110. */
  111. public static function getOrderPrice($where)
  112. {
  113. $price = array();
  114. $price['pay_price'] = 0;//支付金额
  115. $price['refund_price'] = 0;//退款金额
  116. $price['pay_price_wx'] = 0;//微信支付金额
  117. $price['pay_price_yue'] = 0;//余额支付金额
  118. $price['pay_price_zhifubao'] = 0;//支付宝支付金额
  119. $price['pay_price_other'] = 0;//其他支付金额
  120. $list = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->field([
  121. 'sum(a.total_num) as total_num',
  122. 'sum(a.pay_price) as pay_price',
  123. 'sum(a.refund_price) as refund_price'])->find()->toArray();
  124. $price['total_num'] = $list['total_num'];//商品总数
  125. $price['pay_price'] = $list['pay_price'];//支付金额
  126. $price['refund_price'] = $list['refund_price'];//退款金额
  127. $list = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->field('sum(a.pay_price) as pay_price,a.pay_type')->group('a.pay_type')->select()->toArray();
  128. foreach ($list as $v) {
  129. if ($v['pay_type'] == 'weixin') {
  130. $price['pay_price_wx'] = $v['pay_price'];
  131. } elseif ($v['pay_type'] == 'yue') {
  132. $price['pay_price_yue'] = $v['pay_price'];
  133. } elseif ($v['pay_type'] == 'zhifubao') {
  134. $price['pay_price_zhifubao'] = $v['pay_price'];
  135. } else {
  136. $price['pay_price_other'] = $v['pay_price'];
  137. }
  138. }
  139. $price['order_sum'] = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->count();
  140. return $price;
  141. }
  142. /**
  143. * 处理where条件
  144. * @param $where
  145. * @param $model
  146. * @return mixed
  147. */
  148. public static function getOrderWhere($where, $model, $aler = '', $join = '')
  149. {
  150. if ($where['status'] != '') {
  151. $model = self::statusByWhere($where['status'], $model, $aler);
  152. } else {
  153. $model = $model->where($aler . 'is_system_del', 0);
  154. }
  155. if ($where['real_name'] != '') {
  156. $model = $model->where($aler . 'order_id|' . $aler . 'uid' . ($join ? '|' . $join . '.nickname|' . $join . '.uid|' . $join . '.phone' : ''), 'LIKE', "%$where[real_name]%");
  157. }
  158. if ($where['data'] !== '') {
  159. $model = self::getModelTime($where, $model, $aler . 'add_time');
  160. }
  161. return $model;
  162. }
  163. public static function OrderList($where)
  164. {
  165. $model = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->field('a.*,r.nickname,r.phone');
  166. if ($where['order'] != '') {
  167. $model = $model->order(self::setOrder($where['order']));
  168. } else {
  169. $model = $model->order('a.id desc');
  170. }
  171. if (isset($where['excel']) && $where['excel'] == 1) {
  172. $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
  173. } else {
  174. $data = ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
  175. }
  176. foreach ($data as &$item) {
  177. $item['_info'] = TestPaperModel::where('id', $item['test_id'])->find();
  178. $item['order_name'] = '[考试订单]';
  179. $item['color'] = '#895612';
  180. if ($item['paid'] == 1) {
  181. switch ($item['pay_type']) {
  182. case 'weixin':
  183. $item['pay_type_name'] = '微信支付';
  184. break;
  185. case 'yue':
  186. $item['pay_type_name'] = '余额支付';
  187. break;
  188. case 'zhifubao':
  189. $item['pay_type_name'] = '支付宝支付';
  190. break;
  191. default:
  192. $item['pay_type_name'] = '其他支付';
  193. break;
  194. }
  195. } else {
  196. switch ($item['pay_type']) {
  197. case 'offline':
  198. $item['pay_type_name'] = '线下支付';
  199. $item['pay_type_info'] = 1;
  200. break;
  201. default:
  202. $item['pay_type_name'] = '未支付';
  203. break;
  204. }
  205. }
  206. if ($item['paid'] == 0 && $item['status'] == 0) {
  207. $item['status_name'] = '未支付';
  208. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['refund_status'] == 0) {
  209. $item['status_name'] = '已支付';
  210. } else if ($item['paid'] == 1 && $item['refund_status'] == 1) {
  211. $item['status_name'] = <<<HTML
  212. <b style="color:#f124c7">申请退款</b><br/>
  213. HTML;
  214. } else if ($item['paid'] == 1 && $item['refund_status'] == 2) {
  215. $item['status_name'] = '已退款';
  216. }
  217. if ($item['paid'] == 0 && $item['status'] == 0 && $item['refund_status'] == 0) {
  218. $item['_status'] = 1;
  219. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['refund_status'] == 0) {
  220. $item['_status'] = 2;
  221. } else if ($item['paid'] == 1 && $item['refund_status'] == 1) {
  222. $item['_status'] = 3;
  223. } else if ($item['paid'] == 1 && $item['refund_status'] == 2) {
  224. $item['_status'] = 7;
  225. }
  226. }
  227. if (isset($where['excel']) && $where['excel'] == 1) {
  228. self::SaveExcel($data);
  229. }
  230. $count = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->count();
  231. return compact('count', 'data');
  232. }
  233. /**
  234. * 保存并下载excel
  235. * $list array
  236. * return
  237. */
  238. public static function SaveExcel($list)
  239. {
  240. $export = [];
  241. foreach ($list as $index => $item) {
  242. $_info = TestPaperModel::where('id', $item['test_id'])->find();
  243. if ($_info) {
  244. $goodsName = $_info['title'] . '| ' . $_info['money'];
  245. } else {
  246. $goodsName = '考试被删除';
  247. }
  248. if ($item['paid'] == 0 && $item['status'] == 0) {
  249. $item['status_name'] = '未支付';
  250. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['refund_status'] == 0) {
  251. $item['status_name'] = '已支付';
  252. } else if ($item['paid'] == 1 && $item['refund_status'] == 1) {
  253. $item['status_name'] = '退款中';
  254. } else if ($item['paid'] == 1 && $item['refund_status'] == 2) {
  255. $item['status_name'] = '已退款';
  256. }
  257. $export[] = [
  258. $item['order_id'], $item['pay_type_name'],
  259. $item['total_num'], $item['total_price'], $item['pay_price'], $item['refund_price'],
  260. $goodsName,
  261. $item['paid'] == 1 ? '已支付' : '未支付' . '/支付时间: ' . ($item['pay_time'] > 0 ? date('Y/m/d H:i', $item['pay_time']) : '暂无'),
  262. $item['status_name'],
  263. $item['nickname'] . '/' . $item['uid'],
  264. $item['phone']
  265. ];
  266. }
  267. $filename = '考试订单导出' . time() . '.xlsx';
  268. $head = ['订单号', '支付方式', '试卷总数', '试卷总价', '支付金额', '退款金额', '试卷信息', '支付状态', '订单状态', '微信昵称/UID', '手机号'];
  269. PhpSpreadsheetService::outdata($filename, $export, $head);
  270. }
  271. }