package com.sckw.fleet.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.core.exception.BusinessException;
import com.sckw.core.exception.CustomPromptException;
import com.sckw.core.exception.SystemException;
import com.sckw.core.model.enums.SystemTypeEnum;
import com.sckw.core.model.page.PageHelperUtil;
import com.sckw.core.model.page.PageResult;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.context.LoginUserHolder;
import com.sckw.core.web.response.HttpResult;
import com.sckw.core.web.response.ResponseUtil;
import com.sckw.excel.easyexcel.RequestHolder;
import com.sckw.excel.utils.ExcelUtil;
import com.sckw.fleet.model.KwfTruckReport;
import com.sckw.fleet.model.dto.KwfChangeFleetDto;
import com.sckw.fleet.model.dto.KwfTruckReportDto;
import com.sckw.fleet.model.vo.KwfTruckReportVo;
import com.sckw.fleet.service.KwfTruckReportService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author zk
 * @desc 车辆上报
 * @date 2023/7/18 0018
 */
@RestController
@RequestMapping("/kwfReport")
public class KwfTruckReportController {

    @Autowired
    KwfTruckReportService reportService;

    /**
     * @param params
     * @desc 统计
     * @author zk
     * @date 2023/7/18
     **/
    @PostMapping("/statistics")
    public HttpResult statistics(@RequestBody Map<String, Object> params) throws SystemException {
        params.put("entId", LoginUserHolder.getEntId());
        Map countMaps = reportService.statistics(params);
        return HttpResult.ok(countMaps);
    }

    /**
     * @param params {page：页数、pageSize：每页条数、。。。}
     * @desc 分页查询
     * @author zk
     * @date 2023/7/18
     **/
    @PostMapping("/select")
    public HttpResult findPage(@RequestBody Map<String, Object> params) throws SystemException {
        params.put("entId", LoginUserHolder.getEntId());
        // 设置分页参数
        PageHelper.startPage(PageResult.getPage(params), PageResult.getPageSize(params));
        List<KwfTruckReportVo> list = reportService.findPage(params);
        PageResult pageResult = PageHelperUtil.getPageResult(new PageInfo<>(list));
        return HttpResult.ok(pageResult);
    }

    /**
     * @param params 查询参数
     * @desc 导出
     * @author zk
     * @date 2023/07/18
     **/
    @PostMapping(value = "/export", produces = MediaType.APPLICATION_JSON_VALUE)
    public void export(@RequestBody Map<String, Object> params, HttpServletResponse response) {
        params.put("entId", LoginUserHolder.getEntId());
        /**查询数据**/
        List<KwfTruckReportVo> trucks = reportService.findPage(params);

        if (CollectionUtils.isEmpty(trucks)) {
            throw new CustomPromptException(HttpStatus.SUCCESS_CODE, "暂无数据，请确认！");
        }
        ExcelUtil.downData(response, KwfTruckReportVo.class, trucks);
    }

    /**
     * @param params {。。。}
     * @desc 查询
     * @author zk
     * @date 2023/7/6
     **/
    @PostMapping("/findList")
    public HttpResult findList(@RequestBody Map<String, Object> params) throws SystemException {
        params.put("entId", LoginUserHolder.getEntId());
        return HttpResult.ok(reportService.findList(params));
    }

    /**
     * @param params {。。。}
     * @desc 上报车辆业务状态统计
     * @author zk
     * @date 2023/7/6
     **/
    @PostMapping("/busStatusStatistics")
    public HttpResult busStatusStatistics(@RequestBody Map<String, Object> params) throws SystemException {
        params.put("entId", LoginUserHolder.getEntId());
        return HttpResult.ok(reportService.busStatusStatistics(params));
    }

    /**
     * @param file 导入文件
     * @desc 导入
     * @author zk
     * @date 2023/07/18
     **/
    @PostMapping("/import")
    public HttpResult importExcel(@RequestParam("file") MultipartFile file) {
        return reportService.importExcel(file);
    }

    /**
     * @param params 新增参数
     * @return HttpResult
     * @desc 新增用户
     * @author czh
     * @date 2023/7/18
     */
    @PostMapping("/add")
    public HttpResult add(@Valid @RequestBody List<KwfTruckReportDto> params) throws SystemException{
        return reportService.add(params);
    }

    /**
     * @param params {}
     * @desc 更新
     * @author zk
     * @date 2023/7/18
     **/
    @PostMapping("/update")
    public HttpResult update(@Valid @RequestBody KwfTruckReport params) throws SystemException {
        return reportService.update(params);
    }

    /**
     * @param {ids：主键ID（多个以逗号隔开）}
     * @desc 删除
     * @author zk
     * @date 2023/7/18
     **/
    @DeleteMapping("/dels")
    public HttpResult del(@RequestParam String ids) throws SystemException {
        return reportService.del(ids);
    }

    /**
     * @param params {}
     * @desc 车队变更
     * @author zk
     * @date 2023/7/18
     **/
    @PostMapping("/changeFleet")
    public HttpResult changeFleet(@Valid @RequestBody KwfChangeFleetDto params) throws SystemException {
        return reportService.changeFleet(params);
    }

    /**
     * @param params {}
     * @desc 校验识别
     * @author zk
     * @date 2023/7/18
     **/
    @PostMapping("/identify")
    public HttpResult checkReports(@Valid @RequestBody List<String> params) throws SystemException {
        return reportService.checkReports(params);
    }
}
