package com.njcn.csdevice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Objects;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.csdevice.constant.DataParam;
import com.njcn.csdevice.enums.AlgorithmResponseEnum;
import com.njcn.csdevice.mapper.AppLineTopologyDiagramMapper;
import com.njcn.csdevice.mapper.AppProjectMapper;
import com.njcn.csdevice.mapper.AppTopologyDiagramMapper;
import com.njcn.csdevice.pojo.param.AppTopologyDiagramAddParm;
import com.njcn.csdevice.pojo.param.AppTopologyDiagramAuditParm;
import com.njcn.csdevice.pojo.param.AppTopologyDiagramQueryPageParm;
import com.njcn.csdevice.pojo.param.AppTopologyDiagramQueryParm;
import com.njcn.csdevice.pojo.po.AppLineTopologyDiagramPO;
import com.njcn.csdevice.pojo.po.AppProjectPO;
import com.njcn.csdevice.pojo.po.AppTopologyDiagramPO;
import com.njcn.csdevice.pojo.vo.AppTopologyDiagramVO;
import com.njcn.csdevice.pojo.vo.CsLineTopologyTemplateVO;
import com.njcn.csdevice.service.AppTopologyDiagramService;
import com.njcn.csdevice.service.CsLineTopologyTemplateService;
import com.njcn.oss.constant.OssPath;
import com.njcn.oss.utils.FileStorageUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 
 * Description:
 * 接口文档访问地址：http://serverIP:port/swagger-ui.html
 * Date: 2023/3/27  10:24【需求编号】d
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class AppTopologyDiagramServiceImpl extends ServiceImpl<AppTopologyDiagramMapper, AppTopologyDiagramPO> implements AppTopologyDiagramService {


    private final FileStorageUtil fileStorageUtil;
    private final AppProjectMapper appServiceMapper;

    private final AppTopologyDiagramMapper appTopologyDiagramMapper;
    private final AppLineTopologyDiagramMapper appLineTopologyDiagramMapper;
    private final CsLineTopologyTemplateService csLineTopologyTemplateService;
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public AppTopologyDiagramVO addAppTopologyDiagram(AppTopologyDiagramAddParm appTopologyDiagramAddParm) {
        AppTopologyDiagramPO appTopologyDiagramPO = new AppTopologyDiagramPO();

            MultipartFile file = appTopologyDiagramAddParm.getFile ( );
            if(file.getSize()> DataParam.FILE_SIZE){
                throw new BusinessException(AlgorithmResponseEnum.FILE_SIZE_ERROR);
            }
            String filePath = fileStorageUtil.uploadMultipart (file, OssPath.TOPOLOGY);
            appTopologyDiagramPO.setFilePath (filePath);
            appTopologyDiagramPO.setProjectId (appTopologyDiagramAddParm.getProjectId ()==null?"":appTopologyDiagramAddParm.getProjectId ());
            appTopologyDiagramPO.setName (appTopologyDiagramAddParm.getTopologyDiagramName ());
            appTopologyDiagramPO.setStatus ("1");
            boolean save = this.save (appTopologyDiagramPO);



        AppTopologyDiagramVO vo = new AppTopologyDiagramVO();
        BeanUtils.copyProperties(appTopologyDiagramPO, vo);
        return vo;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public Boolean AuditAppTopologyDiagram(AppTopologyDiagramAuditParm appTopologyDiagramAuditParm) {
        /*删除拓扑图*/
        if(Objects.equal(appTopologyDiagramAuditParm.getStatus (),"0")){
            /*校验是否有设备使用拓扑图*/
            QueryWrapper<AppLineTopologyDiagramPO> queryWrap = new QueryWrapper<>();
            queryWrap.lambda().eq(AppLineTopologyDiagramPO::getId, appTopologyDiagramAuditParm.getId()).eq(AppLineTopologyDiagramPO::getStatus, "1");
            List<AppLineTopologyDiagramPO> list = appLineTopologyDiagramMapper.selectList(queryWrap);
            if(!CollectionUtils.isEmpty(list)){
                throw new BusinessException(AlgorithmResponseEnum.CHIRLDREN3_EXIST);
            }
        }
        AppTopologyDiagramPO appTopologyDiagramPO = new AppTopologyDiagramPO();
        if(!Objects.equal (appTopologyDiagramAuditParm.getFile (),null )){
            MultipartFile file = appTopologyDiagramAuditParm.getFile ( );
            long size = file.getSize ( );
            String filePath = fileStorageUtil.uploadMultipart (file, OssPath.TOPOLOGY);
            appTopologyDiagramPO.setFilePath (filePath);
        }
        if(appTopologyDiagramAuditParm.getProjectId ()!=null&&appTopologyDiagramAuditParm.getProjectId ()!=""){
            appTopologyDiagramPO.setProjectId (appTopologyDiagramAuditParm.getProjectId ());
        }
        if(appTopologyDiagramAuditParm.getTopologyDiagramName ()!=null&&appTopologyDiagramAuditParm.getTopologyDiagramName ()!=""){
            appTopologyDiagramPO.setName (appTopologyDiagramAuditParm.getTopologyDiagramName ());
        }
        if(appTopologyDiagramAuditParm.getStatus ()!=null&&appTopologyDiagramAuditParm.getStatus ()!=""){
            appTopologyDiagramPO.setStatus (appTopologyDiagramAuditParm.getStatus ());
        }
        UpdateWrapper<AppTopologyDiagramPO> updateWrapper = new UpdateWrapper<> ();
        updateWrapper.eq ("id",appTopologyDiagramAuditParm.getId ());
        boolean update = this.update (appTopologyDiagramPO, updateWrapper);
        return update;
    }

    @Override
    public List<AppTopologyDiagramVO> queryAppTopologyDiagram(AppTopologyDiagramQueryParm appTopologyDiagramAuditParm) {
        QueryWrapper<AppTopologyDiagramPO> queryWrapper = new QueryWrapper<> ();
        queryWrapper.eq ("status","1").
                eq (StringUtils.isNotBlank (appTopologyDiagramAuditParm.getId ()),"id",appTopologyDiagramAuditParm.getId ()).
                eq (StringUtils.isNotBlank (appTopologyDiagramAuditParm.getProjectId ()),"project_id",appTopologyDiagramAuditParm.getProjectId ()).
                like (StringUtils.isNotBlank (appTopologyDiagramAuditParm.getName ()),"name", appTopologyDiagramAuditParm.getName ()).
                orderByDesc("create_time");

        List<AppTopologyDiagramPO> list = this.list (queryWrapper);

        List<AppTopologyDiagramVO> collect = list.stream ( ).map (temp -> {
            AppTopologyDiagramVO vo = new AppTopologyDiagramVO ( );
            BeanUtils.copyProperties (temp, vo);

            AppProjectPO appProjectPO = appServiceMapper.selectById(vo.getProjectId());
            vo.setProjectName(appProjectPO.getName());
            vo.setFilePath (vo.getFilePath ());
            List<CsLineTopologyTemplateVO> csLineTopologyTemplateVOS = csLineTopologyTemplateService.queryByTopoId(temp.getTopoId());
            vo.setCsLineTopologyTemplateVOList(csLineTopologyTemplateVOS);
            return vo;
        }).collect (Collectors.toList ( ));
        return collect;

    }

    @Override
    public IPage<AppTopologyDiagramVO> queryTopologyDiagramPage(AppTopologyDiagramQueryPageParm appTopologyDiagramQueryPageParm) {

        Page<AppTopologyDiagramVO> returnpage = new Page<> (appTopologyDiagramQueryPageParm.getPageNum ( ), appTopologyDiagramQueryPageParm.getPageSize ( ));
        Page<AppTopologyDiagramPO> tempPage = new Page<> (appTopologyDiagramQueryPageParm.getPageNum ( ), appTopologyDiagramQueryPageParm.getPageSize ( ));

        QueryWrapper<AppTopologyDiagramPO> queryWrapper = new QueryWrapper<> ();
        queryWrapper.eq ("status","1").
                eq (StringUtils.isNotBlank (appTopologyDiagramQueryPageParm.getProjectId ()),"project_id",appTopologyDiagramQueryPageParm.getProjectId ()).
                like (StringUtils.isNotBlank (appTopologyDiagramQueryPageParm.getSearchValue ()),"name", appTopologyDiagramQueryPageParm.getSearchValue ()).
                orderByDesc("create_time");

        tempPage = appTopologyDiagramMapper.selectPage (tempPage, queryWrapper);
        List<AppTopologyDiagramVO> collect = tempPage.getRecords ( ).stream ( ).map (temp -> {
            AppTopologyDiagramVO vo = new AppTopologyDiagramVO ( );
            BeanUtils.copyProperties (temp, vo);

            AppProjectPO appProjectPO = appServiceMapper.selectById(vo.getProjectId());
            vo.setProjectName(appProjectPO.getName());
            vo.setFilePath (temp.getFilePath ( ));
           List<CsLineTopologyTemplateVO> csLineTopologyTemplateVOS = csLineTopologyTemplateService.queryByTopoId(temp.getTopoId());
            vo.setCsLineTopologyTemplateVOList(csLineTopologyTemplateVOS);
            return vo;
        }).collect (Collectors.toList ( ));
        returnpage.setRecords (collect);
        returnpage.setTotal(tempPage.getTotal());
        return returnpage;
    }

    @Override
    public Boolean checkCanDelete(String id) {
        /*校验是否有设备使用拓扑图*/
        QueryWrapper<AppLineTopologyDiagramPO> queryWrap = new QueryWrapper<>();
        queryWrap.lambda().eq(AppLineTopologyDiagramPO::getId, id).eq(AppLineTopologyDiagramPO::getStatus, "1");
        List<AppLineTopologyDiagramPO> list = appLineTopologyDiagramMapper.selectList(queryWrap);
        if(!CollectionUtils.isEmpty(list)){
            return false;
        }
        return true;
    }

    @Override
    public AppTopologyDiagramPO getByProjectId(String projectId) {
        LambdaQueryWrapper<AppTopologyDiagramPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppTopologyDiagramPO::getProjectId,projectId).eq(AppTopologyDiagramPO::getStatus,"1");
        return this.getOne(queryWrapper);
    }
}
