Integral.php 16 KB

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