package com.yifu.cloud.plus.v1.batch.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yifu.cloud.plus.v1.batch.entity.TAttaInfo;
import com.yifu.cloud.plus.v1.batch.mapper.TAttaInfoMapper;
import com.yifu.cloud.plus.v1.batch.service.TAttaInfoService;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.OSSUtil;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URL;
import java.util.List;

/**
 * 附件信息表
 *
 * @author pwang
 * @date 2021-02-19 16:16:07
 */
@Service
public class TAttaInfoServiceImpl extends ServiceImpl<TAttaInfoMapper, TAttaInfo> implements TAttaInfoService {

    @Autowired
    private OSSUtil ossUtil;

    @Override
    public List<TAttaInfo> getTAttaInfoListByDoMainId(String domainId) {
        return baseMapper.getTAttaInfoListByDoMainId(domainId);
    }

	@Override
	public TAttaInfo add(TAttaInfo attaInfo) {
		this.save(attaInfo);
		return attaInfo;
	}

	@Override
	public Boolean updateDomainId(String domainId, List<String> ids){
		if (Common.isEmpty(domainId) || !Common.isNotEmpty(ids)){
			return false;
		}
		return  baseMapper.updateDomainId(domainId,ids) > 0;
	}

	@Override
	public R<List<TAttaInfo>> getAttaByApplyId(String applyId) {
		List<TAttaInfo> list = baseMapper.getAttaByApplyId(applyId);
		if (Common.isNotNull(list)) {
			URL url;
			for (TAttaInfo atta:list) {
				url = ossUtil.getObjectUrl(null, atta.getAttaSrc());
				atta.setAttaUrl(url.toString());
			}
			return R.ok(list);
		}else{
			return R.failed("没有附件");
		}
	}

	@Override
	public void deleteByDomainId(String domainId) {
		baseMapper.deleteByDomainId(domainId);
	}

}
