package com.sckw.product.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.product.model.GoodsListExport;
import com.sckw.product.model.vo.req.*;
import com.sckw.product.service.KwpGoodsService;
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: 商品管理
 * @author: yzc
 * @date: 2023-07-03 8:28
 */
@RestController
@RequestMapping(value = "/kwpGoods")
@RequiredArgsConstructor
public class KwpGoodsController {

    private final KwpGoodsService kwpGoodsService;

    /**
     * @desc: 添加商品草稿
     * @author: yzc
     * @date: 2023-07-05 10:54
     * @Param param:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/addDraft", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult addDraft(@RequestBody @Validated AddGoodsDraftParam param) {
        kwpGoodsService.addDraft(param);
        return HttpResult.ok("添加草稿商品成功");
    }

    /**
     * @desc: 添加商品上架
     * @author: yzc
     * @date: 2023-07-05 10:54
     * @Param addGoodsParam:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/addShelves", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult addShelves(@RequestBody @Validated AddGoodsParam addGoodsParam) {
        kwpGoodsService.addShelves(addGoodsParam);
        return HttpResult.ok("添加上架商品成功");
    }

    /**
     * @desc: 商品详情
     * @author: yzc
     * @date: 2023-07-05 11:50
     * @Param id:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @GetMapping("/detail")
    public HttpResult detail(@RequestParam Long id) {
        return HttpResult.ok(kwpGoodsService.getDetail(id));
    }

    /**
     * @desc: 修改
     * @author: yzc
     * @date: 2023-07-06 9:02
     * @Param updateGoodsParam:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult update(@RequestBody @Validated UpdateGoodsParam updateGoodsParam) {
        kwpGoodsService.update(updateGoodsParam);
        return HttpResult.ok("修改商品成功");
    }


    /**
     * @desc: 分页查询商品列表
     * @author: yzc
     * @date: 2023-07-05 11:55
     * @Param params:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/select", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult select(@RequestBody SelectGoodsListParam params) {
        return HttpResult.ok(kwpGoodsService.select(params));
    }

    /**
     * @desc: 统计查询
     * @author: yzc
     * @date: 2023-07-06 8:33
     * @Param params:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/statistic", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult statistic(@RequestBody StatisticGoodsListParam params) {
        return HttpResult.ok(kwpGoodsService.statistic(params));
    }

    /**
     * @desc: 导出
     * @author: yzc
     * @date: 2023-07-06 8:33
     * @Param params:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/export", produces = MediaType.APPLICATION_JSON_VALUE)
    public void export(@RequestBody ExportStatisticGoodsListParam params, HttpServletResponse response) {
        List<GoodsListExport> list = kwpGoodsService.export(params);
        if (CollectionUtils.isEmpty(list)) {
            throw new CustomPromptException(HttpStatus.SUCCESS_CODE, "暂无数据，请确认");
        }
        ExcelUtil.downData(response, GoodsListExport.class, list);
    }


    /**
     * @desc: 单个上架
     * @author: yzc
     * @date: 2023-07-06 8:56
     * @Param id:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @GetMapping("/batchPutOnShelves")
    public HttpResult batchPutOnShelves(@RequestParam Long id) {
        kwpGoodsService.batchPutOnShelves(id);
        return HttpResult.ok("上架成功");
    }

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

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

    /**
     * @desc: 建材市场分页查询
     * @author: yzc
     * @date: 2023-07-06 9:38
     * @Param params:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/buildingMaterialsMarketList", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult buildingMaterialsMarketList(@RequestBody BuildingMaterialsMarketListParam params) {
        return HttpResult.ok(kwpGoodsService.buildingMaterialsMarketList(params));
    }

    /**
     * @desc: 商品下拉列表
     * @author: yzc
     * @date: 2023-07-06 8:37
     * @Param params:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/dropList", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult dropList(@RequestBody @Validated GoodsDropListReq params) {
        return HttpResult.ok(kwpGoodsService.dropList(params));
    }

    /**
     * @desc: 获取商品预计最大可购数量
     * @author: yzc
     * @date: 2023-07-31 17:42
     * @Param param:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/maxPurchaseNum", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult maxPurchaseNum(@RequestBody @Validated MaxPurchaseNumParam param) {
        return HttpResult.ok(kwpGoodsService.maxPurchaseNum(param));
    }


    /**
     * @desc: 更新商品库存
     * @author: yzc
     * @date: 2023-08-03 17:05
     * @Param param:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/updateAmount", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult updateAmount(@RequestBody @Validated UpdateAmountParam param) {
        kwpGoodsService.updateAmount(param);
        return HttpResult.ok("更新商品库存成功");
    }

    /**
     * @desc: 商品名称判重
     * @author: yzc
     * @date: 2023-08-16 15:24
     * @Param name:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @GetMapping("/nameDuplicationJudgment")
    public HttpResult nameDuplicationJudgment(@RequestParam String name, @RequestParam Long supplyEntId) {
        return HttpResult.ok("商品名称判重成功", kwpGoodsService.nameDuplicationJudgment(name, supplyEntId, null));
    }

    /**
     * @desc: 获取商品价格
     * @author: yzc
     * @date: 2023-09-05 10:54
     * @Param param:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/getPrice", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult getPrice(@RequestBody @Validated GetPriceParam param) {
        return HttpResult.ok("获取商品价格成功", kwpGoodsService.getPrice(param));
    }
}
