package com.koobietech.eas.service.impl;

import com.koobietech.eas.common.result.PageData;
import com.koobietech.eas.mbg.mapper.EasSysUserinfoMapper;
import com.koobietech.eas.mbg.model.EasSysUserinfo;
import com.koobietech.eas.mbg.model.EasSysUserinfoExample;
import com.koobietech.eas.service.EasSysUserInfoService;
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 EasSysUserInfoServiceImpl implements EasSysUserInfoService {

    @Resource
    EasSysUserinfoMapper easSysUserinfoMapper;

    final String userInfoName = "admin";

    @Override
    public Boolean add(EasSysUserinfo userinfo) {
        //给用户信息设置默认密码
        userinfo.setPasswd("123456");
        userinfo.setCreateTime(new Date());
        userinfo.setModifyTime(new Date());
        return easSysUserinfoMapper.insert(userinfo) == 1;
    }

    public EasSysUserinfo getUserById(Long id) {
        return easSysUserinfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public Boolean delete(Long id) {
        //判断是否为admin用户信息，admin用户信息禁止删除
        EasSysUserinfo userinfo = getUserById(id);
        if (userinfo.getUsername().equals(userInfoName)) {
            return false;
        }
        return easSysUserinfoMapper.deleteByPrimaryKey(id) == 1;
    }

    @Override
    public Boolean update(EasSysUserinfo userinfo) {
        //判断是否为admin用户信息，admin用户信息中的Username禁止修改
        EasSysUserinfo user = getUserById(userinfo.getId());
        if (user.getUsername().equals(userInfoName)) {
            userinfo.setUsername(user.getUsername());
        }
        userinfo.setModifyTime(new Date());
        return easSysUserinfoMapper.updateByPrimaryKey(userinfo) == 1;
    }

    @Override
    public PageData query(EasSysUserinfo userinfo) {

        EasSysUserinfoExample easSysUserinfoExample = new EasSysUserinfoExample();
        EasSysUserinfoExample.Criteria criteria = easSysUserinfoExample.createCriteria();
        if (Objects.nonNull(userinfo)) {

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

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

            if (StringUtils.hasText(userinfo.getPasswd())) {
                criteria.andPasswdLike("%" + userinfo.getPasswd() + "%");
            }

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

            if (StringUtils.hasText(userinfo.getRelname())) {
                criteria.andRelnameLike("%" + userinfo.getRelname() + "%");
            }

            if (StringUtils.hasText(userinfo.getUsername())) {
                criteria.andUsernameLike("%" + userinfo.getUsername() + "%");
            }

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

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

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

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

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

        }
        List<EasSysUserinfo> easSysUserinfos = easSysUserinfoMapper.selectByExample(easSysUserinfoExample);
        long l = easSysUserinfoMapper.countByExample(easSysUserinfoExample);
        PageData ret = new PageData();
        ret.setData(easSysUserinfos);
        ret.setTotal(l);
        return ret;
    }
}
