123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- namespace app\common\api;
- use app\common\model\Batch;
- /**
- * Description of BatchApi
- *
- * @author sgq
- */
- class BatchApi {
- public static function getOne($id) {
- return Batch::findOrEmpty($id)->toArray();
- }
- public static function getList($request) {
- $order = trim($request->param("order")) ?: "desc";
- $offset = trim($request->param("offset")) ?: 0;
- $limit = trim($request->param("limit")) ?: 10;
- $type = trim($request->param("type"));
- $active = trim($request->param("active"));
- $where = [];
- if ($type) {
- $where[] = ["type", "=", $type];
- }
- if ($active) {
- $where[] = ["active", "=", $active];
- }
- $count = Batch::where($where)->count();
- $list = Batch::where($where)->limit($offset, $limit)->order("createTime " . $order)->select()->toArray();
- $projects = DictApi::selectByParentCode("declare_type"); //申报类型
- foreach ($list as $key => $item) {
- $list[$key]["type"] = $projects[$item["type"]];
- }
- return ["total" => $count, "rows" => $list];
- }
- public static function create($params) {
- if (!$params["type"])
- return ["msg" => "申报类别不能为空"];
- if (!$params["batch"])
- return ["msg" => "批次不能为空"];
- $where[] = ["type", "=", $params["type"]];
- $where[] = ["batch", "=", $params["batch"]];
- $where[] = ["source", "=", $params["source"]];
- $count = Batch::where($where)->count();
- if ($count > 0) {
- return ["msg" => "该申报类别的批次重复"];
- }
- if (!strtotime($params["startTime"]) && $params["startTime"]) {
- return ["msg" => "申报开始时间错误"];
- }
- if (!strtotime($params["endTime"]) && $params["endTime"]) {
- return ["msg" => "申报截止时间错误"];
- }
- if (!strtotime($params["submitEndTime"]) && $params["submitEndTime"]) {
- return ["msg" => "提交截止时间错误"];
- }
- if (!strtotime($params["publicStartTime"]) && $params["publicStartTime"]) {
- return ["msg" => "公示开始时间错误"];
- }
- if (!strtotime($params["publicEndTime"]) && $params["publicEndTime"]) {
- return ["msg" => "公示截止时间错误"];
- }
- $id = getStringId();
- $data["id"] = $id;
- $data["type"] = $params["type"];
- $data["source"] = $params["source"];
- $data["batch"] = $params["batch"];
- $data["active"] = $params["active"] ?: 2;
- $data["description"] = $params["description"];
- $data["type"] = $params["type"];
- $data["startTime"] = $params["startTime"];
- $data["endTime"] = $params["endTime"];
- $data["submitEndTime"] = $params["submitEndTime"];
- $data["publicStartTime"] = $params["publicStartTime"];
- $data["publicEndTime"] = $params["publicEndTime"];
- $data["averageWage"] = $params["averageWage"];
- $data["createTime"] = date("Y-m-d H:i:s");
- $data["createUser"] = session("user")["uid"];
- Batch::insert($data);
- return ["code" => 200, "msg" => "成功"];
- }
- public static function update($params) {
- if (!$params["type"])
- return ["msg" => "申报类别不能为空"];
- if (!$params["batch"])
- return ["msg" => "批次不能为空"];
- $where[] = ["type", "=", $params["type"]];
- $where[] = ["batch", "=", $params["batch"]];
- $where[] = ["source", "=", $params["source"]];
- $where[] = ["id", "<>", $params["id"]];
- $count = Batch::where($where)->count();
- if ($count > 0) {
- return ["msg" => "该申报类别的批次重复"];
- }
- if (!strtotime($params["startTime"]) && $params["startTime"]) {
- return ["msg" => "申报开始时间错误"];
- }
- if (!strtotime($params["endTime"]) && $params["endTime"]) {
- return ["msg" => "申报截止时间错误"];
- }
- if (!strtotime($params["submitEndTime"]) && $params["submitEndTime"]) {
- return ["msg" => "提交截止时间错误"];
- }
- if (!strtotime($params["publicStartTime"]) && $params["publicStartTime"]) {
- return ["msg" => "公示开始时间错误"];
- }
- if (!strtotime($params["publicEndTime"]) && $params["publicEndTime"]) {
- return ["msg" => "公示截止时间错误"];
- }
- $data["id"] = $params["id"];
- $data["type"] = $params["type"];
- $data["source"] = $params["source"];
- $data["batch"] = $params["batch"];
- $data["active"] = $params["active"] ?: 2;
- $data["description"] = $params["description"];
- $data["type"] = $params["type"];
- $data["startTime"] = $params["startTime"];
- $data["endTime"] = $params["endTime"];
- $data["submitEndTime"] = $params["submitEndTime"];
- $data["publicStartTime"] = $params["publicStartTime"];
- $data["publicEndTime"] = $params["publicEndTime"];
- $data["averageWage"] = $params["averageWage"];
- $data["updateTime"] = date("Y-m-d H:i:s");
- $data["updateUser"] = session("user")["uid"];
- \think\facade\Db::table("sys_batch")->update($data);
- return ["code" => 200, "msg" => "成功"];
- }
- public static function delete($id) {
- Batch::where(["id" => $id])->delete();
- return ["code" => 200, "msg" => "成功"];
- }
- public static function setActive($id, $active) {
- $data["id"] = $id;
- $data["active"] = $active ?: 2;
- Batch::update($data);
- if ($active == 1) {
- self::setOtherNoActive($id);
- }
- return ["code" => 200, "msg" => "成功"];
- }
- public static function setOtherNoActive($except_id) {
- $open = self::getOne($except_id);
- if ($open) {
- $where[] = ["type", "=", $open["type"]];
- $where[] = ["source", "=", $open["source"]];
- $where[] = ["id", "<>", $except_id];
- $data["active"] = 2;
- Batch::where($where)->update($data);
- }
- }
- /**
- *
- * @param type $type 申报类别
- * @param type $talentType 人才类型
- */
- public static function checkBatchValid($params, $talentType) {
- $now = time();
- $where = [];
- $where[] = ["source", "=", $talentType];
- if ($params["type"]) {
- $where[] = ["type", "=", $params["type"]];
- }
- if ($params["year"]) {
- //检查指定批次是否存在,再比对时间
- $where[] = ["batch", "=", $params["year"]];
- $batch = Batch::where($where)->order("startTime desc")->order("endTime desc")->find();
- if (!$batch)
- return ["msg" => "不存在该申报批次"];
- if (strtotime($batch["startTime"]) > $now)
- return ["msg" => sprintf("申报还未开始,日期为:%s - %s", $batch["startTime"], $batch["endTime"])];
- if ($batch["submitEndTime"] && strtotime($batch["submitEndTime"]) < $now)
- return ["msg" => "提交时间已截止,无法操作"];
- } else {
- $where[] = ["active", "=", 1];
- $batch = Batch::where($where)->order("startTime desc")->order("endTime desc")->find();
- if (!$batch)
- return ["msg" => "该申报未启动"];
- if (strtotime($batch["startTime"]) > $now)
- return ["msg" => sprintf("申报还未开始,日期为:%s - %s", $batch["startTime"], $batch["endTime"])];
- if (strtotime($batch["endTime"]) < $now)
- return ["msg" => "申报已结束,无法申报"];
- }
- return ["code" => 200, "batch" => $batch["batch"], "id" => $batch["id"]];
- }
- /**
- *
- * @param type $type 申报项目
- * @param type $talentType 人才类型
- * @return type
- */
- public static function getValidBatch($type, $talentType, $batch = "") {
- $now = date("Y-m-d H:i:s");
- $where = [];
- if ($batch) {
- $where[] = ["batch", "=", $batch];
- } else {
- $where[] = ["active", "=", 1];
- }
- $where[] = ["type", "=", $type];
- $where[] = ["source", "=", $talentType];
- $where[] = ["startTime", "<", $now];
- $where[] = ["endTime", ">", $now];
- return $batch = Batch::where($where)->order("createTime desc")->findOrEmpty();
- }
- }
|