package com.ecep.contract.service;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.ecep.contract.util.ParamUtils;
import com.ecep.contract.vm.ProjectCostViewModel;
import com.ecep.contract.vo.ProjectCostVo;
import com.ecep.contract.vo.ProjectVo;

@Service
public class ProjectCostService extends QueryService<ProjectCostVo, ProjectCostViewModel> {

    public ProjectCostVo findAutoCostByProject(ProjectVo project) {
        Page<ProjectCostVo> page = findAll(ParamUtils.builder().equals("project", project.getId()).build(),
                Pageable.unpaged());
        if (page.isEmpty()) {
            return null;
        }
        return page.getContent().getFirst();
    }

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

    public ProjectCostVo findLatestByProject(ProjectVo project) {
        PageRequest pageRequest = PageRequest.of(0, 1, Sort.Direction.DESC, "version");
        Page<ProjectCostVo> page = findAll(ParamUtils.builder().equals("project", project.getId()).build(),
                pageRequest);
        if (page.isEmpty()) {
            return null;
        }
        return page.getContent().getFirst();
    }

    public List<ProjectCostVo> findAllByProject(ProjectVo project) {
        return findAll(ParamUtils.builder().equals("project", project.getId()).build(), Pageable.unpaged())
                .getContent();
    }

}
