Integral.php 16 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. $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. "shareholder" => "是否股东"
  82. ];
  83. $no_empty = ["name", "card_type", "card_number", "phone", "email", "shareholder"];
  84. $return = [];
  85. foreach ($no_empty as $key) {
  86. if (!$params[$key]) {
  87. $return[] = sprintf("请填写“%s”", $field_dict[$key]);
  88. }
  89. }
  90. if (count($return) > 0) {
  91. $res = ["msg" => implode("<br>", $return)];
  92. echo sprintf("<script>parent.IntegralInfoDlg.submitCallback(%s);</script>", json_encode($res));
  93. exit;
  94. }
  95. $insertDetailList = [];
  96. $tmp_item_ids = [];
  97. $projectTypes = $params["projectType"];
  98. $projectIds = $params["projectId"];
  99. $item_ids = $params["item_id"];
  100. $amounts = $params["amount"];
  101. $detailCounts = count($item_ids);
  102. $ft_ids = [];
  103. for ($i = 0; $i < $detailCounts; $i++) {
  104. if (!in_array($item_ids[$i], $tmp_item_ids)) {
  105. $tmp_item_ids[] = $item_ids[$i];
  106. }
  107. if (!is_numeric($amounts[$i]) || $amounts[$i] < 0) {
  108. throw new ValidateException(sprintf("第%d个积分标准项的数额填写错误,应填入大于0的数字", $i + 1));
  109. }
  110. $integralItemInfo = \app\common\api\IntegralItemApi::getOne($item_ids[$i]);
  111. if (!$integralItemInfo) {
  112. throw new ValidateException(sprintf("第%d个积分标准项不存在", $i + 1));
  113. }
  114. $ft_ids = array_filter(array_merge($ft_ids, (array) explode(",", $integralItemInfo["fileTypeId"])));
  115. }
  116. if (count($tmp_item_ids) != $detailCounts) {
  117. throw new ValidateException("同一个申报中,同一个积分标准不能申报多次");
  118. }
  119. if ($ft_ids) {
  120. $whr = [];
  121. if ($id) {
  122. $whr[] = ["mainId", "=", $id];
  123. $whr[] = ["type", "=", ProjectState::INTEGRAL];
  124. } else {
  125. if ($params["uploadFiles"])
  126. $whr[] = ["id", "in", $params["uploadFiles"]];
  127. }
  128. $whr[] = ["typeId", "in", $ft_ids];
  129. $distinct_filetypes = Db::table("new_talent_file")->where($whr)->distinct(true)->field("typeId")->select();
  130. $upload_type_counts = count($distinct_filetypes);
  131. if ($upload_type_counts != count($ft_ids)) {
  132. $res = ["msg" => "请留意附件上传栏中带*号的内容均为必传项,请上传完整再提交审核"];
  133. echo sprintf("<script>parent.IntegralInfoDlg.submitCallback(%s);</script>", json_encode($res));
  134. exit;
  135. }
  136. }
  137. return $this->save($info, $this->request, IntegralState::SUBMIT);
  138. } else if (in_array($checkState, [IntegralState::VERIFY_FAIL, IntegralState::REVERIFY_FAIL, IntegralState::ZX_FAIL, IntegralState::ANNOUNCED_REVERIFY_FAIL, IntegralState::PUBLISH_FAIL])) {
  139. $res = ["msg" => "审核失败,不能再提交审核"];
  140. echo sprintf("<script>parent.IntegralInfoDlg.submitCallback(%s);</script>", json_encode($res));
  141. exit;
  142. }
  143. $res = ["msg" => "已提交审核,请耐心等待"];
  144. echo sprintf("<script>parent.IntegralInfoDlg.submitCallback(%s);</script>", json_encode($res));
  145. exit;
  146. }
  147. /**
  148. * 保存申报表单
  149. * @param type $info talent_info
  150. * @param type $param request->param();
  151. */
  152. private function save($info, \think\Request $request, $checkState) {
  153. try {
  154. $param = $request->param();
  155. $batch = \app\common\api\BatchApi::getValidBatch(ProjectState::INTEGRAL, $this->user["type"]);
  156. if (!$batch) {
  157. throw new ValidateException("不在积分申报申请时间内");
  158. }
  159. $data["batch_id"] = $batch["id"];
  160. validate(IntegralValidator::class)->check($param);
  161. $id = $param["id"];
  162. $files = $param["uploadFiles"];
  163. if ($id) {
  164. if (!$info || $info["id"] != $id || $info["enterprise_id"] != $this->user["uid"]) {
  165. throw new ValidateException("没有对应的积分申报信息");
  166. }
  167. }
  168. $all_valid_keys = ["name", "card_type", "card_number", "phone", "email", "shareholder"];
  169. foreach ($all_valid_keys as $key) {
  170. $data[$key] = trim($param[$key]);
  171. }
  172. $insertDetailList = [];
  173. $item_ids = $param["item_id"];
  174. $amounts = $param["amount"];
  175. $detailCounts = count($item_ids);
  176. for ($i = 0; $i < $detailCounts; $i++) {
  177. if (!is_numeric($amounts[$i]) || $amounts[$i] < 0) {
  178. throw new ValidateException(sprintf("第%d个积分标准项的数额填写错误,应填入大于0的数字", $i + 1));
  179. }
  180. $tmpItemData = [];
  181. $tmpItemData = [
  182. "id" => getStringId(),
  183. "record_id" => "",
  184. "item_id" => $item_ids[$i],
  185. "amount" => $amounts[$i],
  186. "unit" => ""
  187. ];
  188. if ($param["card_type"] && $param["card_number"]) {
  189. $log = IntegralRecordApi::calIntegral($this->user["uid"], $param["card_type"], $param["card_number"], $item_ids[$i], $amounts[$i]);
  190. $tmpItemData["pre_point"] = $log->points;
  191. $tmpItemData["log"] = json_encode($log);
  192. }
  193. $insertDetailList[] = $tmpItemData;
  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. }