package com.ktg.iscs.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.utils.PDFToImgUtil;
import com.ktg.common.utils.StringUtils;
import com.ktg.iscs.domain.IsMaterialsInstructions;
import com.ktg.iscs.mapper.IsMaterialsInstructionsMapper;
import com.ktg.iscs.service.IIsMaterialsInstructionsService;
import com.ktg.system.domain.SysUploadFile;
import com.ktg.system.service.ISysUploadFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 物资使用说明Service业务层处理
 *
 * @author cgj
 * @date 2025-01-23
 */
@Service
public class IsMaterialsInstructionsServiceImpl extends ServiceImpl<IsMaterialsInstructionsMapper, IsMaterialsInstructions> implements IIsMaterialsInstructionsService {

    @Autowired
    private IsMaterialsInstructionsMapper isMaterialsInstructionsMapper;
    @Autowired
    private ISysUploadFileService iSysUploadFileService;

    @Override
    public Page<IsMaterialsInstructions> getIsMaterialsInstructionsPage(Page<IsMaterialsInstructions> page, IsMaterialsInstructions isMaterialsInstructions) {
        Page<IsMaterialsInstructions> result = isMaterialsInstructionsMapper.getIsMaterialsInstructionsPage(page, isMaterialsInstructions);
        return result;
    }

    @Override
    public Boolean deleteIsMaterialsInstructionsByInstructionsIds(String instructionsIds) {
        Assert.notBlank(instructionsIds, "请选择需要删除的数据!");
        Long[] longIds = Convert.toLongArray(instructionsIds);
        List<IsMaterialsInstructions> isMaterialsInstructions = listByIds(Arrays.asList(longIds));
        boolean b = removeBatchByIds(Arrays.asList(longIds));
        // 开始删除文件
        if (b) {
            for (IsMaterialsInstructions isMaterialsInstruction : isMaterialsInstructions) {
                if (StringUtils.isNotBlank(isMaterialsInstruction.getFileUrl())) {
                    iSysUploadFileService.delFile(isMaterialsInstruction.getFileUrl());
                }
                if (StringUtils.isNotBlank(isMaterialsInstruction.getFilePathImg())) {
                    iSysUploadFileService.delFileByPath(isMaterialsInstruction.getFilePathImg());
                }
            }
        }
        return true;
    }

    @Override
    public Boolean insertIsMaterialsInstructions(IsMaterialsInstructions isMaterialsInstructions) {
        if (StringUtils.isNotBlank(isMaterialsInstructions.getFileUrl())) {
            if (isMaterialsInstructions.getFileUrl().contains(".pdf")) {
                isMaterialsInstructions.setFileType("1");
            }
            if (isMaterialsInstructions.getFileUrl().contains(".mp4")) {
                isMaterialsInstructions.setFileType("0");
            }
        }
        if (StringUtils.isNotBlank(isMaterialsInstructions.getFileUrl()) && "1".equals(isMaterialsInstructions.getFileType())) {
            SysUploadFile uploadFile = iSysUploadFileService.getOne(Wrappers.<SysUploadFile>lambdaQuery()
                    .eq(SysUploadFile::getUrl, isMaterialsInstructions.getFileUrl()));
            if (uploadFile != null && StringUtils.isNotBlank(uploadFile.getUrl())) {
                String outputFilePath = PDFToImgUtil.PDFToLongImgUtil(uploadFile.getPath());
                isMaterialsInstructions.setFileUrlImg(isMaterialsInstructions.getFileUrl().replace(".pdf", ".png"));
                isMaterialsInstructions.setFilePathImg(outputFilePath);
            }
        }
        return save(isMaterialsInstructions);
    }

    @Override
    public Boolean updateIsMaterialsInstructions(IsMaterialsInstructions dto) {
        Assert.notNull(dto.getInstructionsId(), "主键id不能为空！");
        IsMaterialsInstructions instructions = getById(dto.getInstructionsId());
        if (StringUtils.isNotBlank(dto.getFileUrl())) {
            if (dto.getFileUrl().contains(".pdf")) {
                dto.setFileType("1");
            }
            if (dto.getFileUrl().contains(".mp4")) {
                dto.setFileType("0");
            }
        }
        // 如果文件有参数
        if (StringUtils.isNotBlank(dto.getFileUrl())) {
            // 判断和以前的是否相同，如果相同，不用处理文件，如果不同，需要删除文件
            if (StringUtils.isNotBlank(instructions.getFileUrl()) && !instructions.getFileUrl().equals(dto.getFileUrl())) {
                iSysUploadFileService.delFile(instructions.getFileUrl());
                if (StringUtils.isNotBlank(instructions.getFilePathImg())) {
                    iSysUploadFileService.delFileByPath(instructions.getFilePathImg());
                }
            }
            if ("1".equals(dto.getFileType())) {
                SysUploadFile uploadFile = iSysUploadFileService.getOne(Wrappers.<SysUploadFile>lambdaQuery()
                        .eq(SysUploadFile::getUrl, dto.getFileUrl()));
                if (uploadFile != null && StringUtils.isNotBlank(uploadFile.getUrl())) {
                    String outputFilePath = PDFToImgUtil.PDFToLongImgUtil(uploadFile.getPath());
                    dto.setFileUrlImg(dto.getFileUrl().replace(".pdf", ".png"));
                    dto.setFilePathImg(outputFilePath);
                }
            }
            update(Wrappers.<IsMaterialsInstructions>lambdaUpdate()
                    .eq(IsMaterialsInstructions::getInstructionsId, dto.getInstructionsId())
                    .set(IsMaterialsInstructions::getOrderNum, dto.getOrderNum())
                    .set(IsMaterialsInstructions::getInstructionsTitle, dto.getInstructionsTitle())
                    .set(IsMaterialsInstructions::getMaterialsTypeId, dto.getMaterialsTypeId())
                    .set(IsMaterialsInstructions::getFileType, dto.getFileType())
                    .set(IsMaterialsInstructions::getFileUrl, dto.getFileUrl())
                    .set(IsMaterialsInstructions::getRemark, StringUtils.isNotBlank(dto.getRemark()) ? dto.getRemark() : null)
                    .set(IsMaterialsInstructions::getFileUrlImg, "1".equals(dto.getFileType()) ? dto.getFileUrlImg() : null)
                    .set(IsMaterialsInstructions::getFilePathImg, "1".equals(dto.getFileType()) ? dto.getFilePathImg() : null));
        } else {
            if (StringUtils.isNotBlank(instructions.getFileUrl())) {
                iSysUploadFileService.delFile(instructions.getFileUrl());
            }
            if (StringUtils.isNotBlank(instructions.getFilePathImg())) {
                iSysUploadFileService.delFileByPath(instructions.getFilePathImg());
            }
            update(Wrappers.<IsMaterialsInstructions>lambdaUpdate()
                    .eq(IsMaterialsInstructions::getInstructionsId, dto.getInstructionsId())
                    .set(IsMaterialsInstructions::getOrderNum, dto.getOrderNum())
                    .set(IsMaterialsInstructions::getInstructionsTitle, dto.getInstructionsTitle())
                    .set(IsMaterialsInstructions::getMaterialsTypeId, dto.getMaterialsTypeId())
                    .set(IsMaterialsInstructions::getFileType, dto.getFileType())
                    .set(IsMaterialsInstructions::getRemark, StringUtils.isNotBlank(dto.getRemark()) ? dto.getRemark() : null)
                    .set(IsMaterialsInstructions::getFileUrl, null)
                    .set(IsMaterialsInstructions::getFileUrlImg, null)
                    .set(IsMaterialsInstructions::getFilePathImg, null));
        }
        return true;
    }

}
