songhuangfeng123
2022-09-01 b854d8d7604329dcf99584d15f83ff02076d9e07
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package com.gkhy.safePlatform.targetDuty.controller;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gkhy.safePlatform.commons.co.ContextCacheUser;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.query.PageQuery;
import com.gkhy.safePlatform.commons.utils.BeanCopyUtils;
import com.gkhy.safePlatform.commons.utils.PageUtils;
import com.gkhy.safePlatform.commons.vo.ResultVO;
import com.gkhy.safePlatform.targetDuty.entity.TargetDivideDetail;
import com.gkhy.safePlatform.targetDuty.entity.TargetMng;
import com.gkhy.safePlatform.targetDuty.model.dto.req.TargetCheckAndSubmitQueryCriteria;
import com.gkhy.safePlatform.targetDuty.model.dto.req.TargetMngImportExcel;
import com.gkhy.safePlatform.targetDuty.model.dto.req.TargetMngQueryCriteria;
import com.gkhy.safePlatform.targetDuty.model.dto.resp.TargetMngExcel;
import com.gkhy.safePlatform.targetDuty.service.TargetDivideDetailService;
import com.gkhy.safePlatform.targetDuty.service.TargetMngService;
import com.gkhy.safePlatform.targetDuty.utils.DateUtils;
import com.gkhy.safePlatform.targetDuty.utils.poihelper.ExcelLogs;
import com.gkhy.safePlatform.targetDuty.utils.poihelper.ExcelUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.util.*;
 
/**
 * 目标指标(TargetMng)表控制层
 *
 * @author xurui
 * @since 2022-07-20 11:49:22
 */
@RestController
@RequestMapping("targetMng")
public class TargetMngController {
    /**
     * 服务对象
     */
    @Resource
    private TargetMngService targetMngService;
    @Resource
    private TargetDivideDetailService targetDivideDetailService;
    @Autowired
    public HttpServletRequest request;
 
    @Autowired
    public HttpServletResponse response;
 
 
    /**
     * 分页查询所有数据
     *
     * @param pageQuery 查询实体
     * @return 所有数据
     */
    @PostMapping(value = "/page/list")
    public ResultVO selectAll(@RequestBody PageQuery<TargetMngQueryCriteria> pageQuery){
        if(pageQuery.getSearchParams().getTargetType() == null){
            return new ResultVO<>(ResultCodes.CLIENT_PARAM_ILLEGAL,"缺少targetType");
        }
        PageUtils.checkCheck(pageQuery);
        return this.targetMngService.queryAll(pageQuery);
    }
    
 
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping(value = "/selectOne/{id}")
    public ResultVO selectOne(@PathVariable Serializable id) {
        return new ResultVO<>(ResultCodes.OK,this.targetMngService.selectOne(id));
    }
 
    /**
     * 新增或者修改数据
     *
     * @param targetMng 实体对象
     * @return 修改结果
     */
    @PostMapping(value = "/addOrUpdate")
    public ResultVO update(@RequestBody TargetMng targetMng) {
 
        if( !StringUtils.hasText(targetMng.getqName()) ||  !StringUtils.hasText(targetMng.getIndexNum())
                ||  !StringUtils.hasText(targetMng.getYear()) ||  !StringUtils.hasText(targetMng.getValue())
                ||  targetMng.getTargetType() == null){
            return new ResultVO<>(ResultCodes.CLIENT_PARAM_ILLEGAL,"缺少必填字段");
        }
        if (targetMng.getId() == null) {
            return new ResultVO<>(ResultCodes.OK,targetMngService.save(targetMng));
        } else {
            targetMngService.update(targetMng,new UpdateWrapper<TargetMng>().eq("id",targetMng.getId()));
            return new ResultVO<>(ResultCodes.OK);
        }
    }
 
    /**
     * 删除数据
     *
     * @param ids 主键结合
     * @return 删除结果
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public ResultVO delete(@RequestBody Long[] ids) {
        if(ids == null){
            return new ResultVO<>(ResultCodes.CLIENT_PARAM_ILLEGAL);
        }
        List<Long> idList = Arrays.asList(ids);
        //删除关联表数据
        UpdateWrapper<TargetDivideDetail> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("target_id",idList);
        TargetDivideDetail detail = new TargetDivideDetail();
        detail.setDelFlag(1);
        this.targetDivideDetailService.update(detail,updateWrapper);
 
        List<TargetMng> delList = new ArrayList<>();
        idList.forEach(f->{
            TargetMng info = new TargetMng();
            info.setDelFlag(1);
            info.setId(f);
            delList.add(info);
        });
        this.targetMngService.updateBatchById(delList);
        return new ResultVO<>(ResultCodes.OK);
    }
 
    /**
     * 下载模板
     *
     */
    @GetMapping(value = "/exportTemplate")
    public void exportTemplate() throws IOException {
        Map<String,String> map = new LinkedHashMap<>();
        map.put("1","安全目标指标");
        map.put("2","目标指标编号");
        map.put("3","指标类型 1:年指标 2:月指标 3:半年 4:季度");
        map.put("4","年度");
        map.put("5","指标值");
        map.put("6","指标级别 1:公司级 2:部门分厂级 3:工段班组级");
        map.put("7","完成期限(yyyy-MM-dd HH:mm:ss)");
        map.put("8","备注信息");
 
        String fileName = URLEncoder.encode("目标设置数据导入模板.xls", "UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
 
        ExcelUtil.exportExcel(map,new ArrayList<>() , response.getOutputStream());
        response.getOutputStream().close();
    }
 
    /**
     * 导出一览数据
     *
     */
    @GetMapping(value = "/exportData")
    public void exportData(TargetMngQueryCriteria queryCriteria) throws IOException {
        Map<String,String> map = new LinkedHashMap<>();
        map.put("1","安全目标指标");
        map.put("2","目标指标编号");
        map.put("3","年度");
        map.put("4","指标值");
        map.put("5","指标级别");
        map.put("6","指标类型");
        map.put("7","完成期限");
        map.put("8","状态");
        map.put("9","备注信息");
 
        String key = DateUtils.date2String(new Date(), DateUtils.PATTERN_ALLTIME_NOSIGN) ;
        String fileName = URLEncoder.encode("目标设置"+key+".xls", "UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
 
 
        List<TargetMngExcel> respList = BeanCopyUtils.copyBeanList(targetMngService.queryAll(queryCriteria), TargetMngExcel.class);
 
        ExcelUtil.exportExcel(map,respList , response.getOutputStream(),DateUtils.PATTERN_STANDARD);
        response.getOutputStream().close();
    }
 
    /**
     * 导入数据
     *
     */
    @RequestMapping(value = "/importData")
    public ResultVO importData(MultipartFile file) throws IOException {
        String contentType = file.getContentType();
        if(!"application/vnd.ms-excel".equals(contentType)
        && !"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet".equals(contentType)) {
            return new ResultVO<>(ResultCodes.CLIENT_PARAM_ILLEGAL, "上传的excel格式错误");
        }
 
        Collection<TargetMngImportExcel> importExcel = ExcelUtil.importExcel(TargetMngImportExcel.class, file.getInputStream(), "yyyy-MM-dd HH:mm:ss", new ExcelLogs() , 0);
 
        if (CollectionUtils.isEmpty(importExcel)) {
            return new ResultVO<>(ResultCodes.OK);
        }
 
        List<TargetMng> respList = BeanCopyUtils.copyBeanList((List<TargetMngImportExcel>)importExcel, TargetMng.class);
 
        targetMngService.saveBatch(respList);
        return new ResultVO<>(ResultCodes.OK);
    }
 
 
 
    /**
     * 分页查询所有数据 -- 【目标检查上报页面】使用
     *
     * @param pageQuery 查询实体
     * @return 所有数据
     */
    @PostMapping(value = "/checkAndSubimt/list")
    public ResultVO list(Authentication authentication, @RequestBody PageQuery<TargetCheckAndSubmitQueryCriteria> pageQuery){
        if(pageQuery.getSearchParams().getRelateType() == null){
            return new ResultVO<>(ResultCodes.CLIENT_PARAM_ILLEGAL,"缺少targetType");
        }
        PageUtils.checkCheck(pageQuery);
        ContextCacheUser currentUser = (ContextCacheUser) authentication.getPrincipal();
        return this.targetMngService.queryAll(currentUser.getUid(),pageQuery);
    }
 
    /**
     * 统计
     *
     * @return 删除结果
     */
    @GetMapping(value = "/statistics")
    public ResultVO statistics(TargetMngQueryCriteria criteria) {
        return new ResultVO<>(ResultCodes.OK,this.targetMngService.statistics(criteria));
    }
 
 
    public static void main(String[] args) {
        TargetMng mng = new TargetMng();
        mng.setqName("12");
        mng.setIndexNum("3");
        mng.setYear("2021");
        mng.setValue("312");
        mng.setLevel(1);
        mng.setCompleteDate(new Timestamp(new java.util.Date().getTime()));
        mng.setMemo("发发发");
        mng.setTargetType(0);
        mng.setDivideStatus(0);
        System.out.println(JSONObject.toJSONString(mng));
 
    }
}