package com.skr.mdm.handle;

import com.skr.mdm.bean.qo.BaseStrategyQO;
import com.skr.mdm.dao.AuDepartmentDao;
import com.skr.mdm.dao.AuUserDao;
import com.skr.mdm.dao.DeviceEquipmentDao;
import com.skr.mdm.dao.LogAdminDao;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.entity.AuDepartmentDO;
import com.skr.mdm.entity.LogAdminDO;
import com.skr.mdm.enums.IssuedTypeEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

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

    @Autowired
    private LogAdminDao logAdminDao;
    @Autowired
    private DeviceEquipmentDao equipmentDao;
    @Autowired
    private AuUserDao userDao;
    @Autowired
    private AuDepartmentDao departmentDao;

    /**
     * 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> userNameList = userDao.listUserNameByIdList(userIdList);
            logAdminDO.setOperateUser(StringUtils.join(userNameList, "，"));
        }
        logAdminDO.setModule(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.setModule(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> userNameList = equipmentDao.listEquipmentNameByIdList(equipmentIdList);
            logAdminDO.setOperateUser(StringUtils.join(userNameList, "，"));
        }
        logAdminDO.setModule(module);
        logAdminDao.insert(logAdminDO);
    }

    /**
     * log with deptIdList
     * @param loginAdminDTO
     * @param logType
     * @param content
     * @param deptIdList
     * @param module
     */
    public void log(LoginAdminDTO loginAdminDTO, Integer logType, String content, List<Integer> deptIdList, 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(deptIdList)) {
            List<String> deptNameList = departmentDao.selectBatchIds(deptIdList)
                    .stream().map(AuDepartmentDO::getDepartmentName)
                    .collect(Collectors.toList());
            logAdminDO.setOperateUser("部门：" + StringUtils.join(deptNameList, "，"));
        }
        logAdminDO.setModule(module);
        logAdminDao.insert(logAdminDO);
    }


    public void log(BaseStrategyQO baseStrategyQO, LoginAdminDTO loginAdminDTO, Integer logType, String content, String module) {
        if (IssuedTypeEnum.DEPT_GLOBAL.getIssuedType().equals(baseStrategyQO.getGlobalType())) {
            log(loginAdminDTO, logType, content, baseStrategyQO.getCandidateIdList(), module);
            return;
        }
        if (IssuedTypeEnum.DEVICE.getIssuedType().equals(baseStrategyQO.getGlobalType())) {
            log(loginAdminDTO, logType, content, module, baseStrategyQO.getCandidateIdList());
            return;
        }
        LogAdminDO logAdminDO = new LogAdminDO();
        logAdminDO.setCompanyId(loginAdminDTO.getCompanyId());
        logAdminDO.setAdminId(loginAdminDTO.getId());
        logAdminDO.setOperator(loginAdminDTO.getAdminName());
        logAdminDO.setLogType(logType);
        logAdminDO.setContent(content);
        if (IssuedTypeEnum.GLOBAL.getIssuedType().equals(baseStrategyQO.getGlobalType())) {
            logAdminDO.setOperateUser("全局用户");
        }
        logAdminDO.setModule(module);
        logAdminDao.insert(logAdminDO);
    }

    public void log(Integer globalType, List<Integer> candidateIdList, LoginAdminDTO loginAdminDTO, Integer logType,
                    String content, String module) {
        BaseStrategyQO baseStrategyQO = new BaseStrategyQO();
        baseStrategyQO.setGlobalType(globalType);
        baseStrategyQO.setCandidateIdList(candidateIdList);
        log(baseStrategyQO, loginAdminDTO, logType, content, module);
    }
}