package com.gree.mall.manager.controller.material.report;

import com.gree.mall.manager.bean.ExcelData;
import com.gree.mall.manager.bean.material.report.*;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.material.report.ReportLogic;
import com.gree.mall.manager.utils.DateUtils;
import com.gree.mall.manager.utils.excel.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@Api(value = "网点物料报表", tags ={"网点物料报表"} )
@RequestMapping(value = "/websit/report", produces = "application/json; charset=utf-8")
public class ReportController {

    @Resource
    ReportLogic reportLogic;

    @PostMapping("/worker/buy/sum")
    @ApiOperation(value = "销售单按师傅手机号汇总")
    public ResponseHelper<List<MaterialSalesSumBean>> workerBuySum(
            @ApiParam(value = "门店编号") @RequestParam(required = false) String websitId,
            @ApiParam(value = "开始时间") @RequestParam(required = false) String startTime,
            @ApiParam(value = "结束时间") @RequestParam(required = false) String endTime,
            @ApiParam(value = "月份") @RequestParam String month,
            @ApiParam(value = "工程师手机号") @RequestParam(required = false) String workerMobile,
            @ApiParam(value = "工程师编号") @RequestParam(required = false) String workerId,
            @ApiParam(value = "工程师名称") @RequestParam(required = false) String workerName
    ) throws RemoteServiceException {
        List<MaterialSalesSumBean> materialSalesSumBeanList = reportLogic.workerBuySum(startTime, endTime, month, websitId, workerMobile,workerId, workerName);
        return ResponseHelper.success(materialSalesSumBeanList);
    }

    @PostMapping("/worker/buy/sum/detail")
    @ApiOperation(value = "根据手机号和时间获取销售单明细")
    public ResponseHelper<List<MaterialSalesSumDetailBean>> workerBuySumDetail(
            @ApiParam(value = "门店编号", required = true) @RequestParam String websitId,
            @ApiParam(value = "开始时间") @RequestParam(required = false) String startTime,
            @ApiParam(value = "结束时间") @RequestParam(required = false) String endTime,
            @ApiParam(value = "月份") @RequestParam(required = false) String month,
            @ApiParam(value = "工程师手机号", required = true) @RequestParam String workerMobile
    ) throws RemoteServiceException {
        List<MaterialSalesSumDetailBean> saleSumDetailBeanList = reportLogic.workerBuySumDetail(startTime, endTime, month, websitId, workerMobile);
        return ResponseHelper.success(saleSumDetailBeanList);
    }

    @GetMapping("/export/worker/buy/sum")
    @ApiOperation(value = "导出汇总按师傅手机号销售额")
    public void exportSaleSum(
            @ApiParam(value = "门店编号") @RequestParam(required = false) String websitId,
            @ApiParam(value = "开始时间") @RequestParam(required = false) String startTime,
            @ApiParam(value = "结束时间") @RequestParam(required = false) String endTime,
            @ApiParam(value = "月份") @RequestParam(required = false) String month,
            @ApiParam(value = "工程师手机号") @RequestParam(required = false) String workerMobile,
            @ApiParam(value = "工程师编号") @RequestParam(required = false) String workerId,
            @ApiParam(value = "工程师名称") @RequestParam(required = false) String workerName,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        List<MaterialSalesSumBean> materialSalesSumBeanList = reportLogic.workerBuySum(startTime, endTime, month, websitId, workerMobile, workerId, workerName);
        ExcelData excelData = reportLogic.workerBuySumExport(materialSalesSumBeanList);
        ExcelUtils.exportExcel(request,response,"汇总按师傅手机号销售额" + DateUtils.formatDate1(new Date()) + ".xlsx",excelData);
    }

    @GetMapping("/export/worker/buy/sum/Detail")
    @ApiOperation(value = "导出汇总按师傅手机号销售明细")
    public void exportSaleSumDetail(
            @ApiParam(value = "门店编号") @RequestParam(required = false) String websitId,
            @ApiParam(value = "开始时间") @RequestParam(required = false) String startTime,
            @ApiParam(value = "结束时间") @RequestParam(required = false) String endTime,
            @ApiParam(value = "月份") @RequestParam(required = false) String month,
            @ApiParam(value = "工程师手机号") @RequestParam(required = false) String workerMobile,
            @ApiParam(value = "工程师编号") @RequestParam(required = false) String workerId,
            @ApiParam(value = "工程师名称") @RequestParam(required = false) String workerName,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        List<MaterialSalesSumDetailBean> saleSumDetailBeanList = reportLogic.workerBuySumDetail(startTime, endTime, month, websitId, workerMobile, workerId, workerName);
        ExcelData excelData = reportLogic.workerBuySumDetailExport(saleSumDetailBeanList);
        ExcelUtils.exportExcel(request,response,"汇总按师傅手机号销售明细" + DateUtils.formatDate1(new Date()) + ".xlsx",excelData);
    }

    @GetMapping("/export/sum/buy/detail")
    @ApiOperation(value = "导出汇总销售明细")
    public void exportSumSaleDetail(
            @ApiParam(value = "门店编号") @RequestParam(required = false) String websitId,
            @ApiParam(value = "开始时间") @RequestParam(required = false) String startTime,
            @ApiParam(value = "结束时间") @RequestParam(required = false) String endTime,
            @ApiParam(value = "月份") @RequestParam(required = false) String month,
            @ApiParam(value = "工程师手机号") @RequestParam(required = false) String workerMobile,
            @ApiParam(value = "工程师编号") @RequestParam(required = false) String workerId,
            @ApiParam(value = "工程师名称") @RequestParam(required = false) String workerName,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        List<MaterialSalesSumDetailBean> saleSumDetailBeanList = reportLogic.sumBuyDetail(startTime, endTime, month, websitId, workerMobile, workerId, workerName);
        ExcelData excelData = reportLogic.sumBuyDetailExport(saleSumDetailBeanList);
        ExcelUtils.exportExcel(request,response,"汇总销售明细" + DateUtils.formatDate1(new Date()) + ".xlsx",excelData);
    }

    @PostMapping("/purchase-goods-sum")
    @ApiOperation(value = "采购商品汇总")
    public ResponseHelper<List<PurchaseDetailBean>> purchaseGoodsSum(
            @ApiParam(value = "门店编号") @RequestParam(required = false) String websitId,
            @ApiParam(value = "入库开始时间") @RequestParam(required = false) String startTime,
            @ApiParam(value = "入库结束时间") @RequestParam(required = false) String endTime
    ) {
        List<PurchaseDetailBean> purchaseDetailBeanList = reportLogic.purchaseGoodsSum(startTime, endTime, websitId, false);
        return ResponseHelper.success(purchaseDetailBeanList);
    }

    @GetMapping("/export/purchase-goods-sum")
    @ApiOperation(value = "导出采购商品汇总")
    public void exportPurchaseGoodsSum(
            @ApiParam(value = "门店编号") @RequestParam(required = false) String websitId,
            @ApiParam(value = "入库开始时间") @RequestParam(required = false) String startTime,
            @ApiParam(value = "入库结束时间") @RequestParam(required = false) String endTime,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        List<PurchaseDetailBean> purchaseDetailBeanList = reportLogic.purchaseGoodsSum(startTime, endTime, websitId, false);
        List<PurchaseDetailBean> purchaseDetailSumBeanList = reportLogic.purchaseGoodsSum(startTime, endTime, websitId, true);
        ExcelData excelData = reportLogic.purchaseGoodsSumExport(purchaseDetailBeanList, purchaseDetailSumBeanList);
        ExcelUtils.exportExcel(request,response,"采购商品汇总" + DateUtils.formatDate1(new Date()) + ".xlsx",excelData);
    }

    @PostMapping("/purchase-category-sum")
    @ApiOperation(value = "月度网点采购类别")
    public ResponseHelper<List<PurchaseCategoryBean>> purchaseCategorySum(
            @ApiParam(value = "门店编号") @RequestParam(required = false) String websitId,
            @ApiParam(value = "采购月度") @RequestParam String month
    ) throws Exception {
        List<PurchaseCategoryBean> purchaseCategoryBeanList = reportLogic.purchaseCategorySum(websitId, month);
        return ResponseHelper.success(purchaseCategoryBeanList);
    }

    @GetMapping("/export/purchase-category-sum")
    @ApiOperation(value = "导出月度网点采购类别")
    public void exportSaleSumDetail(
            @ApiParam(value = "门店编号") @RequestParam(required = false) String websitId,
            @ApiParam(value = "采购月度") @RequestParam String month,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        List<PurchaseCategoryBean> purchaseCategoryBeanList = reportLogic.purchaseCategorySum(websitId, month);
        ExcelData excelData = reportLogic.purchaseCategorySumExport(purchaseCategoryBeanList);
        ExcelUtils.exportExcel(request,response,"月度网点采购类别" + DateUtils.formatDate1(new Date()) + ".xlsx", excelData);
    }

    @PostMapping("/worker-purchase-category-sum")
    @ApiOperation(value = "月度师傅采购类别")
    public ResponseHelper<List<WorkerPurchaseCategoryBean>> workerPurchaseCategorySum(
            @ApiParam(value = "门店编号") @RequestParam(required = false) String websitId,
            @ApiParam(value = "采购月度") @RequestParam String month
    ) throws Exception {
        List<WorkerPurchaseCategoryBean> workerPurchaseCategoryBeanList = reportLogic.workerPurchaseCategorySum(websitId, month);
        return ResponseHelper.success(workerPurchaseCategoryBeanList);
    }

    @GetMapping("/export/worker-purchase-category-sum")
    @ApiOperation(value = "导出月度师傅采购类别")
    public void exportWorkerPurchaseCategorySum(
            @ApiParam(value = "门店编号") @RequestParam(required = false) String shopId,
            @ApiParam(value = "采购月度") @RequestParam String month,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        List<WorkerPurchaseCategoryBean> workerPurchaseCategoryBeanList = reportLogic.workerPurchaseCategorySum(shopId, month);
        ExcelData excelData = reportLogic.workerPurchaseCategorySumExport(workerPurchaseCategoryBeanList);
        ExcelUtils.exportExcel(request,response,"月度师傅采购类别" + DateUtils.formatDate1(new Date()) + ".xlsx", excelData);
    }


}
