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.WantBuyExport;
import com.sckw.order.model.vo.req.*;
import com.sckw.order.serivce.KwpWantBuyService;
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-24 14:44
 */
@RestController
@RequestMapping(value = "/kwoWantBuy")
@RequiredArgsConstructor
public class KwpWantBuyController {

    private final KwpWantBuyService kwpWantBuyService;

    /**
     * @desc: 求购分页查询
     * @author lt
     * @Date 15:34 2023/7/26 0026
     **/
    @PostMapping(value = "/select", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult select(@RequestBody WantBuySelectParam wantBuySelectParam) {
        return HttpResult.ok(kwpWantBuyService.select(wantBuySelectParam));
    }

    /**
     * @desc: 求购大厅
     * @author lt
     * @Date 15:34 2023/7/26 0026
     **/
    @PostMapping(value = "/buyHallList", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult buyHallList(@RequestBody WantBuySelectParam wantBuySelectParam) {
        return HttpResult.ok(kwpWantBuyService.buyHallList(wantBuySelectParam));
    }

    /**
     * @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(kwpWantBuyService.getContactInfo(id));
    }

    /**
     * @desc: 新增求购草稿
     * @author: yzc
     * @date: 2023-07-24 16:42
     * @Param addWantBuyParam:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/addDraft", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult addDraft(@RequestBody @Validated AddDraftWantBuyParam addDraftWantBuyParam) {
        kwpWantBuyService.addDraft(addDraftWantBuyParam);
        return HttpResult.ok("新增求购草稿成功");
    }

    /**
     * @desc: 新增求购上架
     * @author: yzc
     * @date: 2023-07-24 16:43
     * @Param addWantBuyParam:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/addShelves", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult addShelves(@RequestBody @Validated AddWantBuyParam addWantBuyParam) {
        kwpWantBuyService.addShelves(addWantBuyParam);
        return HttpResult.ok("新增求购上架成功");
    }

    /**
     * @desc: 求购上架
     * @Author: lt
     * @Date: 13:39 2023/7/25 0025
     **/
    @GetMapping(value = "/batchPutOnShelves", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult batchPutOnShelves(@RequestParam String ids) {
        List<Long> list = StringUtils.splitStrToList(ids, Long.class);
        kwpWantBuyService.batchPutOnShelves(list);
        return HttpResult.ok("批量求购上架成功");
    }

    /**
     * @desc: 求购批量下架
     * @Author: lt
     * @Date: 13:39 2023/7/25 0025
     **/
    @PostMapping(value = "/batchTakeOffShelves", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult batchTakeOffShelves(@RequestBody @Validated WantBuyDels wantBuyDels) {
        kwpWantBuyService.batchTakeOffShelves(wantBuyDels);
        return HttpResult.ok("批量下架成功");
    }

    /**
     * @desc: 求购批量删除
     * @Author: lt
     * @Date: 13:39 2023/7/25 0025
     **/
    @PostMapping(value = "/dels", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult dels(@RequestBody @Validated WantBuyDels wantBuyDels) {
        kwpWantBuyService.dels(wantBuyDels);
        return HttpResult.ok("批量删除成功");
    }

    /**
     * @desc: 求购详情
     * @author: yzc
     * @date: 2023-07-24 16:45
     * @Param id:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @GetMapping("/detail")
    public HttpResult detail(@RequestParam Long id) {
        return HttpResult.ok(kwpWantBuyService.detail(id));
    }

    /**
     * @desc: 求购编辑
     * @author lt
     * @Date 15:22 2023/7/26 0026
     **/
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult update(@RequestBody @Validated UpdateWantBuyParam updateWantBuyParam) {
        kwpWantBuyService.update(updateWantBuyParam);
        return HttpResult.ok("求购修改成功!");
    }

    /**
     * @desc 求购角标统计
     * @author lt
     * @Date 15:23 2023/7/26 0026
     **/
    @PostMapping(value = "/statistic", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult statistic(@RequestBody WantBuySelectParam params) {
        return HttpResult.ok(kwpWantBuyService.statistic(params));
    }

    /**
     * @desc: 求购导出
     * @author: yzc
     * @date: 2023-08-16 16:09
     * @Param param:
     * @Param response:
     * @return: void
     */
    @PostMapping(value = "/export", produces = MediaType.APPLICATION_JSON_VALUE)
    public void export(@RequestBody WantBuySelectParam param, HttpServletResponse response) {
        List<WantBuyExport> list = kwpWantBuyService.export(param);
        if (CollectionUtils.isEmpty(list)) {
            throw new CustomPromptException(HttpStatus.SUCCESS_CODE, "暂无数据，请确认");
        }
        ExcelUtil.downData(response, WantBuyExport.class, list);
    }
}
