package com.ff.weijinxiao.service;


import cn.hutool.core.util.StrUtil;
import com.ff.weijinxiao.domain.UserInfo;
import com.ff.weijinxiao.repository.UserInfoRepository;
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 UserInfoService {

    @Resource
    private UserInfoRepository userInfoRepository;

    /**
     * 创建实体
     *
     * @param userInfo
     */
    public UserInfo saveUserInfo(UserInfo userInfo) {
        return userInfoRepository.save(userInfo);
    }

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

    public UserInfo findByUid(String uid) {
        return userInfoRepository.findByUid(uid);
    }

    public UserInfo findByPhone(String phone) {
        return userInfoRepository.findByPhone(phone);
    }

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

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

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


    public List<Object[]> getSupplierReport(String uid, String beginTime, String endTime) {
        return userInfoRepository.getSupplierReport(uid, beginTime, endTime);
    }

    public List<Object[]> getProductReport(String uid, String beginTime, String endTime) {
        return userInfoRepository.getProductReport(uid, beginTime, endTime);
    }

    public List<Object[]> getCustomerReport(String uid, String beginTime, String endTime) {
        return userInfoRepository.getCustomerReport(uid, beginTime, endTime);
    }

    public List<Object[]> getBusinessReport1(String uid, String goodsName, String beginTime, String endTime) {
        if (StrUtil.isNotBlank(goodsName)) {
            return userInfoRepository.getBusinessReport1WithName(uid, goodsName, beginTime, endTime);
        } else {
            return userInfoRepository.getBusinessReport1(uid, beginTime, endTime);
        }
    }

    public List<Object[]> getBusinessReport2(String uid, String goodsName, String beginTime, String endTime) {
        if (StrUtil.isNotBlank(goodsName)) {
            return userInfoRepository.getBusinessReport2WithName(uid, goodsName, beginTime, endTime);
        } else {
            return userInfoRepository.getBusinessReport2(uid, beginTime, endTime);
        }
    }

    public List<Object[]> getBusinessReport3(String uid, String beginTime, String endTime) {
        return userInfoRepository.getBusinessReport3(uid, beginTime, endTime);
    }
}
