package com.lantone.security.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lantone.common.api.CommonResult;
import com.lantone.security.dto.GetDataAuthByIdDTO;
import com.lantone.security.dto.GetDataAuthPageDTO;
import com.lantone.security.dto.GetDoctorPageDTO;
import com.lantone.security.facade.DataAuthManagementFacade;
import com.lantone.security.vo.AddAuthVO;
import com.lantone.security.vo.GetDataAuthPageVO;
import com.lantone.security.vo.GetDoctorPageVO;
import com.lantone.security.vo.UpdateAuthVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * @Description: 数据权限API
 * @author: songxl
 * @time: 2021/7/20 12:39
 */
@RestController
@Api(value = "数据权限API", tags = { "数据权限API" })
@RequestMapping("/dataAuthManage")
public class DataAuthManagementController {
    @Autowired
    private DataAuthManagementFacade dataAuthManagementFacade;

    @ApiOperation(value = "新增数据权限[by:songxl]")
    @PostMapping("/addAuth")
    @Transactional
    public CommonResult<Boolean> addAuth(@RequestBody @Valid AddAuthVO addAuthVO) {
        return CommonResult.success(dataAuthManagementFacade.addDataAuth(addAuthVO));
    }

    @ApiOperation(value = "修改数据权限[by:songxl]")
    @PostMapping("/updateAuth")
    @Transactional
    public CommonResult<Boolean> updateAuth(@RequestBody @Valid UpdateAuthVO updateAuthVO) {
        return CommonResult.success(dataAuthManagementFacade.updateDataAuth(updateAuthVO));
    }

    @ApiOperation(value = "启用禁用数据权限[by:songxl]")
    @PostMapping("/disableAuth")
    @Transactional
    public CommonResult<Boolean> disableAuth(@RequestParam @Valid @NotNull(message = "数据权限id为空") Long id, @RequestParam @Valid @NotBlank(message = "启用禁用status为空") String status) {
        return CommonResult.success(dataAuthManagementFacade.disableDataAuth(id, status));
    }

    @ApiOperation(value = "删除数据权限[by:songxl]")
    @PostMapping("/deleteAuth")
    @Transactional
    public CommonResult<Boolean> deleteAuth(@RequestParam @Valid @NotNull(message = "数据权限id为空") Long id) {
        return CommonResult.success(dataAuthManagementFacade.deleteDataAuth(id));
    }

    @ApiOperation(value = "查看数据权限[by:songxl]")
    @PostMapping("/getDataAuthById")
    public CommonResult<GetDataAuthByIdDTO> getDataAuthById(@RequestParam @Valid @NotNull(message = "数据权限id为空") Long id) {
        return CommonResult.success(dataAuthManagementFacade.getDataAuthById(id));
    }

    @ApiOperation(value = "获取医生列表[by:songxl]")
    @PostMapping("/getDoctorPage")
    public CommonResult<IPage<GetDoctorPageDTO>> getDoctorPage(@RequestBody @Valid GetDoctorPageVO getDoctorPageVO) {
        return CommonResult.success(dataAuthManagementFacade.getDoctorPage(getDoctorPageVO));
    }

    @ApiOperation(value = "获取数据权限列表[by:songxl]")
    @PostMapping("/getDataAuthPage")
    public CommonResult<IPage<GetDataAuthPageDTO>> getDataAuthPage(@RequestBody @Valid GetDataAuthPageVO getDataAuthPageVO) {
        return CommonResult.success(dataAuthManagementFacade.getDataAuthPage(getDataAuthPageVO));
    }

    @ApiOperation(value = "加载所有用户的数据权限[by:cy]")
    @PostMapping("/initUserDataAuth")
    @ApiIgnore
    public CommonResult<Boolean> initUserDataAuth() {
        return CommonResult.success(dataAuthManagementFacade.initUserDataAuth());
    }

}