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

import cn.com.countrygarden.bi.main.constant.wcx.WcxConstant;
import cn.com.countrygarden.bi.main.dao.wcx.WcxReviewMapper;
import cn.com.countrygarden.bi.main.dao.wcx.WcxReviewOtherMapper;
import cn.com.countrygarden.bi.main.dto.wcx.WcxInnovationDto;
import cn.com.countrygarden.bi.main.dto.wcx.WcxInnovationOtherDto;
import cn.com.countrygarden.bi.main.dto.wcx.WcxReviewDto;
import cn.com.countrygarden.bi.main.dto.wcx.WcxReviewOtherDto;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.wcx.WcxInnovation;
import cn.com.countrygarden.bi.main.model.wcx.WcxReview;
import cn.com.countrygarden.bi.main.model.wcx.WcxReviewOther;
import cn.com.countrygarden.bi.main.service.wcx.WcxInnovationService;
import cn.com.countrygarden.bi.main.service.wcx.WcxReviewOtherService;
import cn.com.countrygarden.bi.main.util.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.List;

@Service
public class WcxReviewOtherServiceImpl implements WcxReviewOtherService {
    @Autowired
    private WcxReviewOtherMapper wcxReviewOtherMapper;
    @Autowired
    private WcxReviewMapper wcxReviewMapper;
    @Autowired
    private WcxInnovationService wcxInnovationService;

    /** 0:撤回 **/
    private static final String STATUS_CANCEL = "0";
    /** 1:未屏蔽 **/
    private static final String STATUS_SHOW = "1";
    /** 2:已屏蔽 **/
    private static final String STATUS_HIDE = "2";

    @Override
    public PageData selectPageList(WcxReviewOtherDto dto) {
        int pageOffset = PageUtil.getPageOffset(dto.getPageIndex(), dto.getPageSize());
        dto.setPageOffset(pageOffset);

        List<WcxReviewOtherDto> data = wcxReviewOtherMapper.selectPageData(dto);
        if (data != null && data.size() > 0) {
            for (WcxReviewOtherDto datum : data) {
                WcxInnovationDto inno = wcxInnovationService.select(datum.getReviewInnovationId());
                datum.setWcxInnovationDto(inno);
            }
        }

        Long total = wcxReviewOtherMapper.selectPageCount(dto);

        PageRowBounds pageRowBounds = new PageRowBounds(dto.getPageIndex(), dto.getPageSize());
        pageRowBounds.setTotal(total);
        pageRowBounds.setCount(true);

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

    @Override
    @Transactional
    public void cancelReport(String reviewOtherId, String sysUserCode, String sysUserName) {
        WcxReviewOtherDto reviewOther = wcxReviewOtherMapper.findFirstByPrimaryKey(reviewOtherId);
        wcxReviewOtherMapper.updateStatus(reviewOtherId, WcxConstant.DELETE_YES, WcxConstant.STATUS_DISABLE);
        wcxReviewOtherMapper.updateAudit(reviewOtherId, sysUserCode, sysUserName);
        WcxReviewDto review = wcxReviewMapper.findFirstDto(reviewOther.getReviewId());
        if (review == null) {
            WcxReview wcxReview = new WcxReview(
                    reviewOther.getReviewId(),
                    reviewOther.getReviewContent(),
                    reviewOther.getReviewUserId(),
                    reviewOther.getReviewInnovationId(),
                    WcxConstant.STATUS_ENABLE,
                    reviewOther.getReviewCreateDate());
            wcxReviewMapper.insert(wcxReview);
        }
    }

    @Override
    @Transactional
    public void hideReport(String reviewOtherId, String sysUserCode, String sysUserName) {
        wcxReviewOtherMapper.updateStatus(reviewOtherId, WcxConstant.DELETE_NO, STATUS_HIDE);
        wcxReviewOtherMapper.updateAudit(reviewOtherId, sysUserCode, sysUserName);
        WcxReviewOtherDto reviewOther = wcxReviewOtherMapper.findFirstByPrimaryKey(reviewOtherId);
        wcxReviewMapper.deleteByPrimaryKey(reviewOther.getReviewId());
    }

}
