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

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.entry.vo.*;
import cn.iocoder.yudao.module.relations.dal.dataobject.entry.RelationsEntryDO;
import cn.iocoder.yudao.module.relations.service.entry.RelationsEntryService;

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

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

    @Resource
    private RelationsEntryService entryService;
    @Resource
    private DeptApi deptApi;

    @PostMapping("/create")
    @Operation(summary = "创建入职信息")
    @PreAuthorize("@ss.hasPermission('relations:entry:create')")
    public CommonResult<Long> createEntry(@Valid @RequestBody RelationsEntrySaveReqVO createReqVO) {
        return success(entryService.createEntry(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新入职信息")
    @PreAuthorize("@ss.hasPermission('relations:entry:update')")
    public CommonResult<Boolean> updateEntry(@Valid @RequestBody RelationsEntrySaveReqVO updateReqVO) {
        entryService.updateEntry(updateReqVO);
        return success(true);
    }

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

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

    @GetMapping("/page")
    @Operation(summary = "获得入职信息分页")
    @PreAuthorize("@ss.hasPermission('relations:entry:query')")
    public CommonResult<PageResult<RelationsEntryRespVO>> getEntryPage(@Valid RelationsEntryPageReqVO pageReqVO) {
        PageResult<RelationsEntryDO> pageResult = entryService.getEntryPage(pageReqVO);
        PageResult<RelationsEntryRespVO> result = BeanUtils.toBean(pageResult, RelationsEntryRespVO.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:entry:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEntryExcel(@Valid RelationsEntryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RelationsEntryDO> list = entryService.getEntryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "入职信息.xls", "数据", RelationsEntryRespVO.class,
                        BeanUtils.toBean(list, RelationsEntryRespVO.class));
    }

}