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

import cn.com.poc.common.constant.CommonConstant;
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 org.springframework.stereotype.Service;

import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
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);
    }

    @Override
    public BizSoftwareCopyrightDocRecordEntity getByRelationId(Long relationId) {
        BizSoftwareCopyrightDocRecordModel model = new BizSoftwareCopyrightDocRecordModel();
        model.setRelationId(relationId);
        model.setIsDeleted(CommonConstant.IsDeleted.N);
        List<BizSoftwareCopyrightDocRecordModel> models = this.repository.findByExample(model, null);
        if (CollectionUtils.isNotEmpty(models)) {
            return BizSoftwareCopyrightDocRecordConvert.modelToEntity(models.get(0));
        }
        return null;
    }

    public List<BizSoftwareCopyrightDocRecordEntity> findByExample(BizSoftwareCopyrightDocRecordEntity example,
                                                                   PagingInfo pagingInfo) {
        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) {
        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) {
        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);
            }
        }
    }

}