Exams.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. <?php
  2. namespace app\admin\controller\teacher;
  3. use app\admin\model\department\Department;
  4. use app\common\controller\Backend;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use app\admin\model\teacher\ExamsScore;
  9. use app\admin\model\Fault;
  10. use app\admin\model\Report;
  11. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  12. use PhpOffice\PhpSpreadsheet\IOFactory;
  13. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  14. use PhpOffice\PhpSpreadsheet\Reader\Csv;
  15. use PhpOffice\PhpSpreadsheet\Reader\Xls;
  16. use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  17. /**
  18. * sim-考试表/成绩总分
  19. *
  20. * @icon fa fa-circle-o
  21. */
  22. class Exams extends Backend
  23. {
  24. /**
  25. * Exams模型对象
  26. * @var \app\admin\model\teacher\Exams
  27. */
  28. protected $model = null;
  29. protected $whereExtend = null;
  30. protected $noNeedRight = ['export'];
  31. public function _initialize()
  32. {
  33. parent::_initialize();
  34. $this->model = new \app\admin\model\teacher\Exams;
  35. $this->whereExtend['exam_collection_type'] = 3;
  36. $this->whereExtend['starttime'] = ['>',0];
  37. $this->whereExtend['endtime'] = ['>',0];
  38. $this->relationtTable = 'collection';
  39. }
  40. /**
  41. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  42. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  43. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  44. */
  45. public function index()
  46. {
  47. //设置过滤方法
  48. $this->request->filter(['strip_tags', 'trim']);
  49. if (false === $this->request->isAjax()) {
  50. return $this->view->fetch();
  51. }
  52. //如果发送的来源是 Selectpage,则转发到 Selectpage
  53. if ($this->request->request('keyField')) {
  54. return $this->selectpage();
  55. }
  56. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  57. $list = $this->model
  58. ->where($where)->where($this->whereExtend)
  59. ->order($sort, $order)
  60. ->paginate($limit);
  61. //最高分 最低分 平均分
  62. $select = $this->model->where($where)->where($this->whereExtend)->field('count(*) as count,sum(total_score) as totalscore,min(total_score) as min,max(total_score) as max')->select();
  63. $pingfen = '';
  64. if(!empty($select[0]['totalscore']) && !empty($select[0]['count'])){
  65. $pingfen = bcdiv($select[0]['totalscore'],$select[0]['count']);
  66. }
  67. $defen = ['max'=>$select[0]['max'],'min'=>$select[0]['min'],'pingfen'=>$pingfen];
  68. $one_static = [];
  69. $two_static = [];
  70. $three_static = [];
  71. $where_falut = ['fault_type' => 3,'fault_state'=>0];
  72. $exam_ids = $this->model->where($where)->where($this->whereExtend)->column('exam_id');
  73. foreach ($list as $k => $val){
  74. $one = Fault::where(['sim_type'=>'0001'])->where($where_falut)->field('fault_id,name')->select();
  75. if($val['sim_type']=='0001'){
  76. foreach ($one as $k1 => $val1){
  77. //先查分配次数,在查错误率
  78. $one_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val1['fault_id']])->count();
  79. $error_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val1['fault_id'],'answer_right'=>2])->count();
  80. $one[$k1]['epercent'] = '';
  81. if($one_total>0){
  82. $bili = bcmul(bcdiv($error_total,$one_total,2),100);
  83. $one[$k1]['epercent'] = $bili>0 ? $bili.'%' : '';
  84. }
  85. }
  86. $vaccineCount = array_column($one, 'epercent');
  87. array_multisort($vaccineCount,SORT_DESC,$one);
  88. $one_static = $one;
  89. }
  90. $two = Fault::where(['sim_type'=>'0002'])->where($where_falut)->field('fault_id,name')->select();
  91. if($val['sim_type']=='0002'){
  92. foreach ($two as $k2 => $val2){
  93. $two_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val2['fault_id']])->count();
  94. $error_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val2['fault_id'],'answer_right'=>2])->count();
  95. $two[$k2]['epercent'] = '';
  96. if($two_total>0){
  97. $bili = bcmul(bcdiv($error_total,$two_total,2),100);
  98. $two[$k2]['epercent'] = $bili>0 ? $bili.'%' : '';
  99. }
  100. }
  101. $vaccineCount1 = array_column($two, 'epercent');
  102. array_multisort($vaccineCount1,SORT_DESC,$two);
  103. }
  104. $two_static = $two;
  105. $three = Fault::where(['sim_type'=>'0003'])->where($where_falut)->field('fault_id,name')->select();
  106. if($val['sim_type']=='0003'){
  107. foreach ($three as $k3 => $val3){
  108. $three_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val3['fault_id']])->count();
  109. $error_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val3['fault_id'],'answer_right'=>2])->count();
  110. $three[$k3]['epercent'] = '';
  111. if($three_total>0){
  112. $bili = bcmul(bcdiv($error_total,$three_total,2),100);
  113. $three[$k3]['epercent'] = $bili>0 ? $bili.'%' : '';
  114. }
  115. }
  116. $vaccineCount2 = array_column($three, 'epercent');
  117. array_multisort($vaccineCount2,SORT_DESC,$three);
  118. }
  119. $three_static = $three;
  120. }
  121. //统计错误率
  122. $statistics = [
  123. 'one_static' => $one_static,
  124. 'two_static' => $two_static,
  125. 'three_static' => $three_static,
  126. ];
  127. $result = ['total' => $list->total(), 'rows' => $list->items(),'defen'=>$defen,'tongji'=>$statistics];
  128. return json($result);
  129. }
  130. public function edit($ids = null)
  131. {
  132. $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  133. if (!$row) {
  134. $this->error(__('No Results were found'));
  135. }
  136. $rows = $this->model->get($ids);
  137. $row['seat_id'] = $rows->seat_id;
  138. $row['user_nickname'] = $rows->user_nickname;
  139. $row['user_username'] = $rows->user_username;
  140. $row['is_sure'] = $rows->is_sure;
  141. $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  142. $row['fault_name_one'] = Fault::where('fault_id',$fault_list[0]['fault_id'])->value('name');
  143. $row['fault_name_two'] = Fault::where('fault_id',$fault_list[1]['fault_id'])->value('name');
  144. $row['fault_name_three'] = Fault::where('fault_id',$fault_list[2]['fault_id'])->value('name');
  145. $other_jielun = !empty($row['other_jielun']) ? json_decode($row['other_jielun'],true):[];
  146. $diffInSeconds = $rows['endtime'] - $rows['starttime']; // 两个时间戳之间的差异(秒)
  147. $minutes = floor($diffInSeconds / 60); // 计算分钟数
  148. $seconds = $diffInSeconds % 60; // 计算剩余的秒数
  149. $row['shijian'] = $minutes.'分'.$seconds.'秒';
  150. if (false === $this->request->isPost()) {
  151. $this->view->assign('other_jielun', $other_jielun);
  152. $this->view->assign('row', $row);
  153. return $this->view->fetch();
  154. }
  155. $params = $this->request->post('row/a');
  156. if (empty($params)) {
  157. $this->error(__('Parameter %s can not be empty', ''));
  158. }
  159. $params = $this->preExcludeFields($params);
  160. $result = false;
  161. Db::startTrans();
  162. try {
  163. //是否采用模型验证
  164. $koufen = 0;
  165. if(!empty($other_jielun)){
  166. foreach ($other_jielun as $key => $value) {
  167. if(!empty($value['cx_score'])){
  168. $koufen = $koufen+abs($value['cx_score']);
  169. }
  170. }
  171. }else{
  172. $koufen = 15; //超时未自动交卷的
  173. }
  174. $params['total'] = 100-$params['fault_one_score']-$params['fault_two_score']-$params['fault_three_score']-$params['overtime_score']-$koufen;
  175. $result = Db::name('real_exam_score')->where('id', $row['id'])->update($params);
  176. $rows->total_score = $params['total'];
  177. $rows->is_sure = 1;
  178. $rows->save();
  179. Db::commit();
  180. } catch (ValidateException|PDOException|Exception $e) {
  181. Db::rollback();
  182. $this->error($e->getMessage());
  183. }
  184. if (false === $result) {
  185. $this->error(__('No rows were updated'));
  186. }
  187. $this->success();
  188. }
  189. public function view($ids = null)
  190. {
  191. $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  192. if (!$row) {
  193. $this->error(__('No Results were found'));
  194. }
  195. $rows = $this->model->get($ids);
  196. $row['fault_total'] = $rows->fault_total;
  197. $row['seat_id'] = $rows->seat_id;
  198. $row['user_nickname'] = $rows->user_nickname;
  199. $row['user_username'] = $rows->user_username;
  200. $row['user_depart_id'] = $rows->user_depart_id;
  201. $row['user_depart_name'] = Department::where('id',$rows->user_depart_id)->value('name');
  202. $row['start_time'] = $rows->start_time;
  203. $row['end_time'] = $rows->end_time;
  204. $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  205. $row['fault_name_one'] = Fault::where('fault_id',$fault_list[0]['fault_id'])->value('name');
  206. if($rows['fault_total']>0){
  207. if($rows['fault_total']==2){
  208. $row['fault_name_two'] = Fault::where('fault_id',$fault_list[1]['fault_id'])->value('name');
  209. }
  210. if($rows['fault_total']==3){
  211. $row['fault_name_two'] = Fault::where('fault_id',$fault_list[1]['fault_id'])->value('name');
  212. $row['fault_name_three'] = Fault::where('fault_id',$fault_list[2]['fault_id'])->value('name');
  213. }
  214. }else{
  215. $row['fault_name_two'] = Fault::where('fault_id',$fault_list[1]['fault_id'])->value('name');
  216. $row['fault_name_three'] = Fault::where('fault_id',$fault_list[2]['fault_id'])->value('name');
  217. }
  218. $row['fault_score'] = 75-$row['fault_one_score']-$row['fault_two_score']-$row['fault_three_score'];
  219. $row['weixiu_score'] = 10-$row['overtime_score']??0;
  220. $other_jielun = !empty($row['other_jielun']) ? json_decode($row['other_jielun'],true):[];
  221. $koufen = 0;
  222. if(!empty($other_jielun)){
  223. foreach ($other_jielun as $key => $value) {
  224. if(!empty($value['cx_score'])){
  225. $koufen = $koufen+abs($value['cx_score']);
  226. }
  227. }
  228. }else{
  229. $koufen = 15;
  230. }
  231. $report_score = 15-$koufen;
  232. $row['report_score'] = $report_score>0?$report_score:0;
  233. $total = $row['fault_score']+$row['report_score']+$row['weixiu_score'];
  234. Db::name('real_exam_score')->where('exam_id', $ids)->update(['total'=>$total]);
  235. Db::name('real_exam')->where('exam_id', $ids)->update(['total_score'=>$total]);
  236. $row['total'] = $total;
  237. $this->view->assign('other_jielun', $other_jielun);
  238. $diffInSeconds = $rows['endtime'] - $rows['starttime']; // 两个时间戳之间的差异(秒)
  239. $minutes = floor($diffInSeconds / 60); // 计算分钟数
  240. $seconds = $diffInSeconds % 60; // 计算剩余的秒数
  241. $row['shijian'] = $minutes.'分'.$seconds.'秒';
  242. $this->view->assign('row', $row);
  243. return $this->view->fetch();
  244. }
  245. //正在考试
  246. public function persent($ids = null)
  247. {
  248. //设置过滤方法
  249. $this->request->filter(['strip_tags', 'trim']);
  250. if (false === $this->request->isAjax()) {
  251. $this->assignConfig('ids', $ids);
  252. return $this->view->fetch();
  253. }
  254. //如果发送的来源是 Selectpage,则转发到 Selectpage
  255. if ($this->request->request('keyField')) {
  256. return $this->selectpage();
  257. }
  258. $exam_ids = $this->model->where('exam_collection_id',$ids)->column('exam_id');
  259. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  260. $list = Db::name('real_exam_comp_request')
  261. // ->where('exam_collection_type',3)
  262. ->where($where)->where('exam_id','in',$exam_ids)
  263. ->order($sort, $order)
  264. ->paginate($limit);
  265. unset($v);
  266. $result = ['total' => $list->total(), 'rows' => $list->items()];
  267. return json($result);
  268. }
  269. public function examing($ids = null)
  270. {
  271. //设置过滤方法
  272. $this->request->filter(['strip_tags', 'trim']);
  273. if (false === $this->request->isAjax()) {
  274. $this->assignConfig('ids', $ids);
  275. return $this->view->fetch();
  276. }
  277. //如果发送的来源是 Selectpage,则转发到 Selectpage
  278. if ($this->request->request('keyField')) {
  279. return $this->selectpage();
  280. }
  281. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  282. $list = Db::name('seat')->select();
  283. foreach ($list as $key => $value) {
  284. $exam = $this->model->where('exam_collection_id',$ids)->order('exam_id desc')->where('seat_id',$value['seat_num'])->find();
  285. $user_username = '';
  286. $user_nickname = '';
  287. $exam_status = '未登录';
  288. $total_score = 0;
  289. $fault_names = '';
  290. $exam_id = 0;
  291. $simtype = '';
  292. $sim_state = '';
  293. if(!empty($exam)){
  294. if(!empty($value['current_sim_id'])){
  295. $sim = Db::name('sim')->where('sim_id',$value['current_sim_id'])->find();
  296. if(!empty($sim['sim_state'])){
  297. switch ($sim['sim_state']) {
  298. case '1':
  299. $sim_state = '在线';
  300. break;
  301. case '2':
  302. $sim_state = '离线';
  303. break;
  304. }
  305. }
  306. $simtype = $sim['sim_type'];
  307. }
  308. $user_username = $exam['user_username']??'';
  309. $user_nickname = $exam['user_nickname']??'';
  310. if($exam['exam_status']==1){
  311. $exam_status= '已登录未开始考试';
  312. }else if($exam['exam_status']==4){
  313. $exam_status= '已开始考试';
  314. }else if($exam['exam_status']==5){
  315. $exam_status= '已交卷';
  316. }
  317. $total_score = $exam['total_score']??'';
  318. $fault_names = $exam['fault_names']??'';
  319. $exam_id = $exam['exam_id']??0;
  320. $simtype = $simtype;
  321. }
  322. $list[$key]['user_username'] = $user_username;
  323. $list[$key]['user_nickname'] = $user_nickname;
  324. $list[$key]['exam_status'] = $exam_status;
  325. $list[$key]['total_score'] = $total_score;
  326. $list[$key]['fault_names'] = $fault_names;
  327. $list[$key]['exam_id'] = $exam_id;
  328. $list[$key]['sim_state'] = $sim_state;
  329. $list[$key]['sim_type'] = $simtype;
  330. }
  331. $result = ['total' => count($list), 'rows' => $list];
  332. return json($result);
  333. }
  334. public function score($ids = null)
  335. {
  336. //设置过滤方法
  337. $this->request->filter(['strip_tags', 'trim']);
  338. if (false === $this->request->isAjax()) {
  339. $this->assignConfig('ids', $ids);
  340. return $this->view->fetch();
  341. }
  342. //如果发送的来源是 Selectpage,则转发到 Selectpage
  343. if ($this->request->request('keyField')) {
  344. return $this->selectpage();
  345. }
  346. $exam_ids = $this->model->where('exam_collection_id',$ids)->column('exam_id');
  347. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  348. $list = ExamsScore::where($where)->where('exam_id','in',$exam_ids)
  349. ->order($sort, $order)
  350. ->paginate($limit);
  351. foreach ($list as $k => $v){
  352. $exam = $this->model->where(['exam_id'=>$v['exam_id']])->find();
  353. if($exam){
  354. $v->seat_id = $exam['seat_id'];
  355. $v->user_username = $exam['user_username'];
  356. $v->user_nickname = $exam['user_nickname'];
  357. $v->sim_type = $exam['sim_type'];
  358. }
  359. }
  360. unset($v);
  361. $result = ['total' => $list->total(), 'rows' => $list->items()];
  362. return json($result);
  363. }
  364. public function handle($type = null,$ids = null)
  365. {
  366. $row = Db::name('real_exam_comp_request')->where('rel_id',$ids)->find($ids);
  367. if(!$row){
  368. $this->error('未找到记录');
  369. }
  370. if($type ==1){
  371. $request_status = 2;
  372. }
  373. if($type==2)
  374. {
  375. $request_status= 3;
  376. }
  377. Db::name('real_exam_comp_request')->where('rel_id',$ids)->update(['request_status'=>$request_status]);
  378. //同步更新考试表json串
  379. $exam_info = $this->model->get($row['exam_id']);
  380. if(!empty($exam_info)){
  381. $other_replace = json_decode($exam_info['other_replace'],true)??[];
  382. foreach ($other_replace as $k=>$v){
  383. if($v['fault_id'] == $row['fault_id']){
  384. $other_replace[$k]['request_status'] = $request_status;
  385. }
  386. }
  387. $exam_info->other_replace = json_encode($other_replace);
  388. $exam_info->save();
  389. unset($v);
  390. }
  391. $this->success();
  392. }
  393. //啦
  394. public function editscore($ids = "")
  395. {
  396. $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  397. if (!$row) {
  398. $this->error(__('No Results were found'));
  399. }
  400. $rows = $this->model->get($ids);
  401. $params = $this->request->post('row/a');
  402. if (empty($params)) {
  403. $this->error(__('Parameter %s can not be empty', ''));
  404. }
  405. $params = $this->preExcludeFields($params);
  406. $result = false;
  407. Db::startTrans();
  408. try {
  409. //是否采用模型验证
  410. $result = Db::name('real_exam_score')->where('id', $row['id'])->update($params);
  411. $info = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  412. $total =100-$info['fault_one_score']-$info['fault_two_score']-$info['fault_three_score']-$info['xianxian_score']-$info['yuanyin_socre']-$info['buwei_score']-$info['fangfa_score']-$info['overtime_score']-$info['jielun_score'];;
  413. $rows->total_score = $total;
  414. $rows->is_sure = 1;
  415. $rows->save();
  416. $result = Db::name('real_exam_score')->where('id', $row['id'])->update(['total'=>$total]);
  417. Db::commit();
  418. } catch (ValidateException|PDOException|Exception $e) {
  419. Db::rollback();
  420. $this->error($e->getMessage());
  421. }
  422. if (false === $result) {
  423. $this->error(__('No rows were updated'));
  424. }
  425. $this->success();
  426. }
  427. public function export()
  428. {
  429. set_time_limit(0);
  430. ini_set('memory_limit', '2560M');
  431. //如果发送的来源是 Selectpage,则转发到 Selectpage
  432. if ($this->request->request('keyField')) {
  433. return $this->selectpage();
  434. }
  435. $filter = $this->request->get("filter", '');
  436. $filter = (array)json_decode($filter, true);
  437. $op = $this->request->get("op", '');
  438. $op = (array)json_decode($op, true);
  439. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  440. $list = $this->model
  441. ->where($where)->where($this->whereExtend)
  442. ->order($sort, $order)
  443. ->paginate($limit);
  444. $xlsName = '学员成绩信息';
  445. $this->exportExcel($xlsName,'Excel2007', $list);
  446. }
  447. /**
  448. * 输出到浏览器(需要设置header头)
  449. * @param string $fileName 文件名
  450. * @param string $fileType 文件类型
  451. */
  452. function exportExcel($fileName, $fileType,$data)
  453. {
  454. //文件名称校验
  455. if (!$fileName) {
  456. trigger_error('文件名不能为空', E_USER_ERROR);
  457. }
  458. //Excel文件类型校验
  459. $type = ['Excel2007', 'Xlsx', 'Excel5', 'xls'];
  460. if (!in_array($fileType, $type)) {
  461. trigger_error('未知文件类型', E_USER_ERROR);
  462. }
  463. $ext = '';
  464. if ($fileType == 'Excel2007' || $fileType == 'Xlsx') {
  465. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  466. header('Content-Disposition: attachment;filename="' . $fileName . '.xlsx"');
  467. header('Cache-Control: max-age=0');
  468. $ext = 'Xlsx';
  469. } else { //Excel5
  470. header('Content-Type: application/vnd.ms-excel');
  471. header('Content-Disposition: attachment;filename="' . $fileName . '.xls"');
  472. header('Cache-Control: max-age=0');
  473. $ext = 'Xls';
  474. }
  475. $spreadsheet = new Spreadsheet();
  476. $worksheet = $spreadsheet->getActiveSheet();
  477. $worksheet->getColumnDimension('A')->setWidth(15);
  478. $worksheet->getColumnDimension('B')->setWidth(15);
  479. $worksheet->getColumnDimension('C')->setWidth(15);
  480. $worksheet->getColumnDimension('D')->setWidth(15);
  481. $worksheet->getRowDimension('1')->setRowHeight(25);
  482. //设置工作表标题名称
  483. $worksheet->setTitle('学员成绩信息');
  484. $worksheet->setCellValue('A1',"序号");
  485. $worksheet->setCellValue('B1',"学号");
  486. $worksheet->setCellValue('C1',"姓名");
  487. $worksheet->setCellValue('D1',"总分");
  488. foreach ($data as $ky => $value)
  489. {
  490. $lie = $ky+2;
  491. $worksheet->setCellValue('A'.$lie,++$ky);
  492. $worksheet->setCellValue('B'.$lie,$value['user_username']);
  493. $worksheet->setCellValue('C'.$lie,$value['user_nickname']);
  494. $worksheet->setCellValue('D'.$lie,$value['total_score']);
  495. }
  496. $titlestyleArray = [
  497. 'font' => [
  498. 'name' => '黑体',
  499. 'size' => 14
  500. ],
  501. ];
  502. $headerStyleArray = [
  503. 'font' => [
  504. 'name' => '方正小标宋简体',
  505. 'size' => 18
  506. ],
  507. ];
  508. $commonStyleArray = [
  509. 'alignment' => [
  510. 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
  511. 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
  512. 'wrapText' => true,
  513. ]
  514. ];
  515. $borderStyleArray = [
  516. 'borders' => [
  517. 'allBorders' => [
  518. // 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
  519. ],
  520. ],
  521. 'font' => [
  522. 'name' => '黑体',
  523. ],
  524. 'alignment' => [
  525. 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
  526. 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
  527. 'wrapText' => true,
  528. ]
  529. ];
  530. $worksheet->getStyle('A1:D1')->applyFromArray($titlestyleArray);
  531. $worksheet->getStyle('A1:D1')->applyFromArray($commonStyleArray);
  532. foreach ($data as $k=>$v){
  533. $kk = $k+2;
  534. $worksheet->getStyle('A'.$kk.':D'.$kk)->applyFromArray($borderStyleArray);
  535. }
  536. // $worksheet->getStyle('A3:R3')->applyFromArray($commonStyleArray);
  537. // $worksheet->getStyle('A'.$footer_total.':R'.$footer_total)->applyFromArray($commonStyleArray);
  538. // $worksheet->getStyle('A1:C1')->applyFromArray($borderStyleArray);
  539. // $worksheet->mergeCells('A1:R1');
  540. $writer = IOFactory::createWriter($spreadsheet,$ext);
  541. $writer->save('php://output');
  542. die();
  543. }
  544. public function multi($ids = null)
  545. {
  546. if (false === $this->request->isPost()) {
  547. $this->error(__('Invalid parameters'));
  548. }
  549. $ids = $ids ?: $this->request->post('ids');
  550. if (empty($ids)) {
  551. $this->error(__('Parameter %s can not be empty', 'ids'));
  552. }
  553. if (false === $this->request->has('params')) {
  554. $this->error(__('No rows were updated'));
  555. }
  556. parse_str($this->request->post('params'), $values);
  557. // $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  558. // if (empty($values)) {
  559. // $this->error(__('You have no permission'));
  560. // }
  561. $adminIds = $this->getDataLimitAdminIds();
  562. if (is_array($adminIds)) {
  563. $this->model->where($this->dataLimitField, 'in', $adminIds);
  564. }
  565. $count = 0;
  566. Db::startTrans();
  567. try {
  568. $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
  569. foreach ($list as $item) {
  570. $count += $item->allowField(true)->isUpdate(true)->save($values);
  571. }
  572. Db::commit();
  573. } catch (PDOException|Exception $e) {
  574. Db::rollback();
  575. $this->error($e->getMessage());
  576. }
  577. if ($count) {
  578. $this->success();
  579. }
  580. $this->error(__('No rows were updated'));
  581. }
  582. }