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

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.tkdata.controller.admin.employeebigbrother.vo.EmployeeBigBrotherPageReqVO;
import cn.iocoder.yudao.module.tkdata.controller.admin.employeebigbrother.vo.EmployeeBigBrotherRespVO;
import cn.iocoder.yudao.module.tkdata.controller.admin.employeebigbrother.vo.EmployeeBigBrotherSaveReqVO;
import cn.iocoder.yudao.module.tkdata.controller.admin.employeehosts.vo.CompletedRateVO;
import cn.iocoder.yudao.module.tkdata.controller.admin.employeehosts.vo.EmployeeCompleteBarChartReqVO;
import cn.iocoder.yudao.module.tkdata.dal.dataobject.employeebigbrother.EmployeeBigBrotherDO;
import cn.iocoder.yudao.module.tkdata.service.employeebigbrother.EmployeeBigBrotherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;



@Tag(name = "管理后台 - 大哥数据员工业务")
@RestController
@RequestMapping("/server/employee-big-brother")
@Validated
public class EmployeeBigBrotherController {

    @Resource
    private EmployeeBigBrotherService employeeBigBrotherService;

    @PostMapping("/create")
    @Operation(summary = "创建大哥数据员工业务")
    @PreAuthorize("@ss.hasPermission('server:employee-big-brother:create')")
    public CommonResult<Integer> createEmployeeBigBrother(@Valid @RequestBody EmployeeBigBrotherSaveReqVO createReqVO) {
        return success(employeeBigBrotherService.createEmployeeBigBrother(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新大哥数据员工业务")
    @PreAuthorize("@ss.hasPermission('server:employee-big-brother:update')")
    public CommonResult<Boolean> updateEmployeeBigBrother(@RequestBody EmployeeBigBrotherSaveReqVO updateReqVO) {
        employeeBigBrotherService.updateEmployeeBigBrother(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除大哥数据员工业务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('server:employee-big-brother:delete')")
    public CommonResult<Boolean> deleteEmployeeBigBrother(@RequestParam("id") Integer id) {
        employeeBigBrotherService.deleteEmployeeBigBrother(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除大哥数据员工业务")
                @PreAuthorize("@ss.hasPermission('server:employee-big-brother:delete')")
    public CommonResult<Boolean> deleteEmployeeBigBrotherList(@RequestParam("ids") List<Integer> ids) {
        employeeBigBrotherService.deleteEmployeeBigBrotherListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得大哥数据员工业务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('server:employee-big-brother:query')")
    public CommonResult<EmployeeBigBrotherRespVO> getEmployeeBigBrother(@RequestParam("id") Integer id) {
        EmployeeBigBrotherDO employeeBigBrother = employeeBigBrotherService.getEmployeeBigBrother(id);
        return success(BeanUtils.toBean(employeeBigBrother, EmployeeBigBrotherRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得大哥数据员工业务分页")
    @PreAuthorize("@ss.hasPermission('server:employee-big-brother:query')")
    public CommonResult<PageResult<EmployeeBigBrotherRespVO>> getEmployeeBigBrotherPage(@Valid EmployeeBigBrotherPageReqVO pageReqVO) {
        PageResult<EmployeeBigBrotherDO> pageResult = employeeBigBrotherService.getEmployeeBigBrotherPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EmployeeBigBrotherRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出大哥数据员工业务 Excel")
    @PreAuthorize("@ss.hasPermission('server:employee-big-brother:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEmployeeBigBrotherExcel(@Valid EmployeeBigBrotherPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EmployeeBigBrotherDO> list = employeeBigBrotherService.getEmployeeBigBrotherPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "大哥数据员工业务.xls", "数据", EmployeeBigBrotherRespVO.class,
                        BeanUtils.toBean(list, EmployeeBigBrotherRespVO.class));
    }

    @PostMapping("/allocation")
    @Operation(summary = "分配大哥给员工")
    @PreAuthorize("@ss.hasPermission('server:employee-big-brother:allocation')")
    public CommonResult<Boolean> allocationEmployeeBigBrother(@Valid @RequestBody List<EmployeeBigBrotherSaveReqVO> createReqVO) {
        return success(employeeBigBrotherService.allocationEmployeeBigBrother(createReqVO));
    }


    @GetMapping("/self_page")
    @Operation(summary = "获得大哥数据员工业务分页")
    @PreAuthorize("@ss.hasPermission('server:employee-big-brother:selfquery')")
    public CommonResult<PageResult<EmployeeBigBrotherRespVO>> getEmployeeBigBrotherPageWithSelf(@Valid EmployeeBigBrotherPageReqVO pageReqVO) {
        PageResult<EmployeeBigBrotherDO> pageResult = employeeBigBrotherService.getEmployeeBigBrotherPageWithSelf(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EmployeeBigBrotherRespVO.class));
    }


    @PutMapping("/batch_update")
    @Operation(summary = "批量更新已分配给员工的大哥数据")
    @PreAuthorize("@ss.hasPermission('server:employee-big-brother:batchupdate')")
    public CommonResult<Boolean> batchupdateEmployeeBigBrother(@RequestBody List<EmployeeBigBrotherSaveReqVO> updateReqVO) {
        employeeBigBrotherService.batchUpdateEmployeeBigBrother(updateReqVO);
        return success(true);
    }
    @GetMapping("/self_complete")
    @Operation(summary = "获得自己的建联率")
    @PreAuthorize("@ss.hasPermission('server:employee-hosts:selfquery')")
    public CommonResult<CompletedRateVO> getEmployeeHostsCompleteWithSelf() {
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        return success(employeeBigBrotherService.getEmployeeHostsCompleteWithSelf(loginUserId));
    }


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

}