Integral.php 16 KB

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