package com.app.admin.framework.system.administrator.log;

import com.app.admin.framework.system.administrator.controller.SysAdministratorController;
import com.app.admin.framework.system.administrator.model.SysAdministrator;
import com.app.framework.core.log.intercept.LogInterceptSupport;
import com.app.framework.core.log.result.LogResult;
import com.app.framework.core.support.auth.LoginUser;
import com.app.framework.core.support.enums.BaseEnum;
import com.app.framework.core.support.enums.common.StateEnum;
import com.app.framework.core.utils.request.SessionUtil;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * 系统用户日志记录器
 */
@Component
public class SysAdministratorLogIntercept extends LogInterceptSupport<SysAdministratorController> {

    public LogResult saveByEntity(HttpServletRequest request, SysAdministrator entity) {
        LogResult result = new LogResult();
        result.setOperType(entity.getOid() == null ? "添加用户" : "修改用户");
        result.addColumn("realName", "姓名", entity.getRealName());
        result.addColumn("username", "登录名", entity.getUsername());
        return result;
    }

    public LogResult deleteById(HttpServletRequest request, Long oid) {
        LogResult result = new LogResult();
        result.setOperType("删除用户");
        result.addColumn("oid", "用户ID", oid);
        return result;
    }

    public LogResult batchDelete(HttpServletRequest request, Long oids[]) {
        LogResult result = new LogResult();
        result.setOperType("删除用户");
        result.addColumn("oid", "用户ID", Arrays.toString(oids));
        return result;
    }

    public LogResult updateStatus(Long oid, Integer state) {
        StateEnum stateEnum = BaseEnum.getByValue(StateEnum.class, state);
        LogResult result = new LogResult();
        result.setOperType("修改用户状态");
        result.addColumn("oid", "用户ID", oid);
        result.addColumn("state", "用户状态", stateEnum.getText());
        return result;
    }

    public LogResult updatePwd(HttpServletRequest request, String userPwd, String password) {
        LoginUser loginUser = SessionUtil.getCurrentUser(request);
        LogResult result = new LogResult();
        result.setOperType("修改密码");
        result.addColumn("oid", "用户ID", loginUser.getId());
        result.addColumn("realName", "姓名", loginUser.getRealName());
        return result;
    }

    public LogResult resetPwd(Long oid) {
        LogResult result = new LogResult();
        result.setOperType("重置密码");
        result.addColumn("oid", "用户ID", oid);
        return result;
    }
}
