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

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
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.company.repository.CompanyBlackReasonRepository;
import com.ecep.contract.ds.company.model.Company;
import com.ecep.contract.ds.company.model.CompanyBlackReason;
import com.ecep.contract.service.VoableService;
import com.ecep.contract.util.SpecificationUtils;
import com.ecep.contract.vo.CompanyBlackReasonVo;
import com.fasterxml.jackson.databind.JsonNode;

@Lazy
@Service
@CacheConfig(cacheNames = "company-black-reason")
public class CompanyBlackReasonService
        implements IEntityService<CompanyBlackReason>, QueryService<CompanyBlackReasonVo>,
        VoableService<CompanyBlackReason, CompanyBlackReasonVo> {
    private static final Logger logger = LoggerFactory.getLogger(CompanyContactService.class);

    @Autowired
    private CompanyBlackReasonRepository repository;

    @Cacheable(key = "#p0")
    public CompanyBlackReasonVo findById(Integer id) {
        return repository.findById(id).map(CompanyBlackReason::toVo).orElse(null);
    }

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

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

    @Override
    public CompanyBlackReason getById(Integer id) {
        return repository.findById(id).orElse(null);
    }

    public void delete(CompanyBlackReason entity) {
        repository.delete(entity);
    }

    public List<CompanyBlackReason> findAll(Specification<CompanyBlackReason> spec, Sort by) {
        return repository.findAll(spec, by);
    }

    public List<CompanyBlackReason> findAllByCompany(Company company) {
        return repository.findAllByCompany(company);
    }

    public CompanyBlackReason save(CompanyBlackReason companyBlackReason) {
        return repository.save(companyBlackReason);
    }

    @Override
    public Page<CompanyBlackReasonVo> findAll(JsonNode paramsNode, Pageable pageable) {
        Specification<CompanyBlackReason> spec = null;
        if (paramsNode.has("searchText")) {
            spec = getSpecification(paramsNode.get("searchText").asText());
        }
        // field
        spec = SpecificationUtils.andParam(spec, paramsNode, "company");
        return findAll(spec, pageable).map(CompanyBlackReason::toVo);
    }

    @Override
    public void updateByVo(CompanyBlackReason entity, CompanyBlackReasonVo vo) {

        if (vo.getCompanyId() == null) {
            entity.setCompany(null);
        } else {
            CompanyService companyService = SpringApp.getBean(CompanyService.class);
            if (entity.getCompany() == null || !entity.getCompany().getId().equals(vo.getCompanyId())) {
                entity.setCompany(companyService.getById(vo.getCompanyId()));
            }
        }
        // 更新基础字段
        entity.setType(vo.getType());
        entity.setApplyName(vo.getApplyName());
        entity.setApplyDate(vo.getApplyDate());
        entity.setUpdateTime(vo.getUpdateTime());
        entity.setCreateTime(vo.getCreateTime());
        entity.setIncludeDate(vo.getIncludeDate());
        entity.setBlackReason(vo.getBlackReason());
        entity.setDescription(vo.getDescription());
        entity.setKey(vo.getKey());

    }
}
