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

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

import cn.com.poc.human_resources.convert.BizHumanResourceResumeContentConvert;
import cn.com.poc.human_resources.entity.BizHumanResourceResumeContentEntity;
import cn.com.poc.human_resources.model.BizHumanResourceResumeContentModel;
import cn.com.poc.human_resources.query.ResumeContentQueryCondition;
import cn.com.poc.human_resources.query.ResumeContentQueryItem;
import cn.com.poc.human_resources.repository.BizHumanResourceResumeContentRepository;
import cn.com.poc.human_resources.service.BizHumanResourceResumeContentService;
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 BizHumanResourceResumeContentServiceImpl extends BaseServiceImpl
        implements BizHumanResourceResumeContentService {

    @Resource
    private BizHumanResourceResumeContentRepository repository;

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


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

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

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

        if (entity.getResumeId() != null) {
            model.setResumeId(entity.getResumeId());
        }
        if (entity.getPositionId() != null) {
            model.setPositionId(entity.getPositionId());
        }
        if (entity.getResumeContent() != null) {
            model.setResumeContent(entity.getResumeContent());
        }
        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());
        }
        BizHumanResourceResumeContentModel saveModel = this.repository.save(model);
        return BizHumanResourceResumeContentConvert.modelToEntity(saveModel);
    }

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

    @Override
    public List<ResumeContentQueryItem> searchByResumeContent(String query, Long positionId) {
        ResumeContentQueryCondition resumeContentQueryCondition = new ResumeContentQueryCondition();
        resumeContentQueryCondition.setQuery(query);
        resumeContentQueryCondition.setPositionId(positionId);
        return this.sqlDao.query(resumeContentQueryCondition, ResumeContentQueryItem.class);
    }
}