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

import com.ecep.contract.ds.customer.model.SalesOrder;
import com.ecep.contract.vo.ContractVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.ecep.contract.EntityService;
import com.ecep.contract.IEntityService;
import com.ecep.contract.QueryService;
import com.ecep.contract.SpringApp;
import com.ecep.contract.ds.contract.repository.SalesOrderItemRepository;
import com.ecep.contract.ds.customer.model.SalesOrderItem;
import com.ecep.contract.service.ServiceException;
import com.ecep.contract.service.VoableService;
import com.ecep.contract.util.SpecificationUtils;
import com.ecep.contract.vo.SalesOrderItemVo;
import com.fasterxml.jackson.databind.JsonNode;

import java.util.List;

@Lazy
@Service
@CacheConfig(cacheNames = "contract-sale-order-item")
public class SalesOrderItemService extends EntityService<SalesOrderItem, SalesOrderItemVo, Integer>
        implements IEntityService<SalesOrderItem>, QueryService<SalesOrderItemVo>,
        VoableService<SalesOrderItem, SalesOrderItemVo> {
    @Lazy
    @Autowired
    private SalesOrderItemRepository repository;

    @Override
    public SalesOrderItemRepository getRepository() {
        return repository;
    }

    @Override
    public SalesOrderItem createNewEntity() {
        return new SalesOrderItem();
    }

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

    @Override
    protected Specification<SalesOrderItem> buildSearchSpecification(String searchText) {
        return (root, query, builder) -> {
            return builder.or(
                    builder.like(root.get("code"), "%" + searchText + "%"),
                    builder.like(root.get("name"), "%" + searchText + "%"),
                    builder.like(root.get("description"), "%" + searchText + "%"));
        };
    }

    @Override
    protected Specification<SalesOrderItem> buildParameterSpecification(JsonNode paramsNode) {
        Specification<SalesOrderItem> spec = null;
        spec = SpecificationUtils.andFieldEqualParam(spec, paramsNode, "code");
        spec = SpecificationUtils.andParam(spec, paramsNode, "order");
        return spec;
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
    })
    @Override
    public void delete(SalesOrderItem entity) {
        repository.delete(entity);
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
    })
    @Override
    public SalesOrderItem save(SalesOrderItem entity) {
        return repository.save(entity);
    }

    @Override
    public void updateByVo(SalesOrderItem model, SalesOrderItemVo vo) {
        if (model == null) {
            throw new ServiceException("实体对象不能为空");
        }
        if (vo == null) {
            throw new ServiceException("VO对象不能为空");
        }

        model.setCode(vo.getCode());
        model.setName(vo.getName());
        model.setQuantity(vo.getQuantity());
        model.setPrice(vo.getPrice());
        model.setTaxRate(vo.getTaxRate());
        model.setExclusiveTaxPrice(vo.getExclusiveTaxPrice());

        model.setStartDate(vo.getStartDate());
        model.setEndDate(vo.getEndDate());

        model.setDescription(vo.getDescription());

        if (vo.getOrderId() == null) {
            model.setOrder(null);
        } else {
            model.setOrder(SpringApp.getBean(SaleOrdersService.class).getById(vo.getOrderId()));
        }
    }

    public List<SalesOrderItem> findAllByContract(ContractVo contract) {
        return repository.findAll((root, q, cb) -> {
            return cb.equal(root.get("order").get("contract").get("id"), contract.getId());
        }, Sort.unsorted());
    }

    public List<SalesOrderItem> findAllBySaleOrder(SalesOrder order) {
        return repository.findAllByOrder(order);
    }
}
