package com.skr.mdm.handle;

import cn.hutool.json.JSONUtil;
import com.skr.mdm.dao.DeviceEquipmentDao;
import com.skr.mdm.dao.LogAdminDao;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.entity.LogAdminDO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author nfq
 * @date 2020/9/22 10:25
 */
@Component
public class LogAdminHandler {

    @Autowired
    private LogAdminDao logAdminDao;
    @Autowired
    private DeviceEquipmentDao equipmentDao;

    /**
     * log with userIdList
     * @param loginAdminDTO
     * @param userIdList
     * @param logType
     * @param content
     * @param module
     */
    public void log(LoginAdminDTO loginAdminDTO, List<Integer> userIdList, Integer logType, String content, String module) {
        LogAdminDO logAdminDO = new LogAdminDO();
        logAdminDO.setCompanyId(loginAdminDTO.getCompanyId());
        logAdminDO.setAdminId(loginAdminDTO.getId());
        logAdminDO.setOperator(loginAdminDTO.getAdminName());
        logAdminDO.setLogType(logType);
        logAdminDO.setContent(content);
        if (CollectionUtils.isNotEmpty(userIdList)) {
            List<String> equipmentNameList = equipmentDao.listEquipmentNameByUserId(userIdList);
            logAdminDO.setOperateUser(JSONUtil.toJsonStr(JSONUtil.parseArray(equipmentNameList)));
        }
        logAdminDO.setOperatorMessage(module);
        logAdminDao.insert(logAdminDO);
    }

    /**
     * log with equipmentName
     * @param loginAdminDTO
     * @param equipmentName
     * @param logType
     * @param content
     * @param module
     */
    public void log(LoginAdminDTO loginAdminDTO, String equipmentName, Integer logType, String content, String module) {
        LogAdminDO logAdminDO = new LogAdminDO();
        logAdminDO.setCompanyId(loginAdminDTO.getCompanyId());
        logAdminDO.setAdminId(loginAdminDTO.getId());
        logAdminDO.setOperator(loginAdminDTO.getAdminName());
        logAdminDO.setLogType(logType);
        logAdminDO.setContent(content);
        logAdminDO.setOperateUser(equipmentName);
        logAdminDO.setOperatorMessage(module);
        logAdminDao.insert(logAdminDO);
    }

    /**
     * log with equipmentIdList
     * @param loginAdminDTO
     * @param logType
     * @param content
     * @param module
     * @param equipmentIdList
     */
    public void log(LoginAdminDTO loginAdminDTO, Integer logType, String content, String module, List<Integer> equipmentIdList) {
        LogAdminDO logAdminDO = new LogAdminDO();
        logAdminDO.setCompanyId(loginAdminDTO.getCompanyId());
        logAdminDO.setAdminId(loginAdminDTO.getId());
        logAdminDO.setOperator(loginAdminDTO.getAdminName());
        logAdminDO.setLogType(logType);
        logAdminDO.setContent(content);
        if (CollectionUtils.isNotEmpty(equipmentIdList)) {
            List<String> equipmentNameList = equipmentDao.listUserNameByIdList(equipmentIdList);
            logAdminDO.setOperateUser(JSONUtil.toJsonStr(JSONUtil.parseArray(equipmentNameList)));
        }
        logAdminDO.setOperatorMessage(module);
        logAdminDao.insert(logAdminDO);
    }
}
