package cn.iocoder.yudao.module.inspection.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.module.inspection.entity.InspectionFile;
import cn.iocoder.yudao.module.inspection.entity.WarnMessage;
import cn.iocoder.yudao.module.inspection.mapper.InspectionFileMapper;
import cn.iocoder.yudao.module.inspection.service.IInspectionFileService;
import cn.iocoder.yudao.module.inspection.service.IWarnMessageService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * inspectionFileService业务层处理
 *
 * @author zcy
 * @date 2023-10-13
 */
@Service
public class InspectionFileServiceImpl extends ServiceImpl<InspectionFileMapper,InspectionFile> implements IInspectionFileService
{
    @Autowired
    private IWarnMessageService messageService;

    /**
     * 查询inspectionFile
     *
     * @param id inspectionFile主键
     * @return inspectionFile
     */
    @Override
    public InspectionFile selectInspectionFileById(Long id)
    {
        return baseMapper.selectInspectionFileById(id);
    }


    /**
     * 新增inspectionFile
     *
     * @param inspectionFile inspectionFile
     * @return 结果
     */
    @Override
    public int insertInspectionFile(InspectionFile inspectionFile)
    {

        int insert = baseMapper.insert(inspectionFile);
        if (ObjectUtil.isEmpty(inspectionFile.getFatherId())){
            inspectionFile.setFileCode(inspectionFile.getId()+",");
        }else {
            InspectionFile faFile = this.getById(inspectionFile.getFatherId());

            inspectionFile.setFileCode(faFile.getFileCode()+inspectionFile.getId()+",");
        }
        baseMapper.updateById(inspectionFile);
        if (ObjectUtil.isNotEmpty(inspectionFile.getWarnTime())){
            //同时处理提醒信息
            WarnMessage warnMessage =new WarnMessage();
            warnMessage.setTitle("文件到期提醒");
            warnMessage.setContent("文件名为:"+inspectionFile.getFileName()+"即将过期,请及时处理相关事项！");
            warnMessage.setIsRead("0");
            warnMessage.setPartnerId(inspectionFile.getPartnerId());
            warnMessage.setType("file");
            warnMessage.setObjectId(inspectionFile.getId());
            warnMessage.setWarnTime(inspectionFile.getWarnTime());
            messageService.save(warnMessage);
        }
        return insert;
    }

    /**
     * 修改inspectionFile
     *
     * @param inspectionFile inspectionFile
     * @return 结果
     */
    @Override
    public int updateInspectionFile(InspectionFile inspectionFile)
    {
        LambdaQueryWrapper<WarnMessage> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(WarnMessage::getType,"file").eq(WarnMessage::getObjectId,inspectionFile.getId());
        WarnMessage one = messageService.getOne(queryWrapper);
        if (ObjectUtil.isNotEmpty(one)){
            //更新处理
            messageService.removeById(one.getId());
        }
        if (ObjectUtil.isNotEmpty(inspectionFile.getWarnTime())){
            //同时处理提醒信息
            WarnMessage warnMessage =new WarnMessage();
            warnMessage.setTitle("文件到期提醒");
            warnMessage.setContent("文件名为:"+inspectionFile.getFileName()+"即将过期,请及时处理相关事项！");
            warnMessage.setIsRead("0");
            warnMessage.setPartnerId(inspectionFile.getPartnerId());
            warnMessage.setType("file");
            warnMessage.setObjectId(inspectionFile.getId());
            warnMessage.setWarnTime(inspectionFile.getWarnTime());
            messageService.save(warnMessage);
        }
        return baseMapper.updateById(inspectionFile);
    }

    /**
     * 批量删除inspectionFile
     *
     * @param ids 需要删除的inspectionFile主键
     * @return 结果
     */
    @Override
    public int deleteInspectionFileByIds(Long[] ids)
    {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除inspectionFile信息
     *
     * @param id inspectionFile主键
     * @return 结果
     */
    @Override
    public int deleteInspectionFileById(Long id)
    {
        InspectionFile file = this.getById(id);
        if (file.getType().equals("2")){
            LambdaQueryWrapper<WarnMessage> queryWrapper =new LambdaQueryWrapper<>();
            queryWrapper.eq(WarnMessage::getType,"file").eq(WarnMessage::getObjectId,id);
            WarnMessage one = messageService.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(one)){
                //更新处理
                messageService.removeById(one.getId());
            }
            baseMapper.deleteById(id);
        }else if (file.getType().equals("1")){
            LambdaQueryWrapper<InspectionFile> queryWrapper =new LambdaQueryWrapper<>();
            queryWrapper.likeLeft(InspectionFile::getFileCode,file.getFileCode());
            List<InspectionFile> list = this.list(queryWrapper);
            for (InspectionFile inspectionFile : list) {
                LambdaQueryWrapper<WarnMessage> queryWrapper1 =new LambdaQueryWrapper<>();
                queryWrapper1.eq(WarnMessage::getType,"file").eq(WarnMessage::getObjectId,inspectionFile.getId());
                WarnMessage one = messageService.getOne(queryWrapper1);
                if (ObjectUtil.isNotEmpty(one)){
                    //更新处理
                    messageService.removeById(one.getId());
                }
            }
            baseMapper.delete(queryWrapper);
        }

        return 1;
    }

    @Override
    public IPage<InspectionFile> selectInspectionFileList(Page<InspectionFile> page, InspectionFile inspectionFile) {
        // 创建一个空的 QueryWrapper
        QueryWrapper<InspectionFile> queryWrapper = new QueryWrapper<>();

        // 直接调用 MyBatis-Plus 的 page 方法进行分页查询
        return this.page(page, queryWrapper); // 返回符合条件的分页查询结果
    }
}
