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

import cn.iocoder.yudao.module.basic.controller.admin.dealergoods.vo.DealerAearGoodsUpdateVO;
import cn.iocoder.yudao.module.basic.controller.admin.dealergoods.vo.DealerAearGoodsVO;
import cn.iocoder.yudao.module.basic.controller.admin.dealergoods.vo.DealerGoodsCreateReqVO;
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.dealerinfo.vo.*;
import cn.iocoder.yudao.module.basic.dal.dataobject.dealerinfo.DealerInfoDO;
import cn.iocoder.yudao.module.basic.convert.dealerinfo.DealerInfoConvert;
import cn.iocoder.yudao.module.basic.service.dealerinfo.DealerInfoService;

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

    @Resource
    private DealerInfoService dealerInfoService;

    @PostMapping("/create")
    @ApiOperation("创建经销商")
    @PreAuthorize("@ss.hasPermission('basic:dealer-info:create')")
    public CommonResult<Long> createDealerInfo(@Valid @RequestBody DealerInfoCreateReqVO createReqVO) {
        return success(dealerInfoService.createDealerInfo(createReqVO));
    }

    @PostMapping("/createDealerGoods")
    @ApiOperation("创建经销商并添加对应的商品")
    @PreAuthorize("@ss.hasPermission('basic:dealer-info:create')")
    public CommonResult<Long> createDealerAndAreaGoodsInfo(@RequestBody DealerAearGoodsVO dealerAearGoodsVO) {
        return success(dealerInfoService.createDealerGoodsInfo(dealerAearGoodsVO.getDealerInfo(),dealerAearGoodsVO.getAreaGoodsList()));
    }

    @PutMapping("/update")
    @ApiOperation("更新经销商")
    @PreAuthorize("@ss.hasPermission('basic:dealer-info:update')")
    public CommonResult<Boolean> updateDealerInfo(@Valid @RequestBody DealerInfoUpdateReqVO updateReqVO) {
        dealerInfoService.updateDealerInfo(updateReqVO);
        return success(true);
    }

    @PutMapping("/updateDealerGoods")
    @ApiOperation("更新经销商以及所属商品")
    @PreAuthorize("@ss.hasPermission('basic:dealer-info:update')")
    public CommonResult<Boolean> updateDealerInfo(@RequestBody DealerAearGoodsUpdateVO dealerAearGoodsVO) {
        dealerInfoService.updateDealerAndAreaGoodsInfo(dealerAearGoodsVO.getDealerInfo(),dealerAearGoodsVO.getAreaGoodsList(),
                dealerAearGoodsVO.getAreaGoodsUpdateList());
        return success(true);
    }

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

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

    @GetMapping("/get")
    @ApiOperation("获得经销商")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('basic:dealer-info:query')")
    public CommonResult<DealerInfoRespVO> getDealerInfo(@RequestParam("id") Long id) {
        DealerInfoDO dealerInfo = dealerInfoService.getDealerInfo(id);
        return success(DealerInfoConvert.INSTANCE.convert(dealerInfo));
    }

    @GetMapping("/getDealerGoodsInfoByDealerGoodsId")
    @ApiOperation("根据经销商编码获取经销商信息")
    @ApiImplicitParam(name = "dealerId", value = "经销商编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('basic:dealer-info:query')")
    public CommonResult<DealerInfoDO> getDealerGoodsInfoByDealerGoodsId(@RequestParam("dealerId") String dealerId) {
        DealerInfoDO dealerGoodsId = dealerInfoService.getDealerGoodsInfoByDealerGoodsId(dealerId);
        return success(dealerGoodsId);
    }

    @GetMapping("/pull")
    @ApiOperation("获得所有经销商昵称")
    @PreAuthorize("@ss.hasPermission('basic:dealer-info:query')")
    public CommonResult<List<DealerInfoDO>> getPullInfoList() {
        List<DealerInfoDO> pullList = dealerInfoService.getPullInfoList();
        return success(pullList);
    }

    @GetMapping("/page")
    @ApiOperation("获得经销商分页")
    @PreAuthorize("@ss.hasPermission('basic:dealer-info:query')")
    public CommonResult<PageResult<DealerInfoRespVO>> getDealerInfoPage(@Valid DealerInfoPageReqVO pageVO) {
        PageResult<DealerInfoDO> pageResult = dealerInfoService.getDealerInfoPage(pageVO);
        return success(DealerInfoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/getDealerInfoByGoodsId")
    @ApiOperation("根据商品获得经销商列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "goodsId", value = "商品编码", required = true, example = "GOCD202305221712VZEW", dataTypeClass = String.class),
        @ApiImplicitParam(name = "businessInfoId", value = "商户ID", required = true, example = "1", dataTypeClass = Long.class),
    })
    @PreAuthorize("@ss.hasPermission('basic:dealer-info:query')")
    public CommonResult<List<DealerInfoRespVO>> getDealerInfoByGoodsId(@RequestParam("goodsId") String goodsId, @RequestParam("businessInfoId") Long businessInfoId) {
        List<DealerInfoDO> dealerInfos = dealerInfoService.getDealerInfoByGoodsId(goodsId, businessInfoId);
        return success(DealerInfoConvert.INSTANCE.convertList(dealerInfos));
    }

}
