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

import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.common.pojo.R;
import cn.ey.biz.module.system.controller.admin.delegation.vo.*;
import cn.ey.biz.module.system.convert.delegation.DelegationConvert;
import cn.ey.biz.module.system.service.delegation.UserDelegationService;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;

import static cn.ey.biz.common.pojo.R.success;

@Api(tags = "工时后台 - 代理人关系")
@RestController
@RequestMapping("/system/delegation")
@Validated
public class UserDelegationController {
    @Resource
    private UserDelegationService userDelegationService;

    @PostMapping("/page")
    @ApiOperation("获得代理人关系分页列表")
    @PreAuthorize("@ss.hasPermission('system:delegation:query')")
    public R<PageResult<UserDelegationRespVO>> pageUserdelegations(@Valid @RequestBody UserDelegationPageReqVO reqVO) {
        return success(DelegationConvert.INSTANCE.convertPage(userDelegationService.getUserDelegationPage(reqVO)));
    }

    @PostMapping("/save")
    @ApiOperation("授权字代理人关系")
    @PreAuthorize("@ss.hasPermission('system:delegation:save')")
    public R<Void> saveUserDelegation(@Valid @RequestBody List<UserDelegationSaveReqVO> saveReqVO) {
        userDelegationService.saveUserDelegationList(saveReqVO);
        return success(null);
    }

    @PostMapping("/cancel")
    @ApiOperation("取消授权字代理人关系")
    @PreAuthorize("@ss.hasPermission('system:delegation:cancel')")
    public R<Void> cancelUserDelegation(@RequestBody List<Long> ids) {
        userDelegationService.deleteUserDelegationList(ids);
        return success(null);
    }

}
