package com.chinaweal.aiccs.aiccs.abnormal.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaweal.aiccs.aiccs.abnormal.dto.PYRequestDTO;
import com.chinaweal.aiccs.aiccs.abnormal.entity.Toincludeabn;
import com.chinaweal.aiccs.aiccs.abnormal.mapper.ToincludeabnMapper;
import com.chinaweal.aiccs.aiccs.abnormal.service.TSAttachmentService;
import com.chinaweal.aiccs.aiccs.abnormal.service.ToincludeabnService;
import com.chinaweal.youfool.framework.springboot.exception.custom.BusinessException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 拟列异主体表 服务实现类
 * </p>
 *
 * @author lwd
 * @since 2022-07-22
 */
@Service
public class ToincludeabnServiceImpl extends ServiceImpl<ToincludeabnMapper, Toincludeabn> implements ToincludeabnService {

    @Resource
    private TSAttachmentService attachmentService;

    @Override
    public Toincludeabn selectReportYears(String pripid) {
        return baseMapper.selectReportYears(pripid);
    }

    @Override
    public Toincludeabn getByDataFromId(String dataFromId) {
        LambdaQueryWrapper<Toincludeabn> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Toincludeabn::getDataFromId, dataFromId)
                .eq(Toincludeabn::getDataState, "0");
        return getOne(wrapper);
    }

    @Override
    public List<Toincludeabn> getListByDataFromId(String dataFromId) {
        LambdaQueryWrapper<Toincludeabn> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Toincludeabn::getDataFromId, dataFromId)
                .eq(Toincludeabn::getDataState, "0");
        return list(wrapper);
    }

    @Override
    public boolean saveClueAndFile(String files, Toincludeabn toincludeabn) {
        if (toincludeabn != null && StringUtils.isNotBlank(files)) {
            boolean save = save(toincludeabn);
            if (save) { // 保存成功才上传附件
                try {
                    boolean saveClueFile = attachmentService.saveClueFile(files, toincludeabn.getDataFromId());
                    return saveClueFile;
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new BusinessException(e.getMessage());
                }

            }
        }
        return false;

    }

    @Override
    public boolean deleteById(String userId, String toIncludeId) {
        Toincludeabn toincludeabn = new Toincludeabn();
        toincludeabn.setToIncludeId(toIncludeId);
        toincludeabn.setUpdateUserId(userId);
        toincludeabn.setUpdateTime(LocalDateTime.now());
        toincludeabn.setDataState("1");// "1"表示删除
        return updateById(toincludeabn);
    }

    @Override
    public boolean saveYMLCClueAndFile(List<PYRequestDTO.Attachment> files, Toincludeabn toincludeabn) {
        if (toincludeabn != null && files.size() > 0) {
            boolean save = save(toincludeabn);
            if (save) { // 保存成功才上传附件
                try {
                    return attachmentService.saveYMLCClueFile(files, toincludeabn);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new BusinessException(e.getMessage());
                }

            }
        }
        return false;
    }
}
