package com.zfire.mall.manager.controller.direct;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.direct.DirectTransferList;
import com.zfire.mall.manager.bean.direct.DirectTransferOrderBean;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.direct.DirectTransferLogic;
import com.zfire.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.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Slf4j
@RestController
@Api(value = "直调单发货管理", tags ={"直调单发货管理"} )
@RequestMapping(value = "/direct", produces = "application/json; charset=utf-8")
public class DirectTransferController {


    @Autowired
    DirectTransferLogic directTransferLogic;

    @GetMapping("/list")
    @ApiOperation("列表")
    public ResponseHelper<IPage<DirectTransferList>> list(
            @ApiParam(value = "发货单号",required = false) @RequestParam(required = false) String deliverNo,
            @ApiParam(value = "订单号",required = false) @RequestParam(required = false) String orderNo,
            @ApiParam(value = "工程登录单号",required = false) @RequestParam(required = false) String enginRecordNo,
            @ApiParam(value = "类型:TRADE=商用 HOME=家用 RETAIL=零售单 RETAIL_POLICY 销售政策单",required = false) @RequestParam(required = false) String orderType,
            @ApiParam(value = "经销商名称",required = false) @RequestParam(required = false) String customerName,
            @ApiParam(value = "开始时间",required = false) @RequestParam(required = false) String startTime,
            @ApiParam(value = "结束时间",required = false) @RequestParam(required = false) String endTime,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ){
        IPage<DirectTransferList> list = directTransferLogic.list(deliverNo,orderNo, enginRecordNo, orderType, customerName, startTime, endTime, pageNum, pageSize);
        return ResponseHelper.success(list);
    }



    @GetMapping("/detail")
    @ApiOperation("详情")
    public ResponseHelper<DirectTransferOrderBean> detail(
            @ApiParam(value = "id") @RequestParam(required = false) String id
    ){
        DirectTransferOrderBean detail = directTransferLogic.detail(id);
        return ResponseHelper.success(detail);
    }


    @GetMapping("/export")
    @ApiOperation("直调单发货管理-导出")
    public void export(
            @ApiParam(value = "发货单号",required = false) @RequestParam(required = false) String deliverNo,
            @ApiParam(value = "订单号",required = false) @RequestParam(required = false) String orderNo,
            @ApiParam(value = "工程登录单号",required = false) @RequestParam(required = false) String enginRecordNo,
            @ApiParam(value = "类型:TRADE=商用 HOME=家用 RETAIL=零售单 RETAIL_POLICY 销售政策单",required = false) @RequestParam(required = false) String orderType,
            @ApiParam(value = "经销商名称",required = false) @RequestParam(required = false) String customerName,
            @ApiParam(value = "开始时间",required = false) @RequestParam(required = false) String startTime,
            @ApiParam(value = "结束时间",required = false) @RequestParam(required = false) String endTime,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception{
        IPage<DirectTransferList> list = directTransferLogic.list(deliverNo,orderNo, enginRecordNo, orderType, customerName, startTime, endTime, 1, -1);
        ExcelData excelData = directTransferLogic.export(list.getRecords());
        ExcelUtils.exportExcel(request,response,"直调单列表.xlsx",excelData);
    }


}
