package com.skr.mdm.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.skr.mdm.constant.SendCmdTypeConstants;
import com.skr.mdm.dao.*;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.enums.LogAdminTypeEnum;
import com.skr.mdm.enums.StrategyEquipmentStateEnum;
import com.skr.mdm.handle.IssuedUserHandler;
import com.skr.mdm.handle.LogAdminHandler;
import com.skr.mdm.result.ResponseResult;
import com.skr.mdm.entity.DeviceInstructCaptureimageDO;
import com.skr.mdm.entity.DeviceInstructInformDO;
import com.skr.mdm.entity.DeviceInstructLockscreenDO;
import com.skr.mdm.entity.DeviceInstructPushDO;
import com.skr.mdm.bean.vo.equipment.ClientAdminCmdUserVO;
import com.skr.mdm.bean.vo.equipment.ClientAdminCmdVO;
import com.skr.mdm.bean.vo.equipment.ClientAdminEquipmentVO;
import com.skr.mdm.service.ClientAdminService;
import com.skr.mdm.util.PageUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author nfq
 * @date 2021/2/19 11:56
 */
@Service
public class ClientAdminServiceImpl implements ClientAdminService {

    @Autowired
    private DeviceEquipmentDao equipmentDao;
    @Autowired
    private DeviceInstructLockscreenDao deviceInstructLockscreenDao;
    @Autowired
    private IssuedUserHandler issuedUserHandler;
    @Autowired
    private LogAdminHandler logAdminHandler;
    @Autowired
    private DeviceInstructInformDao deviceInstructInformDao;
    @Autowired
    private DeviceInstructInformUserDao deviceInstructInformUserDao;
    @Autowired
    private DeviceInstructPushDao deviceInstructPushDao;
    @Autowired
    private DeviceInstructCaptureImageDao deviceInstructCaptureimageDao;

    @Override
    public ResponseResult listPermission(Integer adminId) {
        return null;
    }

    @Override
    public ResponseResult listEquipment(Integer pageNum, Integer pageSize, Integer searchType, String searchValue, LoginAdminDTO loginAdminDTO) {
        PageHelper.startPage(pageNum, pageSize);
        List<ClientAdminEquipmentVO> clientAdminEquipmentVOList = equipmentDao.listClientAdminEquipment(loginAdminDTO.getCompanyId(), loginAdminDTO.getId(), searchType, searchValue);
        PageInfo<ClientAdminEquipmentVO> pageInfo = new PageInfo<>(clientAdminEquipmentVOList);
        return ResponseResult.success(PageUtil.getPageResult(pageInfo));
    }

    @Override
    public ResponseResult lockCmd(String lockScreePassword, List<Integer> userIdList, LoginAdminDTO loginAdminDTO) {
        if (CollectionUtils.isEmpty(userIdList) || StringUtils.isBlank(lockScreePassword)) {
            return ResponseResult.success();
        }
        List<Integer> equipmentIdList = equipmentDao.selectDeviceEquipmentIdByUserId(userIdList);
        if (CollectionUtils.isEmpty(equipmentIdList)) {
            return ResponseResult.success();
        }
        Long messageId = MdmIdUtil.getSnowflakeNextId();
        List<DeviceInstructLockscreenDO> deviceInstructLockscreenDOList = new ArrayList<>();
        equipmentIdList.forEach(equipmentId -> {
            DeviceInstructLockscreenDO instructLockscreenDO = DeviceInstructLockscreenDO.builder()
                    .equipmentId(equipmentId)
                    .cmdType(1)
                    .state(StrategyEquipmentStateEnum.ISSUING.getState())
                    .lockScreePassword(lockScreePassword)
                    .isGlobal(0)
                    .messageId(String.valueOf(messageId))
                    .clientType(2)
                    .build();
            deviceInstructLockscreenDOList.add(instructLockscreenDO);
        });
        deviceInstructLockscreenDao.insertBatch(deviceInstructLockscreenDOList);
        issuedUserHandler.issuedUser(equipmentIdList, loginAdminDTO.getCompanyId(), SendCmdTypeConstants.LOCK_SCREEN, lockScreePassword);
        logAdminHandler.log(loginAdminDTO, LogAdminTypeEnum.INSTRUCT_TYPE.getLogType(), "锁屏", "管理员客户端", equipmentIdList);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult informCmd(String inform, List<Integer> userIdList, LoginAdminDTO loginAdminDTO) {
        if (CollectionUtils.isEmpty(userIdList) || StringUtils.isBlank(inform)) {
            return ResponseResult.success();
        }
        List<Integer> equipmentIdList = equipmentDao.selectDeviceEquipmentIdByUserId(userIdList);
        if (CollectionUtils.isEmpty(equipmentIdList)) {
            return ResponseResult.success();
        }
        DeviceInstructInformDO deviceInstructInformDO = DeviceInstructInformDO.builder()
                .companyId(loginAdminDTO.getCompanyId())
                .cmdType(1)
                .content(inform)
                .isGlobal(0)
                .clientType(2)
                .build();
        Integer informId = deviceInstructInformDao.insert(deviceInstructInformDO);
        deviceInstructInformUserDao.insertFormUserList(informId, equipmentIdList, StrategyEquipmentStateEnum.ISSUING.getState());
        // issued
        issuedUserHandler.issuedUser(equipmentIdList, loginAdminDTO.getCompanyId(), SendCmdTypeConstants.SEND_MSG, inform);
        // log
        logAdminHandler.log(loginAdminDTO, LogAdminTypeEnum.INSTRUCT_TYPE.getLogType(), "通知", "管理员客户端", equipmentIdList);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult rollCall(List<Integer> userIdList, LoginAdminDTO loginAdminDTO) {
        if (CollectionUtils.isEmpty(userIdList)) {
            return ResponseResult.success();
        }
        List<Integer> equipmentIdList = equipmentDao.selectDeviceEquipmentIdByUserId(userIdList);
        if (CollectionUtils.isEmpty(equipmentIdList)) {
            return ResponseResult.success();
        }
        Long messageId = MdmIdUtil.getSnowflakeNextId();
        List<DeviceInstructPushDO> deviceInstructPushDOList = new ArrayList<>();
        equipmentIdList.forEach(equipmentId -> {
            DeviceInstructPushDO deviceInstructPushDO = DeviceInstructPushDO.builder()
                    .equipmentId(equipmentId)
                    .messageId(String.valueOf(messageId))
                    .state(StrategyEquipmentStateEnum.ISSUING.getState())
                    .cmdType(2)
                    .isGlobal(0)
                    .clientType(2)
                    .build();
            deviceInstructPushDOList.add(deviceInstructPushDO);
        });
        deviceInstructPushDao.insertBatch(deviceInstructPushDOList);
        // issued
        issuedUserHandler.issuedUser(equipmentIdList, loginAdminDTO.getCompanyId(), SendCmdTypeConstants.ROLL_CALL, "");
        // log
        logAdminHandler.log(loginAdminDTO, LogAdminTypeEnum.INSTRUCT_TYPE.getLogType(), "点名", "管理员客户端", equipmentIdList);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult bell(List<Integer> userIdList, LoginAdminDTO loginAdminDTO) {
        if (CollectionUtils.isEmpty(userIdList)) {
            return ResponseResult.success();
        }
        List<Integer> equipmentIdList = equipmentDao.selectDeviceEquipmentIdByUserId(userIdList);
        if (CollectionUtils.isEmpty(equipmentIdList)) {
            return ResponseResult.success();
        }
        Long messageId = MdmIdUtil.getSnowflakeNextId();
        List<DeviceInstructPushDO> deviceInstructPushDOList = new ArrayList<>();
        equipmentIdList.forEach(equipmentId -> {
            DeviceInstructPushDO deviceInstructPushDO = DeviceInstructPushDO.builder()
                    .equipmentId(equipmentId)
                    .messageId(String.valueOf(messageId))
                    .state(StrategyEquipmentStateEnum.ISSUING.getState())
                    .cmdType(1)
                    .isGlobal(0)
                    .clientType(2)
                    .build();
            deviceInstructPushDOList.add(deviceInstructPushDO);
        });
        deviceInstructPushDao.insertBatch(deviceInstructPushDOList);
        // issued
        issuedUserHandler.issuedUser(equipmentIdList, loginAdminDTO.getCompanyId(), SendCmdTypeConstants.ROLL_CALL, "");
        // log
        logAdminHandler.log(loginAdminDTO, LogAdminTypeEnum.INSTRUCT_TYPE.getLogType(), "点名", "管理员客户端", equipmentIdList);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult unlockCmd(List<Integer> userIdList, LoginAdminDTO loginAdminDTO) {
        if (CollectionUtils.isEmpty(userIdList)) {
            return ResponseResult.success();
        }
        List<Integer> equipmentIdList = equipmentDao.selectDeviceEquipmentIdByUserId(userIdList);
        if (CollectionUtils.isEmpty(equipmentIdList)) {
            return ResponseResult.success();
        }
        Long messageId = MdmIdUtil.getSnowflakeNextId();
        List<DeviceInstructLockscreenDO> deviceInstructLockscreenDOList = new ArrayList<>();
        equipmentIdList.forEach(equipmentId -> {
            DeviceInstructLockscreenDO instructLockscreenDO = DeviceInstructLockscreenDO.builder()
                    .equipmentId(equipmentId)
                    .cmdType(2)
                    .state(StrategyEquipmentStateEnum.ISSUING.getState())
                    .isGlobal(0)
                    .messageId(String.valueOf(messageId))
                    .clientType(2)
                    .build();
            deviceInstructLockscreenDOList.add(instructLockscreenDO);
        });
        deviceInstructLockscreenDao.insertBatch(deviceInstructLockscreenDOList);
        issuedUserHandler.issuedUser(equipmentIdList, loginAdminDTO.getCompanyId(), SendCmdTypeConstants.UNLOCK_SCREEN, "");
        logAdminHandler.log(loginAdminDTO, LogAdminTypeEnum.INSTRUCT_TYPE.getLogType(), "锁屏", "管理员客户端", equipmentIdList);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult takePhotos(List<Integer> userIdList, LoginAdminDTO loginAdminDTO) {
        if (CollectionUtils.isEmpty(userIdList)) {
            return ResponseResult.success();
        }
        List<Integer> equipmentIdList = equipmentDao.selectDeviceEquipmentIdByUserId(userIdList);
        if (CollectionUtils.isEmpty(equipmentIdList)) {
            return ResponseResult.success();
        }
        Long messageId = MdmIdUtil.getSnowflakeNextId();
        List<DeviceInstructCaptureimageDO> deviceInstructCaptureimageDOList = new ArrayList<>();
        equipmentIdList.forEach(equipmentId -> {
            DeviceInstructCaptureimageDO deviceInstructCaptureimageDO = DeviceInstructCaptureimageDO.builder()
                    .equipmentId(equipmentId)
                    .cmdType(1)
                    .state(StrategyEquipmentStateEnum.ISSUING.getState())
                    .isGlobal(0)
                    .messageId(String.valueOf(messageId))
                    .clientType(2)
                    .build();
            deviceInstructCaptureimageDOList.add(deviceInstructCaptureimageDO);
        });
        deviceInstructCaptureimageDao.insertBatch(deviceInstructCaptureimageDOList);
        issuedUserHandler.issuedUser(equipmentIdList, loginAdminDTO.getCompanyId(), SendCmdTypeConstants.TASK_PHOTO, "");
        logAdminHandler.log(loginAdminDTO, LogAdminTypeEnum.INSTRUCT_TYPE.getLogType(), "拍照", "管理员客户端", equipmentIdList);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult screenShot(List<Integer> userIdList, LoginAdminDTO loginAdminDTO) {
        if (CollectionUtils.isEmpty(userIdList)) {
            return ResponseResult.success();
        }
        List<Integer> equipmentIdList = equipmentDao.selectDeviceEquipmentIdByUserId(userIdList);
        if (CollectionUtils.isEmpty(equipmentIdList)) {
            return ResponseResult.success();
        }
        Long messageId = MdmIdUtil.getSnowflakeNextId();
        List<DeviceInstructCaptureimageDO> deviceInstructCaptureimageDOList = new ArrayList<>();
        equipmentIdList.forEach(equipmentId -> {
            DeviceInstructCaptureimageDO deviceInstructCaptureimageDO = DeviceInstructCaptureimageDO.builder()
                    .equipmentId(equipmentId)
                    .cmdType(2)
                    .state(StrategyEquipmentStateEnum.ISSUING.getState())
                    .isGlobal(0)
                    .messageId(String.valueOf(messageId))
                    .clientType(2)
                    .build();
            deviceInstructCaptureimageDOList.add(deviceInstructCaptureimageDO);
        });
        deviceInstructCaptureimageDao.insertBatch(deviceInstructCaptureimageDOList);
        issuedUserHandler.issuedUser(equipmentIdList, loginAdminDTO.getCompanyId(), SendCmdTypeConstants.SCREE_CAPTURE, "");
        logAdminHandler.log(loginAdminDTO, LogAdminTypeEnum.INSTRUCT_TYPE.getLogType(), "截屏", "管理员客户端", equipmentIdList);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult listLockCmd(Integer pageNum, Integer pageSize, Integer searchType, String searchValue, LoginAdminDTO loginAdminDTO) {
        PageHelper.startPage(pageNum, pageSize);
        List<ClientAdminCmdVO> clientAdminCmdVOList = deviceInstructLockscreenDao.listClientAdminCmd(loginAdminDTO.getCompanyId(), searchType, searchValue);
        PageInfo<ClientAdminCmdVO> pageInfo = new PageInfo<>(clientAdminCmdVOList);
        return ResponseResult.success(PageUtil.getPageResult(pageInfo));
    }

    @Override
    public ResponseResult listUserLockCmd(String messageId, Integer pageNum, Integer pageSize, Integer searchType, String searchValue, LoginAdminDTO loginAdminDTO) {
        PageHelper.startPage(pageNum, pageSize);
        List<ClientAdminCmdUserVO> clientAdminCmdUserVOList = deviceInstructLockscreenDao.listUserClientAdminCmd(loginAdminDTO.getCompanyId(), messageId, searchType, searchValue);
        PageInfo<ClientAdminCmdUserVO> pageInfo = new PageInfo<>(clientAdminCmdUserVOList);
        return ResponseResult.success(PageUtil.getPageResult(pageInfo));
    }
}
