package com.hzs.report.report.controller;/**
 * @Description:
 * @Author: yuhui
 * @Time: 2024/9/3 16:36
 * @Classname: WaresSalesVolumeController
 * @PackageName: com.hzs.report.report.controller
 */

import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.report.report.service.WaresSalesVolumeService;
import com.hzs.report.report.vo.WaresSalesVolume;
import com.hzs.report.report.vo.WaresSalesVolumeProduct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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 javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *@BelongsProject: hzs_cloud
 *@BelongsPackage: com.hzs.report.report.controller
 *@Author: yh
 *@CreateTime: 2024-09-03  16:36
 *@Description:   商品销量统计
 *@Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/api/wares-sales-volume")
public class WaresSalesVolumeController extends BaseController
{
    @Autowired
    private ITransactionCommonService transactionCommonService;

    @Autowired
    private WaresSalesVolumeService waresSalesVolumeService;

    /**
     * 专区商品销量统计
     * @return
     */
    @GetMapping("list")
    public TableDataInfo list(String waresCode, String waresName, Date startDate ,Date endDate ){
        Map<String, String> transactionMap = transactionCommonService.exportEnumTransaction(EOrderType.values());
        startPage();
        List<WaresSalesVolume> waresSalesVolumeList = waresSalesVolumeService.queryList(waresCode,waresName,startDate,endDate);
        for (WaresSalesVolume waresSalesVolume : waresSalesVolumeList) {
           List<WaresSalesVolumeProduct> waresSalesVolumeProductList = waresSalesVolumeService.queryProductList(waresSalesVolume.getPkWares());
            waresSalesVolume.setProductInfo(waresSalesVolumeProductList);
            waresSalesVolume.setOrderTypeVal(transactionMap.get(EOrderType.getEnumByValue(waresSalesVolume.getOrderType()).getKey()));
        }
        return getDataTable(waresSalesVolumeList);
    }

    /**
     * 导出
     * @param waresCode
     * @param waresName
     * @param startDate
     * @param endDate
     */
    @PostMapping("export")
    public void export(String waresCode, String waresName, Date startDate , Date endDate, HttpServletResponse response){
        List<WaresSalesVolume> waresSalesVolumeList = waresSalesVolumeService.queryList(waresCode,waresName,startDate,endDate);
        for (WaresSalesVolume waresSalesVolume : waresSalesVolumeList) {
            List<WaresSalesVolumeProduct> waresSalesVolumeProductList = waresSalesVolumeService.queryProductList(waresSalesVolume.getPkWares());
            waresSalesVolume.setProductInfo(waresSalesVolumeProductList);
        }
        ExcelUtil<WaresSalesVolume> util = new ExcelUtil<>(WaresSalesVolume.class);
        util.exportExcel(response, waresSalesVolumeList, "专区商品销量统计导出");
    }

}
