package com.hssx.pcbms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hssx.pcbms.entity.Idea;
import com.hssx.pcbms.entity.IdeaComment;
import com.hssx.pcbms.entity.Score;
import com.hssx.pcbms.entity.vo.IdeaVO;
import com.hssx.pcbms.mapper.IdeaCommentMapper;
import com.hssx.pcbms.mapper.IdeaMapper;
import com.hssx.pcbms.mapper.ScoreMapper;
import com.hssx.pcbms.service.IdeaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hssx.pcbms.util.HttpRespMsg;
import com.hssx.pcbms.util.PageUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 吴涛涛
 * @since 2019-11-02
 */
@Service
public class IdeaServiceImpl extends ServiceImpl<IdeaMapper, Idea> implements IdeaService {
    @Resource
    private IdeaMapper ideaMapper;
    @Resource
    private IdeaCommentMapper ideaCommentMapper;
    @Resource
    private ScoreMapper scoreMapper;

    @Override
    public HttpRespMsg add(Idea idea) {
        HttpRespMsg msg = new HttpRespMsg();
        if (null == idea.getId()) {
            if (1 == idea.getIsDraft()) {
                //只能保留一条草稿
                Idea oldIdea = ideaMapper.selectOne(new QueryWrapper<Idea>().eq("uid", idea.getUid()).eq("is_draft", 1).last("limit 1"));
                if (oldIdea == null) {
                    ideaMapper.insert(idea);
                }
            } else {
                ideaMapper.insert(idea);
            }
        } else {
            Idea oldIdea = ideaMapper.selectById(idea.getId());
            if (oldIdea.getScore() == 0.0) {
                ideaMapper.updateById(idea);
            } else {
                msg.setError("当前建议已被评分，不可进行修改");
            }
        }
        return msg;
    }

    @Override
    public HttpRespMsg del(Idea idea) {
        HttpRespMsg msg = new HttpRespMsg();
        Idea oldIdea = ideaMapper.selectById(idea.getId());
        if (null == oldIdea.getScore()) {
            ideaMapper.deleteById(idea.getId());
            //删除该建议的评论
            ideaCommentMapper.delete(new QueryWrapper<IdeaComment>().eq("idea_id", idea.getId()));
        } else {
            msg.setError("当前建议已被评分，不可进行删除");
        }
        return msg;
    }

    @Override
    public HttpRespMsg ideaList(Idea idea, PageUtil page) {
        HttpRespMsg msg = new HttpRespMsg();
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<IdeaVO> list = ideaMapper.getIdeaListByUid(idea, null);
        PageInfo<IdeaVO> info = new PageInfo<>(list);
        msg.data = info;
        return msg;
    }

    @Override
    public HttpRespMsg ideaListByTime(Idea idea, String time, String scoreId) {
        HttpRespMsg msg = new HttpRespMsg();
        List<IdeaVO> list = ideaMapper.getIdeaListByUid(idea, time);
        Score score = scoreMapper.selectOne(new QueryWrapper<Score>().eq("score_id", scoreId));
        double sum = 0.0;
        if (list.size() > 0) {
            sum = list.stream().mapToDouble(IdeaVO::getScore).sum();
        }
        score.setTotal(score.getPersonalScore() + sum);
        scoreMapper.update(score, new QueryWrapper<Score>().eq("score_id", scoreId));
        msg.data = list;
        return msg;
    }

    @Override
    public HttpRespMsg getAdviceScore(IdeaVO ideaVO) {
        HttpRespMsg msg = new HttpRespMsg();
        List<Idea> list = ideaMapper.getIdeaScoreLists(ideaVO);
        double sum = 0.0;
        if (list.size() > 0) {
            sum = list.stream().mapToDouble(Idea::getScore).sum();
        }
        msg.data = sum;
        return msg;
    }

    @Override
    public HttpRespMsg getDraft(Idea idea) {
        HttpRespMsg msg = new HttpRespMsg();
        Idea draftidea = ideaMapper.selectOne(new QueryWrapper<Idea>().eq("uid", idea.getUid()).eq("is_draft", 1).last("limit 1"));
        if (draftidea != null) {
            msg.data = draftidea;
        } else {
            msg.data = idea;
        }
        return msg;
    }
}
