package com.koobietech.eas.service.impl;

import com.koobietech.eas.common.result.PageDataResult;
import com.koobietech.eas.mbg.mapper.EasEduClassroomMapper;
import com.koobietech.eas.mbg.model.EasEduClassroom;
import com.koobietech.eas.mbg.model.EasEduClassroomExample;
import com.koobietech.eas.service.EasEduClassroomService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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

@Service
public class EasEduClassroomServiceImpl implements EasEduClassroomService {
    @Resource
    EasEduClassroomMapper easEduClassroomMapper;

    @Override
    public Boolean update(EasEduClassroom classroom) {
        classroom.setModifyTime(new Date());
        return easEduClassroomMapper.updateByPrimaryKey(classroom) == 1;
    }

    @Override
    public Boolean add(EasEduClassroom classroom) {
        classroom.setCreateTime(new Date());
        classroom.setModifyTime(new Date());
        return easEduClassroomMapper.insert(classroom) == 1;
    }

    @Override
    public Boolean delete(Integer id) {
        return easEduClassroomMapper.deleteByPrimaryKey(id) == 1;
    }

    @Override
    public PageDataResult query(EasEduClassroom classroom) {

        EasEduClassroomExample easEduClassroomExample = new EasEduClassroomExample();
        EasEduClassroomExample.Criteria criteria = easEduClassroomExample.createCriteria();
        if (Objects.nonNull(classroom)) {

            if (Objects.nonNull(classroom.getId()) && classroom.getId() != 0) {
                criteria.andIdEqualTo(classroom.getId());
            }

            if (StringUtils.hasText(classroom.getName())) {
                criteria.andAddressLike("%" + classroom.getName() + "%");
            }

            if (StringUtils.hasText(classroom.getAddress())) {
                criteria.andAddressLike("%" + classroom.getAddress() + "%");
            }

            if (Objects.nonNull(classroom.getManagerId()) && classroom.getManagerId() != 0) {
                criteria.andManagerLike("%" + classroom.getManagerId() + "%");
            }

            if (StringUtils.hasText(classroom.getManager())) {
                criteria.andManagerLike("%" + classroom.getManager() + "%");
            }

            if (Objects.nonNull(classroom.getFloor()) && classroom.getFloor() != 0) {
                criteria.andFloorEqualTo(classroom.getFloor());
            }
            if (Objects.nonNull(classroom.getCapacity()) && classroom.getCapacity() != 0) {
                criteria.andCapacityEqualTo(classroom.getCapacity());
            }

            if (StringUtils.hasText(classroom.getComment())) {
                criteria.andCommentLike("%" + classroom.getComment() + "%");
            }

            if (Objects.nonNull(classroom.getCreateTime())) {
                criteria.andCreateTimeGreaterThanOrEqualTo(classroom.getCreateTime());
            }

            if (Objects.nonNull(classroom.getModifyTime())) {
                criteria.andModifyTimeGreaterThanOrEqualTo(classroom.getModifyTime());
            }

            if (Objects.nonNull(classroom.getCreateUid()) && classroom.getCreateUid() != 0) {
                criteria.andCreateUidEqualTo(classroom.getCreateUid());
            }

            if (StringUtils.hasText(classroom.getDisabled())) {
                criteria.andDisabledEqualTo(classroom.getDisabled());
            }


        }
        List<EasEduClassroom> classrooms =
                easEduClassroomMapper.selectByExample(easEduClassroomExample);
        long l = easEduClassroomMapper.countByExample(easEduClassroomExample);
        PageDataResult ret = new PageDataResult();
        ret.setData(classrooms);
        ret.setTotal(l);
        return ret;
    }

}
