package com.zt.plat.module.base.controller.admin.base;

import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.module.base.controller.admin.base.vo.CompanyRelativityPageReqVO;
import com.zt.plat.module.base.controller.admin.base.vo.CompanyRelativityRespVO;
import com.zt.plat.module.base.controller.admin.base.vo.CompanyRelativitySaveReqVO;
import com.zt.plat.module.base.dal.dataobject.base.CompanyRelaDeptDO;
import com.zt.plat.module.base.dal.dataobject.base.CompanyRelativityDO;
import com.zt.plat.module.base.service.base.CompanyRelativityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zt.plat.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 公司关系")
@RestController
@RequestMapping("/base/company-relativity")
@Validated
public class CompanyRelativityController {


    @Resource
    private CompanyRelativityService companyRelativityService;

    @PostMapping("/create")
    @Operation(summary = "创建公司关系")
    @PreAuthorize("@ss.hasPermission('base:company-relativity:create')")
    public CommonResult<CompanyRelativityRespVO> createCompanyRelativity(@Valid @RequestBody CompanyRelativitySaveReqVO createReqVO) {
        return success(companyRelativityService.createCompanyRelativity(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新公司关系")
    @PreAuthorize("@ss.hasPermission('base:company-relativity:update')")
    public CommonResult<Boolean> updateCompanyRelativity(@Valid @RequestBody CompanyRelativitySaveReqVO updateReqVO) {
        companyRelativityService.updateCompanyRelativity(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除公司关系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:company-relativity:delete')")
    public CommonResult<Boolean> deleteCompanyRelativity(@RequestParam("id") Long id) {
        companyRelativityService.deleteCompanyRelativity(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除公司关系")
                @PreAuthorize("@ss.hasPermission('base:company-relativity:delete')")
    public CommonResult<Boolean> deleteCompanyRelativityList(@RequestBody BatchDeleteReqVO req) {
        companyRelativityService.deleteCompanyRelativityListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得公司关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:company-relativity:query')")
    public CommonResult<CompanyRelativityRespVO> getCompanyRelativity(@RequestParam("id") Long id) {
        CompanyRelativityDO companyRelativity = companyRelativityService.getCompanyRelativity(id);
        return success(BeanUtils.toBean(companyRelativity, CompanyRelativityRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得公司关系分页")
    @PreAuthorize("@ss.hasPermission('base:company-relativity:query')")
    public CommonResult<PageResult<CompanyRelaDeptDO>> getCompanyRelativityPage(@Valid CompanyRelativityPageReqVO pageReqVO) {
        PageResult<CompanyRelaDeptDO> pageResult = companyRelativityService.getCompanyRelativityPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CompanyRelaDeptDO.class));
    }

    @PostMapping("/createOrUpdate-list")
    @Operation(summary = "批量创建更新公司关系")
    @PreAuthorize("@ss.hasPermission('base:company-relativity:create')")
    public CommonResult<List<CompanyRelativityRespVO>> createOrUpdataCompanyList(@Valid @RequestBody List<CompanyRelativitySaveReqVO> createReqVOS) {
        return success(companyRelativityService.createOrUpdataCompanyList(createReqVOS));
    }
}