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.mapper.BaseAttachInfoMapper;
import com.hw.system.api.domain.SysFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hw.dms.mapper.DmsKnowledgeRepairMapper;
import com.hw.dms.domain.DmsKnowledgeRepair;
import com.hw.dms.service.IDmsKnowledgeRepairService;

/**
 * 维修知识库Service业务层处理
 *
 * @author Open Platform
 * @date 2024-02-26
 */
@Service
public class DmsKnowledgeRepairServiceImpl implements IDmsKnowledgeRepairService
{
    @Autowired
    private DmsKnowledgeRepairMapper dmsKnowledgeRepairMapper;

    @Autowired
    private BaseAttachInfoMapper baseAttachInfoMapper;

    /**
     * 查询维修知识库
     *
     * @param knowledgeRepairId 维修知识库主键
     * @return 维修知识库
     */
    @Override
    public DmsKnowledgeRepair selectDmsKnowledgeRepairByKnowledgeRepairId(Long knowledgeRepairId)
    {
        DmsKnowledgeRepair dmsKnowledgeRepair = dmsKnowledgeRepairMapper.selectDmsKnowledgeRepairByKnowledgeRepairId(knowledgeRepairId);
        List<String> baseAttachInfos = baseAttachInfoMapper.selectListBaseAttachInfos(DmsConstants.MES_BASE_ATTACH_INFO_KNOWLEDGEREPAIR,dmsKnowledgeRepair.getKnowledgeRepairId());
        List<SysFile> sysFiles = new ArrayList<SysFile>();

        for (String string:baseAttachInfos){
            SysFile sysFile = new SysFile();
            sysFile.setUrl(string);
            sysFiles.add(sysFile);
        }
        dmsKnowledgeRepair.setSystemFiles(sysFiles);
        return dmsKnowledgeRepair;
    }

    /**
     * 查询维修知识库列表
     *
     * @param dmsKnowledgeRepair 维修知识库
     * @return 维修知识库
     */
    @Override
    public List<DmsKnowledgeRepair> selectDmsKnowledgeRepairList(DmsKnowledgeRepair dmsKnowledgeRepair)
    {
        return dmsKnowledgeRepairMapper.selectDmsKnowledgeRepairList(dmsKnowledgeRepair);
    }

    /**
     * 新增维修知识库
     *
     * @param dmsKnowledgeRepair 维修知识库
     * @return 结果
     */
    @Override
    public int insertDmsKnowledgeRepair(DmsKnowledgeRepair dmsKnowledgeRepair)
    {
        dmsKnowledgeRepair.setCreateTime(DateUtils.getNowDate());
        dmsKnowledgeRepair.setIsFlag("1");
        int i = dmsKnowledgeRepairMapper.insertDmsKnowledgeRepair(dmsKnowledgeRepair);
        //存储图片
        List<String> fileUrls = dmsKnowledgeRepair.getFileUrls();
        batchInsertInstanceFiles(fileUrls, dmsKnowledgeRepair.getKnowledgeRepairId());

        return i;
    }

    /**
     * 修改维修知识库
     *
     * @param dmsKnowledgeRepair 维修知识库
     * @return 结果
     */
    @Override
    public int updateDmsKnowledgeRepair(DmsKnowledgeRepair dmsKnowledgeRepair)
    {
        dmsKnowledgeRepair.setUpdateTime(DateUtils.getNowDate());
        int i = dmsKnowledgeRepairMapper.updateDmsKnowledgeRepair(dmsKnowledgeRepair);
        baseAttachInfoMapper.deleteListBaseAttachInfos(DmsConstants.MES_BASE_ATTACH_INFO_KNOWLEDGEREPAIR,dmsKnowledgeRepair.getKnowledgeRepairId());

        //存储图片
        List<String> fileUrls = dmsKnowledgeRepair.getFileUrls();
        if(fileUrls!=null) {
            batchInsertInstanceFiles(fileUrls, dmsKnowledgeRepair.getKnowledgeRepairId());
        }
        return i;
    }

    /**
     * 批量删除维修知识库
     *
     * @param knowledgeRepairIds 需要删除的维修知识库主键
     * @return 结果
     */
    @Override
    public int deleteDmsKnowledgeRepairByKnowledgeRepairIds(Long[] knowledgeRepairIds)
    {
        return dmsKnowledgeRepairMapper.deleteDmsKnowledgeRepairByKnowledgeRepairIds(knowledgeRepairIds);
    }

    /**
     * 删除维修知识库信息
     *
     * @param knowledgeRepairId 维修知识库主键
     * @return 结果
     */
    @Override
    public int deleteDmsKnowledgeRepairByKnowledgeRepairId(Long knowledgeRepairId)
    {
        return dmsKnowledgeRepairMapper.deleteDmsKnowledgeRepairByKnowledgeRepairId(knowledgeRepairId);
    }

    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_KNOWLEDGEREPAIR);
                baseAttachInfo.setAttachName("name");
                baseAttachInfo.setAttachPath(fileUrl);
                baseAttachInfo.setProcessId(knowledgeLubeId);
                baseAttachInfos.add(baseAttachInfo);
            }
            baseAttachInfoMapper.batchDmsKnowLedgeLubeFile(baseAttachInfos);
        }
    }
}
