Rechargecard.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. *
  7. *
  8. * ----------------------------------------------------------------------------
  9. *
  10. * 控制器
  11. */
  12. class Rechargecard extends AdminControl
  13. {
  14. const EXPORT_SIZE = 100;
  15. public function initialize()
  16. {
  17. parent::initialize(); // TODO: Change the autogenerated stub
  18. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/rechargecard.lang.php');
  19. }
  20. public function index()
  21. {
  22. $rechargecard_model = model('rechargecard');
  23. $condition = array();
  24. if (request()->isGet()) {
  25. $sn = trim((string)input('param.sn'));
  26. $batchflag = trim((string)input('param.batchflag'));
  27. $state = input('param.state');
  28. if (strlen($sn) > 0) {
  29. $condition[] = array('rc_sn', 'like', "%{$sn}%");
  30. }
  31. if (strlen($batchflag) > 0) {
  32. $condition[] = array('rc_batchflag', 'like', "%{$batchflag}%");
  33. }
  34. if ($state == '0' || $state == '1') {
  35. $condition[] = array('rc_state', '=', $state);
  36. }
  37. }
  38. $cardList = $rechargecard_model->getRechargecardList($condition, 20);
  39. View::assign('card_list', $cardList);
  40. View::assign('show_page', $rechargecard_model->page_info->render());
  41. $this->setAdminCurItem('index');
  42. return View::fetch();
  43. }
  44. public function add_card()
  45. {
  46. if (!request()->isPost()) {
  47. return View::fetch();
  48. } else {
  49. $denomination = (float)input('post.denomination');
  50. if ($denomination < 0.01) {
  51. $this->error(lang('rechargecard_denomination_min'));
  52. }
  53. if ($denomination > 1000) {
  54. $this->error(lang('rechargecard_denomination_max'));
  55. }
  56. $snKeys = array();
  57. switch (input('post.type')) {
  58. case '0':
  59. $total = (int)input('post.total');
  60. if ($total < 1 || $total > 5000) {
  61. $this->error(lang('r0total_message'));
  62. }
  63. $prefix = (string)input('post.prefix');
  64. if (!preg_match('/^[0-9a-zA-Z]{0,16}$/', $prefix)) {
  65. $this->error(lang('r0prefix_message'));
  66. }
  67. while (count($snKeys) < $total) {
  68. $snKeys[$prefix . md5(uniqid(mt_rand(), true))] = null;
  69. }
  70. break;
  71. case '1':
  72. $f = $_FILES['_textfile'];
  73. if (!$f || $f['error'] != 0) {
  74. $this->error(lang('file_upload_fail'));
  75. }
  76. if (!is_uploaded_file($f['tmp_name'])) {
  77. $this->error(lang('file_empty'));
  78. }
  79. foreach (file($f['tmp_name']) as $sn) {
  80. $sn = trim($sn);
  81. if (preg_match('/^[0-9a-zA-Z]{1,50}$/', $sn))
  82. $snKeys[$sn] = null;
  83. }
  84. break;
  85. case '2':
  86. foreach (explode("\n", (string)input('post.manual')) as $sn) {
  87. $sn = trim($sn);
  88. if (preg_match('/^[0-9a-zA-Z]{1,50}$/', $sn))
  89. $snKeys[$sn] = null;
  90. }
  91. break;
  92. default:
  93. $this->error(lang('param_error'));
  94. exit;
  95. }
  96. $totalKeys = count($snKeys);
  97. if ($totalKeys < 1 || $totalKeys > 9999) {
  98. $this->error(lang('recharge_number_error'));
  99. }
  100. if (empty($snKeys)) {
  101. $this->error(lang('recharge_number_invalid'));
  102. }
  103. $snOccupied = 0;
  104. $rechargecard_model = model('rechargecard');
  105. // chunk size = 50
  106. foreach (array_chunk(array_keys($snKeys), 50) as $snValues) {
  107. foreach ($rechargecard_model->getOccupiedRechargecardSNsBySNs($snValues) as $sn) {
  108. $snOccupied++;
  109. unset($snKeys[$sn]);
  110. }
  111. }
  112. if (empty($snKeys)) {
  113. $this->error(lang('recharge_number_exist'));
  114. }
  115. $batchflag = input('post.batchflag');
  116. $adminName = $this->admin_info['admin_name'];
  117. $ts = TIMESTAMP;
  118. $snToInsert = array();
  119. foreach (array_keys($snKeys) as $sn) {
  120. $snToInsert[] = array(
  121. 'rc_sn' => $sn, 'rc_denomination' => $denomination, 'rc_batchflag' => $batchflag, 'rc_admin_name' => $adminName,
  122. 'rc_tscreated' => $ts,
  123. );
  124. }
  125. if (!$rechargecard_model->addRechargecardAll($snToInsert)) {
  126. $this->error(lang('ds_common_op_fail'));
  127. }
  128. $countInsert = count($snToInsert);
  129. $this->log("新增{$countInsert}张充值卡(面额¥{$denomination},批次标识“{$batchflag}”)");
  130. $msg = lang('ds_common_op_succ');
  131. if ($snOccupied > 0)
  132. $msg .= sprintf(lang('rechargecard_number_exist_error'), $snOccupied);
  133. dsLayerOpenSuccess($msg);
  134. }
  135. }
  136. /**
  137. * 删除平台充值卡
  138. */
  139. public function del_card()
  140. {
  141. $rechargecard_model = model('rechargecard');
  142. $rc_id = input('param.rc_id');
  143. $rc_id_array = ds_delete_param($rc_id);
  144. if ($rc_id_array === FALSE) {
  145. ds_json_encode(10001, lang('param_error'));
  146. }
  147. $condition = array(array('rc_id', 'in', $rc_id_array));
  148. $condition[] = array('rc_state', '=', 0);
  149. $result = $rechargecard_model->delRechargecard($condition);
  150. if ($result) {
  151. ds_json_encode(10000, lang('ds_common_del_succ'));
  152. } else {
  153. ds_json_encode(10001, lang('ds_common_del_fail'));
  154. }
  155. }
  156. /**
  157. * 导出
  158. */
  159. public function export_step1()
  160. {
  161. $rechargecard_model = model('rechargecard');
  162. $condition = array();
  163. if (request()->isPost()) {
  164. $sn = trim((string)input('param.sn'));
  165. $batchflag = trim((string)input('param.batchflag'));
  166. $state = trim((string)input('param.state'));
  167. if (strlen($sn) > 0) {
  168. $condition[] = array('rc_sn', 'like', "%{$sn}%");
  169. View::assign('sn', $sn);
  170. }
  171. if (strlen($batchflag) > 0) {
  172. $condition[] = array('rc_batchflag', 'like', "%{$batchflag}%");
  173. View::assign('batchflag', $batchflag);
  174. }
  175. if ($state === '0' || $state === '1') {
  176. $condition[] = array('rc_state', '=', $state);
  177. View::assign('state', $state);
  178. }
  179. }
  180. if (!is_numeric(input('param.page'))) {
  181. $count = $rechargecard_model->getRechargecardCount($condition);
  182. $export_list = array();
  183. if ($count > self::EXPORT_SIZE) { //显示下载链接
  184. $page = ceil($count / self::EXPORT_SIZE);
  185. for ($i = 1; $i <= $page; $i++) {
  186. $limit1 = ($i - 1) * self::EXPORT_SIZE + 1;
  187. $limit2 = $i * self::EXPORT_SIZE > $count ? $count : $i * self::EXPORT_SIZE;
  188. $export_list[] = array($i, '=', $limit1 . ' ~ ' . $limit2);
  189. }
  190. View::assign('export_list', $export_list);
  191. return View::fetch('/public/excel');
  192. } else { //如果数量小,直接下载
  193. $data = $rechargecard_model->getRechargecardList($condition, 0, self::EXPORT_SIZE);
  194. $this->createExcel($data);
  195. }
  196. } else { //下载
  197. $limit1 = (input('param.page') - 1) * self::EXPORT_SIZE;
  198. $limit2 = self::EXPORT_SIZE;
  199. $data = $rechargecard_model->getRechargecardList($condition, $limit2);
  200. $this->createExcel($data);
  201. }
  202. }
  203. /**
  204. * 生成excel
  205. *
  206. * @param array $data
  207. */
  208. private function createExcel($data = array())
  209. {
  210. $excel_obj = new \excel\Excel();
  211. $excel_data = array();
  212. //设置样式
  213. $excel_obj->setStyle(array(
  214. 'id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')
  215. ));
  216. //header
  217. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('rc_sn'));
  218. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('rc_batchflag'));
  219. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('rc_denomination'));
  220. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('rc_admin_name'));
  221. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('rc_tscreated'));
  222. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('receive_user'));
  223. //data
  224. foreach ((array)$data as $k => $v) {
  225. $tmp = array();
  226. $tmp[] = array('data' => "\t" . $v['rc_sn']);
  227. $tmp[] = array('data' => "\t" . $v['rc_batchflag']);
  228. $tmp[] = array('data' => "\t" . $v['rc_denomination']);
  229. $tmp[] = array('data' => "\t" . $v['rc_admin_name']);
  230. $tmp[] = array('data' => "\t" . date('Y-m-d H:i:s', $v['rc_tscreated']));
  231. if ($v['rc_state'] == 1 && $v['member_id'] > 0 && $v['rc_tsused'] > 0) {
  232. $tmp[] = array('data' => "\t" . $v['member_name']);
  233. } else {
  234. $tmp[] = array('data' => "\t-");
  235. }
  236. $excel_data[] = $tmp;
  237. }
  238. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  239. $excel_obj->addArray($excel_data);
  240. $excel_obj->addWorksheet($excel_obj->charset(lang('rechargecard'), CHARSET));
  241. $excel_obj->generateXML($excel_obj->charset(lang('rechargecard'), CHARSET) . input('param.page') . '-' . date('Y-m-d-H', TIMESTAMP));
  242. }
  243. protected function getAdminItemList()
  244. {
  245. $menu_array = array(
  246. array(
  247. 'name' => 'index', 'text' => lang('ds_list'), 'url' => (string)url('Rechargecard/index')
  248. ), array(
  249. 'name' => 'add_card', 'text' => lang('ds_new'), 'url' => "javascript:dsLayerOpen('" . (string)url('Rechargecard/add_card') . "','" . lang('ds_new') . "')"
  250. ),
  251. );
  252. return $menu_array;
  253. }
  254. }