package cn.wisenergy.service.impl;


import cn.wisenergy.common.utils.Md5Util;
import cn.wisenergy.common.utils.exception.BASE_RESP_CODE_ENUM;
import cn.wisenergy.common.utils.exception.BaseCustomException;
import cn.wisenergy.mapper.WorkDeptMapper;
import cn.wisenergy.mapper.WorkUserMapper;
import cn.wisenergy.model.app.WorkUser;
import cn.wisenergy.model.enums.ManagerEnum;
import cn.wisenergy.service.WorkUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * @author chenqi
 * @since 2021-01-19
 */
@Service
@Slf4j
public class WorkUserServiceImpl implements WorkUserService {
    @Autowired
    private WorkUserMapper workUserMapper;

    @Autowired
    private WorkDeptMapper workDeptMapper;

    private static final String LOGIN_SOURCE = "PC";
    @Override
    public WorkUser getById(Integer id) {
        log.info("WorkUserServiceImpl[]getById[]input.param.id:{}"+id);
        if (null == id) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        WorkUser workUser = workUserMapper.selectById(id);
        if (null == workUser) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.DEPT_NOT_HAVE_USER);
        }
        return workUser;
    }

    @Override
    public WorkUser getDeptManagerByIds(List<Integer> ids) {
        log.info("WorkUserServiceImpl[]getDeptManagerByIds[]input.param.ids:{}" + ids);
        if (CollectionUtils.isEmpty(ids)) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        QueryWrapper<WorkUser> wrapper = new QueryWrapper<>();
        wrapper.eq("level", 2);
        wrapper.in("id", ids);
        WorkUser workUser = workUserMapper.selectOne(wrapper);
        return workUser;

    }

    @Override
    public WorkUser login(String loginName, String password, String source) {
        log.info("WorkUserServiceImpl[]login[].input.param" + loginName + password + source);
        if (StringUtils.isEmpty(loginName) || StringUtils.isEmpty(password)) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        //MD5加密
        String md5Password = Md5Util.digestMD5(password);

        HashMap<String, Object> map = new HashMap<>();
        map.put("loginName", loginName);
        map.put("password", md5Password);
        WorkUser user = workUserMapper.getUserInfo(map);
        if (null == user) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.USER_PHONE_IS_ERROR);
        }

        //如果登录来源是PC,判断用户是不是管理员
        if (LOGIN_SOURCE.equals(source)) {
            if (user.getLevel() == ManagerEnum.NOT_MANAGER.getCode()) {
                throw new BaseCustomException(BASE_RESP_CODE_ENUM.THE_USER_NOT_MANAGER_PLASE_MANAGER_LOGIN);
            }
        }
        return user;
    }

    @Override
    public WorkUser getUserById(Integer userId) {
        log.info("UserServiceImpl[]getUserById[]input.param.userId" + userId);
        if (null == userId) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }

        WorkUser user = workUserMapper.getUserById(userId);
        //查询失败
        if (null == user) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.QUERY_USER_INFO_FAIL);
        }

        log.info("UserServiceImpl[]getUserById[]get.user.info.success");
        return user;
    }
}
