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

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
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.tkdata.controller.admin.employeehosts.vo.*;
import cn.iocoder.yudao.module.tkdata.dal.dataobject.employeehosts.EmployeeHostsDO;
import cn.iocoder.yudao.module.tkdata.service.employeehosts.EmployeeHostsService;

@Tag(name = "管理后台 - 员工分配主播表")
@RestController
@RequestMapping("/server/employee-hosts")
@Validated
public class EmployeeHostsController {

    @Resource
    private EmployeeHostsService employeeHostsService;

    @PostMapping("/create")
    @Operation(summary = "创建员工分配主播")
    @PreAuthorize("@ss.hasPermission('server:employee-hosts:create')")
    public CommonResult<Long> createEmployeeHosts(@Valid @RequestBody EmployeeHostsSaveReqVO createReqVO) {
        return success(employeeHostsService.createEmployeeHosts(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新员工分配主播")
    @PreAuthorize("@ss.hasPermission('server:employee-hosts:update')")
    public CommonResult<Boolean> updateEmployeeHosts( @RequestBody EmployeeHostsSaveReqVO updateReqVO) {
        employeeHostsService.updateEmployeeHosts(updateReqVO);
        return success(true);
    }

    @PutMapping("/batch_update")
    @Operation(summary = "批量更新员工分配主播")
    @PreAuthorize("@ss.hasPermission('server:employee-hosts:batchupdate')")
    public CommonResult<Boolean> updateEmployeeHosts(@RequestBody List<EmployeeHostsSaveReqVO> updateReqVOList) {
        employeeHostsService.batchUpdateEmployeeHosts(updateReqVOList);
        return success(true);
    }



    @DeleteMapping("/delete")
    @Operation(summary = "删除员工分配主播")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('server:employee-hosts:delete')")
    public CommonResult<Boolean> deleteEmployeeHosts(@RequestParam("id") Long id) {
        employeeHostsService.deleteEmployeeHosts(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除员工分配主播")
                @PreAuthorize("@ss.hasPermission('server:employee-hosts:delete')")
    public CommonResult<Boolean> deleteEmployeeHostsList(@RequestParam("ids") List<Long> ids) {
        employeeHostsService.deleteEmployeeHostsListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得员工分配主播")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('server:employee-hosts:query')")
    public CommonResult<EmployeeHostsRespVO> getEmployeeHosts(@RequestParam("id") Long id) {
        EmployeeHostsDO employeeHosts = employeeHostsService.getEmployeeHosts(id);
        return success(BeanUtils.toBean(employeeHosts, EmployeeHostsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "管理获得员工分配主播分页")
    @PreAuthorize("@ss.hasPermission('server:employee-hosts:query')")
    public CommonResult<PageResult<EmployeeHostsRespVO>> getEmployeeHostsPage(@Valid EmployeeHostsPageReqVO pageReqVO) {
        PageResult<EmployeeHostsDO> pageResult = employeeHostsService.getEmployeeHostsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EmployeeHostsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出员工分配主播Excel")
    @PreAuthorize("@ss.hasPermission('server:employee-hosts:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEmployeeHostsExcel(@Valid EmployeeHostsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EmployeeHostsDO> list = employeeHostsService.getEmployeeHostsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "员工分配主播.xls", "数据", EmployeeHostsRespVO.class,
                        BeanUtils.toBean(list, EmployeeHostsRespVO.class));
    }

    @GetMapping("/export-employee-excel")
    @Operation(summary = "导出员工分配主播Excel")
    @PreAuthorize("@ss.hasPermission('server:employee-self-hosts:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEmployeeSelfHostsExcel(@Valid EmployeeHostsPageReqVO pageReqVO,
                                         HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<EmployeeHostsDO> employeeHostsPageWithSelf = employeeHostsService.getEmployeeHostsPageWithSelf(pageReqVO);
        List<EmployeeHostsDO> list = employeeHostsPageWithSelf.getList();
        // 导出 Excel
        ExcelUtils.write(response, "员工分配主播.xls", "数据", EmployeeHostsRespVO.class,
                BeanUtils.toBean(list, EmployeeHostsRespVO.class));
    }


    @PostMapping("/allocation")
    @Operation(summary = "批量分配主播给员工")
    @PreAuthorize("@ss.hasPermission('server:employee-hosts:allocation')")
    public CommonResult<Integer> deleteNewHostsList(@RequestBody List<EmployeeHostsSaveReqVO> hostsList) {
        return success(employeeHostsService.allocationHostsEmployee(hostsList));
    }

    @GetMapping("/self_page")
    @Operation(summary = "获得分配给自己的主播分页")
    @PreAuthorize("@ss.hasPermission('server:employee-hosts:selfquery')")
    public CommonResult<PageResult<EmployeeHostsRespVO>> getEmployeeHostsPageWithSelf(@Valid EmployeeHostsPageReqVO pageReqVO) {
        PageResult<EmployeeHostsDO> pageResult = employeeHostsService.getEmployeeHostsPageWithSelf(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EmployeeHostsRespVO.class));
    }

    @GetMapping("/employeeComplete")
    @Operation(summary = "获得员工的建联率")
    @PreAuthorize("@ss.hasPermission('server:employee-hosts:selfquery')")
    public CommonResult<CompletedRateVO> getEmployeeHostsComplete(@Valid Long userId) {
        return success(employeeHostsService.getEmployeeHostsCompleteWithSelf(userId));
    }

    @GetMapping("/self_complete")
    @Operation(summary = "获得自己的建联率")
    @PreAuthorize("@ss.hasPermission('server:employee-hosts:selfquery')")
    public CommonResult<CompletedRateVO> getEmployeeHostsCompleteWithSelf() {
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        return success(employeeHostsService.getEmployeeHostsCompleteWithSelf(loginUserId));
    }

    @PostMapping("/employeeCompleteBarChart")
    @Operation(summary = "获得员工的建联柱状图数据")
    @PreAuthorize("@ss.hasPermission('server:employee-hosts:selfquery')")
    public CommonResult<List<CompletedRateVO>> getEmployeeCompleteBarChart(@RequestBody EmployeeCompleteBarChartReqVO  reqVO) {
        return success(employeeHostsService.getEmployeeHostsCompleteBarChart(reqVO.getUserId(),reqVO.getDays()));
    }

}