package cn.ey.biz.module.system.controller.admin.delegation;

import cn.ey.biz.common.pojo.R;
import cn.ey.biz.framework.operatelog.core.annotations.OperateLog;
import cn.ey.biz.module.system.controller.admin.delegation.vo.UserDelegationUnitReqVO;
import cn.ey.biz.module.system.controller.admin.delegation.vo.UserDelegationUnitRespVO;
import cn.ey.biz.module.system.controller.admin.delegation.vo.UserDelegationUnitSaveReqVO;
import cn.ey.biz.module.system.controller.admin.delegation.vo.UserDelegationUnitVO;
import cn.ey.biz.module.system.service.delegation.UserDelegationUnitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.ey.biz.common.pojo.R.success;
import static cn.ey.biz.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "工时后台 - 代理人代理单元关系")
@RestController
@RequestMapping("/system/delegation-unit")
@Validated
public class UserDelegationUnitController {

    @Resource
    private UserDelegationUnitService userDelegationUnitService;

    @PostMapping("/list")
    @ApiOperation("获得代理人代理单元列表")
    @PreAuthorize("@ss.hasPermission('system:delegationUnit:query')")
    public R<List<UserDelegationUnitVO<UserDelegationUnitRespVO>>> getUserDelegationUnits(@Valid @RequestBody UserDelegationUnitReqVO reqVO) {
        return success(userDelegationUnitService.getUserDelegationUnit(reqVO));
    }

    @PostMapping("/save")
    @ApiOperation("授权字代理人代理单元")
    @PreAuthorize("@ss.hasPermission('system:delegationUnit:save')")
    public R<Void> saveUserDelegationUnits(@Valid @RequestBody List<UserDelegationUnitVO<UserDelegationUnitSaveReqVO>> saveReqVO) {
        userDelegationUnitService.saveUserDelegationUnitList(saveReqVO);
        return success(null);
    }

    @PostMapping("/cancel")
    @ApiOperation("取消授权字代理人代理单元")
    @PreAuthorize("@ss.hasPermission('system:delegationUnit:cancel')")
    public R<Void> cancelUserDelegationUnits(@Valid @RequestBody List<UserDelegationUnitVO<UserDelegationUnitSaveReqVO>> deleteReqVO) {
        userDelegationUnitService.deleteUserDelegationUnitList(deleteReqVO);
        return success(null);
    }

    @GetMapping("/export")
    @ApiOperation("导出代理人代理单元信息")
    @PreAuthorize("@ss.hasPermission('system:delegationUnit:export')")
    @OperateLog(type = EXPORT)
    public void export(HttpServletResponse response, @Valid UserDelegationUnitReqVO reqVO) throws IOException {
        userDelegationUnitService.export(response, reqVO);
    }


}
