xuhy
2024-09-07 97cf3094ce37ee629ca6b948a9317a7ed1796c18
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
package com.ruoyi.web.controller.api;
 
 
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.WebUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.TOrderSale;
import com.ruoyi.system.domain.TOrderSaleGoods;
import com.ruoyi.system.dto.TOrderSaleDTO;
import com.ruoyi.system.export.TOrderMealExportExcel;
import com.ruoyi.system.export.TOrderSaleExportExcel;
import com.ruoyi.system.query.TOrderMealQuery;
import com.ruoyi.system.query.TOrderSaleQuery;
import com.ruoyi.system.service.TOrderSaleGoodsService;
import com.ruoyi.system.service.TOrderSaleService;
import com.ruoyi.system.vo.AmountSumVO;
import com.ruoyi.system.vo.TOrderSaleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
 
/**
 * <p>
 * 销售订单 前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2024-08-14
 */
@Api(tags = "销售订单")
@RestController
@RequestMapping("/t-order-sale")
public class TOrderSaleController {
    private final TOrderSaleService orderSaleService;
    private final TOrderSaleGoodsService orderSaleGoodsService;
    private final TokenService tokenService;
 
    @Autowired
    public TOrderSaleController(TOrderSaleService orderSaleService, TOrderSaleGoodsService orderSaleGoodsService, TokenService tokenService) {
        this.orderSaleService = orderSaleService;
        this.orderSaleGoodsService = orderSaleGoodsService;
        this.tokenService = tokenService;
    }
 
    /**
     * 查询销售列表
     */
    @ApiOperation( value = "查询销售分页列表")
    @PostMapping(value = "/pageList")
    public AjaxResult<PageInfo<TOrderSaleVO>> pageList(@RequestBody TOrderSaleQuery query) {
        query.setShopId(tokenService.getLoginUser().getObjectId());
        return AjaxResult.success(orderSaleService.pageList(query));
    }
 
    /**
     * 查询销售金额统计
     */
    @ApiOperation( value = "查询销售金额统计")
    @PostMapping(value = "/amountSum")
    public AjaxResult<AmountSumVO> amountSum(@RequestBody TOrderMealQuery query) {
        query.setShopId(tokenService.getLoginUser().getObjectId());
        return AjaxResult.success(orderSaleService.amountSum(query));
    }
 
    @ApiOperation( value = "查询销售详情")
    @GetMapping(value = "/getDetailById")
    public AjaxResult<TOrderSaleVO> getDetailById(@RequestParam(value = "id") Long id) {
        TOrderSale orderSale = orderSaleService.getById(id);
        TOrderSaleVO orderSaleVO = new TOrderSaleVO();
        BeanUtils.copyProperties(orderSale, orderSaleVO);
        List<TOrderSaleGoods> list = orderSaleGoodsService.list(Wrappers.lambdaQuery(TOrderSaleGoods.class)
                .eq(TOrderSaleGoods::getOrderId, orderSale.getId()));
        orderSaleVO.setOrderSaleGoods(list);
        return AjaxResult.success(orderSaleVO);
    }
 
    @ApiOperation( value = "删除销售订单")
    @DeleteMapping(value = "/deleteById")
    public AjaxResult<Boolean> deleteById(@RequestParam(value = "id") Long id) {
        orderSaleGoodsService.remove(Wrappers.lambdaQuery(TOrderSaleGoods.class)
                .eq(TOrderSaleGoods::getOrderId,id));
        return AjaxResult.success(orderSaleService.removeById(id));
    }
 
    /**
     * 销售下单接口
     */
    @ApiOperation( value = "销售下单接口")
    @PostMapping(value = "/add")
    public AjaxResult<String> add(@RequestBody TOrderSaleDTO dto) {
        dto.setShopId(tokenService.getLoginUser().getObjectId());
        orderSaleService.add(dto);
        return AjaxResult.success();
    }
 
    /**
     * 导出销售列表
     */
    @ApiOperation(value = "导出销售列表")
    @PostMapping("/exportOrderSale")
    public void exportOrderSale(@RequestBody TOrderMealQuery query)
    {
        List<TOrderSaleVO> list = orderSaleService.exportOrderSale(query);
        List<TOrderMealExportExcel> orderMeals = new ArrayList<>();
        for (TOrderSaleVO orderSaleVO : list) {
            TOrderSaleExportExcel orderSaleExportExcel = new TOrderSaleExportExcel();
            BeanUtils.copyProperties(orderSaleVO, orderSaleExportExcel);
            orderSaleExportExcel.setCreateStrTime(DateUtils.localDateTimeToString(orderSaleVO.getCreateTime()));
            orderSaleExportExcel.setGoodsAmount(orderSaleVO.getOrderSaleGoods().stream().map(TOrderSaleGoods::getThisSalePrice).reduce(BigDecimal::add).get());
            orderSaleExportExcel.setGoodsCount(orderSaleVO.getOrderSaleGoods().size());
        }
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), TOrderMealExportExcel.class, orderMeals);
        HttpServletResponse response = WebUtils.response();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        ServletOutputStream outputStream = null;
        try {
            String fileName = URLEncoder.encode("销售列表.xls", "utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}