package com.aucma.dms.service.impl;

import com.aucma.common.constant.DmsConstants;
import com.aucma.common.exception.ServiceException;
import com.aucma.common.utils.DateUtils;
import com.aucma.dms.domain.BaseAttachInfo;
import com.aucma.dms.domain.DmsKnowledgeLube;
import com.aucma.dms.mapper.BaseAttachInfoMapper;
import com.aucma.dms.domain.dto.AttachFileDTO;
import com.aucma.dms.mapper.DmsBaseLubeStationMapper;
import com.aucma.dms.mapper.DmsKnowledgeLubeMapper;
import com.aucma.dms.service.IDmsKnowledgeLubeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 润滑知识库Service业务层处理
 *
 * @author caoql
 * @date 2024-02-26
 */
@Service
public class DmsKnowledgeLubeServiceImpl implements IDmsKnowledgeLubeService
{
    @Autowired
    private DmsKnowledgeLubeMapper dmsKnowledgeLubeMapper;
    @Autowired
    private DmsBaseLubeStationMapper dmsBaseLubeStationMapper;
    @Autowired
    private BaseAttachInfoMapper baseAttachInfoMapper;

    /**
     * 查询润滑知识库
     *
     * @param knowledgeLubeId 润滑知识库主键
     * @return 润滑知识库
     */
    @Override
    public DmsKnowledgeLube selectDmsKnowledgeLubeByKnowledgeLubeId(Long knowledgeLubeId)
    {
        DmsKnowledgeLube dmsKnowledgeLube = dmsKnowledgeLubeMapper.selectDmsKnowledgeLubeByKnowledgeLubeId(knowledgeLubeId);
        List<String> baseAttachInfos = baseAttachInfoMapper.selectListBaseAttachInfos(DmsConstants.MES_BASE_ATTACH_INFO_KNOWLEDGELUBE,dmsKnowledgeLube.getKnowledgeLubeId());
        List<AttachFileDTO> attachFiles = new ArrayList<AttachFileDTO>();

        for (String url : baseAttachInfos){
            AttachFileDTO attachFile = new AttachFileDTO(url);
            attachFiles.add(attachFile);
        }
        dmsKnowledgeLube.setSystemFiles(attachFiles);
        return dmsKnowledgeLube;
    }

    /**
     * 查询润滑知识库列表
     *
     * @param dmsKnowledgeLube 润滑知识库
     * @return 润滑知识库
     */
    @Override
    public List<DmsKnowledgeLube> selectDmsKnowledgeLubeList(DmsKnowledgeLube dmsKnowledgeLube)
    {
        return dmsKnowledgeLubeMapper.selectDmsKnowledgeLubeList(dmsKnowledgeLube);
    }

    /**
     * 新增润滑知识库
     *
     * @param dmsKnowledgeLube 润滑知识库
     * @return 结果
     */
    @Override
    public int insertDmsKnowledgeLube(DmsKnowledgeLube dmsKnowledgeLube)
    {
        DmsKnowledgeLube KnowledgeLube = new DmsKnowledgeLube();
        KnowledgeLube.setExperienceCode(dmsKnowledgeLube.getExperienceCode());
        List<DmsKnowledgeLube> dmsKnowledgeLubeList =  dmsKnowledgeLubeMapper
                .selectDmsKnowledgeLubeList(KnowledgeLube);
        if (dmsKnowledgeLubeList.size()>0){throw new ServiceException("该编号已存在");}
        dmsKnowledgeLube.setCreateTime(DateUtils.getNowDate());

        dmsKnowledgeLube.setIsFlag("1");
        int i = dmsKnowledgeLubeMapper.insertDmsKnowledgeLube(dmsKnowledgeLube);

        //存储图片
        List<String> fileUrls = dmsKnowledgeLube.getFileUrls();
        batchInsertInstanceFiles(fileUrls,dmsKnowledgeLube.getKnowledgeLubeId());
        return i;

    }

    /**
     * 修改润滑知识库
     *
     * @param dmsKnowledgeLube 润滑知识库
     * @return 结果
     */
    @Override
    public int updateDmsKnowledgeLube(DmsKnowledgeLube dmsKnowledgeLube)
    {
        DmsKnowledgeLube KnowledgeLube = new DmsKnowledgeLube();
        KnowledgeLube.setExperienceCode(dmsKnowledgeLube.getExperienceCode());
        List<DmsKnowledgeLube> dmsKnowledgeLubeList = dmsKnowledgeLubeMapper
                .selectDmsKnowledgeLubeList(KnowledgeLube);
        if (dmsKnowledgeLubeList.size()>0 && !dmsKnowledgeLube.getKnowledgeLubeId()
                .equals(dmsKnowledgeLubeList.get(0).getKnowledgeLubeId())){
            throw new ServiceException("该编号已存在");
        }
        dmsKnowledgeLube.setUpdateTime(DateUtils.getNowDate());
        int i = dmsKnowledgeLubeMapper.updateDmsKnowledgeLube(dmsKnowledgeLube);
        baseAttachInfoMapper.deleteListBaseAttachInfos(DmsConstants.MES_BASE_ATTACH_INFO_KNOWLEDGELUBE,dmsKnowledgeLube.getKnowledgeLubeId());
        //存储图片
        List<String> fileUrls = dmsKnowledgeLube.getFileUrls();
        if (fileUrls!=null) {
            batchInsertInstanceFiles(fileUrls, dmsKnowledgeLube.getKnowledgeLubeId());
        }
        return i;
    }

    /**
     * 批量删除润滑知识库
     *
     * @param knowledgeLubeIds 需要删除的润滑知识库主键
     * @return 结果
     */
    @Override
    public int deleteDmsKnowledgeLubeByKnowledgeLubeIds(Long[] knowledgeLubeIds)
    {
        return dmsKnowledgeLubeMapper.deleteDmsKnowledgeLubeByKnowledgeLubeIds(knowledgeLubeIds);
    }

    /**
     * 删除润滑知识库信息
     *
     * @param knowledgeLubeId 润滑知识库主键
     * @return 结果
     */
    @Override
    public int deleteDmsKnowledgeLubeByKnowledgeLubeId(Long knowledgeLubeId)
    {
        return dmsKnowledgeLubeMapper.deleteDmsKnowledgeLubeByKnowledgeLubeId(knowledgeLubeId);
    }
    public void batchInsertInstanceFiles(List<String> fileUrls, Long knowledgeLubeId ) {
        if (fileUrls != null && !fileUrls.isEmpty()) {
//            List<DmsInstanceFile> dmsInstanceFiles = new ArrayList<>();
//            for (String fileUrl : fileUrls) {
//                DmsInstanceFile dmsInstanceFile = new DmsInstanceFile();
//                dmsInstanceFile.setTargetId(instanceActivityId);
//                dmsInstanceFile.setTargetType(DmsConstants.DMS_INSTANCE_FILE_TARGET_TYPE_FAULT_INSTANCE_ACTIVITY);
//                dmsInstanceFile.setFilePath(fileUrl);
//                dmsInstanceFiles.add(dmsInstanceFile);
//            }
            List<BaseAttachInfo> baseAttachInfos = new ArrayList<>();
            for (String fileUrl : fileUrls){
                BaseAttachInfo baseAttachInfo = new BaseAttachInfo();
                baseAttachInfo.setAttachType(DmsConstants.MES_BASE_ATTACH_INFO_KNOWLEDGELUBE);
                baseAttachInfo.setAttachName("name");
                baseAttachInfo.setAttachPath(fileUrl);
                baseAttachInfo.setProcessId(knowledgeLubeId);
                baseAttachInfos.add(baseAttachInfo);
            }
            baseAttachInfoMapper.batchDmsKnowLedgeLubeFile(baseAttachInfos);
        }
    }
}
