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


import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.dao.wcx.WcxNoticeFileMapper;
import cn.com.countrygarden.bi.main.dao.wcx.WcxNoticeMapper;
import cn.com.countrygarden.bi.main.dto.wcx.WcxNoticeDto;
import cn.com.countrygarden.bi.main.dto.wcx.WcxNoticeFile;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.service.wcx.WcxNoticeService;
import cn.com.countrygarden.bi.main.util.PageUtil;
import cn.com.countrygarden.bi.main.util.wcx.CommonUtil;
import com.github.pagehelper.PageRowBounds;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class WcxNoticeServiceImpl implements WcxNoticeService {

    @Autowired
    private WcxNoticeMapper wcxNoticeMapper;

    @Autowired
    private WcxNoticeFileMapper wcxNoticeFileMapper;


    @Override
    public PageData selectPageList(WcxNoticeDto dto) {
        int pageOffset = PageUtil.getPageOffset(dto.getPageIndex(), dto.getPageSize());
        dto.setPageOffset(pageOffset);
        List<WcxNoticeDto> data = wcxNoticeMapper.selectPageListWcxNotice(dto);

        Long total = wcxNoticeMapper.selectIndexPageWcxNoticeCount(dto);

        PageRowBounds pageRowBounds = new PageRowBounds(dto.getPageIndex(), dto.getPageSize());
        pageRowBounds.setTotal(total);
        pageRowBounds.setCount(true);
        if(CollectionUtils.isNotEmpty(data)){
            data.get(0).setRoleCodeAdmin(dto.getRoleCodeAdmin());
        }
        PageData pageData = new PageData(data, pageRowBounds);
        return pageData;
    }

    @Override
    public Result add(WcxNoticeDto wcxNoticeDto, AccessToken accessToken) {
        wcxNoticeDto.setRegionName("总公司品质管理中心");
        wcxNoticeDto.setUserId(accessToken.getUserCode());
        wcxNoticeDto.setOperator(accessToken.getUserCode());
        wcxNoticeDto.setCreateDate(new Date());
        wcxNoticeDto.setStatus("1");
        wcxNoticeDto.setReleaseStatus("0");
        String innId = CommonUtil.getUUID();
        wcxNoticeDto.setNoticeId(innId);
        int index=wcxNoticeMapper.addNotice(wcxNoticeDto);
        if(index>0){
            if(StringUtils.isNotBlank(wcxNoticeDto.getDialogImageUrl())){
               // String[] imageLi=wcxNoticeDto.getDialogImageUrl().split(",");
                    WcxNoticeFile wcxNoticeFile=new WcxNoticeFile();
                    wcxNoticeFile.setFileId(CommonUtil.getUUID());
                    wcxNoticeFile.setNoticeId(wcxNoticeDto.getNoticeId());
                    wcxNoticeFile.setFileUrl(wcxNoticeDto.getDialogImageUrl());
                    wcxNoticeFile.setFileType("1");
                    wcxNoticeFile.setStatus("1");
                    wcxNoticeFile.setCreateDate(new Date());
                    wcxNoticeFileMapper.batchInsert(wcxNoticeFile);

            }
        }
        return Response.response();
    }

    @Override
    public Result release(String noticeId, String releaseStatus, String userName,AccessToken accessToken) {

        WcxNoticeDto wcxNoticeDto=new WcxNoticeDto();
        wcxNoticeDto.setNoticeId(noticeId);
        if(releaseStatus.equals("1")){
            wcxNoticeDto.setReleaseUser(userName);
            wcxNoticeDto.setReleaseTime(new Date());
        }else {
            wcxNoticeDto.setReleaseUser(null);
            wcxNoticeDto.setReleaseTime(null);
        }
        wcxNoticeDto.setReleaseStatus(releaseStatus);
        wcxNoticeDto.setUpdateTime(new Date());
        wcxNoticeMapper.updateReleaseStatus(wcxNoticeDto);
        return Response.response();
    }

    @Override
    public Result del(String noticeId, AccessToken accessToken) {
        WcxNoticeDto wcxNoticeDto=new WcxNoticeDto();
        wcxNoticeDto.setNoticeId(noticeId);
        wcxNoticeDto.setStatus("0");
        wcxNoticeDto.setUpdateTime(new Date());
        wcxNoticeDto.setUpdateBy(accessToken.getUserCode());
        int index=wcxNoticeMapper.updateDelete(wcxNoticeDto);
        if(index>0){
            wcxNoticeFileMapper.updateDelete(noticeId);
        }
        return Response.response();
    }

    @Override
    public Result check(String noticeId, AccessToken accessToken) {
        return Response.response(wcxNoticeMapper.check(noticeId));
    }

    @Override
    public Result edit(WcxNoticeDto wcxNoticeDto, AccessToken accessToken) {
        wcxNoticeDto.setUpdateTime(new Date());
        wcxNoticeDto.setUpdateBy(accessToken.getUserCode());
        int  index= wcxNoticeMapper.update(wcxNoticeDto);
        if(index>0){
            if(StringUtils.isNotBlank(wcxNoticeDto.getDialogImageUrl())){
                WcxNoticeFile wcxNoticeFile=new WcxNoticeFile();
                wcxNoticeFile.setNoticeId(wcxNoticeDto.getNoticeId());
                wcxNoticeFile.setFileUrl(wcxNoticeDto.getDialogImageUrl());
                wcxNoticeFileMapper.update(wcxNoticeFile);
            }
        }
        return Response.response();
    }


}
