Integral.php 18 KB

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