package com.dtok.api.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.dtok.entity.Account;
import com.dtok.entity.Permission;
import com.dtok.entity.params.PermissionNode;
import com.dtok.framework.response.ResponseData;
import com.dtok.framework.util.ToolUtil;
import com.dtok.repository.PermissionRepository;
import com.dtok.repository.RolePermissionRepository;
import com.dtok.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.Predicate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    PermissionRepository permissionRepository;

    @Autowired
    RolePermissionRepository rolePermissionRepository;

    @Autowired
    PermissionService permissionService;


    @GetMapping("/search")
    @SaCheckPermission("permission")
    public ResponseData search(@RequestParam(value = "code", required = false) String code, @RequestParam(value = "name", required = false) String name) {
        Specification<Permission> query = (root, query1, criteriaBuilder) -> {
            Predicate predicate = criteriaBuilder.conjunction();
            if (ToolUtil.isNotEmpty(code)) {
                predicate.getExpressions().add(criteriaBuilder.or(criteriaBuilder.equal(root.get("code"), code)));
            }
            if (ToolUtil.isNotEmpty(name)) {
                predicate.getExpressions().add(criteriaBuilder.or(criteriaBuilder.like(root.get("name"), "%" + name + "%")));
            }
            return predicate;
        };
        List<Permission> roleList = permissionRepository.findAll(query);
        return ResponseData.success(roleList);
    }

    @PostMapping("/add")
    @SaCheckPermission("permission")
    public ResponseData add(Permission permission) {
        permissionRepository.save(permission);
        return ResponseData.success("添加成功");
    }

    @PostMapping("/update")
    @SaCheckPermission("permission")
    public ResponseData update(Permission permission) {
        permissionRepository.save(permission);
        return ResponseData.success("修改成功");
    }

    @GetMapping("/delete/{code}")
    @SaCheckPermission("permission")
    public ResponseData delete(@PathVariable String code) {
        Optional<Permission> permissionOptional = permissionRepository.findById(code);
        if (!permissionOptional.isPresent()) {
            throw new NullPointerException("没有找到该权限");
        }
        permissionRepository.deleteById(code);
        return ResponseData.success("删除成功");
    }

    @GetMapping("/getTree/{roleCode}")
    public ResponseData getTree(@PathVariable String roleCode) {

        Map<String, Object> result = new HashMap<>();
        result.put("tree", permissionService.getTree());
        result.put("userPermission", rolePermissionRepository.findRolePermissionsByRoleCode(roleCode));
        return ResponseData.success(result);
    }


    @GetMapping("/getPermission/{username}")
    public ResponseData getPermission(@PathVariable String username) {
        return ResponseData.success(permissionService.getTree(username));
    }

    @SaCheckRole(Account.FINAL_ROLE)
    @PostMapping("/authPermission/{roleCode}")
    public ResponseData authPermission(@RequestBody List<PermissionNode> permissions, @PathVariable String roleCode) {
        permissionService.authPermission(permissions, roleCode);
        return ResponseData.success("更新成功");
    }


}
