package com.njcn.csdevice.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.njcn.csdevice.mapper.CsEdDataMapper;
import com.njcn.csdevice.pojo.param.CsEdDataAddParm;
import com.njcn.csdevice.pojo.param.CsEdDataAuditParm;
import com.njcn.csdevice.pojo.param.CsEdDataQueryParm;
import com.njcn.csdevice.pojo.po.CsEdDataPO;
import com.njcn.csdevice.pojo.vo.CsEdDataVO;
import com.njcn.csdevice.service.CsEdDataService;
import com.njcn.oss.constant.OssPath;
import com.njcn.oss.utils.FileStorageUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * Description:
 * 接口文档访问地址：http://serverIP:port/swagger-ui.html
 * Date: 2023/4/3  19:12【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class CsEdDataServiceImpl extends ServiceImpl<CsEdDataMapper, CsEdDataPO> implements CsEdDataService {


    private final FileStorageUtil fileStorageUtil;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean addEdData(CsEdDataAddParm csEdDataAddParm) {
        CsEdDataPO csEdDataPO = new CsEdDataPO();
        BeanUtils.copyProperties(csEdDataAddParm, csEdDataPO);
        String remoteDir = StrUtil.SLASH + OssPath.EDDATA + csEdDataAddParm.getDevTypeName() + StrUtil.SLASH + csEdDataAddParm.getVersionNo() + StrUtil.SLASH;
        String filePath = fileStorageUtil.uploadMultipart(csEdDataAddParm.getFile(), remoteDir, true);
        csEdDataPO.setCrc(csEdDataAddParm.getCrcInfo());
        csEdDataPO.setFilePath(filePath);
        csEdDataPO.setStatus("1");
        boolean save = this.save(csEdDataPO);
        return save;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public Boolean auditEdData(CsEdDataAuditParm csEdDataAuditParm) {
        CsEdDataPO csEdDataPO = new CsEdDataPO();
        BeanUtils.copyProperties(csEdDataAuditParm, csEdDataPO);
        if (!Objects.isNull(csEdDataAuditParm.getFile())) {
            String filePath = fileStorageUtil.uploadMultipart(csEdDataAuditParm.getFile(), StrUtil.SLASH + OssPath.EDDATA + csEdDataAuditParm.getDevTypeName() + StrUtil.SLASH + csEdDataAuditParm.getVersionNo() + StrUtil.SLASH, true);
            csEdDataPO.setFilePath(filePath);
        }
        boolean b = this.updateById(csEdDataPO);
        return b;
    }

    @Override
    public IPage<CsEdDataVO> queryEdDataPage(CsEdDataQueryParm csEdDataQueryParm) {
        Page<CsEdDataVO> returnpage = new Page<>(csEdDataQueryParm.getPageNum(), csEdDataQueryParm.getPageSize());

        returnpage = this.getBaseMapper().getPage(returnpage, csEdDataQueryParm);
        return returnpage;
    }

    @Override
    public CsEdDataVO findByDevTypeId(String devType) {
        CsEdDataPO csEdDataPo = new CsEdDataPO();
        LambdaQueryWrapper<CsEdDataPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CsEdDataPO::getDevType, devType);
        lambdaQueryWrapper.eq(CsEdDataPO::getStatus, 1);
        List<CsEdDataPO> list = this.baseMapper.selectList(lambdaQueryWrapper);
        if (!CollectionUtils.isEmpty(list)) {
            csEdDataPo = list.get(0);
        }
        CsEdDataVO csEdDataVo = new CsEdDataVO();
        BeanUtils.copyProperties(csEdDataPo, csEdDataVo);
        return csEdDataVo;
    }

    public CsEdDataPO findByDevTypeIdAndVersionNo(String devTypeId, String versionNo) {
        return this.lambdaQuery().eq(CsEdDataPO::getDevType, devTypeId)
                .eq(CsEdDataPO::getVersionNo, versionNo)
                .eq(CsEdDataPO::getStatus, 1).one();
    }
}

