package com.zfire.mall.manager.controller.manage.take;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.manage.take.TakeCarRecordBean;
import com.zfire.mall.manager.bean.valid.EditGroup;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.manage.take.TakeCarRecordLogic;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.groups.Default;
import java.util.List;

@Slf4j
@RestController
@Api(value = "提货车辆档案", tags ={"提货车辆档案"} )
@RequestMapping(value = "/take-car-record", produces = "application/json; charset=utf-8")
public class TakeCardRecordController {

    @Resource
    TakeCarRecordLogic takeCarRecordLogic;

    @ApiOperation(value = "提货车辆档案列表")
    @GetMapping("/list")
    public ResponseHelper<IPage<TakeCarRecordBean>> list(
            @ApiParam(value = "经销商名称") @RequestParam(required = false) String customerName,
            @ApiParam(value = "车牌") @RequestParam(required = false) String carBrand,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ) throws Exception {
        IPage<TakeCarRecordBean> listPage = takeCarRecordLogic.listPage(customerName, carBrand, pageNum, pageSize);
        return ResponseHelper.success(listPage);
    }

    @ApiOperation(value = "提货车辆档案列表导出")
    @GetMapping("/export")
    public void export(
            @ApiParam(value = "经销商名称") @RequestParam(required = false) String customerName,
            @ApiParam(value = "车牌") @RequestParam(required = false) String carBrand,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        IPage<TakeCarRecordBean> listPage = takeCarRecordLogic.listPage(customerName, carBrand, 1, -1);
        ExcelData excelData = takeCarRecordLogic.export(listPage.getRecords());
        ExcelUtils.exportExcel(request,response, "提货人车辆.xlsx",excelData);
    }

    @ApiOperation(value = "新增提货车辆档案")
    @PostMapping("/add")
    public ResponseHelper add(
            @ApiParam(required = true, value = "提货车辆档案") @Validated @RequestBody TakeCarRecordBean takeCarRecordBean
    ) throws Exception {
        takeCarRecordLogic.add(takeCarRecordBean);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "编辑提货车辆档案")
    @PostMapping("/edit")
    public ResponseHelper edit(
            @ApiParam(required = true, value = "提货车辆档案") @Validated(value = { EditGroup.class, Default.class }) @RequestBody TakeCarRecordBean takeCarRecordBean
    ) throws Exception {
        takeCarRecordLogic.edit(takeCarRecordBean);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "删除提货车辆档案")
    @PostMapping("/del")
    public ResponseHelper del(
            @ApiParam(required = true, value = "提货车辆档案") @RequestParam List<String> ids
    ) throws Exception {
        takeCarRecordLogic.del(ids);
        return ResponseHelper.success();
    }

//    @GetMapping("/template/download")
//    @ApiOperation("下载模板")
//    public void downloadTemplate(HttpServletResponse response) throws IOException {
//        String downloadFilePath = takeCarRecordLogic.downloadFilePath();
//        String fileName = "/template/提货车辆导入模板.xlsx";
//        CommonUtils.downloadFile(downloadFilePath,response);
//    }
}
