Policy.php 7.1 KB

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