package com.sckw.product.controller;

import com.sckw.core.annotation.Log;
import com.sckw.core.exception.BusinessException;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.utils.StringUtils;
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)
    @Log(description = "添加草稿商品")
    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
     */
    @Log(description = "添加上架商品")
    @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")
    @Log(description = "获取商品详情")
    public HttpResult detail(@RequestParam Long id) {
        return HttpResult.ok(kwpGoodsService.detail(id));
    }

    /**
     * @desc: 修改
     * @author: yzc
     * @date: 2023-07-06 9:02
     * @Param updateGoodsParam:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @Log(description = "修改商品")
    @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
     */
    @Log(description = "分页查询商品列表")
    @PostMapping(value = "/select", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult select(@RequestBody SelectGoodsListParam params) {
        return HttpResult.ok(kwpGoodsService.select(params, true));
    }

    /**
     * @desc: 统计查询
     * @author: yzc
     * @date: 2023-07-06 8:33
     * @Param params:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @Log(description = "商品列表统计查询")
    @PostMapping(value = "/statistic", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult statistic(@RequestBody GoodsListParam 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 ExportGoodsListParam params, HttpServletResponse response) {
        List<GoodsListExport> list = kwpGoodsService.export(params);
        if (CollectionUtils.isEmpty(list)) {
            throw new BusinessException("导出数据为空！");
        }
        ExcelUtil.downData(response, GoodsListExport.class, list);
    }


    /**
     * @desc: 单个上架
     * @author: yzc
     * @date: 2023-07-06 8:56
     * @Param id:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @Log(description = "商品上架")
    @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
     */
    @Log(description = "商品批量下架")
    @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
     */
    @Log(description = "商品批量删除")
    @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
     */
    @Log(description = "建材市场分页查询")
    @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
     */
    @Log(description = "商品下拉列表")
    @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
     */
    @Log(description = "获取商品预计最大可购数量")
    @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
     */
    @Log(description = "更新商品库存")
    @PostMapping(value = "/updateAmount", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult updateAmount(@RequestBody @Validated UpdateAmountParam param) {
        kwpGoodsService.updateAmount(param);
        return HttpResult.ok("更新商品库存成功");
    }
}
