BatchApi.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. namespace app\common\api;
  3. use app\common\model\Batch;
  4. use app\common\state\CommonConst;
  5. /**
  6. * Description of BatchApi
  7. *
  8. * @author sgq
  9. */
  10. class BatchApi {
  11. public static function getOne($id) {
  12. return Batch::findOrEmpty($id)->toArray();
  13. }
  14. public static function getList($request) {
  15. $order = trim($request->param("order")) ?: "desc";
  16. $offset = trim($request->param("offset")) ?: 0;
  17. $limit = trim($request->param("limit")) ?: 10;
  18. $type = trim($request->param("type"));
  19. $active = trim($request->param("active"));
  20. $where = [];
  21. $where[] = ["source", "=", session("user")["type"]];
  22. if ($type) {
  23. $where[] = ["type", "=", $type];
  24. }
  25. if ($active) {
  26. $where[] = ["active", "=", $active];
  27. }
  28. $count = Batch::where($where)->count();
  29. $list = Batch::where($where)->limit($offset, $limit)->order("createTime " . $order)->select()->toArray();
  30. $projects = DictApi::selectByParentCode("declare_type"); //申报类型
  31. foreach ($list as $key => $item) {
  32. $list[$key]["type"] = $projects[$item["type"]];
  33. }
  34. return ["total" => $count, "rows" => $list];
  35. }
  36. public static function create($params) {
  37. if (!$params["type"])
  38. return ["msg" => "申报类别不能为空"];
  39. if (!$params["batch"])
  40. return ["msg" => "批次不能为空"];
  41. if (!in_array(session("user")["type"], [CommonConst::ENTERPRISE_NORMAL, CommonConst::ENTERPRISE_JC]) && $params["type"] == \app\common\state\ProjectState::LIVINGALLOWANCE) {
  42. return ["msg" => "当前账号类型没有操作权限"];
  43. }
  44. $where[] = ["type", "=", $params["type"]];
  45. $where[] = ["batch", "=", $params["batch"]];
  46. $where[] = ["source", "=", $params["source"]];
  47. $count = Batch::where($where)->count();
  48. if ($count > 0) {
  49. return ["msg" => "该申报类别的批次重复"];
  50. }
  51. if (!strtotime($params["startTime"]) && $params["startTime"]) {
  52. return ["msg" => "申报开始时间错误"];
  53. }
  54. if (!strtotime($params["endTime"]) && $params["endTime"]) {
  55. return ["msg" => "申报截止时间错误"];
  56. }
  57. if (!strtotime($params["submitEndTime"]) && $params["submitEndTime"]) {
  58. return ["msg" => "提交截止时间错误"];
  59. }
  60. if (!strtotime($params["publicStartTime"]) && $params["publicStartTime"]) {
  61. return ["msg" => "公示开始时间错误"];
  62. }
  63. if (!strtotime($params["publicEndTime"]) && $params["publicEndTime"]) {
  64. return ["msg" => "公示截止时间错误"];
  65. }
  66. if ($params["active"] == 1 && !self::checkIsAllowedActive(["startTime" => $params["startTime"], "endTime" => $params["endTime"], "type" => $params["type"], "source" => $params["source"]])) {
  67. return ["msg" => "申报时间不能与已经开启的批次存在交叉时间,批次添加失败!"];
  68. }
  69. $id = getStringId();
  70. $data["id"] = $id;
  71. $data["type"] = $params["type"];
  72. $data["source"] = $params["source"];
  73. $data["batch"] = $params["batch"];
  74. $data["active"] = $params["active"] ?: 2;
  75. $data["description"] = $params["description"];
  76. $data["type"] = $params["type"];
  77. $data["startTime"] = $params["startTime"];
  78. $data["endTime"] = $params["endTime"];
  79. $data["submitEndTime"] = $params["submitEndTime"];
  80. $data["publicStartTime"] = $params["publicStartTime"];
  81. $data["publicEndTime"] = $params["publicEndTime"];
  82. $data["averageWage"] = $params["averageWage"];
  83. $data["createTime"] = date("Y-m-d H:i:s");
  84. $data["createUser"] = session("user")["uid"];
  85. Batch::insert($data);
  86. return ["code" => 200, "msg" => "成功"];
  87. }
  88. public static function update($params) {
  89. $batch = self::getOne($params["id"]);
  90. if (!$batch)
  91. return ["msg" => "修改的批次信息不存在"];
  92. if (!$params["type"])
  93. return ["msg" => "申报类别不能为空"];
  94. if (!$params["batch"])
  95. return ["msg" => "批次不能为空"];
  96. if (!in_array(session("user")["type"], [CommonConst::ENTERPRISE_NORMAL, CommonConst::ENTERPRISE_JC]) && $params["type"] == \app\common\state\ProjectState::LIVINGALLOWANCE) {
  97. return ["msg" => "当前账号类型没有操作权限"];
  98. }
  99. $where[] = ["type", "=", $params["type"]];
  100. $where[] = ["batch", "=", $params["batch"]];
  101. $where[] = ["source", "=", $params["source"]];
  102. $where[] = ["id", "<>", $params["id"]];
  103. $count = Batch::where($where)->count();
  104. if ($count > 0) {
  105. return ["msg" => "该申报类别的批次重复"];
  106. }
  107. if (!strtotime($params["startTime"]) && $params["startTime"]) {
  108. return ["msg" => "申报开始时间错误"];
  109. }
  110. if (!strtotime($params["endTime"]) && $params["endTime"]) {
  111. return ["msg" => "申报截止时间错误"];
  112. }
  113. if (!strtotime($params["submitEndTime"]) && $params["submitEndTime"]) {
  114. return ["msg" => "提交截止时间错误"];
  115. }
  116. if (!strtotime($params["publicStartTime"]) && $params["publicStartTime"]) {
  117. return ["msg" => "公示开始时间错误"];
  118. }
  119. if (!strtotime($params["publicEndTime"]) && $params["publicEndTime"]) {
  120. return ["msg" => "公示截止时间错误"];
  121. }
  122. if ($batch["active"] == 1 && !self::checkIsAllowedActive(["id" => $params["id"], "startTime" => $params["startTime"], "endTime" => $params["endTime"], "type" => $params["type"], "source" => $params["source"]])) {
  123. return ["msg" => "申报时间不能与已经开启的批次存在交叉时间,批次编辑失败!"];
  124. }
  125. $data["id"] = $params["id"];
  126. $data["type"] = $params["type"];
  127. $data["source"] = $params["source"];
  128. $data["batch"] = $params["batch"];
  129. $data["active"] = $batch["active"] ?: 2;
  130. $data["description"] = $params["description"];
  131. $data["type"] = $params["type"];
  132. $data["startTime"] = $params["startTime"];
  133. $data["endTime"] = $params["endTime"];
  134. $data["submitEndTime"] = $params["submitEndTime"];
  135. $data["publicStartTime"] = $params["publicStartTime"];
  136. $data["publicEndTime"] = $params["publicEndTime"];
  137. $data["averageWage"] = $params["averageWage"];
  138. $data["updateTime"] = date("Y-m-d H:i:s");
  139. $data["updateUser"] = session("user")["uid"];
  140. \think\facade\Db::table("sys_batch")->update($data);
  141. return ["code" => 200, "msg" => "成功"];
  142. }
  143. public static function delete($id) {
  144. Batch::where(["id" => $id])->delete();
  145. return ["code" => 200, "msg" => "成功"];
  146. }
  147. public static function setActive($id, $active) {
  148. if ($active == 1 && !self::checkIsAllowedActive($id)) {
  149. return ["msg" => "申报时间不能与已经开启的批次存在交叉时间,批次启动失败!"];
  150. }
  151. $data["id"] = $id;
  152. $data["active"] = $active ?: 2;
  153. Batch::update($data);
  154. return ["code" => 200, "msg" => "成功"];
  155. }
  156. /**
  157. *
  158. * @param type $param 当值为整数时,表示id;当值为array时,表示各种参数的集合
  159. * @return boolean
  160. */
  161. public static function checkIsAllowedActive($params) {
  162. if (is_array($params)) {
  163. if ($params["id"]) {
  164. $where[] = ["id", "<>", $params["id"]];
  165. }
  166. $startTime = $params["startTime"];
  167. $endTime = $params["endTime"];
  168. $where[] = ["type", "=", $params["type"]];
  169. $where[] = ["source", "=", $params["source"]];
  170. } else {
  171. $id = $params;
  172. $batch = self::getOne($id);
  173. $startTime = $batch["startTime"];
  174. $endTime = $batch["endTime"];
  175. $where[] = ["id", "<>", $id];
  176. $where[] = ["type", "=", $batch["type"]];
  177. $where[] = ["source", "=", $batch["source"]];
  178. }
  179. $where[] = ["active", "=", 1];
  180. //$whereRaw = sprintf("startTime between '%s'and '%s' or endTime between '%s' and '%s'", $startTime, $endTime, $startTime, $endTime);存在错误
  181. //$exists = Batch::where($where)->whereRaw($whereRaw)->fetchSql(true)->find();
  182. $activeList = Batch::where($where)->select()->toArray();
  183. $startTime = strtotime($startTime);
  184. $endTime = strtotime($endTime);
  185. $allowed = true;
  186. while ($active = array_shift($activeList)) {
  187. $_startTime = strtotime($active["startTime"]);
  188. $_endTime = strtotime($active["endTime"]);
  189. if (($startTime >= $_startTime && $startTime <= $_endTime) || ($endTime >= $_startTime && $endTime <= $_endTime)) {
  190. $allowed = false;
  191. break;
  192. }
  193. }
  194. return $allowed;
  195. }
  196. public static function setOtherNoActive($except_id) {
  197. $open = self::getOne($except_id);
  198. if ($open) {
  199. $where[] = ["type", "=", $open["type"]];
  200. $where[] = ["source", "=", $open["source"]];
  201. $where[] = ["id", "<>", $except_id];
  202. $data["active"] = 2;
  203. Batch::where($where)->update($data);
  204. }
  205. }
  206. /**
  207. *
  208. * @param type $type 申报类别
  209. * @param type $talentType 人才类型
  210. */
  211. public static function checkBatchValid($params, $talentType) {
  212. $now = time();
  213. $where = [];
  214. if ($params["type"] == \app\common\state\ProjectState::LIVINGALLOWANCE) {
  215. if ($talentType == CommonConst::ENTERPRISE_JC) {
  216. $talentType = CommonConst::ENTERPRISE_JC;
  217. } else {
  218. $talentType = CommonConst::ENTERPRISE_NORMAL; //除电路外如果是硕博生活补贴申请,跟晋江人才同批次判断(也就是跟人社同批次)
  219. }
  220. }
  221. $where[] = ["source", "=", $talentType];
  222. if ($params["type"]) {
  223. $where[] = ["type", "=", $params["type"]];
  224. }
  225. if ($params["year"]) {
  226. //检查指定批次是否存在,再比对时间
  227. $where[] = ["batch", "=", $params["year"]];
  228. $batch = Batch::where($where)->order("startTime desc")->order("endTime desc")->find();
  229. if (!$batch)
  230. return ["msg" => "不存在该申报批次"];
  231. if (strtotime($batch["startTime"]) > $now)
  232. return ["msg" => sprintf("申报还未开始,日期为:%s - %s", $batch["startTime"], $batch["endTime"])];
  233. if (strtotime($batch["endTime"]) < $now && !$params["first_submit_time"])
  234. return ["msg" => "申报已结束,无法申报"];
  235. if ($batch["submitEndTime"] && strtotime($batch["submitEndTime"]) < $now)
  236. return ["msg" => "提交时间已截止,无法操作"];
  237. } else {
  238. $where[] = ["active", "=", 1];
  239. $date = date("Y-m-d H:i:s");
  240. $whereRaw = sprintf("startTime < '%s' and endTime > '%s'", $date, $date);
  241. $batch = Batch::where($where)->whereRaw($whereRaw)->order("startTime desc")->order("endTime desc")->find();
  242. if (!$batch)
  243. return ["msg" => "该申报未启动"];
  244. if (strtotime($batch["startTime"]) > $now)
  245. return ["msg" => sprintf("申报还未开始,日期为:%s - %s", $batch["startTime"], $batch["endTime"])];
  246. if (strtotime($batch["endTime"]) < $now)
  247. return ["msg" => "申报已结束,无法申报"];
  248. }
  249. return ["code" => 200, "batch" => $batch["batch"], "id" => $batch["id"]];
  250. }
  251. /**
  252. *
  253. * @param type $type 申报项目
  254. * @param type $talentType 人才类型
  255. * @return type
  256. */
  257. public static function getValidBatch($type, $talentType, $batch = "") {
  258. $now = date("Y-m-d H:i:s");
  259. $where = [];
  260. if ($type == \app\common\state\ProjectState::LIVINGALLOWANCE) {
  261. if ($talentType == CommonConst::ENTERPRISE_JC) {
  262. $talentType = CommonConst::ENTERPRISE_JC;
  263. } else {
  264. $talentType = CommonConst::ENTERPRISE_NORMAL; //除电路外如果是硕博生活补贴申请,跟晋江人才同批次判断(也就是跟人社同批次)
  265. }
  266. }
  267. if ($batch) {
  268. $where[] = ["batch", "=", $batch];
  269. } else {
  270. $where[] = ["active", "=", 1];
  271. }
  272. $where[] = ["type", "=", $type];
  273. $where[] = ["source", "=", $talentType];
  274. $where[] = ["startTime", "<", $now];
  275. $where[] = ["endTime", ">", $now];
  276. return $batch = Batch::where($where)->order("createTime desc")->findOrEmpty();
  277. }
  278. /**
  279. *
  280. * @param type $type 申报项目
  281. * @param type $talentType 人才类型
  282. * @return type
  283. */
  284. public static function getValidBatchs($type, $talentType) {
  285. $now = date("Y-m-d H:i:s");
  286. $where = [];
  287. if ($type == \app\common\state\ProjectState::LIVINGALLOWANCE) {
  288. if ($talentType == CommonConst::ENTERPRISE_JC) {
  289. $talentType = CommonConst::ENTERPRISE_JC;
  290. } else {
  291. $talentType = CommonConst::ENTERPRISE_NORMAL; //除电路外如果是硕博生活补贴申请,跟晋江人才同批次判断(也就是跟人社同批次)
  292. }
  293. }
  294. if ($batch) {
  295. $where[] = ["batch", "=", $batch];
  296. } else {
  297. $where[] = ["active", "=", 1];
  298. }
  299. $where[] = ["type", "=", $type];
  300. $where[] = ["source", "=", $talentType];
  301. return $batch = Batch::where($where)->order("createTime desc")->select()->toArray();
  302. }
  303. }