package cn.com.countrygarden.bi.main.service.wcx.impl;

import cn.com.countrygarden.bi.main.dao.wcx.WcxSuggestMapper;
import cn.com.countrygarden.bi.main.dto.wcx.WcxQueryReplyDto;
import cn.com.countrygarden.bi.main.dto.wcx.WcxQuerySuggestDto;
import cn.com.countrygarden.bi.main.dto.wcx.WcxSuggestDto;
import cn.com.countrygarden.bi.main.dto.wcx.WcxSuggestReplyDto;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.service.wcx.WcxSuggestService;
import cn.com.countrygarden.bi.main.util.wcx.PageUtil;
import com.github.pagehelper.PageRowBounds;
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;

/**
 * @author feng
 */
@Service
public class WcxSuggestServiceImpl implements WcxSuggestService {
    @Autowired
    WcxSuggestMapper wcxSuggestMapper;

    /**
     * 模糊最新查询
     * @param dto
     * @return
     */
    @Override
    public PageData findSuggestByTime(WcxSuggestDto dto) {
        int pageOffset = PageUtil.getPageOffset(dto.getPageIndex(), dto.getPageSize());
        dto.setPageOffset(pageOffset);
        List<WcxQuerySuggestDto> data = wcxSuggestMapper.findSuggestByTime(dto);
        Integer total = wcxSuggestMapper.findSuggestByTimeCount(dto);
        PageRowBounds pageRowBounds = new PageRowBounds(dto.getPageIndex(), dto.getPageSize());
        if (total == null){
            total=0;
        }
        pageRowBounds.setTotal(total.longValue());
        pageRowBounds.setCount(true);
        PageData pageData = new PageData(data ,pageRowBounds);
        return pageData;
    }

    /**
     * 模糊热门查询
     * @param dto
     * @return
     */
    @Override
    public PageData findSuggestByReply(WcxSuggestDto dto) {
        int pageOffset = PageUtil.getPageOffset(dto.getPageIndex(), dto.getPageSize());
        dto.setPageOffset(pageOffset);
        List<WcxQuerySuggestDto> data = wcxSuggestMapper.findSuggestByReply(dto);
        Integer total = wcxSuggestMapper.findSuggestByReplyCount(dto);
        PageRowBounds pageRowBounds = new PageRowBounds(dto.getPageIndex(), dto.getPageSize());
        if (total ==null){
            total =0;
        }
        pageRowBounds.setTotal(total.longValue());
        pageRowBounds.setCount(true);

        PageData pageData = new PageData(data ,pageRowBounds);
        return pageData;
    }

    /**
     * 查看提议对应的回复
     * @param suggestId
     * @param replyContent
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageData findSuggestReply(String suggestId, String replyContent, Integer page, Integer size) {
        int pageOffset = PageUtil.getPageOffset(page, size);
        List<WcxQueryReplyDto> data = wcxSuggestMapper.findSuggestReply(suggestId, replyContent, pageOffset, size);
        Integer total = wcxSuggestMapper.findSuggestReplyPageCount(suggestId, replyContent);
        PageRowBounds pageRowBounds = new PageRowBounds(page, size);
        if (total == null){
            total=0;
        }
        pageRowBounds.setTotal(total.longValue());
        pageRowBounds.setCount(true);

        PageData pageData = new PageData(data, pageRowBounds);
        return pageData;
    }

    @Override
    @Transactional
    public void deleteMySuggest(String suggestId ,String sysUserCode) {
        try{
            WcxSuggestDto wcxSuggestDto = new WcxSuggestDto();
            wcxSuggestDto.setSuggestId(suggestId);
            wcxSuggestDto.setModBy(sysUserCode);
            wcxSuggestDto.setModTime(new Date());
            wcxSuggestMapper.deleteMySuggest(wcxSuggestDto); // 1删除
            wcxSuggestMapper.deleteMySuggestReply(wcxSuggestDto); //1删除
        }catch (Exception e){
            throw  new RuntimeException(e);
        }
    }

    @Override
    public List<WcxQuerySuggestDto> findSuggestByTimeExport(WcxSuggestDto dto) {
       // int pageOffset = PageUtil.getPageOffset(dto.getPageIndex(), dto.getPageSize());
       // dto.setPageOffset(pageOffset);
        List<WcxQuerySuggestDto> data = wcxSuggestMapper.findSuggestByTimeExport(dto);
        return data;
    }

    @Override
    public List<WcxQuerySuggestDto> findSuggestByReplyExport(WcxSuggestDto dto) {
       // int pageOffset = PageUtil.getPageOffset(dto.getPageIndex(), dto.getPageSize());
       // dto.setPageOffset(pageOffset);
        List<WcxQuerySuggestDto> data = wcxSuggestMapper.findSuggestByReplyExport(dto);
        return data;
    }
    
    @Override
    public List<WcxSuggestReplyDto> getSuggestReply(List<WcxQuerySuggestDto> data){
    	List<String> suggestIds = new ArrayList<String>();
    	for (WcxQuerySuggestDto  suggestDto : data) {
    		suggestIds.add(suggestDto.getSuggestId());
		}
    	List<WcxSuggestReplyDto> replyDtos = null;
    	if(suggestIds.size() > 0) {
        	 replyDtos = wcxSuggestMapper.findSuggestReplyById(suggestIds);
    	}
        return replyDtos;
    }
}
