package com.njcn.csdevice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.csdevice.mapper.CsLineTopologyTemplateMapper;
import com.njcn.csdevice.pojo.param.CsLineTopologyTemplateParm;
import com.njcn.csdevice.pojo.po.CsLedger;
import com.njcn.csdevice.pojo.po.CsLineTopologyTemplate;
import com.njcn.csdevice.pojo.vo.CsLineTopologyTemplateVO;
import com.njcn.csdevice.service.CsLineTopologyTemplateService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 
 * Description:
 * Date: 2023/7/10  18:30【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
public class CsLineTopologyTemplateServiceImpl extends ServiceImpl<CsLineTopologyTemplateMapper, CsLineTopologyTemplate> implements CsLineTopologyTemplateService {

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public Boolean add(List<CsLineTopologyTemplateParm> csLineTopologyTemplateParms) {
        List<CsLineTopologyTemplate> csLineTopologyTemplates = new ArrayList<>();
        QueryWrapper<CsLineTopologyTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("topo_id",csLineTopologyTemplateParms.get(0).getTopoId());
        this.remove(queryWrapper);

        csLineTopologyTemplateParms.stream().forEach(csLineTopologyTemplateParm -> {
            CsLineTopologyTemplate cs = new CsLineTopologyTemplate();
            BeanUtils.copyProperties(csLineTopologyTemplateParm, cs);
            cs.setStatus("1");
            csLineTopologyTemplates.add(cs);
        });
        boolean b = this.saveBatch(csLineTopologyTemplates);

        return b;
    }

    @Override
    public List<CsLineTopologyTemplateVO> queryByTopoId(String topoId) {
        List<CsLineTopologyTemplate> list = new ArrayList<>();
        list = this.lambdaQuery().eq(CsLineTopologyTemplate::getTopoId, topoId).eq(CsLineTopologyTemplate::getStatus, 1).list();
        List<CsLineTopologyTemplateVO> collect = list.stream().map(csLineTopologyTemplate -> {
            CsLineTopologyTemplateVO vo = new CsLineTopologyTemplateVO();
            BeanUtils.copyProperties(csLineTopologyTemplate, vo);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public Boolean deleteByTopoId(String topoId) {
        QueryWrapper<CsLineTopologyTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("topo_id",topoId);
        boolean remove = this.remove(queryWrapper);
        return remove;
    }
}
