shichongfu
2023-07-13 054625ce1c40468d0d052369d1de6e0edb8d7ce8
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package com.product.administration.service;
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.product.administration.config.CmnConst;
import com.product.core.config.CoreConst;
import com.product.core.dao.BaseDao;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldMetaEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.service.support.AbstractBaseService;
import com.product.core.service.support.QueryFilterService;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.core.websocket.service.WebsocketMesssageServiceThread;
import com.product.util.BaseUtil;
 
@Component
public class AnnouncementService extends AbstractBaseService {
    @Autowired
    public QueryFilterService queryFilterService;
    @Autowired
    public BaseDao baseDao;
    private final String COOPERATE_URL = "16209107146651641w21286";
 
    public String addAnnouncement(FieldSetEntity fse) throws BaseException {
        fse.setValue("publish_status", 0);
        fse.setValue(CmnConst.CREATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());
        fse.setValue(CmnConst.CREATED_UTC_DATETIME, new Date());
        return baseDao.add(fse);
    }
 
    public boolean releaseAnnouncement(FieldSetEntity fse) throws BaseException {
        fse.setValue("publish_status", 1);
        int user_id = SpringMVCContextHolder.getCurrentUser().getUser_id();
        String user_name = SpringMVCContextHolder.getCurrentUser().getUser_name();
        fse.setValue(CmnConst.CREATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());
        fse.setValue(CmnConst.CREATED_UTC_DATETIME, new Date());
        String[] extentArr = fse.getString("extent").split(",");
        DataTableEntity dt = new DataTableEntity();
        for (int i = 0; i < extentArr.length; i++) {
            FieldSetEntity fs = new FieldSetEntity();
            FieldMetaEntity metaEntity = new FieldMetaEntity();
            metaEntity.setTableName(new Object[]{CmnConst.product_oa_ANNOUNCEMENT_ROLE});
            fs.setMeta(metaEntity);
            fs.setValue("user_id", extentArr[i]);
            fs.setValue("status", 0);
            fs.setValue("announcement_uuid", fse.getString("uuid"));
            dt.addFieldSetEntity(fs);
        }
        fse.addSubDataTable(dt);
        String uuid = "";
        if (StringUtils.isEmpty(fse.getUUID())) {
            uuid = baseDao.add(fse);
        } else {
            boolean update = baseDao.update(fse);
            if (update) {
                uuid = fse.getUUID();
            }
        }
        WebsocketMesssageServiceThread.getInstance().appendMessage(fse.getString("extent"), "[公告]" + user_name + "发布了《" + fse.getString("title") + "》公告", "[公告]" + user_name + "发布了《" + fse.getString("title") + "》公告",
                user_id, "1", COOPERATE_URL + "?" + CoreConst.UUID + "=" + uuid + "&news=1", CmnConst.product_oa_ANNOUNCEMENT, fse.getUUID(), user_id, 0, 0);
 
        return !StringUtils.isEmpty(uuid);
    }
 
    public boolean remindAnnouncement(FieldSetEntity fs) throws BaseException {
        FieldSetEntity fse = baseDao.getFieldSetEntity(CmnConst.product_oa_ANNOUNCEMENT, fs.getUUID(), false);
        String user_name = SpringMVCContextHolder.getCurrentUser().getUser_name();
        int user_id = SpringMVCContextHolder.getCurrentUser().getUser_id();
        WebsocketMesssageServiceThread.getInstance().appendMessage(fs.getString("extent"), "[公告]" + user_name + "提醒你查看公告《" + fse.getString("title") + "》", "[公告]" + user_name + "提醒你查看公告《" + fse.getString("title") + "》",
                user_id, "1", COOPERATE_URL + "?" + CoreConst.UUID + "=" + fs.getUUID() + "&news=1", CmnConst.product_oa_ANNOUNCEMENT, fse.getUUID(), user_id, 0, 0);
 
        return !StringUtils.isEmpty(fs.getUUID());
    }
 
    public boolean updateAnnouncement(FieldSetEntity fse) throws BaseException {
        fse.setValue(CmnConst.UPDATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());
        fse.setValue(CmnConst.UPDATED_UTC_DATETIME, new Date());
        return baseDao.update(fse);
    }
 
    public FieldSetEntity getAnnouncement(FieldSetEntity fse) throws BaseException {
        return baseDao.getFieldSetEntity(CmnConst.product_oa_ANNOUNCEMENT, fse.getString("uuid"), false);
    }
 
    public DataTableEntity listAnnouncement(FieldSetEntity fse) throws BaseException {
        String queryFilter = "";
        if (!BaseUtil.dataTableIsEmpty(fse.getSubDataTable("systemSeniorQueryString"))) {
            queryFilter = " and " + queryFilterService.getQueryFilter(fse);
        }
        DataTableEntity dt = baseDao.listTable(CmnConst.product_oa_ANNOUNCEMENT, "publish_status=0 " + queryFilter, new Object[]{},
                new Object[]{}, " created_utc_datetime desc ", fse.getInteger("pagesize"), fse.getInteger("cpage"));
        baseDao.loadPromptData(dt);
        return dt;
    }
 
    public DataTableEntity listAnnouncementRelease(FieldSetEntity fse) throws BaseException {
        String queryFilter = "";
        if (!BaseUtil.dataTableIsEmpty(fse.getSubDataTable("systemSeniorQueryString"))) {
            queryFilter = " and " + queryFilterService.getQueryFilter(fse);
        }
        DataTableEntity dt = baseDao.listTable(CmnConst.product_oa_ANNOUNCEMENT, "publish_status=1" + queryFilter, new Object[]{},
                new Object[]{}, " created_utc_datetime desc ", fse.getInteger("pagesize"), fse.getInteger("cpage"));
        baseDao.loadPromptData(dt);
        return dt;
    }
 
    public DataTableEntity listAnnouncementReceive(FieldSetEntity fse) throws BaseException {
        String queryFilter = "";
        if (!BaseUtil.dataTableIsEmpty(fse.getSubDataTable("systemSeniorQueryString"))) {
            queryFilter = " and " + queryFilterService.getQueryFilter(fse);
        }
        int user_id = SpringMVCContextHolder.getCurrentUser().getUser_id();
        String id = "," + user_id + ",";
        String type = "";
        if (!StringUtils.isEmpty(fse.getString("type"))) {
            type = " and type='" + fse.getString("type") + "'";
        }
        DataTableEntity dt = baseDao.listTable(CmnConst.product_oa_ANNOUNCEMENT,
                "publish_status=1 and concat(',',extent,',') like '%" + id + "%'" + type + queryFilter, new Object[]{},
                new Object[]{}, CmnConst.CREATED_UTC_DATETIME + " desc", fse.getInteger("pagesize"), fse.getInteger("cpage"));
        baseDao.loadPromptData(dt);
 
        return dt;
    }
 
    public JSONArray treeAnnouncementType(FieldSetEntity fse) throws BaseException {
 
        //封装公告类别树结构
        DataTableEntity dictDt = baseDao.listTable(CmnConst.PRODUCT_SYS_DICT, "dict_name='" + CmnConst.ANNOUNCEMENT_TYPE + "'");
        JSONArray menus = new JSONArray();
        JSONObject parent_menu = new JSONObject();
        parent_menu.put("dict_label", "公告类别");
        parent_menu.put("dict_value", "0");
        JSONArray sub = new JSONArray();
        for (int i = 0; i < dictDt.getRows(); i++) {
            FieldSetEntity fs = dictDt.getFieldSetEntity(i);
            JSONObject submen = BaseUtil.fieldSetEntityToJson(fs);
            sub.add(submen);
        }
        parent_menu.put("children", sub);
        menus.add(parent_menu);
        return menus;
    }
//    public DataTableEntity announcementRoleList(FieldSetEntity fse) throws BaseException {
//        int user_id = SpringMVCContextHolder.getCurrentUser().getUser_id();
//        String id = "," + user_id + ",";
//        DataTableEntity dt = baseDao.listTable(CmnConst.product_oa_ANNOUNCEMENT,
//                "WHERE concat(',',extent,',') like '%" + id + "%'");
//        baseDao.loadPromptData(dt);
//        return dt;
//    }
 
    public boolean delAnnouncement(FieldSetEntity fse) throws BaseException {
        String[] uuid = fse.getString("uuid").split(",");
        return baseDao.delete(CmnConst.product_oa_ANNOUNCEMENT, uuid);
    }
 
    public FieldSetEntity findAnnouncement(FieldSetEntity fse) throws BaseException {
//        FieldSetEntity fs = baseDao.getFieldSetEntity(CmnConst.product_oa_ANNOUNCEMENT, fse.getString("uuid"), false);
        String sqlfs = "SELECT *,(SELECT user_name FROM " + CmnConst.PRODUCT_SYS_USERS + " WHERE user_id=a.created_by)created_by_save,(SELECT user_name FROM " + CmnConst.PRODUCT_SYS_USERS + " WHERE user_id=a.updated_by)updated_by_save from " + CmnConst.product_oa_ANNOUNCEMENT + " a WHERE uuid=?";
        FieldSetEntity fs = baseDao.getFieldSetEntityBySQL(sqlfs, new String[]{fse.getString("uuid")}, false);
        String sql = "SELECT b.user_id user_id,b.show_name show_name,c.org_level_name,a.status FROM " + CmnConst.product_oa_ANNOUNCEMENT_ROLE + " a LEFT JOIN product_sys_staffs b on a.user_id=b.user_id LEFT JOIN product_sys_org_levels c on b.dept_uuid=c.uuid WHERE a.announcement_uuid=?";
        DataTableEntity dt = baseDao.listTable(sql, new String[]{fs.getUUID()});
        fs.addSubDataTable(dt);
        fs.setValue("number", dt.getRows());
        int a = 0;
        for (int i = 0; i < dt.getRows(); i++) {
            if (dt.getInt(i, "status") == 1) {
                a++;
            }
        }
        fs.setValue("readNum", a);
        return fs;
    }
 
    public boolean readAnnouncementRemind(FieldSetEntity fs) throws BaseException {
        FieldSetEntity fse = baseDao.getFieldSetEntity(CmnConst.product_oa_ANNOUNCEMENT, fs.getUUID(), false);
        String expiration_date = fse.getString("expiration_date");
 
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd");
        String current_time = tempDate.format(new Date());
        int compareTo = 0;
        if (expiration_date != null) {
            compareTo = current_time.compareTo(expiration_date);
        }
        String user_name = SpringMVCContextHolder.getCurrentUser().getUser_name();
        int user_id = SpringMVCContextHolder.getCurrentUser().getUser_id();
        if (compareTo <= 0 && fse.getInteger("status") == 1) {
            WebsocketMesssageServiceThread.getInstance().appendMessage(fse.getString("created_by"), "[提醒]" + user_name + "已读了《" + fse.getString("title") + "》公告", "[提醒]" + user_name + "已读了《" + fse.getString("title") + "》公告",
                    user_id, "1", null, null, null, user_id, 0, 0);
        }
        return baseDao.executeUpdate("update product_oa_announcement_role set status=1 where user_id=? and announcement_uuid=?",
                new String[]{"" + user_id, fse.getString("uuid")});
 
    }
 
    public DataTableEntity listAnnouncementGateway(FieldSetEntity fs) throws BaseException {
        //当前人uuid
        int user_id = SpringMVCContextHolder.getCurrentUser().getUser_id();
        String id = "," + user_id + ",";
        DataTableEntity dt = baseDao.listTable(CmnConst.product_oa_ANNOUNCEMENT,
                "publish_status=1 and concat(',',extent,',') like '%" + id + "%'", new Object[]{},
                new Object[]{},  CmnConst.CREATED_UTC_DATETIME + " desc", 5, 1);
        baseDao.loadPromptData(dt);
//        DataTableEntity dt = baseDao.listTable(CmnConst.product_oa_ANNOUNCEMENT, "publish_status=1 and created_by=?", new Object[]{user_id}, new Object[]{}, CmnConst.CREATED_UTC_DATETIME + " desc", 5, 1);
//        baseDao.loadPromptData(dt);
        return dt;
    }
//    public static String emojiConvert1(String str)
//            throws UnsupportedEncodingException {
//            String patternString = "([\\x{10000}-\\x{10ffff}\ud800-\udfff])";
//            Pattern pattern = Pattern.compile(patternString);
//            Matcher matcher = pattern.matcher(str);
//            StringBuffer sb = new StringBuffer();
//            while(matcher.find()) {
//            try {
//            matcher.appendReplacement(
//            sb,
//            "[["
//            + URLEncoder.encode(matcher.group(1),
//            "UTF-8") + "]]");
//            } catch(UnsupportedEncodingException e) {
//            throw e;
//            }
//            }
//            matcher.appendTail(sb);
//            return sb.toString();
//            }
}