package com.koobietech.eas.service.impl;

import com.koobietech.eas.common.result.PageData;
import com.koobietech.eas.mbg.mapper.EasEduCategoryMapper;
import com.koobietech.eas.mbg.model.EasEduCategory;
import com.koobietech.eas.mbg.model.EasEduCategoryExample;
import com.koobietech.eas.service.EasEduCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class EasEduCategoryServiceImpl implements EasEduCategoryService {

    @Resource
    EasEduCategoryMapper easEduCategoryMapper;

    @Override
    public int addEasEduCategory(EasEduCategory easEduCategory) {
        easEduCategory.setCreateTime(new Date());
        easEduCategory.setModifyTime(new Date());
       return  easEduCategoryMapper.insert(easEduCategory);
    }

    @Override
    public int updateEasEduCategory(EasEduCategory easEduCategory) {
        return easEduCategoryMapper.updateByPrimaryKey( easEduCategory );
    }

    @Override
    public int deleteById(int id) {
        return easEduCategoryMapper.deleteByPrimaryKey( id );
    }

    @Override
    public EasEduCategory selectById(Integer id) {
        System.out.println( "ID:" + id );
        return easEduCategoryMapper.selectByPrimaryKey( id );
    }

    @Override
    public List<EasEduCategory> selectAll() {
        return easEduCategoryMapper.selectByExample(null);
    }

    @Override
    public PageData selectByCondition(EasEduCategory easEduCategory) {
        EasEduCategoryExample easEduCategoryExample = new EasEduCategoryExample();
        EasEduCategoryExample.Criteria criteria = easEduCategoryExample.createCriteria();
        if ( Objects.nonNull(easEduCategory) ) {
            if (Objects.nonNull(easEduCategory.getId())) {
                criteria.andIdEqualTo(easEduCategory.getId());
            }
            if (StringUtils.hasText(easEduCategory.getName())) {
                criteria.andNameLike("%" + easEduCategory.getName() + "%");
            }
            if (StringUtils.hasText(easEduCategory.getDescription())) {
                criteria.andDescriptionLike("%" + easEduCategory.getDescription() + "%");
            }
            if (Objects.nonNull(easEduCategory.getCreateTime())) {
                criteria.andCreateTimeGreaterThan(easEduCategory.getCreateTime());
            }
            if (Objects.nonNull(easEduCategory.getModifyTime())) {
                criteria.andCreateTimeGreaterThan(easEduCategory.getModifyTime());
            }
            if (Objects.nonNull(easEduCategory.getCreateUid())) {
                criteria.andCreateUidEqualTo(easEduCategory.getCreateUid());
            }
            if (StringUtils.hasText(easEduCategory.getDisabled())) {
                criteria.andDisabledEqualTo(easEduCategory.getDisabled());
            }
        }
        List<EasEduCategory> easEduCategories = easEduCategoryMapper.selectByExample(easEduCategoryExample);
        long l = easEduCategoryMapper.countByExample(easEduCategoryExample);
        return PageData.init( easEduCategories, l );
    }
}
