package cn.com.poc.ai_software_copyright.service.impl;

import cn.com.yict.framemax.core.exception.BusinessException;
import cn.com.yict.framemax.core.service.impl.BaseServiceImpl;
import cn.com.poc.ai_software_copyright.service.BizSoftwareCopyrightDocRecordService;
import cn.com.poc.ai_software_copyright.model.BizSoftwareCopyrightDocRecordModel;
import cn.com.poc.ai_software_copyright.entity.BizSoftwareCopyrightDocRecordEntity;
import cn.com.poc.ai_software_copyright.convert.BizSoftwareCopyrightDocRecordConvert;
import cn.com.poc.ai_software_copyright.repository.BizSoftwareCopyrightDocRecordRepository;
import cn.com.yict.framemax.data.model.PagingInfo;
import cn.hutool.core.util.ZipUtil;
import org.springframework.stereotype.Service;

import org.apache.commons.collections4.CollectionUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
import java.util.List;
import javax.annotation.Resource;

import org.springframework.util.Assert;

@Service
public class BizSoftwareCopyrightDocRecordServiceImpl extends BaseServiceImpl
        implements BizSoftwareCopyrightDocRecordService {

    @Resource
    private BizSoftwareCopyrightDocRecordRepository repository;

    public BizSoftwareCopyrightDocRecordEntity get(java.lang.Long id) throws Exception {
        Assert.notNull(id);
        BizSoftwareCopyrightDocRecordModel model = this.repository.get(id);
        if (model == null) {
            return null;
        }
        if ("Y".equals(model.getIsDeleted())) {
            return null;
        }
        return BizSoftwareCopyrightDocRecordConvert.modelToEntity(model);
    }


    public List<BizSoftwareCopyrightDocRecordEntity> findByExample(BizSoftwareCopyrightDocRecordEntity example,
                                                                   PagingInfo pagingInfo) throws Exception {
        List<BizSoftwareCopyrightDocRecordEntity> result = new ArrayList<BizSoftwareCopyrightDocRecordEntity>();
        BizSoftwareCopyrightDocRecordModel model = new BizSoftwareCopyrightDocRecordModel();
        if (example != null) {
            model = BizSoftwareCopyrightDocRecordConvert.entityToModel(example);
        }
        model.setIsDeleted("N");
        List<BizSoftwareCopyrightDocRecordModel> models = this.repository.findByExample(model, pagingInfo);
        if (CollectionUtils.isNotEmpty(models)) {
            result = models.stream().map(BizSoftwareCopyrightDocRecordConvert::modelToEntity)
                    .collect(Collectors.toList());
        }
        return result;
    }

    public BizSoftwareCopyrightDocRecordEntity save(BizSoftwareCopyrightDocRecordEntity entity) throws Exception {
        Assert.notNull(entity);
        entity.setId(null);
        entity.setIsDeleted("N");
        BizSoftwareCopyrightDocRecordModel model = BizSoftwareCopyrightDocRecordConvert.entityToModel(entity);
        BizSoftwareCopyrightDocRecordModel saveModel = this.repository.save(model);
        return BizSoftwareCopyrightDocRecordConvert.modelToEntity(saveModel);
    }

    public BizSoftwareCopyrightDocRecordEntity update(BizSoftwareCopyrightDocRecordEntity entity) throws Exception {
        Assert.notNull(entity);
        Assert.notNull(entity.getId(), "update pk can not be null");
        BizSoftwareCopyrightDocRecordModel model = this.repository.get(entity.getId());

        if (entity.getRelationId() != null) {
            model.setRelationId(entity.getRelationId());
        }
        if (entity.getInformation() != null) {
            model.setInformation(entity.getInformation());
        }
        if (entity.getPcOperatingManual() != null) {
            model.setPcOperatingManual(entity.getPcOperatingManual());
        }
        if (entity.getPhOperatingManual() != null) {
            model.setPhOperatingManual(entity.getPhOperatingManual());
        }
        if (entity.getSourceCode() != null) {
            model.setSourceCode(entity.getSourceCode());
        }
        if (entity.getAggregationUrl() != null) {
            model.setAggregationUrl(entity.getAggregationUrl());
        }
        if (entity.getIsDeleted() != null) {
            model.setIsDeleted(entity.getIsDeleted());
        }
        if (entity.getCreator() != null) {
            model.setCreator(entity.getCreator());
        }
        if (entity.getCreatedTime() != null) {
            model.setCreatedTime(entity.getCreatedTime());
        }
        if (entity.getModifier() != null) {
            model.setModifier(entity.getModifier());
        }
        if (entity.getModifiedTime() != null) {
            model.setModifiedTime(entity.getModifiedTime());
        }
        if (entity.getSysVersion() != null) {
            model.setSysVersion(entity.getSysVersion());
        }
        BizSoftwareCopyrightDocRecordModel saveModel = this.repository.save(model);
        return BizSoftwareCopyrightDocRecordConvert.modelToEntity(saveModel);
    }

    public void deletedById(java.lang.Long id) throws Exception {
        Assert.notNull(id);
        BizSoftwareCopyrightDocRecordModel model = this.repository.get(id);
        if (model != null) {
            if ("N".equals(model.getIsDeleted())) {
                model.setIsDeleted("Y");
                this.repository.save(model);
            }
        }
    }

}