zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
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
package com.changhong.epc.form.rest.Assets;
 
import com.changhong.epc.bean.admin.AssetDepreciation;
import com.changhong.epc.bean.form.Accrued;
import com.changhong.epc.bean.form.Asset;
import com.changhong.epc.bean.form.QueryParam;
import com.changhong.epc.bean.form.SectoralAssets;
import com.changhong.epc.constter.form.utf.FormUrlConst;
import com.changhong.epc.form.service.assets.AssetService;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
@RestController
public class AssetsRest implements FormUrlConst {
    @Resource(name="assetService")
    private AssetService assetService;
                
    /**
     * 页面初始化查询资产
     * @throws Exception 
     */    
    @RequestMapping(value = SELECT_ASSETS, method={RequestMethod.OPTIONS, RequestMethod.POST})
    public List<SectoralAssets> selectAssets(@RequestBody Map<String,String> data){
        return assetService.getSectoralAsset(data.get("date"));
    }
    
    /**
     * 同过一级部门进行查找异常数据
     * 
     */
    @RequestMapping(value = SELECT_ERRASSETS, method={RequestMethod.OPTIONS, RequestMethod.POST})
    public List<Asset> selectErrAssets(@RequestBody QueryParam queryParam) throws Exception{
        return assetService.getErrAsset(queryParam);
    }
    
    /**
     *通过子部门查询异常资产 
     * 
     */
    @RequestMapping(value = SELECT_ERRASSETSBYONE, method={RequestMethod.OPTIONS, RequestMethod.POST})
    public List<Asset> selectErrAsset(@RequestBody QueryParam queryParam) throws Exception{
        return assetService.getErrAssetOne(queryParam);
    }
    /**
     * 分页
     * 通过条件查询
     * 查询详细
     * 2018-1-18
     */
    @RequestMapping(value = SELECT_ACCRUED, method={RequestMethod.OPTIONS, RequestMethod.POST})
    public List<Accrued> selectAccrued(@RequestBody QueryParam queryParam){
        try {
            return assetService.getAccrueds(queryParam);
        } catch (ParseException e) {
            return null;
        }
    }
 
    /**
     * 保存前验证资产
     * @param assetDepreciation
     * @return
     */
    @PostMapping(VALIDATE_ASSET)
    public Boolean validateAsset(@RequestBody AssetDepreciation assetDepreciation){
        return assetService.validateAsset(assetDepreciation);
    }
    
    //下载exl
    @GetMapping(SELECT_DOEXL)    
    public boolean doExl(String date,String useDepCode,String useDepName,String state,String assetName,String assetCode,HttpServletRequest request, HttpServletResponse response){
        QueryParam qp = new QueryParam();
        if(date != null && (!date.equals("null")) && (!date.equals(""))){
            qp.setDate(date);
        }
        if(state != null && (!state.equals("null")) && (!state.equals(""))){
            qp.setState(state);
        }
        if(assetCode != null && (!assetCode.equals("null")) && (!assetCode.equals(""))){
            qp.setAssetCode(assetCode);
        }
        if(assetName != null && (!assetName.equals("null")) && (!assetName.equals(""))){
            qp.setAssetName(assetName);
        }
        if(useDepCode != null && (!useDepCode.equals("null")) && (!useDepCode.equals(""))){
            qp.setUseDepCode(useDepCode);
        }
        if(useDepName != null && (!useDepName.equals("null")) && (!useDepName.equals(""))){
            qp.setUseDepName(useDepName);
        }        
        List <Accrued> acc = new ArrayList<Accrued>();
        try {
            acc=assetService.getAccrueds(qp);
        } catch (ParseException e1) {
        }
        
        // 第一步,创建一个webbook,对应一个Excel文件  
        HSSFWorkbook workbook = new HSSFWorkbook();  
        // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet 页 
        HSSFSheet sheet = workbook.createSheet(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));  
        // 第三步,在sheet中添加表头第0行  
        HSSFRow row = sheet.createRow(0);  
        // 第四步,创建单元格,并设置值表头 设置表头居中  
        HSSFCellStyle style = workbook.createCellStyle();  
        // 创建一个居中格  
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
 
        row.createCell(0).setCellValue("序号");  
        row.createCell(1).setCellValue("部门");
        row.createCell(2).setCellValue("资产编号");  
        row.createCell(3).setCellValue("资产名称");  
        row.createCell(4).setCellValue("规格型号");
        row.createCell(5).setCellValue("资产化日期");
        row.createCell(6).setCellValue("预计使用年限(月)");
        row.createCell(7).setCellValue("已使用年限(月)");
        row.createCell(8).setCellValue("原值");
        row.createCell(9).setCellValue("折旧方法");
        row.createCell(10).setCellValue("累计折旧");
        row.createCell(11).setCellValue("本月折旧");
        row.createCell(12).setCellValue("月折旧率(%)");
        row.createCell(13).setCellValue("折后净值");      
        
        for(int i = 0;i < 14;i++){
            //设置表头居中  
            row.getCell(i).setCellStyle(style);
            //设置列宽  
            sheet.setColumnWidth(i, 3500); 
        }
        sheet.setColumnWidth(0, 1500);
        // 第五步,写入实体数据  
        for (int i = 0; i < acc.size(); i++) {  
            row = sheet.createRow(i + 1);  
            // 第六步,创建单元格,并设置值  
            if(acc.get(i).getDepreciationMethod().equals("1")){
                acc.get(i).setDepreciationMethod("双倍余额递减法");
            }else if(acc.get(i).getDepreciationMethod().equals("2")){
                acc.get(i).setDepreciationMethod("平均年限法");
            }else if(acc.get(i).getDepreciationMethod().equals("3")){
                acc.get(i).setDepreciationMethod("年限总和法");
            }
            row.createCell(0).setCellValue(i+1);          
            row.createCell(1).setCellValue(acc.get(i).getUseDepartmentName() == null ? "" : acc.get(i).getUseDepartmentName());
            row.createCell(2).setCellValue(acc.get(i).getAssetsCode() == null ? "" : acc.get(i).getAssetsCode());
            row.createCell(3).setCellValue(acc.get(i).getAssetsName() == null ? "" : acc.get(i).getAssetsName());
            row.createCell(4).setCellValue(acc.get(i).getSpecifications() == null ? "" : acc.get(i).getSpecifications());
            row.createCell(5).setCellValue(acc.get(i).getCapitalizationData() == null ? "" : acc.get(i).getCapitalizationData());
            row.createCell(6).setCellValue(acc.get(i).getEstimateTime() == null ? "" : acc.get(i).getEstimateTime());
            row.createCell(7).setCellValue(acc.get(i).getAlreadyTime() == null ? "" : acc.get(i).getAlreadyTime());
            row.createCell(8).setCellValue(acc.get(i).getOriginalMoney() == null ? "" : acc.get(i).getOriginalMoney());
            row.createCell(9).setCellValue(acc.get(i).getDepreciationMethod() == null ? "" : acc.get(i).getDepreciationMethod());
            row.createCell(10).setCellValue(acc.get(i).getCumulativeDepreciation() == null ? 0 : acc.get(i).getCumulativeDepreciation());
            row.createCell(11).setCellValue(acc.get(i).getCurrentDepreciation() == null ? 0 : acc.get(i).getCurrentDepreciation());
            row.createCell(12).setCellValue(acc.get(i).getDepreciationRate() == null ? 0 : acc.get(i).getDepreciationRate());
            row.createCell(13).setCellValue(acc.get(i).getNetMoney() == null ? 0 : acc.get(i).getNetMoney());
        }
        String downFileName = new String("资产折旧信息"+new SimpleDateFormat("yyyy-MM").format(new Date())+".xls");  
        try {  
            //若不进行编码在IE下会乱码  
            downFileName = URLEncoder.encode(downFileName, "UTF-8");  
        } catch (Exception e) {
        }  
        try (OutputStream os = response.getOutputStream()){
            // 清空response  
            response.reset();  
            response.setContentType("application/msexcel");//设置生成的文件类型  
            response.setCharacterEncoding("UTF-8");//设置文件头编码方式和文件名  
            response.setHeader("Content-Disposition", "attachment; filename=" + new String(downFileName.getBytes("utf-8"), "ISO8859-1"));
            workbook.write(os);  
            os.flush();
        } catch (IOException e) {
            // System.err.println(e.getMessage());
            return false;
        }  
        return true;
    }
 
    @PostMapping(ASSET_UPDATESTATUS)
    public int updateAssetsByState(@RequestBody Map<String,String> map){
        return assetService.updateAssetsByState(map);
    }
 
}