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.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ecep.contract.QueryService;
import com.ecep.contract.ds.company.repository.CompanyInvoiceInfoRepository;
import com.ecep.contract.model.Company;
import com.ecep.contract.model.CompanyInvoiceInfo;
import com.ecep.contract.util.SpecificationUtils;
import com.fasterxml.jackson.databind.JsonNode;

/**
 * 公司发票信息服务
 */
@Lazy
@Service
@CacheConfig(cacheNames = "company-invoice-info")
public class CompanyInvoiceInfoService implements QueryService<CompanyInvoiceInfo> {
    private static final Logger logger = LoggerFactory.getLogger(CompanyInvoiceInfoService.class);
    @Lazy
    @Autowired
    private CompanyInvoiceInfoRepository repository;

    @Cacheable(key = "#p0")
    public CompanyInvoiceInfo findById(int id) {
        return repository.findById(id).orElse(null);
    }

    public List<CompanyInvoiceInfo> searchByCompany(Company company, String searchText) {
        Specification<CompanyInvoiceInfo> spec = (root, query, builder) -> {
            return builder.or(
                    builder.like(root.get("name"), "%" + searchText + "%"),
                    builder.like(root.get("taxId"), "%" + searchText + "%"),
                    builder.like(root.get("address"), "%" + searchText + "%"),
                    builder.like(root.get("phone"), "%" + searchText + "%"),
                    builder.like(root.get("bankName"), "%" + searchText + "%"),
                    builder.like(root.get("bankAccount"), "%" + searchText + "%"));
        };

        if (company != null) {
            spec = spec.and((root, query, builder) -> {
                return builder.equal(root.get("company"), company);
            });
        }
        return repository.findAll(spec, Pageable.ofSize(10)).getContent();
    }

    public Specification<CompanyInvoiceInfo> getSpecification(String searchText) {
        if (!StringUtils.hasText(searchText)) {
            return null;
        }
        return (root, query, builder) -> {
            return builder.or(
                    builder.like(root.get("name"), "%" + searchText + "%"),
                    builder.like(root.get("taxId"), "%" + searchText + "%"),
                    builder.like(root.get("address"), "%" + searchText + "%"),
                    builder.like(root.get("phone"), "%" + searchText + "%"),
                    builder.like(root.get("bankName"), "%" + searchText + "%"),
                    builder.like(root.get("bankAccount"), "%" + searchText + "%"));
        };
    }

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

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