shikeyin
2024-01-11 65da8373531677b1c37a98f53eaa30c892f35e5a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.iplatform.base.controller;
 
import com.iplatform.base.PlatformAdapterController;
import com.iplatform.base.PlatformRuntimeException;
import com.iplatform.base.cache.FormCacheProvider;
import com.iplatform.base.pojo.KeywordsParam;
import com.iplatform.base.pojo.form.RequestForm;
import com.iplatform.base.service.ConfigFormServiceImpl;
import com.iplatform.model.po.S_config_form;
import com.iplatform.model.vo.ConfigFormVo;
import com.walker.db.page.GenericPager;
import com.walker.infrastructure.utils.DateUtils;
import com.walker.infrastructure.utils.JsonUtils;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.infrastructure.utils.UrlUtils;
import com.walker.web.ResponseValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * 配置表单管理功能。
 * @author 时克英
 * @date 2023-05-16
 */
@RestController
@RequestMapping("/system/form")
public class ConfigFormController extends PlatformAdapterController {
 
    private ConfigFormServiceImpl configFormService;
    private FormCacheProvider formCacheProvider;
 
    @Autowired
    public ConfigFormController(ConfigFormServiceImpl configFormService, FormCacheProvider formCacheProvider){
        this.configFormService = configFormService;
        this.formCacheProvider = formCacheProvider;
    }
 
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseValue edit(@RequestParam Integer id, @RequestBody RequestForm requestForm){
        if(requestForm == null || StringUtils.isEmpty(requestForm.getContent()) || id == null){
            return ResponseValue.error("参数错误");
        }
 
        try {
            JsonUtils.jsonStringToObject(requestForm.getContent(), ConfigFormVo.class);
            S_config_form form = this.acquireConfigForm(requestForm, id);
            this.configFormService.save(form);
            this.formCacheProvider.updateCacheData(String.valueOf(form.getId()), form);
            logger.info("编辑表单模板成功:" + requestForm.getName());
 
        } catch (Exception e) {
            logger.error(requestForm.getContent());
            throw new PlatformRuntimeException("更新表单模板格式不正确:" + e.getMessage(), e);
        }
        return ResponseValue.success();
    }
 
    /**
     * 保存新创建表单模板。
     * @param requestForm
     * @return
     * @date 2023-05-23
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ResponseValue add(@RequestBody RequestForm requestForm){
        if(requestForm == null || StringUtils.isEmpty(requestForm.getContent())){
            return ResponseValue.error("参数错误");
        }
        try {
            JsonUtils.jsonStringToObject(requestForm.getContent(), ConfigFormVo.class);
//            S_config_form form = new S_config_form();
//            form.setName(requestForm.getName());
//            form.setInfo(requestForm.getInfo());
//            form.setContent(requestForm.getContent());
//            form.setCreate_time(DateUtils.getDateTimeNumber());
//            form.setUpdate_time(form.getCreate_time());
//            form.setId(this.configFormService.queryNextId());
            S_config_form form = this.acquireConfigForm(requestForm, this.configFormService.queryNextId());
            this.configFormService.insert(form);
            this.formCacheProvider.putCacheData(String.valueOf(form.getId()), form);
            logger.info("创建表单模板成功:" + requestForm.getName());
 
        } catch (Exception e) {
            logger.error(requestForm.getContent());
            throw new PlatformRuntimeException("创建的表单模板格式不正确:" + e.getMessage(), e);
        }
        return ResponseValue.success();
    }
 
    private S_config_form acquireConfigForm(RequestForm requestForm, int id){
        S_config_form form = new S_config_form();
        form.setName(requestForm.getName());
        form.setInfo(requestForm.getInfo());
        form.setContent(requestForm.getContent());
        form.setCreate_time(DateUtils.getDateTimeNumber());
        form.setUpdate_time(form.getCreate_time());
        form.setId(id);
        return form;
    }
 
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public ResponseValue info(@RequestParam(value = "id") Integer id){
//        S_config_form form = this.configFormService.get(new S_config_form(id));
        if(id == null || id.intValue() <= 0){
            return ResponseValue.error();
        }
        S_config_form form = this.formCacheProvider.getCacheData(id.toString());
        if(form == null){
            return ResponseValue.error("表单不存在, id=" + id);
        }
        return ResponseValue.success(form);
    }
 
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseValue list(KeywordsParam keywordsParam){
        String keywords = null;
        if(keywordsParam != null && StringUtils.isNotEmpty(keywordsParam.getKeywords())){
            keywords = UrlUtils.decode(keywordsParam.getKeywords());
        }
        GenericPager<S_config_form> pager = this.configFormService.queryPageFormList(keywords);
        return ResponseValue.success(pager);
    }
}