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.WcxFileMapper;
import cn.com.countrygarden.bi.main.dao.wcx.WcxInnovationMapper;
import cn.com.countrygarden.bi.main.dao.wcx.WcxInnovationOtherMapper;
import cn.com.countrygarden.bi.main.dto.wcx.WcxInnovationDto;
import cn.com.countrygarden.bi.main.dto.wcx.WcxInnovationOtherDto;
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.WcxInnovationOther;
import cn.com.countrygarden.bi.main.service.wcx.WcxFileService;
import cn.com.countrygarden.bi.main.service.wcx.WcxInnovationOtherService;
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;

/**
 * Created by ADMS on 2018/9/13.
 */
@Service
public class WcxInnovationOtherServiceImpl implements WcxInnovationOtherService{
    @Autowired
    private WcxInnovationOtherMapper wcxInnovationOtherMapper;
    @Autowired
    private WcxInnovationMapper wcxInnovationMapper;
    @Autowired
    private WcxFileService wcxFileService;
    @Autowired
    private WcxInnovationOtherService wcxInnovationOtherService;

    /** 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(WcxInnovationOtherDto dto) {
        int pageOffset = PageUtil.getPageOffset(dto.getPageIndex(), dto.getPageSize());
        dto.setPageOffset(pageOffset);

        List<WcxInnovationOtherDto> data = wcxInnovationOtherMapper.selectPageData(dto);
        if (data != null && data.size() > 0) {
            for (WcxInnovationOtherDto datum : data) {
                List<String> picUrls = wcxFileService.getPicUrls(datum.getInnoId());
                if (picUrls == null || picUrls.size() <=0) {
                    String videoUrl = wcxFileService.getVideoUrl(datum.getInnoId());
                    datum.setVideoUrl(videoUrl);
                } else {
                    datum.setPictureUrls(picUrls);
                }
            }
        }

        Long total = wcxInnovationOtherMapper.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 innoOtherId, String sysUserCode, String sysUserName) {
        WcxInnovationOtherDto innoOther = wcxInnovationOtherMapper.findFirstByPrimaryKey(innoOtherId);
        wcxInnovationOtherMapper.updateStatus(innoOtherId, WcxConstant.DELETE_YES, WcxConstant.STATUS_DISABLE);
        wcxInnovationOtherMapper.updateAudit(innoOtherId, sysUserCode, sysUserName);
        WcxInnovationDto dto = wcxInnovationMapper.findFirstDto(innoOther.getInnoId());
        if (dto == null) {
            WcxInnovation model = new WcxInnovation();
            model.setInnovationId(innoOther.getInnoId());
            model.setInnovationTitle(innoOther.getInnoTitle());
            model.setInnovationContentId(innoOther.getInnoContentId());
            model.setInnovationSimpleContent(innoOther.getInnoSimpleContent());
            model.setInnovationType(innoOther.getInnoType());
            model.setBaseline(innoOther.getInnoBaseline());
            model.setClassification(innoOther.getInnoClassification());
            model.setTeamContent(innoOther.getInnoTeamContent());
            model.setProjectName(innoOther.getInnoProjectName());
            model.setRegionName(innoOther.getInnoRegionName());
            model.setTopEndDate(innoOther.getInnoTopEndDate());
            model.setUserId(innoOther.getInnoUserId());
            model.setStatus(innoOther.getStatus());
            model.setCreateDate(innoOther.getCreateDate());
            wcxInnovationMapper.insertOne(model);
        }
    }

    @Override
    @Transactional
    public void hideReport(String innoOtherId, String sysUserCode, String sysUserName) {
        wcxInnovationOtherMapper.updateStatus(innoOtherId, WcxConstant.DELETE_NO, STATUS_HIDE);
        wcxInnovationOtherMapper.updateAudit(innoOtherId, sysUserCode, sysUserName);
        WcxInnovationOtherDto innoOther = wcxInnovationOtherMapper.findFirstByPrimaryKey(innoOtherId);
        wcxInnovationMapper.deleteByPrimateKey(innoOther.getInnoId());
    }

    @Override
    public WcxInnovationOtherDto findFirst(String innoOtherId) {
        WcxInnovationOtherDto dto = wcxInnovationOtherMapper.findFirstByPrimaryKey(innoOtherId);
        List<String> picUrls = wcxFileService.getPicUrls(dto.getInnoId());
        if (picUrls == null || picUrls.size() <=0) {
            String videoUrl = wcxFileService.getVideoUrl(dto.getInnoId());
            dto.setVideoUrl(videoUrl);
        } else {
            dto.setPictureUrls(picUrls);
        }
        return dto;
    }
}
