package com.sckw.order.controller;

import com.sckw.core.exception.CustomPromptException;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.utils.StringUtils;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.response.HttpResult;
import com.sckw.excel.utils.ExcelUtil;
import com.sckw.order.model.dto.TransportDemandExport;
import com.sckw.order.model.vo.req.*;
import com.sckw.order.serivce.KwoTransportDemandService;
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;

/**
 * @desc: 运需controller
 * @author: yzc
 * @date: 2023-07-25 16:21
 */
@RestController
@RequestMapping(value = "/kwoTransportDemand")
@RequiredArgsConstructor
public class KwoTransportDemandController {

    private final KwoTransportDemandService kwoTransportDemandService;

    /**
     * @desc: 新增运需草稿
     * @author: yzc
     * @date: 2023-07-26 9:42
     * @Param param:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/addDraft", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult addDraft(@RequestBody @Validated AddTransportDemandDraftParam param) {
        kwoTransportDemandService.addDraft(param);
        return HttpResult.ok("新增运需草稿成功");
    }


    /**
     * @desc: 新增运需上架
     * @author: yzc
     * @date: 2023-07-26 9:42
     * @Param param:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/addShelves", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult addShelves(@RequestBody @Validated AddTransportDemandParam param) {
        kwoTransportDemandService.addShelves(param);
        return HttpResult.ok("新增运需上架成功");
    }


    /**
     * @desc: 运需详情
     * @author: yzc
     * @date: 2023-07-26 9:43
     * @Param id:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @GetMapping("/detail")
    public HttpResult detail(@RequestParam Long id) {
        return HttpResult.ok(kwoTransportDemandService.detail(id));
    }

    /**
     * @desc: 运需修改
     * @author: yzc
     * @date: 2023-07-26 9:43
     * @Param param:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult update(@RequestBody @Validated UpdateTransportDemandParam param) {
        kwoTransportDemandService.update(param);
        return HttpResult.ok("运需修改成功!");
    }

    /**
     * @desc: 运需分页查询
     * @author: yzc
     * @date: 2023-07-26 10:05
     * @Param param:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/select", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult select(@RequestBody @Validated SelectTransportDemandParam param) {
        return HttpResult.ok(kwoTransportDemandService.select(param));
    }

    /**
     * @desc: 运需统计
     * @author: yzc
     * @date: 2023-07-26 10:09
     * @Param param:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/statistic", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult statistic(@RequestBody StatisticTransportDemandParam param) {
        return HttpResult.ok(kwoTransportDemandService.statistic(param));
    }

    /**
     * @desc: 运需导出
     * @author: yzc
     * @date: 2023-07-26 10:09
     * @Param params:
     * @Param response:
     * @return: void
     */
    @PostMapping(value = "/export", produces = MediaType.APPLICATION_JSON_VALUE)
    public void export(@RequestBody ExportTransportDemandParam param, HttpServletResponse response) {
        List<TransportDemandExport> list = kwoTransportDemandService.export(param);
        if (CollectionUtils.isEmpty(list)) {
            throw new CustomPromptException(HttpStatus.SUCCESS_CODE, "暂无数据，请确认");
        }
        ExcelUtil.downData(response, TransportDemandExport.class, list);
    }

    /**
     * @desc: 运需上架
     * @author: yzc
     * @date: 2023-07-26 10:24
     * @Param id:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @GetMapping("/batchPutOnShelves")
    public HttpResult batchPutOnShelves(@RequestParam String ids) {
        List<Long> list = StringUtils.splitStrToList(ids, ",", Long.class);
        kwoTransportDemandService.batchPutOnShelves(list);
        return HttpResult.ok("批量上架成功");
    }

    /**
     * @desc: 运需批量下架
     * @author: yzc
     * @date: 2023-07-26 10:25
     * @Param ids:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @GetMapping("/batchTakeOffShelves")
    public HttpResult batchTakeOffShelves(@RequestParam String ids) {
        List<Long> list = StringUtils.splitStrToList(ids, ",", Long.class);
        kwoTransportDemandService.batchTakeOffShelves(list);
        return HttpResult.ok("批量下架成功");
    }

    /**
     * @desc: 运需批量删除
     * @author: yzc
     * @date: 2023-07-26 10:26
     * @Param ids:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @GetMapping("/batchDelete")
    public HttpResult batchDelete(@RequestParam String ids) {
        List<Long> list = StringUtils.splitStrToList(ids, ",", Long.class);
        kwoTransportDemandService.batchDelete(list);
        return HttpResult.ok("批量删除成功");
    }

    /**
     * @desc: 运需广场列表
     * @author: yzc
     * @date: 2023-07-27 15:46
     * @Param param:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/demandSquaresList", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult demandSquaresList(@RequestBody @Validated TransportDemandSquaresParam param) {
        return HttpResult.ok(kwoTransportDemandService.demandSquaresList(param));
    }

    /**
     * @desc: 查看联系方式
     * @author: yzc
     * @date: 2023-09-12 9:05
     * @Param id:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @GetMapping("/getContactInfo")
    public HttpResult getContactInfo(@RequestParam Long id) {
        return HttpResult.ok(kwoTransportDemandService.getContactInfo(id));
    }
}
