diff --git a/src/main/java/com/dsideal/QingLong/Collect/Controller/CollectController.java b/src/main/java/com/dsideal/QingLong/Collect/Controller/CollectController.java index 3f7fc5c0..e298149f 100644 --- a/src/main/java/com/dsideal/QingLong/Collect/Controller/CollectController.java +++ b/src/main/java/com/dsideal/QingLong/Collect/Controller/CollectController.java @@ -511,7 +511,7 @@ public class CollectController extends Controller { } //获取登录人员第一个发布任务的角色是什么 int publish_role_id = kvCheck.getInt("publish_role_id"); - Page list = cm.getJobList(publish_role_id,job_name, page, limit); + Page list = cm.getJobList(publish_role_id, job_name, page, limit); renderJson(CommonUtil.renderJsonForLayUI(list)); } @@ -764,4 +764,120 @@ public class CollectController extends Controller { Page list = cm.viewJobList(bureau_id, keyword, is_finish, page, limit); renderJson(CommonUtil.renderJsonForLayUI(list)); } + + /***** 以下为Form表单式填报模块 *********************************/ + /** + * 功能:增加一个表单式的新任务 + * + * @param json + */ + @Before({POST.class}) + @IsLoginInterface({}) + @EmptyInterface({"job_name", "json", "table_name"}) + public void addFormJob(String job_name, String json, String table_name) { + //操作人员 + String person_id = SessionKit.get(getRequest(), getResponse(), "person_id"); + //检查当前登录人员是不是发布任务的角色 + Kv kvCheck = cm.checkPublishRole(person_id); + if (!kvCheck.getBoolean("success")) { + renderJson(kvCheck); + return; + } + //获取登录人员第一个发布任务的角色是什么 + int publish_role_id = kvCheck.getInt("publish_role_id"); + + if (cm.isTableExist(table_name)) { + Map map = new HashMap(); + map.put("success", false); + map.put("message", "表名" + table_name + "已存在,无法创建,请更换后再次提交!"); + renderJson(map); + return; + } + int job_id = cm.addFormJob(person_id, publish_role_id, job_name, table_name, json); + Map map = new HashMap(); + map.put("success", true); + map.put("message", "保存成功!"); + map.put("job_id", job_id); + renderJson(map); + } + + /** + * 功能:更新一个存在的表单式任务 + * + * @param job_id + * @param json + */ + @Before({POST.class}) + @IsLoginInterface({}) + @IsNumericInterface({"job_id"}) + @EmptyInterface({"json", "job_name", "table_name"}) + public void updateFormJob(int job_id, String job_name, String json, String table_name) { + //操作人员 + String person_id = SessionKit.get(getRequest(), getResponse(), "person_id"); + //检查当前登录人员是不是发布任务的角色 + Kv kvCheck = cm.checkPublishRole(person_id); + if (!kvCheck.getBoolean("success")) { + renderJson(kvCheck); + return; + } + //获取登录人员第一个发布任务的角色是什么 + int publish_role_id = kvCheck.getInt("publish_role_id"); + //1、检查输入的表名,是不是在排除了自己本JOB_ID对应的表名外存在,如果是,那么不可以 + String form_table_name = cm.getJob(job_id).getStr("form_table_name"); + + if (!form_table_name.equals(table_name) && cm.isTableExist(table_name)) { + Map map = new HashMap(); + map.put("success", false); + map.put("message", "表名" + table_name + "已存在,无法创建,请更换后再次提交!"); + renderJson(map); + return; + } + cm.updateFormJob(job_id, job_name, json, table_name); + Map map = new HashMap(); + map.put("success", true); + map.put("message", "保存成功!"); + map.put("job_id", job_id); + renderJson(map); + } + + /** + * 功能:填报者保存填报结果 + * + * @param job_id + * @param json + */ + @Before({GET.class}) + @IsLoginInterface({}) + @IsNumericInterface({"job_id"}) + @EmptyInterface({"json"}) + public void saveFormJob(int job_id, String json) { + String table_name = cm.getJob(job_id).getStr("form_table_name"); + //操作人员 + String person_id = SessionKit.get(getRequest(), getResponse(), "person_id"); + //根据人员ID,获取人员所在的单位ID + LoginPersonModel personModel = new LoginPersonModel(); + Record rs = personModel.getLoginInfoByPersonId(person_id); + String bureau_id = rs.get("bureau_id"); + cm.saveFormJob(bureau_id, person_id, table_name, json); + Map map = new HashMap(); + map.put("success", true); + map.put("message", "保存成功!"); + map.put("job_id", job_id); + renderJson(map); + } + + /** + * 功能:获取指定任务指定单位的填报内容 + * + * @param job_id + * @param bureau_id + */ + @Before({GET.class}) + @IsLoginInterface({}) + @IsNumericInterface({"job_id"}) + @EmptyInterface({"bureau_id"}) + public void getFormFillJob(int job_id, String bureau_id) { + Record record = cm.getFormFillJob(job_id, bureau_id); + renderJson(record); + } } \ No newline at end of file diff --git a/src/main/java/com/dsideal/QingLong/Collect/Model/CollectModel.java b/src/main/java/com/dsideal/QingLong/Collect/Model/CollectModel.java index a3a9ccae..690fbb5e 100644 --- a/src/main/java/com/dsideal/QingLong/Collect/Model/CollectModel.java +++ b/src/main/java/com/dsideal/QingLong/Collect/Model/CollectModel.java @@ -2,6 +2,8 @@ package com.dsideal.QingLong.Collect.Model; import cn.hutool.core.date.DateTime; import cn.hutool.core.io.FileUtil; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; import com.dsideal.QingLong.Collect.Const.DataTypeConst; import com.dsideal.QingLong.Interceptor.EmptyInterface; import com.dsideal.QingLong.Interceptor.IsLoginInterface; @@ -1199,4 +1201,117 @@ public class CollectModel { Page pageRecord = Db.paginateByFullSql(page, limit, CommonUtil.getTotalSql(sql), sql); return pageRecord; } + + /** + * 功能:增加一个表单式的新任务 + * + * @param job_name + * @param json + * @return + */ + public int addFormJob(String person_id, int publish_role_id, String job_name, String table_name, String json) { + //1、保存任务信息 + Record record = new Record(); + record.set("job_name", job_name); + record.set("create_time", DateTime.now()); + record.set("person_id", person_id); + record.set("publish_state", 0); + record.set("publish_role_id", publish_role_id); + record.set("job_type", 1); + record.set("form_table_name", table_name); + Db.save("t_collect_job", "job_id", record); + int job_id = record.getInt("job_id"); + //2、需要在这里建表,因为发布时不管建表,只管可见范围 + createTable(job_name, table_name, json); + return job_id; + } + + /** + * 功能:创建表 + * + * @param job_name + * @param table_name + * @param json + */ + private void createTable(String job_name, String table_name, String json) { + JSONArray ja = JSONArray.parseArray(json); + String colSql = "", commentSql = ""; + for (Object o : ja) { + JSONObject jo = (JSONObject) o; + boolean required = jo.getBoolean("required");//必填 + String original_name = jo.getString("label");//描述 + String column_name = jo.getString("id");//字段名 + int data_type_id = jo.getInteger("data_type_id");// 数据类型 + String column_type = getDataType(data_type_id, DataTypeConst.PG_DATA_TYPE); + colSql += column_name + " " + column_type; + + if (required) colSql += " NOT NULL "; + colSql += ","; + commentSql += "COMMENT ON COLUMN \"public\".\"" + table_name + "\".\"" + column_name + "\" IS '" + original_name + "';\n"; + } + + String finalSql = "CREATE TABLE \"public\".\"" + table_name + "\" ("; + finalSql += "\"id\" serial4,"; + finalSql += "\"bureau_id\" char(36) NOT NULL,"; + finalSql += "\"person_id\" char(36) NOT NULL,"; + finalSql += "\"job_id\" int(4) NOT NULL,"; + finalSql += colSql; + finalSql += "PRIMARY KEY (\"id\")"; + finalSql += ");\n"; + + finalSql += "COMMENT ON COLUMN \"public\".\"" + table_name + "\".\"id\" IS '主键,自增长ID';\n"; + finalSql += "COMMENT ON COLUMN \"public\".\"" + table_name + "\".\"bureau_id\" IS '单位ID';\n"; + finalSql += "COMMENT ON COLUMN \"public\".\"" + table_name + "\".\"person_id\" IS '上传人员ID';\n"; + finalSql += "COMMENT ON COLUMN \"public\".\"" + table_name + "\".\"job_id\" IS '任务ID';\n"; + finalSql += commentSql; + finalSql += "COMMENT ON TABLE \"public\".\"" + table_name + "\" IS '" + job_name + "';"; + //添加索引 + finalSql += "CREATE INDEX ON \"public\".\"" + table_name + "\" (\"bureau_id\");"; + finalSql += "CREATE INDEX ON \"public\".\"" + table_name + "\" (\"person_id\");"; + Db.update(finalSql); + } + + /** + * 功能:更新一个存在的表单式任务 + * + * @param job_id + * @param json + */ + public void updateFormJob(int job_id, String job_name, String json, String table_name) { + //删除数据库表 + dropTable(table_name); + //更新数据 + String sql = "update t_collect_job set job_name=?,json=? where job_id=?"; + Db.update(sql, job_name, json, job_id); + //需要在这里建表,因为发布时不管建表,只管可见范围 + createTable(job_name, table_name, json); + } + + /** + * 功能:填报者保存填报结果 + */ + public void saveFormJob(String bureau_id, String person_id, String table_name, String json) { + JSONObject jo = JSONObject.parseObject(json); + Record record = new Record(); + record.set("bureau_id", bureau_id); + record.set("person_id", person_id); + for (Map.Entry entry : jo.entrySet()) { + String key = entry.getKey(); + Object value = entry.getValue(); + record.set(key, value); + } + Db.save(table_name, "id", record); + } + + /** + * 功能:获取指定任务指定单位的填报内容 + * + * @param job_id + * @param bureau_id + */ + public Record getFormFillJob(int job_id, String bureau_id) { + String table_name = getJob(job_id).getStr("form_table_name"); + String sql = "select * from " + table_name + " where bureau_id=? and job_id=?"; + return Db.findFirst(sql, bureau_id, job_id); + } } \ No newline at end of file