package cn.iocoder.yudao.module.cus.controller.admin.management;

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.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

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

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.cus.controller.admin.management.vo.*;
import cn.iocoder.yudao.module.cus.dal.dataobject.management.ManagementDO;
import cn.iocoder.yudao.module.cus.service.management.ManagementService;

@Tag(name = "管理后台 - 客户管理")
@RestController
@RequestMapping("/cus/management")
@Validated
public class ManagementController {

    @Resource
    private ManagementService managementService;

    @PostMapping("/create")
    @Operation(summary = "创建客户管理")
    @PreAuthorize("@ss.hasPermission('cus:management:create')")
    public CommonResult<Long> createManagement(@Valid @RequestBody ManagementSaveReqVO createReqVO) {
        return success(managementService.createManagement(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户管理")
    @PreAuthorize("@ss.hasPermission('cus:management:update')")
    public CommonResult<Boolean> updateManagement(@Valid @RequestBody ManagementSaveReqVO updateReqVO) {
        managementService.updateManagement(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cus:management:delete')")
    public CommonResult<Boolean> deleteManagement(@RequestParam("id") Long id) {
        managementService.deleteManagement(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除客户管理")
                @PreAuthorize("@ss.hasPermission('cus:management:delete')")
    public CommonResult<Boolean> deleteManagementList(@RequestParam("ids") List<Long> ids) {
        managementService.deleteManagementListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cus:management:query')")
    public CommonResult<ManagementRespVO> getManagement(@RequestParam("id") Long id) {
        ManagementDO management = managementService.getManagement(id);
        return success(BeanUtils.toBean(management, ManagementRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户管理分页")
    @PreAuthorize("@ss.hasPermission('cus:management:query')")
    public CommonResult<PageResult<ManagementRespVO>> getManagementPage(@Valid ManagementPageReqVO pageReqVO) {
        PageResult<ManagementDO> pageResult = managementService.getManagementPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ManagementRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客户管理 Excel")
    @PreAuthorize("@ss.hasPermission('cus:management:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportManagementExcel(@Valid ManagementPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ManagementDO> list = managementService.getManagementPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客户管理.xls", "数据", ManagementRespVO.class,
                        BeanUtils.toBean(list, ManagementRespVO.class));
    }

}