package cn.iocoder.yudao.module.relations.controller.admin.turnover;

import cn.iocoder.yudao.module.relations.controller.admin.renew.vo.RelationsRenewRespVO;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
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 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.relations.controller.admin.turnover.vo.*;
import cn.iocoder.yudao.module.relations.dal.dataobject.turnover.RelationsTurnoverDO;
import cn.iocoder.yudao.module.relations.service.turnover.RelationsTurnoverService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 离职信息")
@RestController
@RequestMapping("/personnel/relations/turnover")
@Validated
public class RelationsTurnoverController {

    @Resource
    private RelationsTurnoverService turnoverService;
    @Resource
    private DeptApi deptApi;

    @PostMapping("/create")
    @Operation(summary = "创建离职信息")
    @PreAuthorize("@ss.hasPermission('relations:turnover:create')")
    public CommonResult<Long> createTurnover(@Valid @RequestBody RelationsTurnoverSaveReqVO createReqVO) {
        return success(turnoverService.createTurnover(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新离职信息")
    @PreAuthorize("@ss.hasPermission('relations:turnover:update')")
    public CommonResult<Boolean> updateTurnover(@Valid @RequestBody RelationsTurnoverSaveReqVO updateReqVO) {
        turnoverService.updateTurnover(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除离职信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('relations:turnover:delete')")
    public CommonResult<Boolean> deleteTurnover(@RequestParam("id") Long id) {
        turnoverService.deleteTurnover(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得离职信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('relations:turnover:query')")
    public CommonResult<RelationsTurnoverRespVO> getTurnover(@RequestParam("id") Long id) {
        return success(turnoverService.getById(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得离职信息分页")
    @PreAuthorize("@ss.hasPermission('relations:turnover:query')")
    public CommonResult<PageResult<RelationsTurnoverRespVO>> getTurnoverPage(@Valid RelationsTurnoverPageReqVO pageReqVO) {
        PageResult<RelationsTurnoverDO> pageResult = turnoverService.getTurnoverPage(pageReqVO);
        PageResult<RelationsTurnoverRespVO> result = BeanUtils.toBean(pageResult, RelationsTurnoverRespVO.class);
        if (result != null && result.getList() != null && result.getList().size() > 0) {
            result.getList().forEach(respVO -> {
                // 部门
                if (respVO.getDeptId() != null) {
                    DeptRespDTO dept = deptApi.getDept(respVO.getDeptId());
                    if (dept != null && StringUtils.isNotBlank(dept.getName())) {
                        respVO.setDeptName(dept.getName());
                    }
                }
            });
        }
        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出离职信息 Excel")
    @PreAuthorize("@ss.hasPermission('relations:turnover:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTurnoverExcel(@Valid RelationsTurnoverPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RelationsTurnoverDO> list = turnoverService.getTurnoverPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "离职信息.xls", "数据", RelationsTurnoverRespVO.class,
                        BeanUtils.toBean(list, RelationsTurnoverRespVO.class));
    }

}