package com.bonus.material.owner.service.impl;

import com.bonus.common.biz.domain.BmFileInfo;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.utils.bean.BeanUtils;
import com.bonus.common.core.utils.bean.BeanValidators;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.material.device.domain.DevInfo;
import com.bonus.material.device.domain.dto.DevInfoImpDto;
import com.bonus.material.device.mapper.BmFileInfoMapper;
import com.bonus.material.owner.domain.FileInfo;
import com.bonus.material.owner.domain.Ownerdomin;
import com.bonus.material.owner.mapper.OwnerMapper;
import com.bonus.material.owner.service.OwnerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.Validator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class OwnerServiceImpl implements OwnerService {

    @Resource
    private OwnerMapper ownerMapper;

    @Resource
    private BmFileInfoMapper bmFileInfoMapper;

    @Resource
    protected Validator validator;

    @Override
    public List<Ownerdomin> list(Ownerdomin ownerdomin) {
        ownerdomin.setCreator(SecurityUtils.getLoginUser().getSysUser().getUserId()+"");
        ownerdomin.setCompanyId(SecurityUtils.getLoginUser().getSysUser().getCompanyId());
        List<Ownerdomin> list = ownerMapper.list(ownerdomin);
        return list;
    }

    @Override
    public Integer add(Ownerdomin ownerdomin) {
        ownerdomin.setCreator(SecurityUtils.getLoginUser().getSysUser().getUserId()+"");
        ownerdomin.setCompanyId(SecurityUtils.getLoginUser().getSysUser().getCompanyId());
        Integer add = ownerMapper.add(ownerdomin);
        //判断是否存在图片等
        if (ownerdomin.getMainFileList() != null && ownerdomin.getMainFileList().size() > 0) {
            bmFileInfoMapper.insertRentInfo(ownerdomin);
        }
        return add;
    }

    @Override
    public Integer edit(Ownerdomin ownerdomin) {
        Integer edit = ownerMapper.edit(ownerdomin);
        //删除对应保存到表里面的信息
        ownerMapper.deleteFileInfo(ownerdomin.getId());
        if (ownerdomin.getMainFileList() != null && ownerdomin.getMainFileList().size() > 0) {
           BmFileInfo bmFileInfo = new BmFileInfo();
           bmFileInfo.setTaskId(Long.valueOf(ownerdomin.getId()));
           bmFileInfo.setFileName(bmFileInfo.getFileName());
           bmFileInfo.setFileUrl(bmFileInfo.getFileUrl());
            bmFileInfoMapper.insertRentInfo(ownerdomin);
        }
        return edit;
    }

    @Override
    public Integer del(Ownerdomin ownerdomin) {
        return ownerMapper.del(ownerdomin);
    }

    @Override
    public String importMaProp(List<Ownerdomin> maPropInfoList, String  type, Long userId)  {
        if (StringUtils.isNull(maPropInfoList) || maPropInfoList.isEmpty()) {
            throw new ServiceException("导入的数据不能为空！");
        }

// 过滤掉空对象或空行
        maPropInfoList = maPropInfoList.stream()
                .filter(Objects::nonNull)
                .filter(item -> StringUtils.isNotEmpty(item.getMaName())) // 替换为你必须字段
                .collect(Collectors.toList());

        int failureNum = 0;
        StringBuilder failureMsg = new StringBuilder();
        StringBuilder successMsg = new StringBuilder();

        for (Ownerdomin devInfo : maPropInfoList) {
            try {
                BeanValidators.validateWithException(validator, devInfo);
                devInfo.setCreator(userId + "");
                Ownerdomin ownerdomin = new Ownerdomin();
                BeanUtils.copyProperties(devInfo, ownerdomin);
                ownerdomin.setType(type);
                ownerdomin.setCompanyId(SecurityUtils.getLoginUser().getSysUser().getCompanyId());
                ownerMapper.add(ownerdomin);
                successMsg.append(" 导入成功");
            } catch (Exception e) {
                failureNum++;
                String msg = " 导入失败：";
                failureMsg.append(msg).append(e.getMessage());
                log.error(msg, e);
            }
        }

        return successMsg.toString();
    }
}
