Integral.php 15 KB

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