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

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.com.poc.ai_software_copyright.convert.BizSoftwareCopyrightConvert;
import cn.com.poc.ai_software_copyright.entity.BizSoftwareCopyrightEntity;
import cn.com.poc.ai_software_copyright.model.BizSoftwareCopyrightModel;
import cn.com.poc.ai_software_copyright.query.SoftwareCopyrightQueryCondition;
import cn.com.poc.ai_software_copyright.query.SoftwareCopyrightQueryItem;
import cn.com.poc.ai_software_copyright.repository.BizSoftwareCopyrightRepository;
import cn.com.poc.ai_software_copyright.service.BizSoftwareCopyrightService;
import cn.com.yict.framemax.core.service.impl.BaseServiceImpl;
import cn.com.yict.framemax.data.model.PagingInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

@Service
public class BizSoftwareCopyrightServiceImpl extends BaseServiceImpl
        implements BizSoftwareCopyrightService {

    @Resource
    private BizSoftwareCopyrightRepository repository;

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


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

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

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

        if (entity.getBaseInfo() != null) {
            model.setBaseInfo(entity.getBaseInfo());
        }
        if (entity.getTechnicalContent() != null) {
            model.setTechnicalContent(entity.getTechnicalContent());
        }
        if (entity.getReferenceDocumentFileUrl() != null) {
            model.setReferenceDocumentFileUrl(entity.getReferenceDocumentFileUrl());
        }
        if (entity.getGeneratedDocType() != null) {
            model.setGeneratedDocType(entity.getGeneratedDocType());
        }
        if (entity.getGeneratedStatus() != null) {
            model.setGeneratedStatus(entity.getGeneratedStatus());
        }
        if (entity.getErrorMess() != null) {
            model.setErrorMess(entity.getErrorMess());
        }
        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());
        }
        BizSoftwareCopyrightModel saveModel = this.repository.save(model);
        return BizSoftwareCopyrightConvert.modelToEntity(saveModel);
    }

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

    @Override
    public List<SoftwareCopyrightQueryItem> softwareCopyrightQuery(SoftwareCopyrightQueryCondition condition,
                                                                   PagingInfo pagingInfo) {
        return this.sqlDao.query(condition, SoftwareCopyrightQueryItem.class, pagingInfo);
    }
}