package com.njcn.csdevice.service.impl;

import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.csdevice.enums.AlgorithmResponseEnum;
import com.njcn.csdevice.mapper.CsLedgerMapper;
import com.njcn.csdevice.mapper.CsTouristDataPOMapper;
import com.njcn.csdevice.pojo.param.CsTouristDataParm;
import com.njcn.csdevice.pojo.po.CsLedger;
import com.njcn.csdevice.pojo.po.CsTouristDataPO;
import com.njcn.csdevice.pojo.vo.CsTouristDataParmVO;
import com.njcn.csdevice.service.CsTouristDataPOService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 
 * Description:
 * Date: 2023/6/27  15:50【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class CsTouristDataPOServiceImpl extends ServiceImpl<CsTouristDataPOMapper, CsTouristDataPO> implements CsTouristDataPOService{
    private final CsLedgerMapper csLedgerMapper;
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public Boolean add(List<CsTouristDataParm> csTouristDataParms) {
        QueryWrapper<CsTouristDataPO> QueryWrapper = new QueryWrapper<>();
        QueryWrapper<CsLedger> csLedgerQueryWrapper = new QueryWrapper<>();
        this.getBaseMapper().delete(null);
        List<CsTouristDataPO> list = new ArrayList<>();
        csTouristDataParms.forEach(temp->{
            QueryWrapper.clear();
            QueryWrapper.eq(CsTouristDataPO.COL_DEVICE_ID,temp.getDeviceId());
            CsTouristDataPO csTouristDataPO = this.getBaseMapper().selectOne(QueryWrapper);
            if(!Objects.isNull(csTouristDataPO)){
                return;
            }
            csTouristDataPO = new CsTouristDataPO();
            csTouristDataPO.setDeviceId(temp.getDeviceId());
            csLedgerQueryWrapper.clear();
            csLedgerQueryWrapper.eq("id",temp.getDeviceId()).eq("state",1);
            CsLedger csLedger = csLedgerMapper.selectOne(csLedgerQueryWrapper);
            String[] split = csLedger.getPids().split(",");
            csTouristDataPO.setEnginerId(split[1]);
            csTouristDataPO.setProjectId(split[2]);
            list.add(csTouristDataPO);

        });
        return this.saveBatch(list);
    }

    @Override
    public List<CsTouristDataParmVO> queryAll() {
        List<CsTouristDataParmVO> result = this.getBaseMapper().queryAll();
        return result;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public Boolean remove(List<CsTouristDataParm> csTouristDataParms) {
        List<String> collect = csTouristDataParms.stream().map(CsTouristDataParm::getDeviceId).collect(Collectors.toList());
        if(CollectionUtils.isEmpty(collect)){
            throw new BusinessException(AlgorithmResponseEnum.DEVICE_LOSE);
        }
        QueryWrapper<CsTouristDataPO> QueryWrapper = new QueryWrapper<>();
        QueryWrapper.in(CsTouristDataPO.COL_DEVICE_ID,collect);
        boolean remove = this.remove(QueryWrapper);
        return remove;
    }
}
