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.common.pojo.exception.BusinessException;
import com.njcn.csdevice.enums.AlgorithmResponseEnum;
import com.njcn.csdevice.mapper.CsDataEffectiveMapper;
import com.njcn.csdevice.pojo.param.CsDataEffectiveAddParm;
import com.njcn.csdevice.pojo.param.CsDataEffectiveAuditParm;
import com.njcn.csdevice.pojo.param.CsDataEffectiveQueryParm;
import com.njcn.csdevice.pojo.po.CsDataEffectivePO;
import com.njcn.csdevice.pojo.vo.CsDataEffectiveVO;
import com.njcn.csdevice.service.CsDataEffectiveService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 
 * Description:
 * 接口文档访问地址：http://serverIP:port/swagger-ui.html
 * Date: 2023/4/18  9:49【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
public class CsDataEffectiveServiceImpl extends ServiceImpl<CsDataEffectiveMapper, CsDataEffectivePO> implements CsDataEffectiveService{

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean add(CsDataEffectiveAddParm csDataEffectiveAddParm) {

        CsDataEffectiveQueryParm csDataEffectiveQueryParm =  new CsDataEffectiveQueryParm();
        csDataEffectiveQueryParm.setDevId (csDataEffectiveAddParm.getDevId ());
        csDataEffectiveQueryParm.setProjectId (csDataEffectiveAddParm.getProjectId ());
        List<CsDataEffectiveVO> list = this.queryDataEffective (csDataEffectiveQueryParm);
        if(list.size ()>0){
            throw new BusinessException (AlgorithmResponseEnum.DATA_ERROR);
        }
        CsDataEffectivePO csDataEffectivePO = new CsDataEffectivePO ();

        BeanUtils.copyProperties (csDataEffectiveAddParm, csDataEffectivePO);
        boolean save = this.save (csDataEffectivePO);
        return save;
    }

    @Override
    public List<CsDataEffectiveVO> queryDataEffective(CsDataEffectiveQueryParm csDataEffectiveQueryParm) {
        QueryWrapper<CsDataEffectivePO> queryWrapper = new QueryWrapper<> ();
        queryWrapper.eq (StringUtils.isNotBlank (csDataEffectiveQueryParm.getId ()),"id",csDataEffectiveQueryParm.getId ()).
                eq (StringUtils.isNotBlank (csDataEffectiveQueryParm.getProjectId ()),"project_id",csDataEffectiveQueryParm.getProjectId ()).
                eq (StringUtils.isNotBlank (csDataEffectiveQueryParm.getDevId ()),"dev_id",csDataEffectiveQueryParm.getDevId ());
        List<CsDataEffectivePO> csDataEffectivePOS = this.getBaseMapper ( ).selectList (queryWrapper);
        List<CsDataEffectiveVO> collect = csDataEffectivePOS.stream ( ).map (temp -> {
            CsDataEffectiveVO vo = new CsDataEffectiveVO ( );
            BeanUtils.copyProperties (temp, vo);
            return vo;
        }).collect (Collectors.toList ( ));
        return collect;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean auditDataEffective(CsDataEffectiveAuditParm auditParm) {
        CsDataEffectiveQueryParm csDataEffectiveQueryParm =  new CsDataEffectiveQueryParm();
        csDataEffectiveQueryParm.setDevId (auditParm.getDevId ());
        csDataEffectiveQueryParm.setProjectId (auditParm.getProjectId ());
        List<CsDataEffectiveVO> list = this.queryDataEffective (csDataEffectiveQueryParm);
        if(list.size ()>0){
            throw new BusinessException (AlgorithmResponseEnum.DATA_ERROR);
        }
        CsDataEffectivePO csDataEffectivePO = new CsDataEffectivePO ();

        BeanUtils.copyProperties (auditParm, csDataEffectivePO);
        boolean flag = this.updateById (csDataEffectivePO);
        return flag;
    }

}
