package cn.iocoder.yudao.module.system.controller.admin.deptsaporg;

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.module.system.controller.admin.deptsaporg.vo.DeptSapOrgPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.deptsaporg.vo.DeptSapOrgRespVO;
import cn.iocoder.yudao.module.system.controller.admin.deptsaporg.vo.DeptSapOrgSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.deptsaporg.DeptSapOrgDO;
import cn.iocoder.yudao.module.system.service.deptsaporg.DeptSapOrgService;
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 = "管理后台 - PMS 组织部门与SAP工厂/成本中心 对应关系")
@RestController
@RequestMapping("/system/dept-sap-org")
@Validated
public class DeptSapOrgController {

    @Resource
    private DeptSapOrgService deptSapOrgService;

    @PostMapping("/create")
    @Operation(summary = "创建PMS 组织部门与SAP工厂/成本中心 对应关系")
    @PreAuthorize("@ss.hasPermission('system:dept-sap-org:create')")
    public CommonResult<Long> createDeptSapOrg(@Valid @RequestBody DeptSapOrgSaveReqVO createReqVO) {
        return success(deptSapOrgService.createDeptSapOrg(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新PMS 组织部门与SAP工厂/成本中心 对应关系")
    @PreAuthorize("@ss.hasPermission('system:dept-sap-org:update')")
    public CommonResult<Boolean> updateDeptSapOrg(@Valid @RequestBody DeptSapOrgSaveReqVO updateReqVO) {
        deptSapOrgService.updateDeptSapOrg(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除PMS 组织部门与SAP工厂/成本中心 对应关系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:dept-sap-org:delete')")
    public CommonResult<Boolean> deleteDeptSapOrg(@RequestParam("id") Long id) {
        deptSapOrgService.deleteDeptSapOrg(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得PMS 组织部门与SAP工厂/成本中心 对应关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:dept-sap-org:query')")
    public CommonResult<DeptSapOrgRespVO> getDeptSapOrg(@RequestParam("id") Long id) {
        DeptSapOrgDO deptSapOrg = deptSapOrgService.getDeptSapOrg(id);
        return success(BeanUtils.toBean(deptSapOrg, DeptSapOrgRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得PMS 组织部门与SAP工厂/成本中心 对应关系分页")
    @PreAuthorize("@ss.hasPermission('system:dept-sap-org:query')")
    public CommonResult<PageResult<DeptSapOrgRespVO>> getDeptSapOrgPage(@Valid DeptSapOrgPageReqVO pageReqVO) {
        PageResult<DeptSapOrgDO> pageResult = deptSapOrgService.getDeptSapOrgPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeptSapOrgRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出PMS 组织部门与SAP工厂/成本中心 对应关系 Excel")
    @PreAuthorize("@ss.hasPermission('system:dept-sap-org:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeptSapOrgExcel(@Valid DeptSapOrgPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeptSapOrgDO> list = deptSapOrgService.getDeptSapOrgPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "PMS 组织部门与SAP工厂/成本中心 对应关系.xls", "数据", DeptSapOrgRespVO.class,
                        BeanUtils.toBean(list, DeptSapOrgRespVO.class));
    }

}