futian.liu
2023-12-02 8e41786892a4bd7cff2d63bde8cb0636cdb0650c
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
package com.consum.base.controller;
 
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.hutool.core.util.ReflectUtil;
import com.consum.base.BaseController;
import com.consum.base.core.utils.CommonUtil;
import com.consum.base.core.utils.MapUtils;
import com.consum.base.core.utils.MapperUtil;
import com.consum.base.core.utils.PageUtil;
import com.consum.base.pojo.LWhFormOutputParam;
import com.consum.base.pojo.excel.OutputExcelTemplate;
import com.consum.base.pojo.query.LWhFormOutputQry;
import com.consum.base.pojo.response.FormOutputGoodsVO;
import com.consum.base.pojo.response.FormOutputTemplateInfoVO;
import com.consum.base.pojo.response.FormOutputVO;
import com.consum.base.pojo.response.GoodsModelVO;
import com.consum.base.pojo.response.LWhFormOutputExtendVO;
import com.consum.base.service.LWarehouseFlowService;
import com.consum.base.service.LWhFormOutputServiceImpl;
import com.consum.base.service.LWhProcureModelService;
import com.consum.model.po.FinSysTenantUser;
import com.consum.model.po.LWhFormOutput;
import com.iplatform.model.po.S_user_core;
import com.walker.db.page.GenericPager;
import com.walker.infrastructure.utils.CollectionUtils;
import com.walker.infrastructure.utils.DateUtils;
import com.walker.web.ResponseValue;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.compress.utils.Lists;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * @Description 出库单
 * @Author 卢庆阳
 * @Date 2023/10/27
 */
@Api(value = "出库单", tags = "出库单")
@RestController
@RequestMapping("/pc/l/wh/form/output")
public class LWhFormOutputController extends BaseController {
 
    @Autowired
    private LWhFormOutputServiceImpl lWhFormOutputService;
    @Autowired
    private LWhProcureModelService lWhProcureModelService;
    @Resource
    private LWarehouseFlowService lWarehouseFlowService;
 
    /**
     * @Description 新增出库单
     * @Author 卢庆阳
     * @Date 2023/10/27
     */
    @ApiOperation(value = "新增出库单", notes = "新增出库单")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "param", value = "出库单实体", dataType = "lWhFormOutputParam", dataTypeClass = LWhFormOutputParam.class, paramType = "body")
    })
    @PostMapping("/add")
    public ResponseValue add() {
        LWhFormOutputParam param = CommonUtil.getObjFromReqBody(LWhFormOutputParam.class);
        S_user_core currentUser = this.getCurrentUser();
        if (currentUser == null) {
            return ResponseValue.error("登录用户信息不存在");
        }
        int result = this.lWhFormOutputService.add(param, currentUser, this.getSysInfo());
        if (result > 0) {
            return ResponseValue.success(1);
        }
        return ResponseValue.error("新增失败!");
    }
 
    /**
     * @Description 列表查询
     */
    @ApiOperation(value = "列表查询", notes = "列表查询")
    @ApiImplicitParam(name = "param", value = "出库单实体", required = true, dataType = "LWhFormOutputParam", paramType = "query")
    @GetMapping("/list")
    public ResponseValue queryFormOutputList() {
        LWhFormOutputQry param = CommonUtil.getObjFromReq(LWhFormOutputQry.class);
        S_user_core currentUser = this.getCurrentUser();
        if (currentUser == null) {
            return ResponseValue.error("登录用户信息不存在");
        }
        /*当前登录人只能看到自己机构下的列表*/
        FinSysTenantUser sysInfo = this.getSysInfo();
        String tenantId = sysInfo.getTenantId();
        if (param.getAgencyId() == null) {
            param.setAgencyId(Long.valueOf(tenantId));
        }
        GenericPager genericPager = lWhFormOutputService.queryFormOutputList(param);
        List<LWhFormOutput> data = genericPager.getDatas();
 
        List<FormOutputVO> resultList = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(data)) {
            data.forEach(item -> {
                // 查询型号数量
                FormOutputVO formOutputVO = new FormOutputVO();
                BeanUtils.copyProperties(item, formOutputVO);
 
                List<FormOutputTemplateInfoVO> procureTemplateInfoList = Lists.newArrayList();
 
                // 查询型号数量
                String sql = "SELECT bgt.id,bgm.GOODS_TEMPLATES_ID,GOODS_NAME,sum( counts ) count FROM "
                    + "l_wh_procure_model pm LEFT JOIN base_goods_models bgm ON pm.BASE_GOODS_MODELS_ID = bgm.id "
                    + "LEFT JOIN base_goods_template bgt ON bgm.GOODS_TEMPLATES_ID = bgt.id "
                    + "WHERE pm.BUSINESS_ID =:id GROUP BY bgm.GOODS_TEMPLATES_ID";
                Map<String, Object> paramMap = new HashMap<>();
                paramMap.put("id", item.getId());
                List<Map<String, Object>> procureModelList = lWhProcureModelService.select(sql, paramMap, new MapperUtil());
                for (Map<String, Object> map : procureModelList) {
                    FormOutputTemplateInfoVO procureTemplateInfoVO = MapUtils.convertMapToObj(map, FormOutputTemplateInfoVO.class);
                    procureTemplateInfoList.add(procureTemplateInfoVO);
                }
                formOutputVO.setFormOutputTemplateInfoList(procureTemplateInfoList);
 
                resultList.add(formOutputVO);
            });
        }
        try {
            Field fieldDatas = GenericPager.class.getDeclaredField("datas");
//            fieldDatas.setAccessible(true);
//            fieldDatas.set(genericPager, resultList);
            ReflectUtil.setFieldValue(genericPager, fieldDatas, resultList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseValue.success(genericPager);
    }
 
    /**
     * 根据id查询详情
     */
    @ApiOperation(value = "根据id查询详情", notes = "根据id查询详情")
    @ApiImplicitParam(name = "id", value = "出库单id", required = true, dataType = "Long", paramType = "query")
    @GetMapping("/detail")
    public ResponseValue getById(Long id) throws IllegalAccessException {
        if (id == null) {
            return ResponseValue.error("出库单id为空");
        }
        LWhFormOutput lWhFormOutput = lWhFormOutputService.get(new LWhFormOutput(id));
        LWhFormOutputExtendVO lWhFormOutputExtendVO = new LWhFormOutputExtendVO();
        BeanUtils.copyProperties(lWhFormOutput, lWhFormOutputExtendVO);
        lWhFormOutputExtendVO.setDoc(lWhFormOutput.getOutputDoc());
 
        String sql = "SELECT bgt.id, CATEGORY_ID, CATEGORY_NAME, GOODS_NAME "
            + "FROM l_wh_procure_model pm LEFT JOIN base_goods_models bgm ON bgm.id = pm.BASE_GOODS_MODELS_ID "
            + "LEFT JOIN base_goods_template bgt ON bgt.id = bgm.GOODS_TEMPLATES_ID "
            + "WHERE pm.BUSINESS_ID =:id GROUP BY bgt.id ";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", lWhFormOutputExtendVO.getId());
        List<Map<String, Object>> procureModelList = lWhFormOutputService.select(sql, paramMap, new MapperUtil());
        List<FormOutputGoodsVO> fromOutputGoods = Lists.newArrayList();
        for (Map<String, Object> map : procureModelList) {
            FormOutputGoodsVO procureTemplateInfoVO = MapUtils.convertMapToObj(map, FormOutputGoodsVO.class);
 
            // 查询型号数量
            List<GoodsModelVO> goodsModelVOList = Lists.newArrayList();
            String sql2 = "SELECT pm.id,pm.BASE_GOODS_MODELS_NAME,bgm.UNIT,COUNTS,total_amount "
                + "FROM l_wh_procure_model pm LEFT JOIN base_goods_models bgm ON bgm.id = pm.BASE_GOODS_MODELS_ID "
                + "LEFT JOIN base_goods_template bgt ON bgt.id = bgm.GOODS_TEMPLATES_ID "
                + "WHERE pm.BUSINESS_ID =:id";
            List<Map<String, Object>> modelList = lWhFormOutputService.select(sql2, paramMap, new MapperUtil());
            modelList.forEach(item -> {
                GoodsModelVO goodsModelVO = MapUtils.convertMapToObj(item, GoodsModelVO.class);
                goodsModelVOList.add(goodsModelVO);
            });
            procureTemplateInfoVO.setModels(goodsModelVOList);
            fromOutputGoods.add(procureTemplateInfoVO);
        }
        lWhFormOutputExtendVO.setFromOutputGoods(fromOutputGoods);
 
        return ResponseValue.success("查询成功!", lWhFormOutputExtendVO);
    }
 
 
    @ApiOperation(value = "查询出库单详情明细", notes = "查询出库单详情明细")
    @ApiImplicitParam(name = "formOutputQry", value = "出库单详情查询条件", required = true, dataType = "LWhFormOutputQry", paramType = "query")
    @GetMapping("/detail/list")
    public ResponseValue queryFormOutputDetailList() {
        LWhFormOutputQry formOutputQry = CommonUtil.getObjFromReq(LWhFormOutputQry.class);
        S_user_core currentUser = this.getCurrentUser();
        if (currentUser == null) {
            return ResponseValue.error("登录用户信息不存在");
        }
        PageUtil genericPager = lWarehouseFlowService.queryAllBusinessFlow(formOutputQry);
        return ResponseValue.success(genericPager);
    }
 
 
    @ApiOperation(value = "出库单导出", notes = "出库单导出")
    @ApiImplicitParam(name = "id", value = "出库单id", required = true, dataType = "Long", paramType = "query")
    @GetMapping("/list/export")
    public void export(Long id, HttpServletResponse response) throws Exception {
        FinSysTenantUser sysInfo = this.getSysInfo();
        if (sysInfo == null) {
            throw new RuntimeException("登录用户信息不存在");
        }
        ClassPathResource classPathResource = new ClassPathResource("import/出库单.xls");
        // 获取文件输入流
        InputStream inputStream = classPathResource.getInputStream();
        Workbook wb = new HSSFWorkbook(inputStream);
        TemplateExportParams params = new TemplateExportParams();
        params.setTemplateWb(wb);
        params.setHeadingStartRow(2);
 
        List<OutputExcelTemplate> exportList = lWhFormOutputService.getExportList(id);
 
        if (CollectionUtils.isEmpty(exportList)) {
            throw new RuntimeException("数据为空");
        }
        int countNum = exportList.stream().filter(item -> item.getNum() != null).mapToInt(OutputExcelTemplate::getNum).sum();
        int totalAmount = exportList.stream().filter(item -> item.getTotalAmount() != null).mapToInt(OutputExcelTemplate::getTotalAmount).sum();
        Optional<OutputExcelTemplate> first = exportList.stream().findFirst();
        OutputExcelTemplate templateExcelExport = first.get();
        String businessFormCode = templateExcelExport.getBusinessFormCode();
        Long createTime = templateExcelExport.getCreateTime();
        String operatorName = templateExcelExport.getOperatorName();
 
        Map<String, Object> map = new HashMap<>();
        map.put("code", businessFormCode);
        map.put("date", DateUtils.toShowDate(createTime));
        map.put("name", operatorName);
        map.put("countNum", countNum);
        map.put("totalAmount", totalAmount);
 
        Workbook workbook = ExcelExportUtil.exportExcel(params, OutputExcelTemplate.class, exportList, map);
        downLoadExcel("出库单", response, workbook);
 
    }
 
 
}