package com.mvc.service.beihai;


import com.mvc.model.beihai.DdUser;
import com.mvc.netty.dealconstant.ResourcePath;
import com.mvc.netty.dealconstant.ResourceTag;
import com.mvc.netty.dealconstant.ResultTag;
import com.mvc.netty.dealconstant.ResultType;
import com.mvc.utils.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserService {

    @Autowired
    private Environment env;

    public final String defaultPasswd = "beihai@1";

    public Map<String,DdUser> userMap = new HashMap<>();

    /**
     * 查询用户
     */
    public Map<String, Object> login(DdUser ddUser) {
        DdUser ddUser1 = new DdUser(2,"bhdc1","9334782673ee8564867b6daad97a1362",1,2,"广西投资集团北海发电有限公司","bhdc.png","bhdc_name.png",
                "[{\"to\":\"/datashow\",\"icon\":\"chart-trending-o\",\"cname\":\"原始数据\"}]",false,"");
        DdUser ddUser2 = new DdUser(3,"wbc","9334782673ee8564867b6daad97a1362",1,2,"广西投资集团北海发电有限公司","bhdc.png","bhdc_name.png",
                "[{\"to\":\"/datashow\",\"icon\":\"chart-trending-o\",\"cname\":\"原始数据\"}]",false,"");
        userMap.put("bhdc1",ddUser1);
        userMap.put("wbc",ddUser2);
        // http://114.242.31.4:6090+/image/+company/logo/bhdc.png
        var baseLogoUrl = env.getProperty(ResourceTag.ResourceUrlTag.getTag()) + ResourcePath.Image.getPath() + ResourcePath.Company.getPath();
        // http://114.242.31.4:6090+/image/+company/name/bhdc_name.png
        var baseNameUrl = env.getProperty(ResourceTag.ResourceUrlTag.getTag()) + ResourcePath.Image.getPath() + ResourcePath.CompanyName.getPath();
        Map<String, Object> retMap = new HashMap<>();
        DdUser user = userMap.get(ddUser.getUserName());

        if (user != null && user.getLogo() != null) {
            user.setLogo(baseLogoUrl + user.getLogo());
            user.setNamePic(baseNameUrl + user.getNamePic());
        }

        if (user == null) {
            retMap.put(ResultTag.ERROR.getTag(), ResultType.FAILURE.getResult());
            retMap.put(ResultTag.MESSAGE.getTag(), "用户名密码错误");
        } else {
            // 超级密码的情况
            if (user.getPasswd2().equals(SecurityUtil.toMD5(ddUser.getPasswd()))) {
                retMap.put(ResultTag.ERROR.getTag(), ResultType.SUCCESS.getResult());
                retMap.put(ResultTag.DATA.getTag(), user);
                return retMap;
            }
            // 该用户密码为空
            if (user.getPasswd() == null || "".equals(user.getPasswd())) {
                retMap.put(ResultTag.ERROR.getTag(), ResultType.FAILURE.getResult());
                retMap.put(ResultTag.MESSAGE.getTag(), "用户名密码错误");
                return retMap;
            }
            //密码校验
            if (user.getPasswd().equals(SecurityUtil.toMD5(ddUser.getPasswd()))) {
                retMap.put(ResultTag.ERROR.getTag(), ResultType.SUCCESS.getResult());
                retMap.put(ResultTag.DATA.getTag(), user);
            } else {
                retMap.put(ResultTag.ERROR.getTag(), ResultType.FAILURE.getResult());
                retMap.put(ResultTag.MESSAGE.getTag(), "用户名密码错误");
            }
        }

        return retMap;
    }

   /* *//**
     * 根据companyId，检索分类信息
     *//*
    public Map<String, Object> findByCompany(DdUser ddUser) {
        Map<String, Object> retMap = new HashMap<>();
        if (ddUser.getCompanyId() == null) {
            retMap.put(ResultTag.ERROR.getTag(), ResultType.FAILURE.getResult());
            retMap.put(ResultTag.MESSAGE.getTag(), "未选择企业");
            return retMap;
        }
        var classifyList = dbUserDao.selectByCompany(ddUser);

        retMap.put(ResultTag.ERROR.getTag(), ResultType.SUCCESS.getResult());
        retMap.put(ResultTag.DATA.getTag(), classifyList);
        return retMap;
    }

    *//**
     * 保存
     *//*
    public Map<String, Object> save(DdUser ddUser) {
        Map<String, Object> retMap = new HashMap<>();
        try {
            ddUser.setUid(ddUser.getUid() == 0? null : ddUser.getUid());
            var count = 0;
            if (ddUser.getUid()== null) {
                count = dbUserDao.countByCompany(ddUser);
                if (count == 0) {
                    ddUser.setPasswd(SecurityUtil.toMD5(defaultPasswd));
                    dbUserDao.insert(ddUser);
                    retMap.put(ResultTag.ERROR.getTag(), ResultType.SUCCESS.getResult());
                } else {
                    retMap.put(ResultTag.ERROR.getTag(), ResultType.FAILURE.getResult());
                    retMap.put(ResultTag.MESSAGE.getTag(), "用户已经存在");
                }
            } else {
                count = dbUserDao.countByCompanyExcept(ddUser);
                if (count > 0) {
                    retMap.put(ResultTag.ERROR.getTag(), ResultType.FAILURE.getResult());
                    retMap.put(ResultTag.MESSAGE.getTag(), "用户已经存在");
                } else {
                    dbUserDao.updateByPrimaryKey(ddUser);
                    retMap.put(ResultTag.ERROR.getTag(), ResultType.SUCCESS.getResult());
                }
            }
        } catch (Exception e) {
            retMap.put(ResultTag.ERROR.getTag(), ResultType.FAILURE.getResult());
            retMap.put(ResultTag.MESSAGE.getTag(), e.getMessage());
        }
        return retMap;
    }

    *//**
     * 移除
     *//*
    public Map<String, Object> remove(DdUser ddUser) {
        Map<String, Object> retMap = new HashMap<>();
        try {
            if (ddUser.getUid() != null) {
                dbUserDao.deleteByPrimaryKey(ddUser.getUid());
                retMap.put(ResultTag.ERROR.getTag(), ResultType.SUCCESS.getResult());
            } else {
                retMap.put(ResultTag.ERROR.getTag(), ResultType.FAILURE.getResult());
                retMap.put(ResultTag.MESSAGE.getTag(), "UID不能为空");
            }
        } catch (Exception e) {
            retMap.put(ResultTag.ERROR.getTag(), ResultType.FAILURE.getResult());
            retMap.put(ResultTag.MESSAGE.getTag(), e.getMessage());
        }
        return retMap;
    }

    *//**
     * 重置密码
     *//*
    public Map<String, Object> resetPassword(DdUser ddUser) {
        Map<String, Object> retMap = new HashMap<>();
        try {
            if (ddUser.getUserName() != null) {
                DdUser user = dbUserDao.selectByUsername(ddUser.getUserName());
                if (!SecurityUtil.toMD5(ddUser.getPasswd2()).equals(user.getPasswd())) {
                    retMap.put(ResultTag.ERROR.getTag(), ResultType.FAILURE.getResult());
                    retMap.put(ResultTag.MESSAGE.getTag(), "旧密码输入错误");
                    return retMap;
                }
                ddUser.setPasswd(SecurityUtil.toMD5(ddUser.getPasswd()));
                dbUserDao.resetPassword(ddUser);
                retMap.put(ResultTag.ERROR.getTag(), ResultType.SUCCESS.getResult());
            } else {
                retMap.put(ResultTag.ERROR.getTag(), ResultType.FAILURE.getResult());
                retMap.put(ResultTag.MESSAGE.getTag(), "重置密码失败");
            }
        } catch (Exception e) {
            retMap.put(ResultTag.ERROR.getTag(), ResultType.FAILURE.getResult());
            retMap.put(ResultTag.MESSAGE.getTag(), e.getMessage());

        }
        return retMap;
    }*/
}
