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.DmsKnowledgeMaint;
import com.aucma.dms.mapper.BaseAttachInfoMapper;
import com.aucma.dms.domain.dto.AttachFileDTO;
import com.aucma.dms.mapper.DmsBaseMaintStationMapper;
import com.aucma.dms.mapper.DmsKnowledgeMaintMapper;
import com.aucma.dms.service.IDmsKnowledgeMaintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 保养知识库Service业务层处理
 *
 * @author Open Platform
 * @date 2024-02-26
 */
@Service
public class DmsKnowledgeMaintServiceImpl implements IDmsKnowledgeMaintService
{
    @Autowired
    private DmsKnowledgeMaintMapper dmsKnowledgeMaintMapper;

    @Autowired
    private DmsBaseMaintStationMapper dmsBaseMaintStationMapper;
    @Autowired
    private BaseAttachInfoMapper baseAttachInfoMapper;
    /**
     * 查询保养知识库
     *
     * @param knowledgeMaintId 保养知识库主键
     * @return 保养知识库
     */
    @Override
    public DmsKnowledgeMaint selectDmsKnowledgeMaintByKnowledgeMaintId(Long knowledgeMaintId)
    {
        DmsKnowledgeMaint dmsKnowledgeMaint = dmsKnowledgeMaintMapper.selectDmsKnowledgeMaintByKnowledgeMaintId(knowledgeMaintId);
        List<String> baseAttachInfos = baseAttachInfoMapper.selectListBaseAttachInfos(DmsConstants.MES_BASE_ATTACH_INFO_KNOWLEDGEMAINT,dmsKnowledgeMaint.getKnowledgeMaintId());
        List<AttachFileDTO> attachFiles = new ArrayList<AttachFileDTO>();

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

    /**
     * 查询保养知识库列表
     *
     * @param dmsKnowledgeMaint 保养知识库
     * @return 保养知识库
     */
    @Override
    public List<DmsKnowledgeMaint> selectDmsKnowledgeMaintList(DmsKnowledgeMaint dmsKnowledgeMaint)
    {
        return dmsKnowledgeMaintMapper.selectDmsKnowledgeMaintList(dmsKnowledgeMaint);
    }

    /**
     * 新增保养知识库
     *
     * @param dmsKnowledgeMaint 保养知识库
     * @return 结果
     */
    @Override
    public int insertDmsKnowledgeMaint(DmsKnowledgeMaint dmsKnowledgeMaint)
    {
        DmsKnowledgeMaint KnowledgeMaint = new DmsKnowledgeMaint();
        KnowledgeMaint.setExperienceCode(dmsKnowledgeMaint.getExperienceCode());
        List<DmsKnowledgeMaint> DmsKnowledgeMaintList = dmsKnowledgeMaintMapper
                .selectDmsKnowledgeMaintList(KnowledgeMaint);
        if (DmsKnowledgeMaintList.size()>0){throw new ServiceException("知识库编号已存在");}
        dmsKnowledgeMaint.setCreateTime(DateUtils.getNowDate());
        dmsKnowledgeMaint.setIsFlag("1");
        int i = dmsKnowledgeMaintMapper.insertDmsKnowledgeMaint(dmsKnowledgeMaint);
        //存储图片
        List<String> fileUrls = dmsKnowledgeMaint.getFileUrls();
        batchInsertInstanceFiles(fileUrls, dmsKnowledgeMaint.getKnowledgeMaintId());
        return i;
    }

    /**
     * 修改保养知识库
     *
     * @param dmsKnowledgeMaint 保养知识库
     * @return 结果
     */
    @Override
    public int updateDmsKnowledgeMaint(DmsKnowledgeMaint dmsKnowledgeMaint)
    {
        DmsKnowledgeMaint KnowledgeMaint = new DmsKnowledgeMaint();
        KnowledgeMaint.setExperienceCode(dmsKnowledgeMaint.getExperienceCode());
        List<DmsKnowledgeMaint> DmsKnowledgeMaintList = dmsKnowledgeMaintMapper
                .selectDmsKnowledgeMaintList(KnowledgeMaint);
        if (DmsKnowledgeMaintList.size()>0 && !dmsKnowledgeMaint.getKnowledgeMaintId()
                .equals(DmsKnowledgeMaintList.get(0).getKnowledgeMaintId())){
            throw new ServiceException("知识库编号已存在");
        }
        dmsKnowledgeMaint.setUpdateTime(DateUtils.getNowDate());
        int i = dmsKnowledgeMaintMapper.updateDmsKnowledgeMaint(dmsKnowledgeMaint);
        baseAttachInfoMapper.deleteListBaseAttachInfos(DmsConstants.MES_BASE_ATTACH_INFO_KNOWLEDGEMAINT,dmsKnowledgeMaint.getKnowledgeMaintId());
        List<String> fileUrls = dmsKnowledgeMaint.getFileUrls();
        if (fileUrls!=null) {
            batchInsertInstanceFiles(fileUrls, dmsKnowledgeMaint.getKnowledgeMaintId());
        }
        return i;
    }

    /**
     * 批量删除保养知识库
     *
     * @param knowledgeMaintIds 需要删除的保养知识库主键
     * @return 结果
     */
    @Override
    public int deleteDmsKnowledgeMaintByKnowledgeMaintIds(Long[] knowledgeMaintIds)
    {
        return dmsKnowledgeMaintMapper.deleteDmsKnowledgeMaintByKnowledgeMaintIds(knowledgeMaintIds);
    }

    /**
     * 删除保养知识库信息
     *
     * @param knowledgeMaintId 保养知识库主键
     * @return 结果
     */
    @Override
    public int deleteDmsKnowledgeMaintByKnowledgeMaintId(Long knowledgeMaintId)
    {
        return dmsKnowledgeMaintMapper.deleteDmsKnowledgeMaintByKnowledgeMaintId(knowledgeMaintId);
    }
    public void batchInsertInstanceFiles(List<String> fileUrls, Long knowledgeLubeId ) {
        if (fileUrls != null && !fileUrls.isEmpty()) {

            List<BaseAttachInfo> baseAttachInfos = new ArrayList<>();
            for (String fileUrl : fileUrls){
                BaseAttachInfo baseAttachInfo = new BaseAttachInfo();
                baseAttachInfo.setAttachType(DmsConstants.MES_BASE_ATTACH_INFO_KNOWLEDGEMAINT);
                baseAttachInfo.setAttachName("设备保养");
                baseAttachInfo.setAttachPath(fileUrl);
                baseAttachInfo.setCreateTime(new Date());
                baseAttachInfo.setProcessId(knowledgeLubeId);
                baseAttachInfos.add(baseAttachInfo);
            }
            baseAttachInfoMapper.batchDmsKnowLedgeLubeFile(baseAttachInfos);
        }
    }
}
