DataDownloadOrder.php 12 KB

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