package com.njcn.csdevice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
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.CsDevModelRelationMapper;
import com.njcn.csdevice.pojo.param.CsDevModelRelationAddParm;
import com.njcn.csdevice.pojo.param.CsDevModelRelationAuidtParm;
import com.njcn.csdevice.pojo.param.CsDevModelRelationQueryParm;
import com.njcn.csdevice.pojo.po.CsDevModelRelationPO;
import com.njcn.csdevice.pojo.vo.CsDevModelRelationVO;
import com.njcn.csdevice.service.CsDevModelRelationService;
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  13:49【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
public class CsDevModelRelationServiceImpl extends ServiceImpl<CsDevModelRelationMapper, CsDevModelRelationPO> implements CsDevModelRelationService{

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CsDevModelRelationPO addDevModelRelation(CsDevModelRelationAddParm addParm) {
        CsDevModelRelationQueryParm queryParm = new CsDevModelRelationQueryParm();
        queryParm.setDevId (addParm.getDevId ());
        queryParm.setModelId (addParm.getModelId ());
        queryParm.setStatus ("1");
        List<CsDevModelRelationVO> csDevModelRelationVOS = this.queryDevModelRelation (queryParm);
        if(csDevModelRelationVOS.size ()>0){
            throw new BusinessException (AlgorithmResponseEnum.DATA_ERROR);
        }
        CsDevModelRelationPO csDevModelRelationPO = new CsDevModelRelationPO();
        BeanUtils.copyProperties (addParm, csDevModelRelationPO);
        csDevModelRelationPO.setStatus ("1");
        this.save (csDevModelRelationPO);
        return csDevModelRelationPO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean AuditDevModelRelation(CsDevModelRelationAuidtParm auditParm) {
        CsDevModelRelationQueryParm queryParm = new CsDevModelRelationQueryParm();
        queryParm.setId (auditParm.getId ());
        List<CsDevModelRelationVO> csDevModelRelationVOS = this.queryDevModelRelation (queryParm);
        CsDevModelRelationVO csDevModelRelationVO = csDevModelRelationVOS.get (0);
        CsDevModelRelationQueryParm queryParm2 = new CsDevModelRelationQueryParm();
        queryParm2.setDevId (StringUtils.isNotBlank (auditParm.getDevId ())?auditParm.getDevId ():csDevModelRelationVO.getDevId ());
        queryParm2.setModelId (StringUtils.isNotBlank (auditParm.getModelId ())?auditParm.getModelId ():csDevModelRelationVO.getModelId ());
        queryParm2.setStatus ("1");
        List<CsDevModelRelationVO> csDevModelRelationVOS2 = this.queryDevModelRelation (queryParm2);
        if(csDevModelRelationVOS.size ()>0){
            throw new BusinessException (AlgorithmResponseEnum.DATA_ERROR);
        }
        CsDevModelRelationPO csDevModelRelationPO = new CsDevModelRelationPO();
        BeanUtils.copyProperties (auditParm, csDevModelRelationPO);
        boolean b = this.updateById (csDevModelRelationPO);
        return b;
    }

    @Override
    public List<CsDevModelRelationVO> queryDevModelRelation(CsDevModelRelationQueryParm queryParm) {
        QueryWrapper<CsDevModelRelationPO> queryWrapper = new QueryWrapper<> ();
        queryWrapper.eq (StringUtils.isNotBlank (queryParm.getId ()),"id",queryParm.getId ()).
                eq (StringUtils.isNotBlank (queryParm.getModelId ()),"model_id",queryParm.getModelId ()).
                eq (StringUtils.isNotBlank (queryParm.getDevId ()),"dev_id",queryParm.getDevId ()).
                eq (StringUtils.isNotBlank (queryParm.getStatus ()),"status",queryParm.getStatus ());

        List<CsDevModelRelationPO> csDevModelRelationPOS = this.getBaseMapper ( ).selectList (queryWrapper);
        List<CsDevModelRelationVO> collect = csDevModelRelationPOS.stream ( ).map (temp -> {
            CsDevModelRelationVO vo = new CsDevModelRelationVO ( );
            BeanUtils.copyProperties (temp, vo);
            return vo;
        }).collect (Collectors.toList ( ));
        return collect;
    }

    @Override
    public List<CsDevModelRelationPO> findModelByDevId(String devId) {
        return this.lambdaQuery().eq(CsDevModelRelationPO::getDevId,devId).eq(CsDevModelRelationPO::getStatus,1).list();
    }

    @Override
    public String getModelByType(String devId, Integer type) {
        return this.baseMapper.getModelByType(devId,type);
    }

    @Override
    public String getVersionByDevId(String devId) {
        return this.baseMapper.getVersionByDevId(devId);
    }

    @Override
    public void updateDataByList(List<String> list, String id) {
        LambdaUpdateWrapper<CsDevModelRelationPO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(CsDevModelRelationPO::getDevId,list).set(CsDevModelRelationPO::getModelId,id);
        this.update(updateWrapper);
    }
}
