BatchApi.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace app\common\api;
  3. use app\common\model\Batch;
  4. /**
  5. * Description of BatchApi
  6. *
  7. * @author sgq
  8. */
  9. class BatchApi {
  10. public static function getOne($id) {
  11. return Batch::findOrEmpty($id)->toArray();
  12. }
  13. public static function getList($request) {
  14. $order = trim($request->param("order")) ?: "desc";
  15. $offset = trim($request->param("offset")) ?: 0;
  16. $limit = trim($request->param("limit")) ?: 10;
  17. $type = trim($request->param("type"));
  18. $active = trim($request->param("active"));
  19. $where = [];
  20. if ($type) {
  21. $where[] = ["type", "=", $type];
  22. }
  23. if ($active) {
  24. $where[] = ["active", "=", $active];
  25. }
  26. $count = Batch::where($where)->count();
  27. $list = Batch::where($where)->limit($offset, $limit)->order("createTime " . $order)->select()->toArray();
  28. $projects = DictApi::selectByParentCode("declare_type"); //申报类型
  29. foreach ($list as $key => $item) {
  30. $list[$key]["type"] = $projects[$item["type"]];
  31. }
  32. return ["total" => $count, "rows" => $list];
  33. }
  34. public static function create($params) {
  35. if (!$params["type"])
  36. return ["msg" => "申报类别不能为空"];
  37. if (!$params["batch"])
  38. return ["msg" => "批次不能为空"];
  39. $where[] = ["type", "=", $params["type"]];
  40. $where[] = ["batch", "=", $params["batch"]];
  41. $where[] = ["source", "=", $params["source"]];
  42. $count = Batch::where($where)->count();
  43. if ($count > 0) {
  44. return ["msg" => "该申报类别的批次重复"];
  45. }
  46. if (!strtotime($params["startTime"]) && $params["startTime"]) {
  47. return ["msg" => "申报开始时间错误"];
  48. }
  49. if (!strtotime($params["endTime"]) && $params["endTime"]) {
  50. return ["msg" => "申报截止时间错误"];
  51. }
  52. if (!strtotime($params["submitEndTime"]) && $params["submitEndTime"]) {
  53. return ["msg" => "提交截止时间错误"];
  54. }
  55. if (!strtotime($params["publicStartTime"]) && $params["publicStartTime"]) {
  56. return ["msg" => "公示开始时间错误"];
  57. }
  58. if (!strtotime($params["publicEndTime"]) && $params["publicEndTime"]) {
  59. return ["msg" => "公示截止时间错误"];
  60. }
  61. $id = getStringId();
  62. $data["id"] = $id;
  63. $data["type"] = $params["type"];
  64. $data["source"] = $params["source"];
  65. $data["batch"] = $params["batch"];
  66. $data["active"] = $params["active"] ?: 2;
  67. $data["description"] = $params["description"];
  68. $data["type"] = $params["type"];
  69. $data["startTime"] = $params["startTime"];
  70. $data["endTime"] = $params["endTime"];
  71. $data["submitEndTime"] = $params["submitEndTime"];
  72. $data["publicStartTime"] = $params["publicStartTime"];
  73. $data["publicEndTime"] = $params["publicEndTime"];
  74. $data["averageWage"] = $params["averageWage"];
  75. $data["createTime"] = date("Y-m-d H:i:s");
  76. $data["createUser"] = session("user")["uid"];
  77. Batch::insert($data);
  78. return ["code" => 200, "msg" => "成功"];
  79. }
  80. public static function update($params) {
  81. if (!$params["type"])
  82. return ["msg" => "申报类别不能为空"];
  83. if (!$params["batch"])
  84. return ["msg" => "批次不能为空"];
  85. $where[] = ["type", "=", $params["type"]];
  86. $where[] = ["batch", "=", $params["batch"]];
  87. $where[] = ["source", "=", $params["source"]];
  88. $where[] = ["id", "<>", $params["id"]];
  89. $count = Batch::where($where)->count();
  90. if ($count > 0) {
  91. return ["msg" => "该申报类别的批次重复"];
  92. }
  93. if (!strtotime($params["startTime"]) && $params["startTime"]) {
  94. return ["msg" => "申报开始时间错误"];
  95. }
  96. if (!strtotime($params["endTime"]) && $params["endTime"]) {
  97. return ["msg" => "申报截止时间错误"];
  98. }
  99. if (!strtotime($params["submitEndTime"]) && $params["submitEndTime"]) {
  100. return ["msg" => "提交截止时间错误"];
  101. }
  102. if (!strtotime($params["publicStartTime"]) && $params["publicStartTime"]) {
  103. return ["msg" => "公示开始时间错误"];
  104. }
  105. if (!strtotime($params["publicEndTime"]) && $params["publicEndTime"]) {
  106. return ["msg" => "公示截止时间错误"];
  107. }
  108. $data["id"] = $params["id"];
  109. $data["type"] = $params["type"];
  110. $data["source"] = $params["source"];
  111. $data["batch"] = $params["batch"];
  112. $data["active"] = $params["active"] ?: 2;
  113. $data["description"] = $params["description"];
  114. $data["type"] = $params["type"];
  115. $data["startTime"] = $params["startTime"];
  116. $data["endTime"] = $params["endTime"];
  117. $data["submitEndTime"] = $params["submitEndTime"];
  118. $data["publicStartTime"] = $params["publicStartTime"];
  119. $data["publicEndTime"] = $params["publicEndTime"];
  120. $data["averageWage"] = $params["averageWage"];
  121. $data["updateTime"] = date("Y-m-d H:i:s");
  122. $data["updateUser"] = session("user")["uid"];
  123. \think\facade\Db::table("sys_batch")->update($data);
  124. return ["code" => 200, "msg" => "成功"];
  125. }
  126. public static function delete($id) {
  127. Batch::where(["id" => $id])->delete();
  128. return ["code" => 200, "msg" => "成功"];
  129. }
  130. public static function setActive($id, $active) {
  131. $data["id"] = $id;
  132. $data["active"] = $active ?: 2;
  133. Batch::update($data);
  134. if ($active == 1) {
  135. self::setOtherNoActive($id);
  136. }
  137. return ["code" => 200, "msg" => "成功"];
  138. }
  139. public static function setOtherNoActive($except_id) {
  140. $open = self::getOne($except_id);
  141. if ($open) {
  142. $where[] = ["type", "=", $open["type"]];
  143. $where[] = ["source", "=", $open["source"]];
  144. $where[] = ["id", "<>", $except_id];
  145. $data["active"] = 2;
  146. Batch::where($where)->update($data);
  147. }
  148. }
  149. /**
  150. *
  151. * @param type $type 申报类别
  152. * @param type $talentType 人才类型
  153. */
  154. public static function checkBatchValid($params, $talentType) {
  155. $now = time();
  156. $where = [];
  157. $where[] = ["source", "=", $talentType];
  158. if ($params["type"]) {
  159. $where[] = ["type", "=", $params["type"]];
  160. }
  161. if ($params["year"]) {
  162. //检查指定批次是否存在,再比对时间
  163. $where[] = ["batch", "=", $params["year"]];
  164. $batch = Batch::where($where)->order("startTime desc")->order("endTime desc")->find();
  165. if (!$batch)
  166. return ["msg" => "不存在该申报批次"];
  167. if (strtotime($batch["startTime"]) > $now)
  168. return ["msg" => sprintf("申报还未开始,日期为:%s - %s", $batch["startTime"], $batch["endTime"])];
  169. if ($batch["submitEndTime"] && strtotime($batch["submitEndTime"]) < $now)
  170. return ["msg" => "提交时间已截止,无法操作"];
  171. } else {
  172. $where[] = ["active", "=", 1];
  173. $batch = Batch::where($where)->order("startTime desc")->order("endTime desc")->find();
  174. if (!$batch)
  175. return ["msg" => "该申报未启动"];
  176. if (strtotime($batch["startTime"]) > $now)
  177. return ["msg" => sprintf("申报还未开始,日期为:%s - %s", $batch["startTime"], $batch["endTime"])];
  178. if (strtotime($batch["endTime"]) < $now)
  179. return ["msg" => "申报已结束,无法申报"];
  180. }
  181. return ["code" => 200, "batch" => $batch["batch"], "id" => $batch["id"]];
  182. }
  183. /**
  184. *
  185. * @param type $type 申报项目
  186. * @param type $talentType 人才类型
  187. * @return type
  188. */
  189. public static function getValidBatch($type, $talentType, $batch = "") {
  190. $now = date("Y-m-d H:i:s");
  191. $where = [];
  192. if ($batch) {
  193. $where[] = ["batch", "=", $batch];
  194. } else {
  195. $where[] = ["active", "=", 1];
  196. }
  197. $where[] = ["type", "=", $type];
  198. $where[] = ["source", "=", $talentType];
  199. $where[] = ["startTime", "<", $now];
  200. $where[] = ["endTime", ">", $now];
  201. return $batch = Batch::where($where)->order("createTime desc")->findOrEmpty();
  202. }
  203. }