Policy.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. namespace app\admin\controller;
  8. use app\admin\common\AdminController;
  9. use think\facade\Db;
  10. /**
  11. * Description of Policy
  12. *
  13. * @author sgq
  14. */
  15. class Policy extends AdminController {
  16. protected $companyId;
  17. public function __construct(\think\App $app) {
  18. parent::__construct($app);
  19. $this->companyId = $this->user["companyId"];
  20. return json(["msg" => "政策录入已经结束"]);
  21. }
  22. public function index() {
  23. $company = \app\common\api\CompanyApi::getOne($this->companyId);
  24. $tmps = Db::table("new_policy_tmp")->where("companyId", "=", $this->companyId)->column("files", "policyId");
  25. $policyIds = array_keys($tmps);
  26. $policys = Db::table("new_policy")->order("level asc")->select()->toArray();
  27. foreach ($policys as $key => $policy) {
  28. if (in_array($policy["id"], $policyIds)) {
  29. $policys[$key]["checked"] = true;
  30. }
  31. $policys[$key]["files"] = $tmps[$policy["id"]];
  32. }
  33. return view("", ["company" => $company, "policys" => $policys]);
  34. }
  35. public function submit() {
  36. if ($this->request->isPost()) {
  37. $datas = $this->request["data"];
  38. $success = 0;
  39. $error = 0;
  40. foreach ($datas as $key => $data) {
  41. $policyId = $data["id"];
  42. $uniqueId = sprintf("%s_%s", $policyId, $this->companyId);
  43. $record = Db::table("new_policy_tmp")->where("id", "=", $uniqueId)->findOrEmpty();
  44. if ($data["checked"] == 1) {
  45. //需要本单位审核
  46. $save["id"] = $uniqueId;
  47. $save["companyId"] = $this->companyId;
  48. $save["policyId"] = $policyId;
  49. $save["files"] = $data["files"];
  50. if ($record) {
  51. $save["updateUser"] = $this->user["uid"];
  52. $save["updateTime"] = date("Y-m-d H:i:s");
  53. if (Db::table("new_policy_tmp")->update($save)) {
  54. $success++;
  55. } else {
  56. $error++;
  57. }
  58. } else {
  59. $save["createUser"] = $this->user["uid"];
  60. $save["createTime"] = date("Y-m-d H:i:s");
  61. if (Db::table("new_policy_tmp")->insert($save)) {
  62. $success++;
  63. } else {
  64. $error++;
  65. }
  66. }
  67. } else {
  68. //取消本单位审核
  69. if ($record) {
  70. if (Db::table("new_policy_tmp")->delete($uniqueId)) {
  71. $success++;
  72. } else {
  73. $error++;
  74. }
  75. } else {
  76. $success++;
  77. }
  78. }
  79. }
  80. return json(["msg" => sprintf("共处理{%d}条审核单位设置,成功{%d}条,失败{%d}条", count($datas), $success, $error)]);
  81. }
  82. }
  83. public function checked() {
  84. if ($this->request->isPost()) {
  85. $policyId = $this->request->param("id");
  86. $uniqueId = sprintf("%s_%s", $policyId, $this->companyId);
  87. $checked = $this->request->param("checked") ?: 1;
  88. $lockFileName = sprintf("storage/policy_logs/%s.lock", $uniqueId);
  89. if (!file_exists("storage/policy_logs")) {
  90. mkdir("storage/policy_logs");
  91. }
  92. $lockFile = fopen($lockFileName, "a");
  93. if (flock($lockFile, LOCK_EX | LOCK_NB)) {//文件锁(独占)
  94. try {
  95. $record = Db::table("new_policy_tmp")->where("id", "=", $uniqueId)->findOrEmpty();
  96. if ($checked == 1) {
  97. //需要本单位审核
  98. if ($record) {
  99. $log = sprintf("[%s][account:%s][ERR]Already checked!", date("Y-m-d H:i:s"), $this->user["account"]);
  100. fwrite($lockFile, $log . "\n");
  101. return json(["msg" => "已经添加审核,不需要重复添加"]);
  102. } else {
  103. $data["id"] = $uniqueId;
  104. $data["companyId"] = $this->companyId;
  105. $data["policyId"] = $policyId;
  106. $data["createUser"] = $this->user["uid"];
  107. $data["createTime"] = date("Y-m-d H:i:s");
  108. if (Db::table("new_policy_tmp")->insert($data)) {
  109. $log = sprintf("[%s][account:%s][INFO]Checked!", date("Y-m-d H:i:s"), $this->user["account"]);
  110. fwrite($lockFile, $log . "\n");
  111. return json(["msg" => "添加审核成功", "code" => 200]);
  112. } else {
  113. $log = sprintf("[%s][account:%s][ERR]Check failure!", date("Y-m-d H:i:s"), $this->user["account"]);
  114. fwrite($lockFile, $log . "\n");
  115. return json(["msg" => "添加审核失败"]);
  116. }
  117. }
  118. } else {
  119. //取消本单位审核
  120. if ($record) {
  121. if (Db::table("new_policy_tmp")->delete($uniqueId)) {
  122. $log = sprintf("[%s][account:%s][INFO]Deleted!", date("Y-m-d H:i:s"), $this->user["account"]);
  123. fwrite($lockFile, $log . "\n");
  124. return json(["msg" => "取消审核成功", "code" => 200]);
  125. } else {
  126. $log = sprintf("[%s][account:%s][ERR]Delete failure!", date("Y-m-d H:i:s"), $this->user["account"]);
  127. fwrite($lockFile, $log . "\n");
  128. return json(["msg" => "取消审核失败"]);
  129. }
  130. } else {
  131. $log = sprintf("[%s][account:%s][ERR]Already deleted!", date("Y-m-d H:i:s"), $this->user["account"]);
  132. fwrite($lockFile, $log . "\n");
  133. return json(["msg" => "已经取消审核,不需要重复取消"]);
  134. }
  135. }
  136. } catch (\Exception $e) {
  137. $log = sprintf("[%s][account:%s][ERR]%s", date("Y-m-d H:i:s"), $this->user["account"], $e->getMessage());
  138. fwrite($lockFile, $log . "\n");
  139. return json(["msg" => $e->getMessage()]);
  140. } finally {
  141. flock($lockFile, LOCK_UN);
  142. }
  143. } else {
  144. return json(["msg" => "已经有同单位的人员在操作同一条记录,请稍候片刻"]);
  145. }
  146. }
  147. }
  148. }