package com.hs.admin.service.impl;

import com.hs.admin.common.enums.StateType;
import com.hs.admin.mapper.SysUserMapper;
import com.hs.admin.model.SysUser;
import com.hs.admin.model.reqmodel.AddUserReq;
import com.hs.admin.model.reqmodel.UpdateUserReq;
import com.hs.admin.model.reqmodel.UserReq;
import com.hs.admin.model.respmodel.SysUserList;
import com.hs.admin.service.DicOrgService;
import com.hs.admin.service.SysRoleService;
import com.hs.admin.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private DicOrgService dicOrgService;

    @Override
    public SysUser findByLoginName(String loginName) {
        return sysUserMapper.selectByCode(loginName);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addUser(Long userId, AddUserReq user) {
        if (user != null) {
            //添加用户信息
            SysUser sysUser = new SysUser();
            sysUser.setUserCode(user.getUserCode());
            sysUser.setUserName(user.getUserName());
            sysUser.setPassword(user.getPassword());
            sysUser.setIdcard(user.getIdcard());
            sysUser.setMobile(user.getMobile());
            sysUser.setSex(user.getSex());
            sysUser.setCreateDate(new Date());
            sysUser.setCreateUserid(userId);
            sysUser.setState((short) StateType.ON.getValue());
            sysUser.setId(sysUserMapper.selectId());
            sysUserMapper.insert(sysUser);

            //添加角色信息
            sysRoleService.saveUserRole(userId, (long) sysUser.getId(), user.getRoleId());

            //添加科室信息
            dicOrgService.saveUserOrg(userId, (long) sysUser.getId(), user.getOrg());

            return true;
        }
        return false;
    }

    @Override
    public boolean deleteUser(UserReq.DeleteUserReq user) {
        if (user != null) {
            SysUser sysUser = sysUserMapper.selectByPrimaryKey(user.getUserId());
            if (sysUser != null) {
                sysUser.setState(sysUser.getState().equals((short) StateType.OFF.getValue()) ? (short) StateType.ON.getValue() : (short) StateType.OFF.getValue());
                sysUserMapper.updateByPrimaryKey(sysUser);
                return true;
            }
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateUser(UpdateUserReq user) {
        if (user != null) {
            SysUser sysUser = sysUserMapper.selectByPrimaryKey(user.getUserId());
            if (sysUser != null) {
                //修改用户
                sysUser.setUserCode(user.getUserCode());
                sysUser.setUserName(user.getUserName());
                sysUser.setPassword(user.getPassword());
                sysUser.setIdcard(user.getIdcard());
                sysUser.setMobile(user.getMobile());
                sysUser.setSex(user.getSex());
                sysUserMapper.updateByPrimaryKey(sysUser);

                //修改角色信息
                sysRoleService.saveUserRole(1l, (long) sysUser.getId(), user.getRoleId());

                //修改科室信息
                dicOrgService.saveUserOrg(1l, (long) sysUser.getId(), user.getOrg());

                return true;
            }
        }
        return false;
    }

    @Override
    public List<SysUserList> getAll(UserReq.GetUserReq user) {
        List<SysUserList> list = sysUserMapper.selectUserList();
        if (user.getUserName() != null) {
            list = list.stream().filter(
                    o -> (o.getUserName() != null && o.getUserName().toLowerCase().contains(user.getUserName().toLowerCase())))
                    .collect(Collectors.toList());
        }
        return list;
    }

    @Override
    public SysUserList info(Integer userId) {
        SysUserList sysUser = sysUserMapper.selectUserById(userId);
        return sysUser;
    }
}
