DataDownloadOrder.php 11 KB

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