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

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ecep.contract.IEntityService;
import com.ecep.contract.QueryService;
import com.ecep.contract.SpringApp;
import com.ecep.contract.ds.other.repository.EmployeeAuthBindRepository;
import com.ecep.contract.model.Employee;
import com.ecep.contract.model.EmployeeAuthBind;
import com.ecep.contract.service.VoableService;
import com.ecep.contract.util.SpecificationUtils;
import com.ecep.contract.vo.EmployeeAuthBindVo;
import com.ecep.contract.vo.EmployeeVo;
import com.fasterxml.jackson.databind.JsonNode;

@Lazy
@Service
@CacheConfig(cacheNames = "employee-auth-bind")
public class EmployeeAuthBindService implements IEntityService<EmployeeAuthBind>, QueryService<EmployeeAuthBindVo>,
        VoableService<EmployeeAuthBind, EmployeeAuthBindVo> {
    @Lazy
    @Autowired
    private EmployeeAuthBindRepository repository;

    @Override
    public EmployeeAuthBindVo findById(Integer id) {
        return repository.findById(id).map(EmployeeAuthBind::toVo).orElse(null);
    }

    public EmployeeAuthBind getById(Integer id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public Specification<EmployeeAuthBind> getSpecification(String searchText) {
        if (!StringUtils.hasText(searchText)) {
            return null;
        }
        return (root, query, builder) -> {
            return builder.or(
                    builder.like(root.get("ip"), "%" + searchText + "%"),
                    builder.like(root.get("mac"), "%" + searchText + "%"),
                    builder.like(root.get("description"), "%" + searchText + "%"));
        };
    }

    @Override
    public Page<EmployeeAuthBind> findAll(Specification<EmployeeAuthBind> spec, Pageable pageable) {
        return repository.findAll(spec, pageable);
    }

    @Override
    public Page<EmployeeAuthBindVo> findAll(JsonNode paramsNode, Pageable pageable) {
        Specification<EmployeeAuthBind> spec = null;
        if (paramsNode.has("searchText")) {
            spec = getSpecification(paramsNode.get("searchText").asText());
        }
        // 可以根据需要添加更多参数处理
        spec = SpecificationUtils.andParam(spec, paramsNode, "employee");
        return findAll(spec, pageable).map(EmployeeAuthBind::toVo);
    }

    @Override
    public void delete(EmployeeAuthBind entity) {
        repository.delete(entity);
    }

    @Override
    public EmployeeAuthBind save(EmployeeAuthBind entity) {
        return repository.save(entity);
    }

    public List<EmployeeAuthBind> findAllByEmployee(Employee employee, Sort sort) {
        return repository.findAllByEmployeeId(employee.getId(), sort);
    }

    public List<EmployeeAuthBind> findAllByEmployee(EmployeeVo employee, Sort sort) {
        return repository.findAllByEmployeeId(employee.getId(), sort);
    }

    @Override
    public void updateByVo(EmployeeAuthBind model, EmployeeAuthBindVo vo) {
        // 从VO映射属性到实体
        model.setIp(vo.getIp());
        model.setMac(vo.getMac());
        model.setCreateTime(vo.getCreateTime());
        model.setUpdateTime(vo.getUpdateTime());
        model.setDescription(vo.getDescription());

        // employee和updater属性需要通过ID查找对应的实体
        EmployeeService employeeService = SpringApp.getBean(EmployeeService.class);
        if (vo.getEmployeeId() == null) {
            model.setEmployee(null);
        } else {
            if (model.getEmployee() == null || !model.getEmployee().getId().equals(vo.getEmployeeId())) {
                Employee employee = employeeService.getById(vo.getEmployeeId());
                model.setEmployee(employee);
            }
        }
        if (vo.getUpdaterId() == null) {
            model.setUpdater(null);
        } else {
            if (model.getUpdater() == null || !model.getUpdater().getId().equals(vo.getUpdaterId())) {
                Employee updater = employeeService.getById(vo.getUpdaterId());
                model.setUpdater(updater);
            }
        }
    }

}
