package com.example.hospital.api.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
import com.example.hospital.api.db.dao.MisUserDao;
import com.example.hospital.api.service.MisUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Service
public class MisUserServiceImpl implements MisUserService {
    @Resource
    private MisUserDao misUserDao;


    @Override
    public Integer login(Map param) {
        String username = MapUtil.getStr(param, "username");
        String password = MapUtil.getStr(param, "password");
        MD5 md5 = MD5.create();
        String temp = md5.digestHex(username);
        String tempStart = StrUtil.subWithLength(temp, 0, 6);
        String tempEnd = StrUtil.subSuf(temp, temp.length() - 3);
        password = md5.digestHex(tempStart + password + tempEnd);
        param.replace("password", password);
        Integer userId = misUserDao.login(param);
        return userId;
    }

    public String updatePassword(Map param) {
        int id = MapUtil.getInt(param, "id");
        String password = MapUtil.getStr(param, "password");
        String newPassword = MapUtil.getStr(param, "newPassword");

        String username = misUserDao.searchUsernameById(id);
        MD5 md5 = MD5.create();
        String temp = md5.digestHex(username);
        String tempStart = StrUtil.subWithLength(temp, 0, 6);
        String tempEnd = StrUtil.subSuf(temp, temp.length() - 3);
        password = md5.digestHex(tempStart + password + tempEnd);
        param.replace("password", password);
        param.put("username", username);

        Integer userId = misUserDao.login(param);
        if (userId == null || userId != id) {
            return "原始密码不正确";
        }

        newPassword = md5.digestHex(tempStart + newPassword + tempEnd);
        param.replace("newPassword", newPassword);
        int num = misUserDao.updatePassword(param);
        if (num == 1) {
            return "密码更新成功";
        }
        else {
            return "密码更新失败";
        }
    }
}
