package com.koobietech.eas.service.impl;

import com.koobietech.eas.common.result.PageDataResult;
import com.koobietech.eas.mbg.mapper.EasSysDepartmentMapper;
import com.koobietech.eas.mbg.model.EasSysDepartment;
import com.koobietech.eas.mbg.model.EasSysDepartmentExample;
import com.koobietech.eas.service.EasSysDepartmentService;
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 EasSysDepartmentServiceImpl implements EasSysDepartmentService {

    @Resource
    EasSysDepartmentMapper easSysDepartmentMapper;

    @Override
    public Boolean update(EasSysDepartment department) {
        department.setModifyTime(new Date());
        return easSysDepartmentMapper.updateByPrimaryKey(department) == 1;
    }

    @Override
    public Boolean add(EasSysDepartment department) {
        department.setCreateTime(new Date());
        department.setModifyTime(new Date());
        return easSysDepartmentMapper.insert(department) == 1;
    }

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

    @Override
    public PageDataResult query(EasSysDepartment department) {

        EasSysDepartmentExample departmentExample = new EasSysDepartmentExample();
        EasSysDepartmentExample.Criteria criteria = departmentExample.createCriteria();
        if (Objects.nonNull(department)) {

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

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

            if (StringUtils.hasText(department.getDepname())) {
                criteria.andDepnameLike("%" + department.getDepname() + "%");
            }

            if (StringUtils.hasText(department.getEmail())) {
                criteria.andEmailLike("%" + department.getEmail() + "%");
            }

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

            if (StringUtils.hasText(department.getPhone())) {
                criteria.andPhoneLike("%" + department.getPhone() + "%");
            }

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

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

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

            if (StringUtils.hasText(department.getDisabled())) {
                criteria.andDisabledEqualTo(department.getDisabled());
            }
        }
        List<EasSysDepartment> departments =
                easSysDepartmentMapper.selectByExample(departmentExample);
        long l = easSysDepartmentMapper.countByExample(departmentExample);
        PageDataResult ret = new PageDataResult();
        ret.setData(departments);
        ret.setTotal(l);
        return ret;
    }
}
