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.dto.KwfFleetDto;
import com.sckw.fleet.model.vo.KwfFleetVo;
import com.sckw.fleet.service.KwfFleetService;
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 java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author zk
 * @desc 车队
 * @date 2023/7/6 0006
 */
@RestController
@RequestMapping("/kwfFleet")
public class KwfFleetController {

    @Autowired
    KwfFleetService fleetService;

    /**
     * @param id 主键ID
     * @desc 根据主键查询
     * @author zk
     * @date 2023/7/6
     **/
    @GetMapping("/detail")
    public HttpResult selectByKey(Long id) throws SystemException {
        return HttpResult.ok(fleetService.selectByKey(id));
    }

    /**
     * @param params {page：页数、pageSize：每页条数、。。。}
     * @desc 分页查询
     * @author zk
     * @date 2023/7/6
     **/
    @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<KwfFleetVo> list = fleetService.findPage(params);
        PageResult pageResult = PageHelperUtil.getPageResult(new PageInfo<>(list));
        return HttpResult.ok(pageResult);
    }

    /**
     * @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(fleetService.findList(params));
    }

    /**
     * @param params 查询参数
     * @desc 导出
     * @author zk
     * @date 2023/07/11
     **/
    @PostMapping(value = "/export", produces = MediaType.APPLICATION_JSON_VALUE)
    public void export(@RequestBody Map<String, Object> params, HttpServletResponse response) {
        /**查询分页数据**/
        List<KwfFleetVo> fleets = fleetService.findPage(params);

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

    /**
     * @param params 新增参数
     * @return HttpResult
     * @desc 新增用户
     * @author czh
     * @date 2023/7/6
     */
    @PostMapping("/add")
    public HttpResult add(@Valid @RequestBody KwfFleetDto params) {
        return fleetService.add(params);
    }

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

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

    /**
     * @param params {entId 企业ID, fleetName 车队班组名称}
     * @desc 车辆实况统计
     * @author zk
     * @date 2023/8/4
     **/
    @PostMapping("/findTruckList")
    public HttpResult findTruckList(@RequestBody Map<String, Object> params) throws SystemException {
        return HttpResult.ok(fleetService.findTruckList(params));
    }
}
