TalentCondition.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\common\AdminController;
  4. use app\common\api\TalentConditionApi;
  5. use think\facade\Db;
  6. /**
  7. * Description of TalentCondition
  8. *
  9. * @author sgq
  10. */
  11. class TalentCondition extends AdminController {
  12. /**
  13. * @auth {{/identifyCondition}}
  14. * @return type
  15. */
  16. function index() {
  17. return view();
  18. }
  19. /**
  20. * @auth {{/identifyCondition/list}}
  21. * @return type
  22. */
  23. function list() {
  24. $result = TalentConditionApi::getListByCondition($this->request->param());
  25. return json($result);
  26. }
  27. /**
  28. * @auth {{/identifyCondition/add}}
  29. * @return type
  30. */
  31. function add() {
  32. if ($this->request->isPost()) {
  33. TalentConditionApi::edit($this->request->param());
  34. return json(["code" => 200, "msg" => "添加认定条件成功"]);
  35. }
  36. return view();
  37. }
  38. /**
  39. * @auth {{/identifyCondition/update}}
  40. * @return type
  41. */
  42. function edit() {
  43. if ($this->request->isPost()) {
  44. TalentConditionApi::edit($this->request->param());
  45. return json(["code" => 200, "msg" => "编辑认定条件成功"]);
  46. }
  47. $id = $this->request->param("id");
  48. $info = TalentConditionApi::getOne($id);
  49. return view("", ["info" => $info]);
  50. }
  51. /**
  52. * @auth {{/identifyCondition/delete}}
  53. * @return type
  54. */
  55. function delete() {
  56. $id = $this->request->param("id");
  57. TalentConditionApi::delete($id);
  58. return json(["code" => 200, "msg" => "删除成功"]);
  59. }
  60. /**
  61. * @auth {{/identifyCondition/import}}
  62. */
  63. function import() {
  64. ignore_user_abort(true);
  65. set_time_limit(0);
  66. if (!$this->request->file())
  67. return json(["msg" => "没有选择文件"]);
  68. $excel = $this->request->file("file");
  69. if (!isExcelFile($excel->getMime()))
  70. return json(["msg" => "不是正确的Excel文件"]);
  71. $mapping = [
  72. 0 => "type",
  73. 1 => "talentLevel",
  74. 2 => "name",
  75. 3 => "activeYear",
  76. 4 => "description"
  77. ];
  78. $path = $excel->getRealPath();
  79. $datas = getExcelDatas($path);
  80. $datas = array_slice($datas, 1); //去标题
  81. $inserts = [];
  82. while ($row = array_shift($datas)) {
  83. $cols = count($row);
  84. $companyIds = [];
  85. $new = [];
  86. for ($i = 0; $i < $cols; $i++) {
  87. if ($i < count($mapping)) {
  88. $new[$mapping[$i]] = $row[$i];
  89. } else {
  90. $companyIds[] = $row[$i];
  91. }
  92. }
  93. $new["companyIds"] = $companyIds ? implode(",", $companyIds) : null;
  94. $new["createTime"] = date("Y-m-d H:i:s");
  95. $inserts[] = $new;
  96. }
  97. $chunks = array_chunk($inserts, 200);
  98. foreach ($chunks as $chunk) {
  99. Db::table("new_talent_condition")->insertAll($chunk);
  100. }
  101. $data = ["code" => 200, "msg" => "导入成功"];
  102. echo sprintf('<script>parent.IdentifyCondition.callBack(%s);</script>', json_encode($data));
  103. }
  104. }