package com.sckw.manage.controller;

import com.sckw.core.annotation.RepeatSubmit;
import com.sckw.core.exception.CustomPromptException;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.response.HttpResult;
import com.sckw.excel.utils.ExcelUtil;
import com.sckw.manage.model.vo.req.*;
import com.sckw.manage.model.vo.res.LineFreightRateExport;
import com.sckw.manage.service.KwmLineFreightRateService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: yzc
 * @date: 2024-03-01  09:20
 * @desc: 路径管理
 */
@RestController
@RequestMapping(value = "/kwmLineFreightRate")
@RequiredArgsConstructor
public class KwmLineFreightRateController {

    private final KwmLineFreightRateService lineFreightRateService;

    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    @RepeatSubmit(interval = 1000, message = "前方拥堵，请稍后尝试")
    public HttpResult add(@RequestBody @Validated AddLineFreightRateParam addLineFreightRateParam) {
        lineFreightRateService.add(addLineFreightRateParam);
        return HttpResult.ok(" 新增运价成功");
    }

    @GetMapping("/detail")
    public HttpResult detail(@RequestParam Long id) {
        return HttpResult.ok(lineFreightRateService.detail(id));
    }

    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
    @RepeatSubmit(interval = 1000, message = "前方拥堵，请稍后尝试")
    public HttpResult update(@RequestBody @Validated UpdateLineFreightRateParam updateLineFreightRateParam) {
        lineFreightRateService.update(updateLineFreightRateParam);
        return HttpResult.ok(" 编辑运价成功");
    }

    @PostMapping(value = "/select", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult select(@RequestBody SelectLineFreightRateParam params) {
        return HttpResult.ok(lineFreightRateService.select(params));
    }

    @PostMapping(value = "/choose", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult choose(@RequestBody ChooseLineFreightRateParam params) {
        return HttpResult.ok(lineFreightRateService.choose(params));
    }

    @PostMapping(value = "/export", produces = MediaType.APPLICATION_JSON_VALUE)
    public void export(@RequestBody ExportLineFreightRateParam params, HttpServletResponse response) {
        List<LineFreightRateExport> list = lineFreightRateService.export(params);
        if (CollectionUtils.isEmpty(list)) {
            throw new CustomPromptException(HttpStatus.SUCCESS_CODE, "暂无数据，请确认");
        }
        ExcelUtil.downData(response, LineFreightRateExport.class, list);
    }

    @GetMapping("/batchDelete")
    public HttpResult batchDelete(@RequestParam List<Long> ids) {
        lineFreightRateService.batchDelete(ids);
        return HttpResult.ok("批量删除成功");
    }

    @PostMapping("/findList")
    public HttpResult findList(@RequestBody LineFreightRateFindListParam params) {
        return HttpResult.ok(lineFreightRateService.findList(params));
    }
}