package cn.iocoder.hake.module.pay.controller.admin.serviceregist;

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.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

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

import cn.iocoder.hake.framework.common.pojo.PageParam;
import cn.iocoder.hake.framework.common.pojo.PageResult;
import cn.iocoder.hake.framework.common.pojo.CommonResult;
import cn.iocoder.hake.framework.common.util.object.BeanUtils;

import static cn.iocoder.hake.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.hake.framework.common.pojo.CommonResult.success;

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

import cn.iocoder.hake.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.hake.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.hake.module.pay.enums.ErrorCodeConstants.SERVICE_REGIST_EXISTS;

import cn.iocoder.hake.module.pay.controller.admin.serviceregist.vo.*;
import cn.iocoder.hake.module.pay.dal.dataobject.serviceregist.ServiceRegistDO;
import cn.iocoder.hake.module.pay.service.serviceregist.ServiceRegistService;

@Tag(name = "管理后台 - 商户服务注册")
@RestController
@RequestMapping("/mch/service-regist")
@Validated
public class ServiceRegistController {

    @Resource
    private ServiceRegistService serviceRegistService;

    @PostMapping("/create")
    @Operation(summary = "创建商户服务注册")
    @PreAuthorize("@ss.hasPermission('mch:service-regist:create')")
    public CommonResult<Long> createServiceRegist(@Valid @RequestBody ServiceRegistSaveReqVO createReqVO) {
        ServiceRegistDO serviceRegistDO = serviceRegistService.getServiceRegist(createReqVO.getAppId(),createReqVO.getChannelId(),createReqVO.getMchNo());
        if (serviceRegistDO == null) {
            throw exception(SERVICE_REGIST_EXISTS);
        }
        return success(serviceRegistService.createServiceRegist(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商户服务注册")
    @PreAuthorize("@ss.hasPermission('mch:service-regist:update')")
    public CommonResult<Boolean> updateServiceRegist(@Valid @RequestBody ServiceRegistSaveReqVO updateReqVO) {
        serviceRegistService.updateServiceRegist(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商户服务注册")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mch:service-regist:delete')")
    public CommonResult<Boolean> deleteServiceRegist(@RequestParam("id") Long id) {
        serviceRegistService.deleteServiceRegist(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除商户服务注册")
                @PreAuthorize("@ss.hasPermission('mch:service-regist:delete')")
    public CommonResult<Boolean> deleteServiceRegistList(@RequestParam("ids") List<Long> ids) {
        serviceRegistService.deleteServiceRegistListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商户服务注册")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mch:service-regist:query')")
    public CommonResult<ServiceRegistRespVO> getServiceRegist(@RequestParam("id") Long id) {
        ServiceRegistDO serviceRegist = serviceRegistService.getServiceRegist(id);
        return success(BeanUtils.toBean(serviceRegist, ServiceRegistRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商户服务注册分页")
    @PreAuthorize("@ss.hasPermission('mch:service-regist:query')")
    public CommonResult<PageResult<ServiceRegistRespVO>> getServiceRegistPage(@Valid ServiceRegistPageReqVO pageReqVO) {
        PageResult<ServiceRegistDO> pageResult = serviceRegistService.getServiceRegistPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ServiceRegistRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商户服务注册 Excel")
    @PreAuthorize("@ss.hasPermission('mch:service-regist:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportServiceRegistExcel(@Valid ServiceRegistPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ServiceRegistDO> list = serviceRegistService.getServiceRegistPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商户服务注册.xls", "数据", ServiceRegistRespVO.class,
                        BeanUtils.toBean(list, ServiceRegistRespVO.class));
    }

}