Integral.php 15 KB

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