package com.ssj.service.conch.game.service.impl;

import com.ssj.bean.sys.game.domain.Ranklist;
import com.ssj.bean.weixin.user.domain.User;
import com.ssj.dao.sys.game.dao.RanklistDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.persistence.DynamicSpecifications;
import com.ssj.framework.core.persistence.SearchFilter;
import com.ssj.framework.core.util.ResponseEntity;
import com.ssj.framework.core.util.StringUtil;
import com.ssj.service.conch.game.dto.RanklistDto;
import com.ssj.service.conch.game.request.RanklistReq;
import com.ssj.service.conch.game.service.RanklistService;
import com.ssj.service.weixin.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author Mount Liu
 * @date 2018/1/22
 */
@Service
@Transactional
public class RanklistServiceImpl extends BaseServiceImpl<Ranklist, String> implements RanklistService {

    @Autowired
    private RanklistDao dao;

    @Autowired
    private UserService userService;

    @Override
    public PagingAndSortingRepository<Ranklist, String> getDao() {
        return dao;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<Ranklist> findByPage(Map<String, Object> params, Pageable sp) {
        Map<String, SearchFilter> filters = SearchFilter.parse(params);
        Specification<Ranklist> specification = DynamicSpecifications.bySearchFilter(filters.values(), Ranklist.class);
        return dao.findAll(specification, sp);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public ResponseEntity rankingList(RanklistReq req, Pageable pageable) {
        ResponseEntity responseEntity = new ResponseEntity();
        Map<String, Object> data = new HashMap<>();
        Map<String, Object> params = new HashMap<>(16);
        List<RanklistDto> list = new ArrayList<>();
        if (StringUtil.isNotBlank(req.getMaxCreateTime())) {
            params.put("LTE_createTime", req.getMaxCreateTime());
        }
        Page<Ranklist> page = this.findByPage(params, pageable);
        for (Ranklist ranklist :page.getContent()){
            RanklistDto  dto = new RanklistDto();
            dto.setName(ranklist.getName());
            dto.setPhoto(ranklist.getPhoto());
            dto.setScore(ranklist.getScore());
            list.add(dto);
        }
        data.put("pages", page.getTotalElements());
        data.put("pageSize", req.getPageSize());
        data.put("pageNo", req.getPageNo());
        data.put("list", list);
        responseEntity.success(data, "成功");
        return responseEntity;
    }

    /**
     *
     * @param userId
     * @param score
     * @return
     */
    @Override
    public ResponseEntity saveResult(String userId, double score){
        ResponseEntity responseEntity = new ResponseEntity();
        Ranklist ranklist = dao.findByUserId(userId);
        if (ranklist == null) {
            User user = userService.getById(userId);
            Ranklist newRanklist = new Ranklist();
            newRanklist.setUserId(userId);
            newRanklist.setName(user.getName());
            newRanklist.setPhoto(user.getPhoto());
            newRanklist.setScore(score);
            newRanklist.setCreateTime(new Date());
            this.save(newRanklist);
        }
        responseEntity.success("成功");
        return responseEntity;
    }

    @Override
    public Ranklist findByUserId(String userId) {
        return dao.findByUserId(userId);
    }
}
