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

import cn.iocoder.yudao.module.relations.controller.admin.entry.vo.RelationsEntryRespVO;
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.renew.vo.*;
import cn.iocoder.yudao.module.relations.dal.dataobject.renew.RelationsRenewDO;
import cn.iocoder.yudao.module.relations.service.renew.RelationsRenewService;

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

@Tag(name = "管理后台 - 续签信息")
@RestController
@RequestMapping("/personnel/relations/renew")
@Validated
public class RelationsRenewController {

    @Resource
    private RelationsRenewService renewService;
    @Resource
    private DeptApi deptApi;

    @PostMapping("/create")
    @Operation(summary = "创建续签信息")
    @PreAuthorize("@ss.hasPermission('relations:renew:create')")
    public CommonResult<Long> createRenew(@Valid @RequestBody RelationsRenewSaveReqVO createReqVO) {
        return success(renewService.createRenew(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新续签信息")
    @PreAuthorize("@ss.hasPermission('relations:renew:update')")
    public CommonResult<Boolean> updateRenew(@Valid @RequestBody RelationsRenewSaveReqVO updateReqVO) {
        renewService.updateRenew(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得续签信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('relations:renew:query')")
    public CommonResult<RelationsRenewRespVO> getRenew(@RequestParam("id") Long id) {
//        RelationsRenewDO renew = renewService.getRenew(id);
        return success(renewService.getById(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得续签信息分页")
    @PreAuthorize("@ss.hasPermission('relations:renew:query')")
    public CommonResult<PageResult<RelationsRenewRespVO>> getRenewPage(@Valid RelationsRenewPageReqVO pageReqVO) {
        PageResult<RelationsRenewDO> pageResult = renewService.getRenewPage(pageReqVO);
        PageResult<RelationsRenewRespVO> result = BeanUtils.toBean(pageResult, RelationsRenewRespVO.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:renew:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRenewExcel(@Valid RelationsRenewPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RelationsRenewDO> list = renewService.getRenewPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "续签信息.xls", "数据", RelationsRenewRespVO.class,
                        BeanUtils.toBean(list, RelationsRenewRespVO.class));
    }

}