package com.koobietech.eas.service.impl;

import com.koobietech.eas.common.result.PageData;
import com.koobietech.eas.mbg.mapper.EasSysPermissionMapper;
import com.koobietech.eas.mbg.model.EasSysPermission;
import com.koobietech.eas.mbg.model.EasSysPermissionExample;
import com.koobietech.eas.service.EasSysPermissionService;
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 EasSysPermissionServiceImpl implements EasSysPermissionService {

    @Resource
    EasSysPermissionMapper easSysPermissionMapper;

    @Override
    public Boolean update(EasSysPermission permission) {
        permission.setModifyTime(new Date());
        return easSysPermissionMapper.updateByPrimaryKey(permission) == 1;
    }

    @Override
    public Boolean add(EasSysPermission permission) {
        permission.setCreateTime(new Date());
        permission.setModifyTime(new Date());
        return easSysPermissionMapper.insert(permission) == 1;
    }

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

    @Override
    public PageData query(EasSysPermission permission) {
        EasSysPermissionExample easSysPermissionExample = new EasSysPermissionExample();
        EasSysPermissionExample.Criteria criteria = easSysPermissionExample.createCriteria();
        if (Objects.nonNull(permission)) {

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

            if (Objects.nonNull(permission.getIsActive()) && permission.getIsActive() != 0) {
                criteria.andIsActiveEqualTo(permission.getIsActive());
            }

            if (StringUtils.hasText(permission.getName())) {
                criteria.andNameLike("%" + permission.getName() + "%");
            }

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

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

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

            if (StringUtils.hasText(permission.getDisabled())) {
                criteria.andDisabledEqualTo(permission.getDisabled());
            }
        }
        List<EasSysPermission> easSysPermissions = easSysPermissionMapper.selectByExample(easSysPermissionExample);
        long l = easSysPermissionMapper.countByExample(easSysPermissionExample);
        PageData ret = new PageData();
        ret.setData(easSysPermissions);
        ret.setTotal(l);
        return ret;
    }
}
