package com.sooka.module.web.cms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sooka.common.utils.JsonUtil;
import com.sooka.module.web.cms.service.VoteService;
import com.sooka.module.web.cms.vo.VoteVo;
import com.sooka.mybatis.mapper.TCmsVoteMapper;
import com.sooka.mybatis.model.TCmsVote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class VoteServiceImpl implements VoteService {

    @Autowired
    private TCmsVoteMapper cmsVoteMapper;

    //@CacheEvict(value = "cms-vote-cache",allEntries = true,beforeInvocation = true)
    @Transactional(transactionManager = "masterTransactionManager",rollbackFor = Exception.class)
    @Override
    public String save(VoteVo vote) {
        vote.setContent(vote.getContent());
        vote.setFlag(0);
        vote.setNumber("0");
        vote.setCreatetime(new Date());
        vote.setPublisher("admin");
        vote.setXh(vote.getXh());
        if(cmsVoteMapper.saveTopic(vote)>0){
            return JsonUtil.toSUCCESS("保存成功！", "vote-tab", true);
        }
        return JsonUtil.toERROR("保存失败！");
    }

//保存投票选项

    @Override
    public String save(TCmsVote pojo) {
        return null;
    }

    //@CacheEvict(value = "cms-vote-cache",allEntries = true)
    @Transactional(transactionManager = "masterTransactionManager",rollbackFor = Exception.class)
    @Override
    public String update(VoteVo vote) {
        vote.setContent(vote.getContent());
        vote.setFlag(0);
        vote.setNumber("0");
        vote.setCreatetime(new Date());
        vote.setPublisher("admin");
        vote.setXh(vote.getXh());
        if(cmsVoteMapper.updateByPrimaryKey(vote)>0){
            return JsonUtil.toSUCCESS("修改成功！","vote-tab",false);
        }
        return JsonUtil.toERROR("修改失败！");
    }

    @Override
    public String update(TCmsVote vote) {
        return null;
    }

    @Override
    public VoteVo findVoById(Integer id) {
        return null;
    }

    @Transactional(transactionManager = "masterTransactionManager",rollbackFor = Exception.class)
    @Override
    public String saveOp(List<String> options, Integer flag,Integer xh) {

        List<TCmsVote> opList = new ArrayList<>();
        for(String ops :options){
            TCmsVote vote = new TCmsVote();
            vote.setContent(ops);
            vote.setFlag(flag);
            vote.setNumber("0");
            vote.setCreatetime(new Date());
            vote.setPublisher("admin");
            vote.setXh(xh);
            opList.add(vote);
        }
        if(cmsVoteMapper.saveOption(opList)>0){
            return JsonUtil.toSUCCESS("保存成功！","vote-tab",false);
        }
        return JsonUtil.toERROR("保存失败！");
    }

    @Override
    public String delete(Integer[] ids) {
        return null;
    }

    @Override
    public String delete(Integer vote_id) {
        if(vote_id!=null){
            cmsVoteMapper.deleteOption(vote_id);
            deleteTopic(vote_id);
            return JsonUtil.toSUCCESS("删除成功！","vote-tab",false);
        }
        return JsonUtil.toERROR("删除失败！");
    }

    @Override
    public void deleteOp(Integer vote_id) {
       cmsVoteMapper.deleteOption(vote_id);
    }

    @Override
    public TCmsVote findTopicByOpId(Integer vote_id) {
        return cmsVoteMapper.findTopicByOpId(vote_id);
    }

    @Override
    public String vote(Integer vote_id) {
        if(cmsVoteMapper.vote(vote_id)>0){
            return JsonUtil.toSUCCESS("投票成功！");
        }
        return JsonUtil.toERROR("投票失败！");
    }

    @Override
    public List<TCmsVote> findTO() {
        return cmsVoteMapper.findTO();
    }

    private String deleteTopic(Integer vote_id) {
        if(vote_id!=null){
            cmsVoteMapper.deleteTopic(vote_id);
            return JsonUtil.toSUCCESS("删除成功！","vote-tab",false);
        }
        return JsonUtil.toERROR("删除失败！");
}

    @Override
    public List<TCmsVote> findOption(Integer vote_id) {
        return cmsVoteMapper.findOption(vote_id);
    }

    @Override
    public List<TCmsVote> findAll() {
        return cmsVoteMapper.selectAll();
    }

    @Override
    public PageInfo<TCmsVote> page(Integer pageNumber, Integer pageSize, TCmsVote pojo) {
        PageHelper.startPage(pageNumber,pageSize);
        return new PageInfo<>(findList(pojo));
    }

    @Override
    public PageInfo<TCmsVote> page(Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        return new PageInfo<>(findAll());
    }

    @Override
    public TCmsVote findById(Integer vote_id) {
        return cmsVoteMapper.selectByPrimaryKey(vote_id);
    }

    @Override
    public List<TCmsVote> findList(TCmsVote pojo) {
        return cmsVoteMapper.select(pojo);
    }
}
