package cn.iocoder.yudao.module.basic.controller.admin.dealergoods;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.basic.controller.admin.dealergoods.vo.*;
import cn.iocoder.yudao.module.basic.dal.dataobject.dealergoods.DealerGoodsDO;
import cn.iocoder.yudao.module.basic.convert.dealergoods.DealerGoodsConvert;
import cn.iocoder.yudao.module.basic.service.dealergoods.DealerGoodsService;

@Api(tags = "管理后台 - 经销商经销产品")
@RestController
@RequestMapping("/basic/dealer-goods")
@Validated
public class DealerGoodsController {

    @Resource
    private DealerGoodsService dealerGoodsService;

    @PostMapping("/create")
    @ApiOperation("创建经销商经销产品")
    @PreAuthorize("@ss.hasPermission('basic:dealer-goods:create')")
    public CommonResult<Long> createDealerGoods(@Valid @RequestBody DealerGoodsCreateReqVO createReqVO) {
        return success(dealerGoodsService.createDealerGoods(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新经销商经销产品")
    @PreAuthorize("@ss.hasPermission('basic:dealer-goods:update')")
    public CommonResult<Boolean> updateDealerGoods(@Valid @RequestBody DealerGoodsUpdateReqVO updateReqVO) {
        dealerGoodsService.updateDealerGoods(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除经销商经销产品")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('basic:dealer-goods:delete')")
    public CommonResult<Boolean> deleteDealerGoods(@RequestParam("id") Long id) {
        dealerGoodsService.deleteDealerGoods(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得经销商经销产品")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('basic:dealer-goods:query')")
    public CommonResult<DealerGoodsRespVO> getDealerGoods(@RequestParam("id") Long id) {
        DealerGoodsDO dealerGoods = dealerGoodsService.getDealerGoods(id);
        return success(DealerGoodsConvert.INSTANCE.convert(dealerGoods));
    }

    @GetMapping("/listQuery")
    @ApiOperation("获得经销商经销产品列表，根据请求参数获取")
    @PreAuthorize("@ss.hasPermission('basic:dealer-goods:query')")
    public CommonResult<List<DealerGoodsRespVO>> getDealerGoodsListQuery(DealerGoodsListReqVO dealerGoodsListReqVO) {
        List<DealerGoodsDO> list = dealerGoodsService.getDealerGoodsListQuery(dealerGoodsListReqVO);
        return success(DealerGoodsConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得经销商经销产品分页")
    @PreAuthorize("@ss.hasPermission('basic:dealer-goods:query')")
    public CommonResult<PageResult<DealerGoodsRespVO>> getDealerGoodsPage(@Valid DealerGoodsPageReqVO pageVO) {
        PageResult<DealerGoodsDO> pageResult = dealerGoodsService.getDealerGoodsPage(pageVO);
        return success(DealerGoodsConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/getSaleAreasByGoodsAndDealer")
    @ApiOperation("根据商品ID和经销商ID获取所有对应的销售区域")
    @PreAuthorize("@ss.hasPermission('basic:dealer-goods:query')")
    public CommonResult<List<DealerGoodsDO>> getSaleAreasByGoodsAndDealer(@RequestParam("goodsId") String goodsId,
                                                                   @RequestParam("dealerId") String dealerId) {
        List<DealerGoodsDO> saleAreasByGoodsAndDealer = dealerGoodsService.getSaleAreasByGoodsAndDealer(goodsId, dealerId);
        return success(saleAreasByGoodsAndDealer);
    }
}
