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

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.module.basic.controller.admin.businessinfo.vo.*;
import cn.iocoder.yudao.module.basic.dal.dataobject.businessinfo.BusinessInfoDO;
import cn.iocoder.yudao.module.basic.convert.businessinfo.BusinessInfoConvert;
import cn.iocoder.yudao.module.basic.service.businessinfo.BusinessInfoService;

@Api(tags = "管理后台 - 商户")
@RestController
@RequestMapping("/basic/business-info")
@Validated
public class BusinessInfoController {

    @Resource
    private BusinessInfoService businessInfoService;

    @PostMapping("/create")
    @ApiOperation("创建商户")
    @PreAuthorize("@ss.hasPermission('basic:business-info:create')")
    public CommonResult<Long> createBusinessInfo(@Valid @RequestBody BusinessInfoCreateReqVO createReqVO) {
        return success(businessInfoService.createBusinessInfo(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新商户")
    @PreAuthorize("@ss.hasPermission('basic:business-info:update')")
    public CommonResult<Boolean> updateBusinessInfo(@Valid @RequestBody BusinessInfoUpdateReqVO updateReqVO) {
        businessInfoService.updateBusinessInfo(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @ApiOperation("获得商户")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('basic:business-info:query')")
    public CommonResult<BusinessInfoDO> getBusinessInfo(@RequestParam("id") Long id) {
        BusinessInfoDO businessInfo = businessInfoService.getBusinessInfo(id);
        return success(BusinessInfoConvert.INSTANCE.convert(businessInfo));
    }

    @GetMapping("/page")
    @ApiOperation("获得商户分页")
    @PreAuthorize("@ss.hasPermission('basic:business-info:query')")
    public CommonResult<PageResult<BusinessInfoRespVO>> getBusinessInfoPage(@Valid BusinessInfoPageReqVO pageVO) {
        PageResult<BusinessInfoDO> pageResult = businessInfoService.getBusinessInfoPage(pageVO);
        return success(BusinessInfoConvert.INSTANCE.convertPage(pageResult));
    }

    /**
     *获取所有商户昵称 前端调用接口
     * @return
     */
    @GetMapping("/getBusinessNameList")
    @ApiOperation("获取商户名称列表")
    public CommonResult<List<BusinessInfoRespVO>> getBusinessNameList() {
        List<BusinessInfoDO> list = businessInfoService.getBusinessNameList();
        return success(BusinessInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/getBusinessInfoByBusinessId")
    @ApiOperation("根据商户编码获取商户信息")
    @ApiImplicitParam(name = "businessId", value = "商户编码", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('basic:business-info:query')")
    public CommonResult<BusinessInfoDO> getBusinessInfoByBusinessId(@RequestParam("businessId")  String businessId) {
        BusinessInfoDO businessInfoByBusinessId = businessInfoService.getBusinessInfoByBusinessId(businessId);
        return success(BusinessInfoConvert.INSTANCE.convert(businessInfoByBusinessId));
    }
}
