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.DmsKnowledgeRepair;
import com.aucma.dms.domain.dto.AttachFileDTO;
import com.aucma.dms.mapper.BaseAttachInfoMapper;
import com.aucma.dms.mapper.DmsKnowledgeRepairMapper;
import com.aucma.dms.service.IDmsKnowledgeRepairService;
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 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<AttachFileDTO> attachFiles = new ArrayList<AttachFileDTO>();

        for (String url : baseAttachInfos){
            AttachFileDTO attachFile = new AttachFileDTO(url);
            attachFiles.add(attachFile);
        }
        dmsKnowledgeRepair.setSystemFiles(attachFiles);
        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 KnowledgeRepair = new DmsKnowledgeRepair();
        KnowledgeRepair.setExperienceCode(dmsKnowledgeRepair.getExperienceCode());
        List<DmsKnowledgeRepair> dmsKnowledgeRepairList =  dmsKnowledgeRepairMapper
                .selectDmsKnowledgeRepairList(KnowledgeRepair);
        if (dmsKnowledgeRepairList.size() > 0) {throw new ServiceException("知识库编号已存在");}
        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 ledgeRepair = new DmsKnowledgeRepair();
        ledgeRepair.setExperienceCode(dmsKnowledgeRepair.getExperienceCode());
        List<DmsKnowledgeRepair> dmsKnowledgeRepairList = dmsKnowledgeRepairMapper
                .selectDmsKnowledgeRepairList(ledgeRepair);
        if (dmsKnowledgeRepairList.size() > 0 && !dmsKnowledgeRepair.getKnowledgeRepairId()
                .equals(dmsKnowledgeRepairList.get(0).getKnowledgeRepairId())) {
            throw new ServiceException("知识库编号已存在");
        }
        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 knowledgeRepairId ) {
        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("维修知识库附件");
                baseAttachInfo.setAttachPath(fileUrl);
                baseAttachInfo.setProcessId(knowledgeRepairId);
                baseAttachInfo.setCreateTime(new Date());
                baseAttachInfos.add(baseAttachInfo);
            }
            baseAttachInfoMapper.batchDmsKnowLedgeLubeFile(baseAttachInfos);
        }
    }
}
