package com.ecep.contract.ds.other.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ecep.contract.ds.other.service.EmployeeRoleService;
import com.ecep.contract.model.EmployeeRole;
import com.ecep.contract.util.SecurityUtils;
import com.ecep.contract.util.SpecificationUtils;

@RestController
@RequestMapping("/employeeRole")
public class EmployeeRoleController {
    @Autowired
    private EmployeeRoleService employeeRoleService;

    @RequestMapping("/findById")
    public EmployeeRole findById(Integer id) {
        return employeeRoleService.findById(id);
    }

    @RequestMapping("/list")
    public Page<EmployeeRole> list(
            Map<String, Object> params,
            @RequestParam(defaultValue = "0", name = "page") int pageNumber,
            @RequestParam(defaultValue = "10", name = "size") int pageSize) {
        Specification<EmployeeRole> spec = null;
        if (!SecurityUtils.currentUserHasRole("ROLE_ADMIN")) {
            spec = SpecificationUtils.and(spec, (root, query, cb) -> cb.equal(root.get("systemAdministrator"), false));
        }

        String searchText = (String) params.get("searchText");
        if (StringUtils.hasText(searchText)) {
            spec = SpecificationUtils.andWith(searchText, (text) -> (root, query, cb) -> {
                return cb.like(root.get("name"), "%" + text + "%");
            });
        }

        Sort sort = Sort.by(Sort.Order.desc("id"));
        Pageable pageable = PageRequest.of(pageNumber, pageSize, sort);
        return employeeRoleService.findAll(spec, pageable);
    }

    @RequestMapping("/save")
    public EmployeeRole save(EmployeeRole role) {
        // 只有系统管理员才能保存角色
        if (!SecurityUtils.currentUserHasRole("ROLE_ADMIN")) {
            throw new SecurityException("无权限执行此操作");
        }
        return employeeRoleService.save(role);
    }

    @RequestMapping("/delete")
    public void delete(Integer id) {
        // 只有系统管理员才能删除角色
        if (!SecurityUtils.currentUserHasRole("ROLE_ADMIN")) {
            throw new SecurityException("无权限执行此操作");
        }
        EmployeeRole role = employeeRoleService.findById(id);
        if (role != null && role.isSystemAdministrator()) {
            throw new SecurityException("不能删除系统管理员角色");
        }
        employeeRoleService.delete(role);
    }

    @RequestMapping("/getFunctionsByRoleId")
    public java.util.List<com.ecep.contract.model.Function> getFunctionsByRoleId(Integer roleId) {
        return employeeRoleService.getFunctionsByRoleId(roleId);
    }
}