package cn.iocoder.yudao.module.server.controller.admin.techorg;

import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.framework.security.core.LoginUser;
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.server.controller.admin.techorg.vo.*;
import cn.iocoder.yudao.module.server.dal.dataobject.techorg.TechOrgDO;
import cn.iocoder.yudao.module.server.service.techorg.TechOrgService;

@Tag(name = "管理后台 - 技术运维单位")
@RestController
@RequestMapping("/server/tech-org")
@Validated
public class TechOrgController {

    @Resource
    private TechOrgService techOrgService;

    @PostMapping("/create")
    @Operation(summary = "创建技术运维单位")
    @PreAuthorize("@ss.hasPermission('server:tech-org:create')")
    public CommonResult<Long> createTechOrg(@Valid @RequestBody TechOrgSaveReqVO createReqVO) {
        return success(techOrgService.createTechOrg(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新技术运维单位")
    @PreAuthorize("@ss.hasPermission('server:tech-org:update')")
    public CommonResult<Boolean> updateTechOrg(@Valid @RequestBody TechOrgSaveReqVO updateReqVO) {
        techOrgService.updateTechOrg(updateReqVO);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除技术运维单位")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('server:tech-org:delete')")
    public CommonResult<Boolean> deleteTechOrg(@RequestParam("id") Long id) {
        techOrgService.deleteTechOrg(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得技术运维单位")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('server:tech-org:query')")
    public CommonResult<TechOrgRespVO> getTechOrg(@RequestParam("id") Long id) {
        TechOrgDO techOrg = techOrgService.getTechOrg(id);
        return success(BeanUtils.toBean(techOrg, TechOrgRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得技术运维单位分页")
    @PreAuthorize("@ss.hasPermission('server:tech-org:query')")
    public CommonResult<PageResult<TechOrgRespVO>> getTechOrgPage(@Valid TechOrgPageReqVO pageReqVO) {
        //获取当前用户的部门ID
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        if(1 != loginUser.getId()){
            Long deptId = SecurityFrameworkUtils.getLoginUserDeptId();
            pageReqVO.setTechOrgId(deptId);
        }

        PageResult<TechOrgDO> pageResult = techOrgService.getTechOrgPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TechOrgRespVO.class));
    }

    @GetMapping("/getTechOrgList")
    @Operation(summary = "获得技术运维单位列表")
    @DataPermission(enable = false)  // 禁用数据权限过滤
    public CommonResult<List<TechOrgRespVO>> getTechOrgList(@Valid TechOrgPageReqVO pageReqVO){
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TechOrgDO> list = techOrgService.getTechOrgPage(pageReqVO).getList();
        return success(BeanUtils.toBean(list, TechOrgRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出技术运维单位 Excel")
    @PreAuthorize("@ss.hasPermission('server:tech-org:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTechOrgExcel(@Valid TechOrgPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        //获取当前用户的部门ID
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        if(1 != loginUser.getId()){
            Long deptId = SecurityFrameworkUtils.getLoginUserDeptId();
            pageReqVO.setTechOrgId(deptId);
        }
        List<TechOrgDO> list = techOrgService.getTechOrgPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "技术运维单位.xls", "数据", TechOrgRespVO.class,
                        BeanUtils.toBean(list, TechOrgRespVO.class));
    }

}