package com.ecep.contract.service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import com.ecep.contract.vm.EmployeeViewModel;
import com.ecep.contract.vo.EmployeeRoleVo;
import com.ecep.contract.vo.EmployeeVo;
import com.fasterxml.jackson.databind.JsonNode;

@Service
@CacheConfig(cacheNames = "employee")
public class EmployeeService extends QueryService<EmployeeVo, EmployeeViewModel> {

    public static final int DEFAULT_SYSTEM_EMPLOYEE_ID = 26;

    @Cacheable(key = "#p0")
    @Override
    public EmployeeVo findById(Integer id) {
        return super.findById(id);
    }

    public EmployeeVo findByName(String name) {
        try {
            return async("findByName", name, String.class).handle((response, ex) -> {
                EmployeeVo newEntity = createNewEntity();
                return updateValue(newEntity, response);
            }).get();
        } catch (Exception e) {
            throw new RuntimeException("查询实体失败:" + name, e);
        }
    }

    public EmployeeVo findByCode(String code) {
        try {
            return async("findByCode", code, String.class).handle((response, ex) -> {
                if (ex != null) {
                    throw new RuntimeException("远程方法+findByCode+调用失败", ex);
                }
                EmployeeVo newEntity = createNewEntity();
                return updateValue(newEntity, response);
            }).get();
        } catch (Exception e) {
            throw new RuntimeException("查询实体失败:" + code, e);
        }
    }

    public List<EmployeeRoleVo> getRolesByEmployeeId(Integer employeeId) {
        try {
            return async("getRolesByEmployeeId", List.of(employeeId), List.of(Integer.class)).handle((response, ex) -> {
                if (ex != null) {
                    throw new RuntimeException("远程方法+getRolesByEmployeeId+调用失败", ex);
                }
                if (response != null) {
                    try {
                        List<EmployeeRoleVo> content = new ArrayList<>();
                        for (JsonNode node : response) {
                            EmployeeRoleVo newEntity = new EmployeeRoleVo();
                            objectMapper.updateValue(newEntity, node);
                            content.add(newEntity);
                        }
                        return content;
                    } catch (Exception e) {
                        throw new RuntimeException(response.toString(), e);
                    }
                }
                return null;
            }).get();
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
    })
    @Override
    public EmployeeVo save(EmployeeVo entity) {
        return super.save(entity);
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
    })
    @Override
    public void delete(EmployeeVo entity) {
        super.delete(entity);
    }

    public void getUpdateEmployeeRoles(int employeeId, List<EmployeeRoleVo> roles) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'getUpdateEmployeeRoles'");
    }
}
