TestPaperOrder.php 12 KB

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