1821349743@qq.com
2023-02-20 fca33f5fba0959d665d749cc78730c1efd1159e8
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.product.text.message.service;
 
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.beust.jcommander.internal.Lists;
import com.google.common.collect.Maps;
import com.product.common.lang.StringUtils;
import com.product.core.dao.BaseDao;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.module.sys.entity.SystemUser;
import com.product.text.message.config.CmnConst;
import com.product.util.BaseUtil;
 
/**
 * 
* Copyright  PRODUCT-BASE
* @Title: PRODUCT-BASE-
* @Project: MessageTemplateService
* @Date: 2021年6月22日 下午3:48:42
* @Author: 杜洪波
* @Description:短信模板
 */
@Component
public class MessageTemplateService {
 
    @Autowired
    BaseDao baseDao;
    
    
    /**
     *     短信模板列表
     * @param fse
     * @return
     */
    public DataTableEntity listMessageTemplate(FieldSetEntity fse) {
        DataTableEntity dt=baseDao.listTable(CmnConst.PRODUCT_SYS_MESSAGE_TEMPLATE, null, new Object[] {}, null, null, fse.getInteger("pagesize"), fse.getInteger("cpage"));
        baseDao.loadPromptData(dt);
        return dt;
    }
    
    /**
     *     短信模板详情
     * @param uuid
     * @return
     */
    @SuppressWarnings("unchecked")
    public String findMessageTemplate(String uuid) {
        FieldSetEntity fse=baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_MESSAGE_TEMPLATE, uuid, false);
        JSONObject result = new JSONObject();
        Map<String, Object> values = (Map) fse.getValues();
        result.putAll(values);
        String template_content_config = result.getString(CmnConst.TEMPLATE_CONTENT_CONFIG);
        if (!JSON.isValidArray(template_content_config)) {
            template_content_config=null;
        }
        result.put(CmnConst.TEMPLATE_CONTENT_CONFIG, configDataTransition(template_content_config));
        
        String template_addressee_config = result.getString(CmnConst.TEMPLATE_ADDRESSEE_CONFIG);
        if (!JSON.isValidArray(template_addressee_config)) {
            template_addressee_config=null;
        }
        result.put(CmnConst.TEMPLATE_ADDRESSEE_CONFIG, configDataTransition(template_addressee_config));
        
        return BaseUtil.success(result, null);
    }
    
    /**
     *     短信模板新增
     * @param fse
     * @return
     */
    public boolean addMessageTemplate(FieldSetEntity fse) {
        SystemUser user = SpringMVCContextHolder.getCurrentUser();
        if (user.getUserType() == 1) {
            fse.setValue(CmnConst.TEMPLATE_CODE, UUID.randomUUID().toString());
        }else {
            fse.remove("uuid");
        }
        fse.setValue(CmnConst.ORG_LEVEL_UUID, user.getOrg_level_uuid());
        fse.setValue(CmnConst.CREATED_BY, SpringMVCContextHolder.getCurrentUserId());
        fse.setValue(CmnConst.CREATED_UTC_DATETIME, new Date());
        return baseDao.saveFieldSetEntity(fse);
    }
    
    /**
     *     短信模板修改
     * @param fse
     * @return
     */
    public boolean updateMessageTemplate(FieldSetEntity fse) {
        fse.setValue(CmnConst.UPDATED_BY, SpringMVCContextHolder.getCurrentUserId());
        fse.setValue(CmnConst.UPDATED_UTC_DATETIME, new Date());
        return baseDao.saveFieldSetEntity(fse);
    }
    
    /**
     *     短信模板删除
     * @param uuid
     * @return
     */
    public boolean deleteMessageTemplate(String uuid) {
        return baseDao.delete(CmnConst.PRODUCT_SYS_MESSAGE_TEMPLATE    , "uuid=?", new Object[] {uuid});
    }
    
     /**
     * 预警配置数据装换
     */
    private List<List<JSONObject>> configDataTransition(String arratStr) {
        List<List<JSONObject>> list = Lists.newArrayList();
        if(!StringUtils.isEmpty(arratStr)){
            JSONArray array = JSON.parseArray(arratStr);
            if(array.size()==0){
                return configDataTransition(null);
            }
            List<JSONObject> row = Lists.newArrayList();
            for (int i = 0; i < array.size(); i++) {
                if (i != 0 && i % 6 == 0) {
                    list.add(row);
                    row = Lists.newArrayList();
                }
                row.add(array.getJSONObject(i));
 
            }
            if (row.size() > 0) {
                if (row.size() < 6) {
                    Map<String, Object> template = null;
                    while (row.size() < 6) {
                        template = Maps.newHashMap();
                        template.put("label", "");
                        template.put("val", "");
                        row.add(new JSONObject(template));
                    }
                }
                list.add(row);
            }
            if (!StringUtils.isEmpty(list.get(list.size() - 1).get(5).getString("label"))) {
                List<JSONObject> templates = Lists.newArrayList();
                JSONObject template = null;
                row=Lists.newArrayList();
                while (row.size() == 6) {
                    template = new JSONObject();
                    template.put("label", "");
                    template.put("val", "");
                    row.add(template);
                }
                list.add(templates);
            }
        }else{
            JSONObject template = null;
            List<JSONObject> row=Lists.newArrayList();
            while (row.size() < 6) {
                template = new JSONObject();
                template.put("label", "");
                template.put("val", "");
                row.add(template);
            }
            list.add(row);
        }
        return list;
    }
 
}