futian.liu
2023-12-07 76c7d995ac56b60880e998df5e9e3d82fea313f8
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package com.consum.base.controller;
 
import java.lang.reflect.Field;
import java.util.ArrayList;
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.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
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;
 
import com.consum.base.BaseController;
import com.consum.base.core.utils.CommonUtil;
import com.consum.base.core.utils.PageUtil;
import com.consum.base.pojo.LWhFormProcureGoodsInfoParam;
import com.consum.base.pojo.LWhFormProcureParam;
import com.consum.base.pojo.dto.GoodModelInfoDTO;
import com.consum.base.pojo.excel.ProcureExcelTemplate;
import com.consum.base.pojo.query.FormProcureQry;
import com.consum.base.pojo.response.FormProcureVO;
import com.consum.base.pojo.response.GoodsModelVO;
import com.consum.base.pojo.response.GoodsTemplateCountVO;
import com.consum.base.pojo.response.GoodsTemplateInfoVO;
import com.consum.base.pojo.response.LWhFormProcureExtendVO;
import com.consum.base.service.BaseGoodsTemplateService;
import com.consum.base.service.LWhFormProcureGoodsService;
import com.consum.base.service.LWhFormProcureService;
import com.consum.base.service.LWhProcureModelService;
import com.consum.base.service.impl.LWhFormProcureCoreService;
import com.consum.model.po.FinSysTenantUser;
import com.consum.model.po.LWhFormProcure;
import com.consum.model.po.LWhFormProcureGoods;
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 cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.hutool.core.util.ReflectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
 
/**
 * @ClassName LWhFormProcureController
 * @Date 2023/10/27
 * @Description
 * @Version 1.0
 **/
@Api(value = "仓库表单采购", tags = "仓库表单采购")
@RestController
@RequestMapping("/pc/whForm/procure")
public class LWhFormProcureController extends BaseController {
 
    @Resource
    private LWhFormProcureService lWhFormProcureService;
    @Resource
    private LWhFormProcureGoodsService lWhFormProcureGoodsServiceImpl;
    @Resource
    private LWhFormProcureCoreService lWhFormProcureCoreService;
    @Resource
    private BaseGoodsTemplateService baseGoodsTemplateService;
    @Resource
    private LWhProcureModelService lWhProcureModelService;
 
    /**
     * @Description 新增
     */
    @PostMapping("/add")
    public ResponseValue add() {
        LWhFormProcureParam param = CommonUtil.getObjFromReqBody(LWhFormProcureParam.class);
        LWhFormProcureParam param2 = new LWhFormProcureParam();
        CommonUtil.copyProperties(param, param2);
        param = param2;
        return this.add(param);
    }
 
    private ResponseValue add(LWhFormProcureParam param) {
        if (param.getWarehouseId() == null) {
            return ResponseValue.error("仓库ID不能为空");
        }
 
        List<LWhFormProcureGoodsInfoParam> procureGoods = param.getProcureGoods();
        if (CollectionUtils.isEmpty(procureGoods)) {
            return ResponseValue.error("采购单不能为空");
        }
        FinSysTenantUser sysTenantUser = this.getSysInfo();
        S_user_core currentUser = this.getCurrentUser();
        lWhFormProcureService.add(param, sysTenantUser, currentUser);
        return ResponseValue.success();
    }
 
    /**
     * @Description 列表查询
     */
    @ApiOperation(value = "采购单列表查询", notes = "采购单列表查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "param", value = "采购查询条件", required = true,
        dataType = "FormProcureQryDto", paramType = "query")})
    @GetMapping("/list")
    public ResponseValue queryFormProcureList() {
        FormProcureQry param = CommonUtil.getObjFromReq(FormProcureQry.class);
        FormProcureQry param2 = new FormProcureQry();
        CommonUtil.copyProperties(param, param2);
        param = param2;
        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));
        }
        PageUtil genericPager = lWhFormProcureService.queryFormProcureList(param);
        List<LWhFormProcure> data = genericPager.getDatas();
        ArrayList<FormProcureVO> result = new ArrayList<>();
        if (!CollectionUtils.isEmpty(data)) {
            data.forEach(item -> {
                FormProcureVO fromProcureVO = new FormProcureVO();
                BeanUtils.copyProperties(item, fromProcureVO);
 
                // 查询型号数量
                List<GoodsTemplateCountVO> procureCount =
                    lWhProcureModelService.getProcureCountByBusinessId(item.getId());
                fromProcureVO.setFromProcureTemplateInfoList(procureCount);
 
                result.add(fromProcureVO);
            });
        }
        try {
            Field fieldDatas = GenericPager.class.getDeclaredField("datas");
            // fieldDatas.setAccessible(true);
            // fieldDatas.set(genericPager, result);
            ReflectUtil.setFieldValue(genericPager, fieldDatas, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        genericPager.setDatas(result);
        return ResponseValue.success(genericPager);
    }
 
    /**
     * @Description 编辑
     */
    @PostMapping("/edit")
    public ResponseValue edit() {
        LWhFormProcureParam param = CommonUtil.getObjFromReqBody(LWhFormProcureParam.class);
        LWhFormProcureParam param2 = new LWhFormProcureParam();
        CommonUtil.copyProperties(param, param2);
        param = param2;
        ResponseValue delFlag = delById(param.getId());
        if (delFlag.getCode() == ResponseValue.CODE_SUCCESS) {
            return this.add(param);
        }
        return ResponseValue.error("编辑失败!");
    }
 
    /**
     * @Description 根据id删除
     */
    @DeleteMapping("/del")
    public ResponseValue delById(Long id) {
        if (id == null) {
            return ResponseValue.error("参数不能为空!");
        }
        LWhFormProcure lWhFormProcure = lWhFormProcureService.get(new LWhFormProcure(id));
        if (lWhFormProcure == null) {
            return ResponseValue.error("删除失败!");
        }
        if (lWhFormProcure.getStates() != 1) {
            return ResponseValue.error("待入库状态才能删除!");
        }
        int num = lWhFormProcureService.delete(new LWhFormProcure(id));
        if (num == 0) {
            return ResponseValue.error("删除失败!");
        }
        lWhFormProcureGoodsServiceImpl.update("delete from L_WH_FORM_PROCURE_GOODS where WH_FORM_PROCURE_ID=" + id);
 
        lWhProcureModelService.update("delete from L_WH_PROCURE_MODEL where BUSINESS_TYPE =1 and BUSINESS_ID=" + id);
        return ResponseValue.success(1);
    }
 
    /**
     * 入库操作
     */
    @PostMapping("/income")
    public ResponseValue income(Long id) {
        String errMsg = lWhFormProcureCoreService.doProcure(id, getCurrentUser());
        if (errMsg == null) {
            return ResponseValue.success();
        }
        return ResponseValue.error(errMsg);
    }
 
    /**
     * 根据id查询详情
     */
    @GetMapping("/detail")
    public ResponseValue getById(Long id) throws IllegalAccessException {
        if (id == null) {
            return ResponseValue.error("采购单id为空");
        }
        LWhFormProcure lWhFormProcure = lWhFormProcureService.get(new LWhFormProcure(id));
        if (lWhFormProcure == null) {
            return ResponseValue.error("采购单不存在");
        }
        LWhFormProcureExtendVO lWhFormProcureExtendVO = new LWhFormProcureExtendVO();
        BeanUtils.copyProperties(lWhFormProcure, lWhFormProcureExtendVO);
 
        // 物品
        LWhFormProcureGoods lWhFormProcureGoods = new LWhFormProcureGoods();
        lWhFormProcureGoods.setWhFormProcureId(id);
        List<LWhFormProcureGoods> formProcureGoods = lWhFormProcureGoodsServiceImpl.select(lWhFormProcureGoods);
 
        List<GoodsTemplateInfoVO> goodsTemplateInfoList = new ArrayList<>();
        for (LWhFormProcureGoods formProcureGood : formProcureGoods) {
            GoodsTemplateInfoVO goodsTemplateInfoVO = new GoodsTemplateInfoVO();
            BeanUtils.copyProperties(formProcureGood, goodsTemplateInfoVO);
 
            List<GoodModelInfoDTO> goodsModelInfoList =
                lWhProcureModelService.getGoodsModelListByBusinessId(null, formProcureGood.getId());
            if (CollectionUtils.isEmpty(goodsModelInfoList)) {
                continue;
            }
            GoodModelInfoDTO goodModelInfoDTO = goodsModelInfoList.stream().findFirst().orElse(null);
            if (goodModelInfoDTO != null) {
                goodsTemplateInfoVO.setBaseCategoryName(goodModelInfoDTO.getCategoryName());
            }
            List<GoodsModelVO> goodsModelList = Lists.newArrayList();
            for (GoodModelInfoDTO goodModelInfo : goodsModelInfoList) {
                GoodsModelVO goodsModelVO = getGoodsModelVO(goodModelInfo);
                goodsModelList.add(goodsModelVO);
            }
 
            ReflectUtil.setFieldValue(goodsTemplateInfoVO, "models", goodsModelList);
 
            goodsTemplateInfoList.add(goodsTemplateInfoVO);
        }
        lWhFormProcureExtendVO.setProcureGoods(goodsTemplateInfoList);
        return ResponseValue.success("查询成功!", lWhFormProcureExtendVO);
    }
 
    private GoodsModelVO getGoodsModelVO(GoodModelInfoDTO goodModelInfo) {
        GoodsModelVO goodsModelVO = new GoodsModelVO();
 
        goodsModelVO.setId(goodModelInfo.getId());
        goodsModelVO.setBaseGoodsModelsName(goodModelInfo.getBaseGoodsModelsName());
        goodsModelVO.setUnit(goodModelInfo.getUnit());
        goodsModelVO.setCounts(goodModelInfo.getCounts());
        goodsModelVO.setTotalAmount(
            goodModelInfo.getTotalAmount() != null ? goodModelInfo.getTotalAmount().doubleValue() : null);
        goodsModelVO.setWorehouseCount(goodModelInfo.getWorehouseCount());
        goodsModelVO.setPrice(goodModelInfo.getPrice());
        goodsModelVO.setBaseGoodsModelsId(goodModelInfo.getBaseGoodsModelsId());
        return goodsModelVO;
    }
 
    @ApiOperation(value = "采购单明细查询", notes = "采购单明细查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "param", value = "采购单明细查询", required = true,
        dataType = "FormProcureQryDto", paramType = "query")})
    @GetMapping("detail/list")
    public ResponseValue queryFormProcureDetailList() {
        FormProcureQry formProcureQry = CommonUtil.getObjFromReq(FormProcureQry.class);
        FormProcureQry param2 = new FormProcureQry();
        CommonUtil.copyProperties(formProcureQry, param2);
        formProcureQry = param2;
 
        S_user_core currentUser = this.getCurrentUser();
        if (currentUser == null) {
            return ResponseValue.error("登录用户信息不存在");
        }
 
        GenericPager genericPager = lWhFormProcureService.queryFormProcureDetailList(formProcureQry);
        return ResponseValue.success(genericPager);
    }
 
    @ApiOperation(value = "采购单导出", notes = "采购单导出")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "采购单id", required = true, dataType = "Long", paramType = "query")})
    @GetMapping("/list/export")
    public void export(Long id, HttpServletResponse response) throws Exception {
        TemplateExportParams params = new TemplateExportParams("import/采购入库单.xls");
        params.setHeadingStartRow(2);
        FinSysTenantUser sysInfo = this.getSysInfo();
        if (sysInfo == null) {
            throw new RuntimeException("登录用户信息不存在");
        }
 
        List<ProcureExcelTemplate> exportList = lWhFormProcureService.getExportList(id);
        if (CollectionUtils.isEmpty(exportList)) {
            throw new RuntimeException("数据为空");
        }
 
        int countNum =
            exportList.stream().filter(item -> item.getNum() != null).mapToInt(ProcureExcelTemplate::getNum).sum();
        double totalAmount = exportList.stream().filter(export -> export.getTotalAmount() != null)
            .mapToDouble(ProcureExcelTemplate::getAmount).sum();
        Optional<ProcureExcelTemplate> first = exportList.stream().findFirst();
        ProcureExcelTemplate 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, ProcureExcelTemplate.class, exportList, map);
        downLoadExcel("采购入库单", response, workbook);
 
    }
}