EventSignUp.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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\ump;
  12. use traits\ModelTrait;
  13. use basic\ModelBasic;
  14. use think\Db;
  15. use app\merchant\model\user\User;
  16. use app\merchant\model\ump\EventData as EventDataModel;
  17. use service\PhpSpreadsheetService;
  18. /**
  19. * 活动报名
  20. * Class EventSignUp
  21. * @package app\merchant\model\ump
  22. */
  23. class EventSignUp extends ModelBasic
  24. {
  25. use ModelTrait;
  26. public static function orderCount($mer_id)
  27. {
  28. $data['wz'] = self::statusByWhere('', new self(), '', 0, $mer_id)->count();
  29. $data['wf'] = self::statusByWhere('', new self(), '', 1, $mer_id)->count();
  30. $data['yt'] = self::statusByWhere('', new self(), '', -2, $mer_id)->count();
  31. return $data;
  32. }
  33. public static function getUserSignUpAll($where)
  34. {
  35. $model = self::getOrderWhere($where, self::alias('s')->join('user r', 'r.uid=s.uid', 'LEFT'), 's.', 'r')->field('s.*,r.nickname,r.phone');
  36. $model = $model->order('s.add_time DESC');
  37. if (isset($where['excel']) && $where['excel'] >= 1) {
  38. $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
  39. } else {
  40. $data = $model->page((int)$where['page'], (int)$where['limit'])->select();
  41. $data = count($data) ? $data->toArray() : [];
  42. }
  43. foreach ($data as &$v) {
  44. $v['addTime'] = date('Y-m-d H:i:s', $v['add_time']);
  45. $v['refund_reason_time'] = date('Y-m-d H:i:s', $v['refund_reason_time']);
  46. $event = EventRegistration::where('id', $v['activity_id'])->field('is_fill,title,image')->find();
  47. $v['order_name'] = '[活动订单]';
  48. $v['_info'] = $event;
  49. $v['color'] = '#895612';
  50. $v['is_fill'] = $event['is_fill'];
  51. $v['number'] = $v['number'] <= 0 ? 1 : $v['number'];
  52. if ($v['pay_type'] == 'weixin') {
  53. $v['pay_type'] = '微信支付';
  54. } elseif ($v['pay_type'] == 'zhifubao') {
  55. $v['pay_type'] = '支付宝支付';
  56. } elseif ($v['pay_type'] == 'yue') {
  57. $v['pay_type'] = '余额支付';
  58. } else {
  59. $v['pay_type'] = '其他支付';
  60. }
  61. if ($v['status']) {
  62. $v['write_off'] = '已核销';
  63. } else {
  64. $v['write_off'] = '未核销';
  65. }
  66. if ($v['user_info'] && $v['is_fill']) {
  67. $user_info = json_decode($v['user_info']);
  68. if (is_array($user_info)) {
  69. $userInfo = '';
  70. foreach ($user_info as $ks => $item) {
  71. if(is_array($item->event_value)) $item->event_value=implode(',',$item->event_value);
  72. $userInfo .= '<b >' . $item->event_name . ':' . $item->event_value . '</b><br/>';
  73. }
  74. $v['userInfo'] = $userInfo;
  75. } else {
  76. if ($user_info->sex == 1) {
  77. $sex = '男';
  78. } elseif ($user_info->sex == 2) {
  79. $sex = '女';
  80. } else {
  81. $sex = '保密';
  82. }
  83. $v['userInfo'] = <<<HTML
  84. <b >姓名:$user_info->name</b><br/>
  85. <b >电话:$user_info->phone</b><br/>
  86. <b >性别:$sex</b><br/>
  87. <b >年龄:$user_info->age</b><br/>
  88. <b >公司:$user_info->company</b><br/>
  89. <b >备注:$user_info->remarks</b><br/>
  90. HTML;
  91. }
  92. } else {
  93. $v['userInfo'] = '无';
  94. }
  95. if ($v['paid'] == 0) {
  96. $v['status_name'] = '未支付';
  97. } else if ($v['paid'] == 1 && $v['refund_status'] == 0) {
  98. $v['status_name'] = '已支付';
  99. } else if ($v['paid'] == 1 && $v['refund_status'] == 2) {
  100. $v['status_name'] = '已退款';
  101. }
  102. if ($v['paid'] == 0 && $v['refund_status'] == 0) {
  103. $v['_status'] = 1;
  104. } else if ($v['paid'] == 1 && $v['refund_status'] == 0) {
  105. $v['_status'] = 2;
  106. } else if ($v['paid'] == 1 && $v['refund_status'] == 2) {
  107. $v['_status'] = 7;
  108. }
  109. }
  110. if (isset($where['excel']) && $where['excel'] == 1) {
  111. self::SaveExcel1($data);
  112. }
  113. if (isset($where['excel']) && $where['excel'] == 2) {
  114. self::SaveExcel2($data);
  115. }
  116. $count = self::getOrderWhere($where, self::alias('s')->join('user r', 'r.uid=s.uid', 'LEFT'), 's.', 'r')->count();
  117. return compact('data', 'count');
  118. }
  119. public static function statusByWhere($status, $model = null, $alert = '', $type = 0, $mer_id = 0)
  120. {
  121. if ($model == null) $model = new self;
  122. switch ($type) {
  123. case 1:
  124. $model = $model->where($alert . 'refund_status', 0);
  125. break;
  126. case -2:
  127. $model = $model->where($alert . 'refund_status', 2);
  128. break;
  129. }
  130. if ($mer_id) $model = $model->where($alert . 'mer_id', $mer_id);
  131. $model = $model->where($alert . 'is_del', 0);
  132. if ('' === $status)
  133. return $model->where($alert . 'paid', 1);
  134. else if ($status == 1)//已支付 未核销
  135. return $model->where($alert . 'paid', 1)->where($alert . 'status', 0);
  136. else if ($status == 2)//已支付 已核销
  137. return $model->where($alert . 'paid', 1)->where($alert . 'status', 1);
  138. else
  139. return $model->where($alert . 'paid', 1);
  140. }
  141. /**
  142. * 保存并下载excel
  143. * $list array
  144. * return
  145. */
  146. public static function SaveExcel1($list)
  147. {
  148. $export = [];
  149. foreach ($list as $index => $item) {
  150. $title = $item['_info']['title'];
  151. if ($item['user_info'] && $item['is_fill']) {
  152. $user_info = json_decode($item['user_info']);
  153. if (is_array($user_info)) {
  154. $userInfo = '';
  155. foreach ($user_info as $ks => $value) {
  156. $userInfo .= $value->event_name . ':' . $value->event_value . "\n";
  157. }
  158. } else {
  159. if ($user_info->sex == 1) {
  160. $sex = '男';
  161. } elseif ($user_info->sex == 2) {
  162. $sex = '女';
  163. } else {
  164. $sex = '保密';
  165. }
  166. $userInfo = '姓名:' . $user_info->name . "\n"
  167. . '电话:' . $user_info->phone . "\n"
  168. . '性别:' . $sex . "\n"
  169. . '年龄:' . $user_info->age . "\n"
  170. . '公司:' . $user_info->company . "\n"
  171. . '备注:' . $user_info->remarks;
  172. }
  173. } else {
  174. $userInfo = '无';
  175. }
  176. $refund_status = '';
  177. if ($item['refund_status'] == 2) {
  178. $refund_status = '金额:' . $item['refund_price'] . '元/时间:' . $item['refund_reason_time'];
  179. }
  180. $export[] = [
  181. $item['order_id'],
  182. $title,
  183. $userInfo,
  184. $item['pay_type'],
  185. $item['pay_price'],
  186. $item['status'] == 1 ? '已核销' : '未核销',
  187. $item['addTime'],
  188. $item['status_name'],
  189. $refund_status
  190. ];
  191. }
  192. $filename = '活动报名导出' . time() . '.xlsx';
  193. $head = ['订单号', '活动标题', '报名信息', '支付方式', '支付金额', '核销状态', '报名时间', '订单状态', '退款金额'];
  194. PhpSpreadsheetService::outdata($filename, $export, $head);
  195. }
  196. /**
  197. * 保存并下载excel
  198. * $list array
  199. * return
  200. */
  201. public static function SaveExcel2($list)
  202. {
  203. $export = [];
  204. foreach ($list as $index => $item) {
  205. $title = $item['_info']['title'];
  206. $refund_status = '';
  207. if ($item['refund_status'] == 2) {
  208. $refund_status = '金额:' . $item['refund_price'] . '元/时间:' . $item['refund_reason_time'];
  209. }
  210. $export[] = [
  211. $item['order_id'],
  212. $item['nickname'] . '/' . $item['uid'],
  213. $title . '/' . $item['activity_id'],
  214. $item['pay_type'],
  215. $item['pay_price'],
  216. $item['addTime'],
  217. $item['status_name'],
  218. $refund_status
  219. ];
  220. }
  221. $filename = '报名订单导出' . time() . '.xlsx';
  222. $head = ['订单号', '用户信息', '活动标题', '支付方式', '支付金额', '报名时间', '订单状态', '退款金额'];
  223. PhpSpreadsheetService::outdata($filename, $export, $head);
  224. }
  225. public static function getBadge($where)
  226. {
  227. $price = self::getOrderPrice($where);
  228. return [
  229. [
  230. 'name' => '订单数量',
  231. 'field' => '件',
  232. 'count' => $price['order_sum'],
  233. 'background_color' => 'layui-bg-blue',
  234. 'col' => 2
  235. ],
  236. [
  237. 'name' => '订单总金额',
  238. 'field' => '元',
  239. 'count' => $price['pay_price'],
  240. 'background_color' => 'layui-bg-blue',
  241. 'col' => 2
  242. ],
  243. [
  244. 'name' => '退款金额',
  245. 'field' => '元',
  246. 'count' => $price['refund_price'],
  247. 'background_color' => 'layui-bg-blue',
  248. 'col' => 3
  249. ],
  250. [
  251. 'name' => '微信支付金额',
  252. 'field' => '元',
  253. 'count' => $price['pay_price_wx'],
  254. 'background_color' => 'layui-bg-blue',
  255. 'col' => 2
  256. ],
  257. [
  258. 'name' => '余额支付金额',
  259. 'field' => '元',
  260. 'count' => $price['pay_price_yue'],
  261. 'background_color' => 'layui-bg-blue',
  262. 'col' => 2
  263. ],
  264. [
  265. 'name' => '支付宝支付金额',
  266. 'field' => '元',
  267. 'count' => $price['pay_price_zhifubao'],
  268. 'background_color' => 'layui-bg-blue',
  269. 'col' => 2
  270. ]
  271. ];
  272. }
  273. /**
  274. * 处理where条件
  275. * @param $where
  276. * @param $model
  277. * @return mixed
  278. */
  279. public static function getOrderWhere($where, $model, $aler = '', $join = '')
  280. {
  281. $model = self::statusByWhere($where['status'], $model, $aler, $where['type']);
  282. if (isset($where['id']) && $where['id'] > 0) {
  283. $model = $model->where($aler . 'activity_id', $where['id']);
  284. }
  285. if (isset($where['real_name']) && $where['real_name'] != '') {
  286. $model = $model->where($aler . 'order_id|' . $aler . 'code|' . $aler . 'uid' . ($join ? '|' . $join . '.nickname|' . $join . '.uid|' . $join . '.phone' : ''), 'LIKE', "%$where[real_name]%");
  287. }
  288. if (isset($where['mer_id']) && $where['mer_id']) {
  289. $model = $model->where($aler . 'mer_id', $where['mer_id']);
  290. }
  291. if (isset($where['data']) && $where['data'] !== '') {
  292. $model = self::getModelTime($where, $model, $aler . 'add_time');
  293. }
  294. return $model;
  295. }
  296. /**
  297. * 处理订单金额
  298. * @param $where
  299. * @return array
  300. */
  301. public static function getOrderPrice($where)
  302. {
  303. $price = [];
  304. $price['pay_price'] = 0;//支付金额
  305. $price['pay_price_wx'] = 0;//微信支付金额
  306. $price['pay_price_yue'] = 0;//余额支付金额
  307. $price['pay_price_zhifubao'] = 0;//支付宝支付金额
  308. $price['refund_price'] = 0;//退款金额
  309. $list = self::getOrderWhere($where, self::alias('s')->join('user r', 'r.uid=s.uid', 'LEFT'), 's.', 'r')->field(['sum(s.pay_price) as pay_price', 'sum(s.refund_price) as refund_price'])->find()->toArray();
  310. $price['pay_price'] = $list['pay_price'];//支付金额
  311. $price['refund_price'] = $list['refund_price'];//退款金额
  312. $list = self::getOrderWhere($where, self::alias('s')->join('user r', 'r.uid=s.uid', 'LEFT'), 's.', 'r')->field('sum(s.pay_price) as pay_price,s.pay_type')->group('s.pay_type')->select()->toArray();
  313. foreach ($list as $v) {
  314. if ($v['pay_type'] == 'weixin') {
  315. $price['pay_price_wx'] = $v['pay_price'];
  316. } elseif ($v['pay_type'] == 'yue') {
  317. $price['pay_price_yue'] = $v['pay_price'];
  318. } elseif ($v['pay_type'] == 'zhifubao') {
  319. $price['pay_price_zhifubao'] = $v['pay_price'];
  320. }
  321. }
  322. $price['order_sum'] = self::getOrderWhere($where, self::alias('s')->join('user r', 'r.uid=s.uid', 'LEFT'), 's.', 'r')->count();
  323. return $price;
  324. }
  325. }