package com.koobietech.eas.service.impl;
import com.koobietech.eas.common.result.PageData;
import com.koobietech.eas.mbg.mapper.EasEduSubjectsMapper;
import com.koobietech.eas.mbg.model.EasEduCategoryExample;
import com.koobietech.eas.mbg.model.EasEduSubjects;
import com.koobietech.eas.mbg.model.EasEduSubjectsExample;
import com.koobietech.eas.service.EasEduSubjectsService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class EasEduSubjectsServiceImpl implements EasEduSubjectsService {

    @Resource
    EasEduSubjectsMapper easEduSubjectsMapper;


    @Override
    public int addSubject(EasEduSubjects easEduSubjects) {
        easEduSubjects.setCreateTime(new Date());
        easEduSubjects.setModifyTime(new Date());
        return easEduSubjectsMapper.insert(easEduSubjects);
    }

    @Override
    public int updateSubjects(EasEduSubjects easEduSubjects) {
        return easEduSubjectsMapper.updateByPrimaryKey(easEduSubjects);
    }

    @Override
    public int deleteSubjects(int id) {
        return easEduSubjectsMapper.deleteByPrimaryKey( id );
    }

    @Override
    public EasEduSubjects selectById(Integer id) {
        System.out.println("Id" + id);
        return easEduSubjectsMapper.selectByPrimaryKey(id);
    }

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

    @Override
    public PageData selectByCondition(EasEduSubjects easEduSubjects) {
        EasEduSubjectsExample easEduSubjectsExample = new EasEduSubjectsExample();
        EasEduSubjectsExample.Criteria criteria = easEduSubjectsExample.createCriteria();
        if ( Objects.nonNull(easEduSubjects) ) {
            if (Objects.nonNull(easEduSubjects.getId())) {
                criteria.andIdEqualTo(easEduSubjects.getId());
            }
            if (StringUtils.hasText(easEduSubjects.getName())) {
                criteria.andNameLike("%" + easEduSubjects.getName() + "%");
            }
//            if (StringUtils.hasText(easEduSubjects.getDescription())) {
//                criteria.andDescriptionLike("%" + easEduSubjects.getDescription() + "%");
//            }
            if (Objects.nonNull(easEduSubjects.getCategoryId())){
                criteria.andCategoryIdEqualTo(easEduSubjects.getCategoryId());
            }
            if (Objects.nonNull(easEduSubjects.getCreateTime())) {
                criteria.andCreateTimeGreaterThan(easEduSubjects.getCreateTime());
            }
            if (Objects.nonNull(easEduSubjects.getModifyTime())) {
                criteria.andCreateTimeGreaterThan(easEduSubjects.getModifyTime());
            }
            if (Objects.nonNull(easEduSubjects.getCreateUid())) {
                criteria.andCreateUidEqualTo(easEduSubjects.getCreateUid());
            }
            if (StringUtils.hasText(easEduSubjects.getDisabled())) {
                criteria.andDisabledEqualTo(easEduSubjects.getDisabled());
            }
        }
        List<EasEduSubjects> easEduSubjects1 = easEduSubjectsMapper.selectByExample(easEduSubjectsExample);
        long l = easEduSubjectsMapper.countByExample(easEduSubjectsExample);
        return PageData.init(easEduSubjects1,l);
    }
}
