package com.liangjian.dataplatform.member.service.impl;

import com.liangjian.dataplatform.member.dao.elasticsearch.UserPointLogRepository;
import com.liangjian.dataplatform.member.dao.secondary.WeixinUserInfoRepository;
import com.liangjian.dataplatform.member.entity.po.elasticsearch.UserPointLog;
import com.liangjian.dataplatform.member.entity.po.secondary.WeixinUserInfo;
import com.liangjian.dataplatform.member.entity.pojo.BirdGirdFilter;
import com.liangjian.dataplatform.member.entity.vo.request.BirdGirdRequest;
import com.liangjian.dataplatform.member.service.BirdGirdQueryRequest;
import com.liangjian.dataplatform.member.service.ISecretEncodeDecodeService;
import com.liangjian.dataplatform.member.service.IWeixinUserInfoQueryService;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.Predicate;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @Author: 黄子奇
 * @Date: 2019/12/6
 */
@Service
public class WeixinUserInfoQueryService implements IWeixinUserInfoQueryService, BirdGirdQueryRequest<WeixinUserInfo> {
    @Autowired
    private WeixinUserInfoRepository weixinUserInfoRepository;
    @Autowired
    private ISecretEncodeDecodeService iSecretEncodeDecodeService;
    @Autowired
    private UserPointLogRepository userPointLogRepository;

    @Override
    public Page<WeixinUserInfo> queryByCondition(BirdGirdRequest birdGirdRequest) {
        Pageable pageable = PageRequest.of(birdGirdRequest.getPageIndex(),birdGirdRequest.getPageSize(), Sort.Direction.DESC, "id");
        List<BirdGirdFilter> filters = birdGirdRequest.getFilters();
        for (BirdGirdFilter filter : filters) {
            if(filter.getField().contains("Phone")) {
                String secretPhone = filter.getValue(); //其实传上来的是明文
                secretPhone = iSecretEncodeDecodeService.encode(secretPhone);
                filter.setValue(secretPhone);
            }
        }
        birdGirdRequest.setFilters(filters);
        Specification<WeixinUserInfo> specification = (root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> where = buildConditionByFilters(birdGirdRequest,root,criteriaBuilder);
            return criteriaBuilder.and(where.toArray(new Predicate[where.size()]));
        };
        Page<WeixinUserInfo> weixinUserInfos=weixinUserInfoRepository.findAll(specification, pageable);
        for (WeixinUserInfo weixinUserInfo:weixinUserInfos.getContent()){
            String sePhone=iSecretEncodeDecodeService.decode(weixinUserInfo.getSecretPhone());
            weixinUserInfo.setRemark(sePhone.substring(0,3)+"****"+sePhone.substring(7,sePhone.length()));
        }
        return weixinUserInfos;
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public synchronized boolean update(UserPointLog log) {
        log.setId(null);
        log.setCrtDate(new Date());
        log.setLastUpdate(new Date());
        try {
            Optional<WeixinUserInfo> opt = weixinUserInfoRepository.queryBySecretPhone(log.getSecretPhone());
            if(!opt.isPresent()) {
                throw new Exception("can't find user by secretPhone :" + log.getSecretPhone());
            }
            WeixinUserInfo userInfo = opt.get();
            userInfo.setPointUpdateDate(new Date());
            if(log.isIncrease()) {
                userInfo.setPoint(userInfo.getPoint() + log.getValue());
            } else {
                userInfo.setPoint(userInfo.getPoint() - log.getValue());
            }

            weixinUserInfoRepository.save(userInfo);
            userPointLogRepository.save(log);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

}
