package com.hw.dms.service.impl;

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

import com.hw.common.core.constant.DmsConstants;
import com.hw.common.core.utils.DateUtils;
import com.hw.dms.domain.BaseAttachInfo;
import com.hw.dms.domain.DmsBaseMaintStation;
import com.hw.dms.mapper.BaseAttachInfoMapper;
import com.hw.dms.mapper.DmsBaseMaintStationMapper;
import com.hw.system.api.domain.SysFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hw.dms.mapper.DmsKnowledgeMaintMapper;
import com.hw.dms.domain.DmsKnowledgeMaint;
import com.hw.dms.service.IDmsKnowledgeMaintService;

/**
 * 保养知识库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<SysFile> sysFiles = new ArrayList<SysFile>();

        for (String string:baseAttachInfos){
            SysFile sysFile = new SysFile();
            sysFile.setUrl(string);
            sysFiles.add(sysFile);
        }
        dmsKnowledgeMaint.setSystemFiles(sysFiles);
        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.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.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("name");
                baseAttachInfo.setAttachPath(fileUrl);
                baseAttachInfo.setProcessId(knowledgeLubeId);
                baseAttachInfos.add(baseAttachInfo);
            }
            baseAttachInfoMapper.batchDmsKnowLedgeLubeFile(baseAttachInfos);
        }
    }
}
