张磊磊
2023-11-08 2b2d292c1a274aa95aab8c2ac20530e54a503923
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
package com.thhy.materials.modules.biz.concret.service.impl;
 
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.thhy.general.common.BasicResult;
import com.thhy.general.config.SysUserInfo;
import com.thhy.general.utils.UserInfoUtils;
import com.thhy.materials.modules.biz.concret.entity.SysSteel;
import com.thhy.materials.modules.biz.concret.entity.TSteelConsumption;
import com.thhy.materials.modules.biz.concret.entity.TSteelNeed;
import com.thhy.materials.modules.biz.concret.entity.dto.RecordDto;
import com.thhy.materials.modules.biz.concret.mapper.SysSteelMapper;
import com.thhy.materials.modules.biz.concret.mapper.TSteelConsumptionMapper;
import com.thhy.materials.modules.biz.concret.service.TSteelConsumptionService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.bouncycastle.asn1.eac.EACTags;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Service
public class TSteelConsumptionServiceImpl implements TSteelConsumptionService {
 
    @Resource
    private TSteelConsumptionMapper consumptionMapper;
    @Resource
    private SysSteelMapper steelMapper;
 
    @Override
    @Transactional
    public BasicResult consumptionInsert(Map<String, Object> values) {
 
        String consumptionId = consumptionMapper.allByOne(values);
        if(!StringUtil.isEmpty(consumptionId)){
            return BasicResult.faild("11111","类型重复","error");
        }
        values.put("consumptionId","");
        consumptionMapper.consumptionInsert(values);
        List<Map<String,Object>> needList = (List<Map<String, Object>>) values.get("needList");
        for (Map<String,Object> objectMap : needList){
            objectMap.put("consumptionId",values.get("consumptionId"));
            consumptionMapper.steelNeedInsert(objectMap);
        }
        return BasicResult.success("添加成功");
    }
 
    @Override
    public BasicResult consumptionList(HashMap<String, Object> values) {
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String companyId = sysUserInfo.getCompanyId();
        values.put("companyId",companyId);
        Integer pageSize = Integer.valueOf(values.get("pageSize").toString());
        Integer pageNum = Integer.valueOf(values.get("pageNum").toString());
        PageHelper.startPage(pageNum,pageSize);
        List<TSteelConsumption> consumptions = consumptionMapper.consumptionList(values);
        if(consumptions.size()>0){
            for (TSteelConsumption consumption :consumptions){
                List<TSteelNeed> steelNeeds = consumptionMapper.steelNeedList(consumption.getConsumptionId());
                System.out.println(steelNeeds);
                if(steelNeeds!=null && steelNeeds.size()>0){
                    for (TSteelNeed steelNeed : steelNeeds ){
                        // Double i = (Double.valueOf(steelNeed.getNeedNum().toString()) * Double.valueOf(steelNeed.getCoefficient().toString()));
                        BigDecimal b1 = new BigDecimal(Double.toString(Double.valueOf(steelNeed.getNeedNum().toString())));
                        BigDecimal b2 = new BigDecimal(Double.toString(Double.valueOf(steelNeed.getCoefficient().toString())));
                        Double v = b1.multiply(b2).doubleValue();
                        System.out.println(b1+"*"+b2+"="+v);
                        //转化为 4位
                        DecimalFormat decimalFormat = new DecimalFormat("#.####");
                        String format = decimalFormat.format(v);
                        steelNeed.setTotals(format);
                    }
                }
                consumption.setTSteelNeeds(steelNeeds);
            }
        }
        PageInfo<TSteelConsumption> tSteelConsumptionPageInfo = new PageInfo<>(consumptions);
        return BasicResult.success(tSteelConsumptionPageInfo);
    }
 
    @Override
    @Transactional
    public BasicResult consumptionDel(String consumptionId) {
        consumptionMapper.consumptionDel(consumptionId);
        consumptionMapper.consumptionNeedDel(consumptionId);
        return BasicResult.success("删除成功");
    }
 
    @Override
    public BasicResult consumptionInfo(String consumptionId) {
        TSteelConsumption consumption = consumptionMapper.consumptionInfo(consumptionId);
        List<TSteelNeed> steelNeeds = consumptionMapper.steelNeedList(consumption.getConsumptionId());
        consumption.setTSteelNeeds(steelNeeds);
        return BasicResult.success(consumption);
    }
 
    @Override
    @Transactional
    public BasicResult consumptionUpdate(Map<String, Object> values) {
        String consumptionId = values.get("consumptionId").toString();
        String consumptionId1 = consumptionMapper.allByOne(values);
        if(!StringUtil.isEmpty(consumptionId1)&&!consumptionId.equals(consumptionId1)){
            return BasicResult.faild("11111","类型重复","error");
        }
        consumptionMapper.consumptionUpdate(values);
        consumptionMapper.consumptionNeedDel(consumptionId);
        List<Map<String,Object>> needList = (List<Map<String, Object>>) values.get("needList");
        for (Map<String,Object> objectMap : needList){
            objectMap.put("consumptionId",values.get("consumptionId"));
            consumptionMapper.steelNeedInsert(objectMap);
        }
        return BasicResult.success("修改成功");
    }
 
    @Override
    public BasicResult consumptionExcel(HashMap<String, Object> values, HttpServletResponse response) {
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String companyId = sysUserInfo.getCompanyId();
        values.put("companyId",companyId);
 
        List<TSteelConsumption> consumptions = consumptionMapper.consumptionList(values);
        for (TSteelConsumption consumption :consumptions){
            List<TSteelNeed> steelNeeds = consumptionMapper.steelNeedList(consumption.getConsumptionId());
            for (TSteelNeed steelNeed : steelNeeds ){
               // Double i = (Double.valueOf(steelNeed.getNeedNum().toString()) * Double.valueOf(steelNeed.getCoefficient().toString()));
                BigDecimal b1 = new BigDecimal(Double.toString(Double.valueOf(steelNeed.getNeedNum().toString())));
                BigDecimal b2 = new BigDecimal(Double.toString(Double.valueOf(steelNeed.getCoefficient().toString())));
                Double v = b1.multiply(b2).doubleValue();
                steelNeed.setTotals(v.toString());
            }
            consumption.setTSteelNeeds(steelNeeds);
        }
        //List<RecordDto> recordDtos = new ArrayList<>();
        XSSFWorkbook book = new XSSFWorkbook();
        XSSFSheet sheet = book.createSheet();
        XSSFRow row1 = sheet.createRow(0);
        row1.createCell(0).setCellValue("项目名称");
        row1.createCell(1).setCellValue("尺寸");
        row1.createCell(2).setCellValue("配筋");
        row1.createCell(3).setCellValue("块号");
        List<SysSteel> steels = steelMapper.steelPull(sysUserInfo.getCompanyId());
        int leg=4;
//        int rowSizes=1;
//        int legs=4;
        for (int i =0 ;i<steels.size();i++){
            row1.createCell(leg).setCellValue(steels.get(i).getSteelName()+""+steels.get(i).getSteelModel()+"("+steels.get(i).getUnit()+")");
            leg++;
//            for (TSteelConsumption s : consumptions){
//                XSSFRow row2 = sheet.createRow(rowSizes);
//                //钢筋id
//                String steelId = steels.get(i).getSteelId();
//                for (TSteelNeed need : s.getTSteelNeeds()){
//                    if (need.getSteelId().equals(steelId)){
//                        System.out.println("-----"+need.getNeedNum()+"类型"+steels.get(i).getSteelName());
//                        row2.createCell(legs).setCellValue(need.getNeedNum());
//                    }
//                }
//                legs++;
//            }
        }
 
        int rowSize=1;
        for (Integer i =0;i<consumptions.size();i++){
            XSSFRow row2 = sheet.createRow(rowSize);
            row2.createCell(0).setCellValue(consumptions.get(i).getProName());
            row2.createCell(1).setCellValue(consumptions.get(i).getSizeName());
            row2.createCell(2).setCellValue(consumptions.get(i).getReinforcementName());
            row2.createCell(3).setCellValue(consumptions.get(i).getBlockName());
            List<TSteelNeed> tSteelNeeds = consumptions.get(i).getTSteelNeeds();
 
            for (int is =0 ;is<steels.size();is++){
                Integer legs =4;
                for (int iss =0 ;iss<tSteelNeeds.size();iss++){
                    if (tSteelNeeds.get(iss).getSteelId().equals(steels.get(iss).getSteelId())){
                        row2.createCell(legs).setCellValue(tSteelNeeds.get(iss).getTotals());
                        legs++;
                    }else {
                        //row2.createCell(legs).setCellValue(0);
                        legs++;
                    }
                }
            }
            rowSize++;
        }
 
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=test.xls");
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            ;
            book.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //IOUtils.closeQuietly(out);
        }
 
        return null;
    }
}