package com.ff.weijinxiao.service;


import cn.hutool.core.util.StrUtil;
import com.ff.weijinxiao.domain.AccountBook;
import com.ff.weijinxiao.domain.UserInfo;
import com.ff.weijinxiao.repository.AccountBookRepository;
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.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class AccountBookService {

    @Resource
    private AccountBookRepository accountBookRepository;

    @Resource
    private UserInfoRepository userInfoRepository;


    /**
     * 创建实体
     *
     * @param accountBook
     */
    public AccountBook saveAccountBook(AccountBook accountBook) {
        return accountBookRepository.save(accountBook);
    }

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

    public AccountBook findByOpenId(String openId) {
        Optional<AccountBook> optional = accountBookRepository.findOne(new Specification<AccountBook>() {
            @Override
            public Predicate toPredicate(Root<AccountBook> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate p = criteriaBuilder.and(criteriaBuilder.equal(root.get("openId").as(String.class), openId));
                return p;
            }
        });
        if (optional.isPresent()) {
            return optional.get();
        } else {
            return null;
        }
    }

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

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

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


    public AccountBook getDefaultAccountBook(UserInfo userInfo) {
        if (null == userInfo.getAccountBookId()) {
            Map<String, Object> param = new HashMap<>();
            param.put("EQ_isDelete", 0);
            param.put("EQ_uid", userInfo.getUid());
            List<AccountBook> list = findList(param);
            if (null != list) {
                userInfo.setAccountBookId(list.get(0).getId());
                userInfoRepository.save(userInfo);
            }
        }
        return findById(userInfo.getAccountBookId());
    }

    public List<AccountBook> getMyAccountBooks(String uid, String name) {
        if (StrUtil.isNotBlank(name)) {
            return accountBookRepository.searchAccountBooks(uid, name);
        } else {
            return accountBookRepository.searchAccountBooks(uid);
        }
    }
}
