package cn.iocoder.yudao.module.tkdata.controller.admin.newhosts;

import cn.iocoder.yudao.module.tkdata.controller.admin.newhosts.vo.AllocationHostsSaveReqVO;
import cn.iocoder.yudao.module.tkdata.controller.admin.newhosts.vo.NewHostsPageReqVO;
import cn.iocoder.yudao.module.tkdata.controller.admin.newhosts.vo.NewHostsRespVO;
import cn.iocoder.yudao.module.tkdata.controller.admin.newhosts.vo.NewHostsSaveReqVO;
import cn.iocoder.yudao.module.tkdata.dal.dataobject.newhosts.NewHostsDO;
import cn.iocoder.yudao.module.tkdata.service.newhosts.NewHostsService;
import lombok.extern.slf4j.Slf4j;
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.*;



@Slf4j
@Tag(name = "管理后台 - 主播数据")
@RestController
@RequestMapping("/server/new-hosts")
@Validated
public class NewHostsController {

    @Resource
    private NewHostsService newHostsService;

    @PostMapping("/create")
    @Operation(summary = "创建主播数据")
    @PreAuthorize("@ss.hasPermission('server:new-hosts:create')")
    public CommonResult<Long> createNewHosts(@Valid @RequestBody NewHostsSaveReqVO createReqVO) {
        return success(newHostsService.createNewHosts(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新主播数据")
    @PreAuthorize("@ss.hasPermission('server:new-hosts:update')")
    public CommonResult<Boolean> updateNewHosts(@Valid @RequestBody NewHostsSaveReqVO updateReqVO) {
        newHostsService.updateNewHosts(updateReqVO);
        return success(true);
    }

    @PutMapping("/batch-update")
    @Operation(summary = "批量更新主播数据")
    @PreAuthorize("@ss.hasPermission('server:new-hosts:update')")
    public CommonResult<Boolean> updateNewHosts(@Valid @RequestBody List<NewHostsSaveReqVO> updateReqVO) {
        newHostsService.batchUpdateNewHosts(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除主播数据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('server:new-hosts:delete')")
    public CommonResult<Boolean> deleteNewHosts(@RequestParam("id") Long id) {
        newHostsService.deleteNewHosts(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除主播数据")
                @PreAuthorize("@ss.hasPermission('server:new-hosts:delete')")
    public CommonResult<Boolean> deleteNewHostsList(@RequestParam("ids") List<Long> ids) {
        newHostsService.deleteNewHostsListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得主播数据")
    @Parameter(name = "id", description = "编号", required = true, example = "80")
    @PreAuthorize("@ss.hasPermission('server:new-hosts:query')")
    public CommonResult<NewHostsRespVO> getNewHosts(@RequestParam("id") Long id) {
        NewHostsDO newHosts = newHostsService.getNewHosts(id);
        return success(BeanUtils.toBean(newHosts, NewHostsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得主播数据分页")
    @PreAuthorize("@ss.hasPermission('server:new-hosts:query')")
    public CommonResult<PageResult<NewHostsRespVO>> getNewHostsPage(@Valid NewHostsPageReqVO pageReqVO) {
        PageResult<NewHostsDO> pageResult = newHostsService.getNewHostsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, NewHostsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出主播数据 Excel")
    @PreAuthorize("@ss.hasPermission('server:new-hosts:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportNewHostsExcel(@Valid NewHostsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<NewHostsDO> list = newHostsService.getNewHostsPage(pageReqVO).getList();
        if (pageReqVO.getDeleteFlag()){
            newHostsService.deleteExportData(list);
            log.info("删除导出数据{}",list.size() );
        }
        // 导出 Excel
        ExcelUtils.write(response, "主播数据.xls", "数据", NewHostsRespVO.class,
                        BeanUtils.toBean(list, NewHostsRespVO.class));
    }

}