Policy.php 7.1 KB

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