Integral.php 16 KB

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