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

import java.util.Comparator;
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.util.StringUtils;

import com.ecep.contract.IEntityService;
import com.ecep.contract.ds.project.repository.ProjectCostRepository;
import com.ecep.contract.model.Contract;
import com.ecep.contract.model.Project;
import com.ecep.contract.model.ProjectCost;

@Lazy
@Service
public class ProjectCostService implements IEntityService<ProjectCost> {
    private static final Logger logger = LoggerFactory.getLogger(ProjectCostService.class);

    @Lazy
    @Autowired
    private ProjectCostRepository repository;


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

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

    public ProjectCost findByContract(Contract contract) {
        return repository.findByContract(contract).orElse(null);
    }

    public ProjectCost findLatestVersionByProject(Project project) {
        List<ProjectCost> list = repository.findAllByProject(project);
        if (list.isEmpty()) {
            return null;
        }
        return list.stream().max(Comparator.comparingInt(ProjectCost::getVersion)).orElse(null);
    }

    /**
     * 在项目下查找最新的项目成本
     */
    public ProjectCost findLatestByProject(Project project) {
        return repository.findTopOneByProjectAndVersionGreaterThanOrderByVersionDesc(project, 0).orElse(null);
    }

    /**
     * 在项目下查找V0版本的项目成本
     */
    public ProjectCost findAutoCostByProject(Project project) {
        return repository.findOneByProjectAndVersion(project, 0).orElse(null);
    }

    public List<ProjectCost> findAllByProject(Project project) {
        return repository.findAllByProject(project);
    }


    public ProjectCost save(ProjectCost cost) {
        return repository.save(cost);
    }

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

    public void delete(ProjectCost cost) {
        repository.delete(cost);
    }

    public ProjectCost newInstanceByProject(Project project) {
        ProjectCost cost = new ProjectCost();
        cost.setProject(project);
        // 0.3‰：购销合同、建筑安装工程承包合同、技术合同
        cost.setStampTax(0.03f);
        cost.setTaxAndSurcharges(11f);
        return cost;
    }

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