BatchApi.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. if ($params["active"] == 1 && !self::checkIsAllowedActive(["startTime" => $params["startTime"], "endTime" => $params["endTime"], "type" => $params["type"], "source" => $params["source"]])) {
  63. return ["msg" => "申报时间不能与已经开启的批次存在交叉时间,批次添加失败!"];
  64. }
  65. $id = getStringId();
  66. $data["id"] = $id;
  67. $data["type"] = $params["type"];
  68. $data["source"] = $params["source"];
  69. $data["batch"] = $params["batch"];
  70. $data["active"] = $params["active"] ?: 2;
  71. $data["description"] = $params["description"];
  72. $data["type"] = $params["type"];
  73. $data["startTime"] = $params["startTime"];
  74. $data["endTime"] = $params["endTime"];
  75. $data["submitEndTime"] = $params["submitEndTime"];
  76. $data["publicStartTime"] = $params["publicStartTime"];
  77. $data["publicEndTime"] = $params["publicEndTime"];
  78. $data["averageWage"] = $params["averageWage"];
  79. $data["createTime"] = date("Y-m-d H:i:s");
  80. $data["createUser"] = session("user")["uid"];
  81. Batch::insert($data);
  82. return ["code" => 200, "msg" => "成功"];
  83. }
  84. public static function update($params) {
  85. $batch = self::getOne($params["id"]);
  86. if (!$batch)
  87. return ["msg" => "修改的批次信息不存在"];
  88. if (!$params["type"])
  89. return ["msg" => "申报类别不能为空"];
  90. if (!$params["batch"])
  91. return ["msg" => "批次不能为空"];
  92. $where[] = ["type", "=", $params["type"]];
  93. $where[] = ["batch", "=", $params["batch"]];
  94. $where[] = ["source", "=", $params["source"]];
  95. $where[] = ["id", "<>", $params["id"]];
  96. $count = Batch::where($where)->count();
  97. if ($count > 0) {
  98. return ["msg" => "该申报类别的批次重复"];
  99. }
  100. if (!strtotime($params["startTime"]) && $params["startTime"]) {
  101. return ["msg" => "申报开始时间错误"];
  102. }
  103. if (!strtotime($params["endTime"]) && $params["endTime"]) {
  104. return ["msg" => "申报截止时间错误"];
  105. }
  106. if (!strtotime($params["submitEndTime"]) && $params["submitEndTime"]) {
  107. return ["msg" => "提交截止时间错误"];
  108. }
  109. if (!strtotime($params["publicStartTime"]) && $params["publicStartTime"]) {
  110. return ["msg" => "公示开始时间错误"];
  111. }
  112. if (!strtotime($params["publicEndTime"]) && $params["publicEndTime"]) {
  113. return ["msg" => "公示截止时间错误"];
  114. }
  115. if ($batch["active"] == 1 && !self::checkIsAllowedActive(["id" => $params["id"], "startTime" => $params["startTime"], "endTime" => $params["endTime"], "type" => $params["type"], "source" => $params["source"]])) {
  116. return ["msg" => "申报时间不能与已经开启的批次存在交叉时间,批次编辑失败!"];
  117. }
  118. $data["id"] = $params["id"];
  119. $data["type"] = $params["type"];
  120. $data["source"] = $params["source"];
  121. $data["batch"] = $params["batch"];
  122. $data["active"] = $batch["active"] ?: 2;
  123. $data["description"] = $params["description"];
  124. $data["type"] = $params["type"];
  125. $data["startTime"] = $params["startTime"];
  126. $data["endTime"] = $params["endTime"];
  127. $data["submitEndTime"] = $params["submitEndTime"];
  128. $data["publicStartTime"] = $params["publicStartTime"];
  129. $data["publicEndTime"] = $params["publicEndTime"];
  130. $data["averageWage"] = $params["averageWage"];
  131. $data["updateTime"] = date("Y-m-d H:i:s");
  132. $data["updateUser"] = session("user")["uid"];
  133. \think\facade\Db::table("sys_batch")->update($data);
  134. return ["code" => 200, "msg" => "成功"];
  135. }
  136. public static function delete($id) {
  137. Batch::where(["id" => $id])->delete();
  138. return ["code" => 200, "msg" => "成功"];
  139. }
  140. public static function setActive($id, $active) {
  141. if ($active == 1 && !self::checkIsAllowedActive($id)) {
  142. return ["msg" => "申报时间不能与已经开启的批次存在交叉时间,批次启动失败!"];
  143. }
  144. $data["id"] = $id;
  145. $data["active"] = $active ?: 2;
  146. Batch::update($data);
  147. return ["code" => 200, "msg" => "成功"];
  148. }
  149. /**
  150. *
  151. * @param type $param 当值为整数时,表示id;当值为array时,表示各种参数的集合
  152. * @return boolean
  153. */
  154. public static function checkIsAllowedActive($params) {
  155. if (is_array($params)) {
  156. if ($params["id"]) {
  157. $where[] = ["id", "<>", $params["id"]];
  158. }
  159. $startTime = $params["startTime"];
  160. $endTime = $params["endTime"];
  161. $where[] = ["type", "=", $params["type"]];
  162. $where[] = ["source", "=", $params["source"]];
  163. } else {
  164. $id = $params;
  165. $batch = self::getOne($id);
  166. $startTime = $batch["startTime"];
  167. $endTime = $batch["endTime"];
  168. $where[] = ["id", "<>", $id];
  169. $where[] = ["type", "=", $batch["type"]];
  170. $where[] = ["source", "=", $batch["source"]];
  171. }
  172. $where[] = ["active", "=", 1];
  173. $whereRaw = sprintf("startTime between '%s'and '%s' or endTime between '%s' and '%s'", $startTime, $endTime, $startTime, $endTime);
  174. $exists = Batch::where($where)->whereRaw($whereRaw)->find();
  175. if ($exists)
  176. return false;
  177. return true;
  178. }
  179. public static function setOtherNoActive($except_id) {
  180. $open = self::getOne($except_id);
  181. if ($open) {
  182. $where[] = ["type", "=", $open["type"]];
  183. $where[] = ["source", "=", $open["source"]];
  184. $where[] = ["id", "<>", $except_id];
  185. $data["active"] = 2;
  186. Batch::where($where)->update($data);
  187. }
  188. }
  189. /**
  190. *
  191. * @param type $type 申报类别
  192. * @param type $talentType 人才类型
  193. */
  194. public static function checkBatchValid($params, $talentType) {
  195. $now = time();
  196. $where = [];
  197. $where[] = ["source", "=", $talentType];
  198. if ($params["type"]) {
  199. $where[] = ["type", "=", $params["type"]];
  200. }
  201. if ($params["year"]) {
  202. //检查指定批次是否存在,再比对时间
  203. $where[] = ["batch", "=", $params["year"]];
  204. $batch = Batch::where($where)->order("startTime desc")->order("endTime desc")->find();
  205. if (!$batch)
  206. return ["msg" => "不存在该申报批次"];
  207. if (strtotime($batch["startTime"]) > $now)
  208. return ["msg" => sprintf("申报还未开始,日期为:%s - %s", $batch["startTime"], $batch["endTime"])];
  209. if (strtotime($batch["endTime"]) < $now && !$params["first_submit_time"])
  210. return ["msg" => "申报已结束,无法申报"];
  211. if ($batch["submitEndTime"] && strtotime($batch["submitEndTime"]) < $now)
  212. return ["msg" => "提交时间已截止,无法操作"];
  213. } else {
  214. $where[] = ["active", "=", 1];
  215. $date = date("Y-m-d H:i:s");
  216. $whereRaw = sprintf("startTime < '%s' and endTime > '%s'", $date, $date);
  217. $batch = Batch::where($where)->whereRaw($whereRaw)->order("startTime desc")->order("endTime desc")->find();
  218. if (!$batch)
  219. return ["msg" => "该申报未启动"];
  220. if (strtotime($batch["startTime"]) > $now)
  221. return ["msg" => sprintf("申报还未开始,日期为:%s - %s", $batch["startTime"], $batch["endTime"])];
  222. if (strtotime($batch["endTime"]) < $now)
  223. return ["msg" => "申报已结束,无法申报"];
  224. }
  225. return ["code" => 200, "batch" => $batch["batch"], "id" => $batch["id"]];
  226. }
  227. /**
  228. *
  229. * @param type $type 申报项目
  230. * @param type $talentType 人才类型
  231. * @return type
  232. */
  233. public static function getValidBatch($type, $talentType, $batch = "") {
  234. $now = date("Y-m-d H:i:s");
  235. $where = [];
  236. if ($batch) {
  237. $where[] = ["batch", "=", $batch];
  238. } else {
  239. $where[] = ["active", "=", 1];
  240. }
  241. $where[] = ["type", "=", $type];
  242. $where[] = ["source", "=", $talentType];
  243. $where[] = ["startTime", "<", $now];
  244. $where[] = ["endTime", ">", $now];
  245. return $batch = Batch::where($where)->order("createTime desc")->findOrEmpty();
  246. }
  247. }