Integral.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. namespace app\enterprise\controller;
  3. use app\enterprise\common\EnterpriseController;
  4. use think\facade\Db;
  5. use app\common\api\EnterpriseApi;
  6. use app\common\state\IntegralState;
  7. use app\common\api\IntegralRecordApi;
  8. use app\common\api\TalentLogApi;
  9. use app\common\state\ProjectState;
  10. use app\enterprise\validate\IntegralValidator;
  11. use think\exception\ValidateException;
  12. /**
  13. * Description of 积分申报
  14. *
  15. * @author sgq
  16. */
  17. class Integral extends EnterpriseController {
  18. public function index() {
  19. return view();
  20. }
  21. public function list() {
  22. $res = IntegralRecordApi::getList($this->request->param());
  23. return json($res);
  24. }
  25. /**
  26. * 积分申报入口
  27. */
  28. public function apply(\think\Request $request) {
  29. $type = $this->user["type"];
  30. $param = $request->param();
  31. $id = isset($param["id"]) ? $param["id"] : 0;
  32. $info = IntegralRecordApi::getOne($id);
  33. $ep = EnterpriseApi::getOne($this->user["uid"]);
  34. if ($info) {
  35. $info["real_state"] = TalentLogApi::getLastLog($id, ProjectState::INTEGRAL)["state"];
  36. }
  37. if ($info && in_array($info["checkState"], [IntegralState::VERIFY_PASS, IntegralState::REVERIFY_PASS, IntegralState::REVERIFY_FAIL])) {
  38. return $this->view($request);
  39. exit();
  40. }
  41. if ($request->isPost()) {
  42. $checkState = $info["checkState"] ?: 0;
  43. if ($checkState == IntegralState::SAVE || $checkState == 0) {
  44. $this->save($info, $request, IntegralState::SAVE);
  45. } else if (in_array($checkState, [IntegralState::VERIFY_FAIL, IntegralState::REVERIFY_FAIL])) {
  46. $res = ["msg" => "审核失败,不能再保存"];
  47. echo sprintf("<script>parent.IntegralInfoDlg.infoCallback(%s);</script>", json_encode($res));
  48. exit;
  49. }
  50. $res = ["msg" => "已提交审核,请耐心等待"];
  51. echo sprintf("<script>parent.IntegralInfoDlg.infoCallback(%s);</script>", json_encode($res));
  52. exit;
  53. }
  54. $checkState = $info["checkState"] ?: 0;
  55. $info["enterprise"] = $ep;
  56. return view("", ["year" => date("Y"), "checkState" => $checkState, "row" => $info]);
  57. }
  58. public function view(\think\Request $request) {
  59. switch ($this->user["type"]) {
  60. case 1:
  61. $tpl = "view"; //晋江人才
  62. break;
  63. case 2:
  64. $tpl = "ic_view"; //集成电路
  65. break;
  66. }
  67. $id = $request->param("id");
  68. $info = \app\common\api\VerifyApi::getTalentInfoById($id);
  69. return view($tpl, ["row" => $info]);
  70. }
  71. /**
  72. * 提交表单(新:混合基础信息人才申报信息)晋江人才
  73. */
  74. private function submitToCheck() {
  75. $params = $this->request->param();
  76. $id = $params["id"];
  77. $info = IntegralRecordApi::chkIsOwner($id, $this->user["uid"]);
  78. if ($info) {
  79. $info["real_state"] = TalentLogApi::getLastLog($id, ProjectState::INTEGRAL)["state"];
  80. }
  81. $checkState = $info["checkState"];
  82. if ($checkState == IntegralState::SAVE || !$id) {
  83. $field_dict = [
  84. "name" => "姓名",
  85. "card_type" => "证件类型",
  86. "card_number" => "证件号码",
  87. "phone" => "手机号码",
  88. "email" => "电子邮箱"
  89. ];
  90. $no_empty = ["name", "card_type", "card_number", "phone", "email"];
  91. $return = [];
  92. foreach ($no_empty as $key) {
  93. if (!$params[$key]) {
  94. $return[] = sprintf("请填写“%s”", $field_dict[$key]);
  95. }
  96. }
  97. if (count($return) > 0) {
  98. $res = ["msg" => implode("<br>", $return)];
  99. echo sprintf("<script>parent.IntegralInfoDlg.submitCallback(%s);</script>", json_encode($res));
  100. exit;
  101. }
  102. $insertDetailList = [];
  103. $tmp_item_ids = [];
  104. $projectTypes = $params["projectType"];
  105. $projectIds = $params["projectId"];
  106. $item_ids = $params["item_id"];
  107. $amounts = $params["amount"];
  108. $detailCounts = count($item_ids);
  109. $ft_ids = [];
  110. for ($i = 0; $i < $detailCounts; $i++) {
  111. if (!in_array($item_ids[$i], $tmp_item_ids)) {
  112. $tmp_item_ids[] = $item_ids[$i];
  113. }
  114. if (!is_numeric($amounts[$i]) || $amounts[$i] < 0) {
  115. throw new ValidateException(sprintf("第%d个积分标准项的数额填写错误,应填入大于0的数字", $i + 1));
  116. }
  117. $integralItemInfo = \app\common\api\IntegralItemApi::getOne($item_ids[$i]);
  118. if (!$integralItemInfo) {
  119. throw new ValidateException(sprintf("第%d个积分标准项不存在", $i + 1));
  120. }
  121. $ft_ids = array_filter(array_merge($ft_ids, array(explode(",", $integralItemInfo["fileTypeId"]))));
  122. }
  123. if (count($tmp_item_ids) != $detailCounts) {
  124. throw new ValidateException("同一个申报中,同一个积分标准不能申报多次");
  125. }
  126. if ($ft_ids) {
  127. $whr = [];
  128. if ($id) {
  129. $whr[] = ["mainId", "=", $id];
  130. $whr[] = ["type", "=", ProjectState::INTEGRAL];
  131. } else {
  132. if ($params["uploadFiles"])
  133. $whr[] = ["id", "in", $params["uploadFiles"]];
  134. }
  135. $whr[] = ["typeId", "in", $ft_ids];
  136. $distinct_filetypes = Db::table("new_talent_file")->where($whr)->distinct(true)->field("typeId")->select();
  137. $upload_type_counts = count($distinct_filetypes);
  138. if ($upload_type_counts != count($ft_ids)) {
  139. $res = ["msg" => "请留意附件上传栏中带*号的内容均为必传项,请上传完整再提交审核"];
  140. echo sprintf("<script>parent.IntegralInfoDlg.submitCallback(%s);</script>", json_encode($res));
  141. exit;
  142. }
  143. }
  144. return $this->save($info, $this->request, IntegralState::SUBMIT);
  145. } else if (in_array($checkState, [IntegralState::VERIFY_FAIL, IntegralState::REVERIFY_FAIL, IntegralState::ZX_FAIL, IntegralState::ANNOUNCED_REVERIFY_FAIL, IntegralState::PUBLISH_FAIL])) {
  146. $res = ["msg" => "审核失败,不能再提交审核"];
  147. echo sprintf("<script>parent.IntegralInfoDlg.submitCallback(%s);</script>", json_encode($res));
  148. exit;
  149. }
  150. $res = ["msg" => "已提交审核,请耐心等待"];
  151. echo sprintf("<script>parent.IntegralInfoDlg.submitCallback(%s);</script>", json_encode($res));
  152. exit;
  153. }
  154. /**
  155. * 保存申报表单
  156. * @param type $info talent_info
  157. * @param type $param request->param();
  158. */
  159. private function save($info, \think\Request $request, $checkState) {
  160. try {
  161. $param = $request->param();
  162. $batch = \app\common\api\BatchApi::getValidBatch(ProjectState::INTEGRAL, $this->user["type"]);
  163. if (!$batch) {
  164. throw new ValidateException("不在人才认定申报申请时间内");
  165. }
  166. $data["batch_id"] = $batch["id"];
  167. validate(IntegralValidator::class)->check($param);
  168. $id = $param["id"];
  169. $files = $param["uploadFiles"];
  170. if ($id) {
  171. if (!$info || $info["id"] != $id || $info["enterprise_id"] != $this->user["uid"]) {
  172. throw new ValidateException("没有对应的积分申报信息");
  173. }
  174. }
  175. $all_valid_keys = ["name", "card_type", "card_number", "phone", "email"];
  176. foreach ($all_valid_keys as $key) {
  177. $data[$key] = trim($param[$key]);
  178. }
  179. $insertDetailList = [];
  180. $item_ids = $param["item_id"];
  181. $amounts = $param["amount"];
  182. $detailCounts = count($item_ids);
  183. for ($i = 0; $i < $detailCounts; $i++) {
  184. if (!is_numeric($amounts[$i]) || $amounts[$i] < 0) {
  185. throw new ValidateException(sprintf("第%d个积分标准项的数额填写错误,应填入大于0的数字", $i + 1));
  186. }
  187. $insertDetailList[] = [
  188. "id" => getStringId(),
  189. "record_id" => "",
  190. "item_id" => $item_ids[$i],
  191. "amount" => $amounts[$i],
  192. "unit" => ""
  193. ];
  194. }
  195. if ($info["real_state"] == IntegralState::VERIFY_REJECT) {
  196. //真实状态是驳回,需要判断什么字段可以提交
  197. $modify_fields = array_filter(explode(",", $info["modify_fields"]));
  198. $tmp_data = $data;
  199. $data = [];
  200. foreach ($modify_fields as $field) {
  201. $data[$field] = $tmp_data[$field];
  202. }
  203. $tmp_item_ids = [];
  204. if (!$info["modify_files"]) {
  205. $insertDetailList = []; //不能修改项目,清空
  206. }
  207. }
  208. $data["checkState"] = $checkState;
  209. $data["id"] = $id;
  210. $success_msg = "提交成功";
  211. $error_msg = "提交失败";
  212. if ($checkState == IntegralState::SAVE) {
  213. $success_msg = "保存成功";
  214. if ($data["id"]) {
  215. //编辑
  216. $data["updateTime"] = date("Y-m-d H:i:s");
  217. \app\common\model\IntegralRecord::update($data);
  218. $last_log = TalentLogApi::getLastLog($data["id"], ProjectState::INTEGRAL);
  219. if ($last_log["new_state"] != IntegralState::SAVE) {
  220. TalentLogApi::write(1, $data["id"], $checkState, "保存未提交", 1);
  221. } else {
  222. TalentLogApi::setActive($last_log["id"], 1); //更新修改时间
  223. }
  224. } else {
  225. //新增
  226. $data["id"] = getStringId();
  227. $data["enterprise_id"] = $this->user["uid"];
  228. $data["createTime"] = date("Y-m-d H:i:s");
  229. \app\common\model\IntegralRecord::insert($data);
  230. TalentLogApi::write(ProjectState::INTEGRAL, $data["id"], $checkState, "保存未提交", 1);
  231. $whr = [];
  232. $whr[] = ["fileId", "in", $files];
  233. $upd_checklog["mainId"] = $data["id"];
  234. Db::table("new_talent_checklog")->where($whr)->save($upd_checklog);
  235. }
  236. } else if ($checkState == IntegralState::SUBMIT) {
  237. if (!$info["first_submit_time"]) {
  238. $data["first_submit_time"] = date("Y-m-d H:i:s");
  239. } else {
  240. $data["new_submit_time"] = date("Y-m-d H:i:s");
  241. }
  242. if ($data["id"]) {
  243. $data["updateTime"] = date("Y-m-d H:i:s");
  244. \app\common\model\IntegralRecord::update($data);
  245. } else {
  246. //新增
  247. $data["id"] = getStringId();
  248. $data["enterprise_id"] = $this->user["uid"];
  249. $data["createTime"] = date("Y-m-d H:i:s");
  250. \app\common\model\IntegralRecord::insert($data);
  251. $whr = [];
  252. $whr[] = ["fileId", "in", $files];
  253. $upd_checklog["mainId"] = $data["id"];
  254. Db::table("new_talent_checklog")->where($whr)->save($upd_checklog);
  255. }
  256. TalentLogApi::write(ProjectState::INTEGRAL, $data["id"], $checkState, "确认提交审核", 1);
  257. } else {
  258. throw new ValidateException($error_msg);
  259. }
  260. if ($data["id"]) {
  261. \app\common\model\IntegralDetail::where("record_id", "=", $data["id"])->delete();
  262. if ($insertDetailList) {
  263. foreach ($insertDetailList as $insertItem) {
  264. $integralItemInfo = \app\common\api\IntegralItemApi::getOne($item_ids[$i]);
  265. $insertItem["record_id"] = $data["id"];
  266. $insertItem["unit"] = $integralItemInfo["unit"];
  267. \app\common\model\IntegralDetail::insert($insertItem);
  268. }
  269. }
  270. //删除多余的附件,一般是选择人才类型留下来的
  271. $whr = [];
  272. $whr[] = ["mainId", "=", $data["id"]];
  273. $whr[] = ["type", "=", ProjectState::INTEGRAL];
  274. $whr[] = ["id", "not in", $files];
  275. $_wait_del_files = Db::table("new_talent_file")->where($whr)->select()->toArray();
  276. $_logfileIds[] = [];
  277. foreach ($_wait_del_files as $_del_file) {
  278. $_logfileIds[] = $_del_file["id"];
  279. @unlink("storage/" . $_del_file ["url"]);
  280. }
  281. Db::table("new_talent_file")->where($whr)->delete();
  282. if ($_logfileIds) {
  283. $whr = [];
  284. $whr[] = ["fileId", "in", $_logfileIds];
  285. $_upd_checklog["description"] = "删除附件";
  286. $_upd_checklog["updateUser"] = sprintf("%s(%s)", $this->user["account"], $this->user["companyName"] ?: $this->user["rolename"]);
  287. $_upd_checklog["updateTime"] = date("Y-m-d H:i:s");
  288. Db::table("new_talent_checklog")->where($whr)->save($_upd_checklog);
  289. }
  290. $whr = [];
  291. $whr[] = ["id", "in", $files];
  292. Db::table("new_talent_file")->where($whr)->save(["mainId" => $data["id"]]);
  293. $res = ["code" => 200, "msg" => $success_msg, "obj" => ["id" => $data["id"], "checkState" => $checkState]];
  294. $callback = $checkState == IntegralState::SAVE ? "infoCallback" : "submitCallback";
  295. echo sprintf("<script>parent.IntegralInfoDlg.{$callback}(%s);</script>", json_encode($res));
  296. exit();
  297. } else {
  298. throw new ValidateException($error_msg);
  299. }
  300. } catch (ValidateException $e) {
  301. $res = ["msg" => $e->getMessage()];
  302. $callback = $checkState == IntegralState::SAVE ? "infoCallback" : "submitCallback";
  303. echo sprintf("<script>parent.IntegralInfoDlg.{$callback}(%s);</script>", json_encode($res));
  304. exit();
  305. }
  306. }
  307. public function delete() {
  308. $id = $this->request->param("id");
  309. $info = IntegralRecordApi::chkIsOwner($id, $this->user["uid"]);
  310. if (!$info) {
  311. return json(["msg" => "操作失败"]);
  312. }
  313. $checkState = $info["checkState"];
  314. if (in_array($checkState, [0, 1])) {
  315. $log = TalentLogApi::getLastLog($id, ProjectState::INTEGRAL);
  316. if ($log["state"] > 1) {
  317. //有提交审核记录
  318. return json(["msg" => "该申报已提交审核,无法删除"]);
  319. }
  320. }
  321. $data["id"] = $id;
  322. $data["delete"] = 1;
  323. \app\common\model\IntegralRecord::update($data);
  324. return json(["msg" => "删除成功"]);
  325. }
  326. }