BatchApi.php 8.6 KB

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