package com.ff.weijinxiao.service;


import com.ff.weijinxiao.domain.Customer;
import com.ff.weijinxiao.repository.CustomerRepository;
import com.ff.weijinxiao.util.SpecificationUtil;
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 javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class CustomerService {

    @Resource
    private CustomerRepository customerRepository;

    /**
     * 创建实体
     *
     * @param customer
     */
    public Customer saveCustomer(Customer customer) {
        return customerRepository.save(customer);
    }

    /**
     * 查找实体
     *
     * @param id
     * @return
     */
    public Customer findById(Integer id) {
        Optional<Customer> optional = customerRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        } else {
            return null;
        }
    }

    public Customer findByOnlyID(String onlyID) {
        return customerRepository.findByOnlyID(onlyID);
    }

    public void deleteById(Integer id) {
        customerRepository.deleteById(id);
    }

    /**
     * 分页条件查询
     *
     * @param page
     * @param size
     * @param param
     * @return
     */
    public Page<Customer> findPage(int page, int size, Map<String, Object> orParam, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "id";
        Pageable pageable = new PageRequest(page - 1, size, direction, sort);
        return customerRepository.findAll(SpecificationUtil.buildSpecification(param, orParam, Customer.class), pageable);
    }

    /**
     * 根据条件查询List
     */
    public List<Customer> findList(Map<String, Object> param) {
        return customerRepository.findAll(SpecificationUtil.buildSpecification(param, Customer.class));
    }

    public Double getCustomerTotalAmount(String uid) {
        return customerRepository.getCustomerTotalAmount(uid);
    }

    public Double getCustomerTotalAmountByCustomerOnlyId(String uid, String customerOnlyId) {
        return customerRepository.getCustomerTotalAmountByCustomerOnlyId(uid, customerOnlyId);
    }

    public Double getCustomerAleadyPayed1(String uid) {
        return customerRepository.getCustomerAleadyPayed1(uid);
    }

    public Double getCustomerAleadyPayed1ByCostomerOnlyId(String uid, String customerOnlyId) {
        return customerRepository.getCustomerAleadyPayed1ByCostomerOnlyId(uid, customerOnlyId);
    }

    public Double getCustomerAleadyPayed2(String uid) {
        return customerRepository.getCustomerAleadyPayed2(uid);
    }

    public Double getCustomerAleadyPayed2ByCostomerOnlyId(String uid, String customerOnlyId) {
        return customerRepository.getCustomerAleadyPayed2ByCostomerOnlyId(uid, customerOnlyId);
    }

    public Integer countCustomerBuyedGoods(String uid, String customerOnlyId) {
        return customerRepository.countCustomerBuyedGoods(uid, customerOnlyId);
    }
}
