TalentCondition.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. $params = $this->request->param();
  34. $res = $this->myValid($params);
  35. if ($res !== true)
  36. return $res;
  37. if (TalentConditionApi::edit($params))
  38. return json(["code" => 200, "msg" => "添加认定条件成功"]);
  39. return json(["msg" => "添加认定条件失败"]);
  40. }
  41. return view();
  42. }
  43. /**
  44. * @auth {{/identifyCondition/update}}
  45. * @return type
  46. */
  47. function edit() {
  48. if ($this->request->isPost()) {
  49. $params = $this->request->param();
  50. $res = $this->myValid($params);
  51. if ($res !== true)
  52. return $res;
  53. if (TalentConditionApi::edit($params))
  54. return json(["code" => 200, "msg" => "编辑认定条件成功"]);
  55. return json(["msg" => "编辑认定条件失败"]);
  56. }
  57. $id = $this->request->param("id");
  58. $info = TalentConditionApi::getOne($id);
  59. return view("", ["info" => $info]);
  60. }
  61. private function myValid($params) {
  62. if (!$params["type"] || !in_array($params["type"], [1, 2]))
  63. return json(["msg" => "请选择人才类别"]);
  64. if (!$params["talentLevel"] || !in_array($params["talentLevel"], [1, 2, 3, 4, 5, 6, 7]))
  65. return json(["msg" => "请选择人才层次"]);
  66. if (!$params["name"])
  67. return json(["msg" => "请填写名称"]);
  68. if (!$params["companyIds"]) {
  69. return json(["msg" => "没有设置审核单位"]);
  70. }
  71. if (!$params["bindFileTypes"]) {
  72. return json(["msg" => "没有设置审核附件"]);
  73. }
  74. $bindFileTypes = array_filter(explode(",", $params["bindFileTypes"]));
  75. $total = count($bindFileTypes);
  76. $tmp = [];
  77. foreach ($params["relation"] as $_companyId => $_relation) {
  78. $_relations = explode(",", $_relation);
  79. for ($i = 0; $i < count($_relations); $i++) {
  80. if (in_array($_relations[$i], $bindFileTypes))
  81. $tmp[] = $_relations[$i];
  82. }
  83. }
  84. $_valid_count = count(array_unique($tmp));
  85. if ($_valid_count != $total) {
  86. return json(["msg" => "选择了审核单位及审核附件后,每个附件必须与其中一个审核单位关联"]);
  87. }
  88. return true;
  89. }
  90. /**
  91. * @auth {{/identifyCondition/delete}}
  92. * @return type
  93. */
  94. function delete() {
  95. $id = $this->request->param("id");
  96. TalentConditionApi::delete($id);
  97. return json(["code" => 200, "msg" => "删除成功"]);
  98. }
  99. /**
  100. * @auth {{/identifyCondition/import}}
  101. */
  102. function import() {
  103. ignore_user_abort(true);
  104. set_time_limit(0);
  105. if (!$this->request->file())
  106. return json(["msg" => "没有选择文件"]);
  107. $excel = $this->request->file("file");
  108. if (!isExcelFile($excel->getMime()))
  109. return json(["msg" => "不是正确的Excel文件"]);
  110. $mapping = [
  111. 0 => "type",
  112. 1 => "talentLevel",
  113. 2 => "name",
  114. 3 => "activeYear",
  115. 4 => "description"
  116. ];
  117. $path = $excel->getRealPath();
  118. $datas = getExcelDatas($path);
  119. $datas = array_slice($datas, 1); //去标题
  120. $inserts = [];
  121. while ($row = array_shift($datas)) {
  122. $cols = count($row);
  123. $companyIds = [];
  124. $new = [];
  125. for ($i = 0; $i < $cols; $i++) {
  126. if ($i < count($mapping)) {
  127. $new[$mapping[$i]] = $row[$i];
  128. } else {
  129. $companyIds[] = $row[$i];
  130. }
  131. }
  132. $new["companyIds"] = $companyIds ? implode(",", $companyIds) : null;
  133. $new["createTime"] = date("Y-m-d H:i:s");
  134. $inserts[] = $new;
  135. }
  136. $chunks = array_chunk($inserts, 200);
  137. foreach ($chunks as $chunk) {
  138. Db::table("new_talent_condition")->insertAll($chunk);
  139. }
  140. $data = ["code" => 200, "msg" => "导入成功"];
  141. echo sprintf('<script>parent.IdentifyCondition.callBack(%s);</script>', json_encode($data));
  142. }
  143. }