package com.dxhy.manage.controller;

import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.utils.PageUtils;
import com.dxhy.common.utils.R;
import com.dxhy.manage.entity.SysInterfaceInfo;
import com.dxhy.manage.service.SysInterfaceService;
import org.springframework.http.ResponseEntity;
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 java.util.*;

/**
 * lxx
 */
@RestController
@RequestMapping("/manage/interfaceAuthority")
public class SysInterfaceController extends AbstractController {
    @Resource
    private SysInterfaceService sysInterfaceService;

    /**
     * 查询接口权限列表
     */
    @RequestMapping("/list")
    public ResponseEntity<?> listInterface(@RequestBody Map<String, Object> params) {
        PageUtils page = sysInterfaceService.queryPage(params);
        return ResponseEntity.ok(R.ok().put("page", page));
    }

    /**
     * 查询接口权限列表
     */
    @RequestMapping("/listInterface")
    public ResponseEntity<?> listInterfaceInfo(@RequestBody Map<String, Object> params) {
        PageUtils page = sysInterfaceService.listInterfaceInfo(params);
        return ResponseEntity.ok(R.ok().put("page", page));
    }

    /**
     * 删除接口权限
     */
    @RequestMapping("/delete")
    public ResponseEntity<?> delete(long id) {
        try {
            sysInterfaceService.deleteInterface(id);
            return ResponseEntity.ok(R.ok("删除成功！"));

        } catch (Exception e) {
            return ResponseEntity.ok(R.ok("删除失败！"));

        }
    }

    /**
     * 未分配权限查询
     */
    @RequestMapping("/unAuthList")
    public ResponseEntity<?> unAuthList(@RequestBody Map<String, Object> params) {
        return ResponseEntity.ok(Objects.requireNonNull(R.ok().put("page", sysInterfaceService.unAuthList(params))));
    }

    /**
     * 分配权限
     */
    @RequestMapping("/insertCompanyAuth")
    public ResponseEntity<?> insertCompanyAuth(@RequestBody Map<String, Object> params) {
        try {
            sysInterfaceService.insertCompanyAuth(params);
            return ResponseEntity.ok(R.ok("保存成功"));

        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error("保存失败"));

        }
    }

    /**
     * 新增接口
     */
    @RequestMapping("/insertInterface")
    public ResponseEntity<?> insertInterface(@RequestBody SysInterfaceInfo sysInterfaceInfo) {
        try {
            sysInterfaceService.insertInterface(sysInterfaceInfo);
            return ResponseEntity.ok(R.ok("保存成功"));

        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error("保存失败"));

        }
    }

    /**
     * 删除接口
     */
    @RequestMapping("/deleteInterface")
    public ResponseEntity<?> deleteInterface(long id) {
        try {
            sysInterfaceService.deleteInterfaceByid(id);
            return ResponseEntity.ok(R.ok("保存成功"));

        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error("保存失败"));

        }
    }

    /**
     * 未分配权限集团查询
     */
    @RequestMapping("/unDeptList")
    public ResponseEntity<?> unDeptList(@RequestBody Map<String, Object> params) {
        return ResponseEntity.ok(Objects.requireNonNull(R.ok().put("page", sysInterfaceService.unDeptList(params))));
    }

    /**
     * 接口批量授权
     */
    @RequestMapping("/insertBatchAuth")
    public ResponseEntity<?> insertBatchAuth(@RequestBody Map<String, Object> params) {
        try {
            sysInterfaceService.insertBatchAuth(params);
            return ResponseEntity.ok(R.ok("保存成功"));

        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error("保存失败"));

        }
    }

}
