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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yifu.cloud.plus.v1.csp.entity.TCspAttaInfo;
import com.yifu.cloud.plus.v1.csp.mapper.TAttaInfoMapper;
import com.yifu.cloud.plus.v1.csp.service.TAttaInfoService;
import com.yifu.cloud.plus.v1.csp.vo.TAttaInfoVo;
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.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

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

    @Autowired
    private OSSUtil ossUtil;

    /**
     * 附件信息表简单分页查询
     *
     * @param tAttaInfo 附件信息表
     * @return
     */
    @Override
    public IPage<TCspAttaInfo> getTAttaInfoPage(Page<TCspAttaInfo> page, TCspAttaInfo tAttaInfo) {
        return baseMapper.getTAttaInfoPage(page, tAttaInfo);
    }

    /**
     * @Author fxj
     * @Description
     * @Date 13:48 2022/6/17
     * @Param
     * @return
    **/
    @Override
    public List<TCspAttaInfo> getTAttaInfoList(TCspAttaInfo tAttaInfo) {
        return baseMapper.getTAttaInfoList(tAttaInfo);
    }
    @Override
    public List<TCspAttaInfo> getTAttaInfoListByDoMainId(String domainId) {
        return baseMapper.getTAttaInfoListByDoMainId(domainId);
    }

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

	/**
	 * @Author fxj
	 * @Description
	 * @Date 13:48 2022/6/17
	 * @Param
	 * @return
	**/
    @Override
    public void update(List<TCspAttaInfo> attaInfoList, String domainId, String relationType) {
        if (attaInfoList != null) {
            for (TCspAttaInfo attaInfo : attaInfoList) {
                attaInfo.setDomainId(domainId);
                attaInfo.setRelationType(relationType);
                this.updateById(attaInfo);
            }
        }
    }

    /**
     * @Author fxj
     * @Description
     * @Date 13:48 2022/6/17
     * @Param
     * @return
    **/
    @Override
    public Boolean batchOssFileDelete(List<TCspAttaInfo> delAttaList) {
        try {
            for (TCspAttaInfo delAtta : delAttaList) {
                if (this.removeById(delAtta.getId())) {
                    ossUtil.deleteObject(null, delAtta.getAttaSrc());
                }
            }
            return true;
        } catch (Exception e) {
            log.error("附件删除异常：" + e.getMessage());
            return false;
        }
    }

    @Override
    public List<TCspAttaInfo> getKeenAtta(LocalDateTime dayStart, Collection<String> values) {
        return baseMapper.getKeenAtta(dayStart,values);
    }
	@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<TCspAttaInfo>> getAttaByApplyId(String applyId) {
		List<TCspAttaInfo> list = baseMapper.getAttaByApplyId(applyId);
		if (Common.isNotNull(list)) {
			URL url;
			for (TCspAttaInfo 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);
	}

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

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

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

	@Override
	public void deleteByDomainIdAndType(String domainId, int relationType) {
		// 学历
		if (relationType == 0) {
			baseMapper.deleteByDomainIdAndType(domainId, "0");
			baseMapper.deleteByDomainIdAndType(domainId, "26");
		} else if (relationType == 1) {
			// 执业资格证书
			baseMapper.deleteByDomainIdAndType(domainId, "2");
		} else if (relationType == 2) {
			// 其他附件
			baseMapper.deleteByDomainIdAndType(domainId, "27");
		}
	}

	@Override
	public List<TAttaInfoVo> getAttInfoByParam(String departNo, String startDate, String endDate) {
		return baseMapper.getAttInfoByParam(departNo,startDate,endDate);
	}
	@Override
	public int getAttInfoCountByParam(String departNo, String startDate, String endDate) {
		return baseMapper.getAttInfoCountByParam(departNo,startDate,endDate);
	}

}
