package com.ktg.iscs.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.utils.DateUtils;
import com.ktg.common.utils.SecurityUtils;
import com.ktg.framework.websocket.WebSocketJobTicketLog;
import com.ktg.iscs.domain.IsTicketOperLog;
import com.ktg.iscs.mapper.IsTicketOperLogMapper;
import com.ktg.iscs.service.IIsTicketOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 作业操作过程Service业务层处理
 *
 * @author cgj
 * @date 2025-07-14
 */
@Service
public class IsTicketOperLogServiceImpl extends ServiceImpl<IsTicketOperLogMapper, IsTicketOperLog> implements IIsTicketOperLogService {

    @Autowired
    private IsTicketOperLogMapper isTicketOperLogMapper;

    @Override
    public Page<IsTicketOperLog> getIsTicketOperLogPage(Page<IsTicketOperLog> page, IsTicketOperLog isTicketOperLog) {
        Page<IsTicketOperLog> result = page(page, Wrappers.<IsTicketOperLog>lambdaQuery()
                .eq(isTicketOperLog.getOperationType() != null, IsTicketOperLog::getOperationType, isTicketOperLog.getOperationType())
                .eq(isTicketOperLog.getTicketId() != null, IsTicketOperLog::getTicketId, isTicketOperLog.getTicketId())
                .orderByAsc(IsTicketOperLog::getOperationId));
        return result;
    }

    @Override
    public Boolean addLog1(Long jobId, String jobName) {
        Date date = new Date();
        Long userId = SecurityUtils.getUserId();
        String username = SecurityUtils.getUsername();
        IsTicketOperLog isTicketOperLog = new IsTicketOperLog();
        isTicketOperLog.setTicketId(jobId);
        isTicketOperLog.setTicketName(jobName);
        isTicketOperLog.setOperationType(0);
        isTicketOperLog.setOperationTime(date);
        isTicketOperLog.setOperationUserId(userId);
        isTicketOperLog.setOperationUserName(username);
        isTicketOperLog.setOperationContent("启动作业：" + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, date) + " 作业[" +  jobName + "]已启动。");
        save(isTicketOperLog);
        WebSocketJobTicketLog.sendMessage(String.valueOf(jobId), isTicketOperLog.toString());
        return true;
    }

    @Override
    public Boolean addLog2(Long jobId, String jobName, String title, Long userId, String userName) {
        Date date = new Date();
        IsTicketOperLog isTicketOperLog = new IsTicketOperLog();
        isTicketOperLog.setTicketId(jobId);
        isTicketOperLog.setTicketName(jobName);
        isTicketOperLog.setOperationType(1);
        isTicketOperLog.setOperationTime(date);
        isTicketOperLog.setOperationUserId(userId);
        isTicketOperLog.setOperationUserName(userName);
        isTicketOperLog.setOperationContent("操作确认：" + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, date) + "[" + userName +"]已确认执行[" +  title + "]。");
        save(isTicketOperLog);
        WebSocketJobTicketLog.sendMessage(String.valueOf(jobId), isTicketOperLog.toString());
        return true;
    }

    /**
     * 人员变动
     * type 0移除 1添加
     * lockType 0上锁人 1共锁人
     * @return
     */
    @Override
    public Boolean addLog3(Long jobId, String jobName, String type, String lockType, String lockUser) {
        Date date = new Date();
        Long userId = SecurityUtils.getUserId();
        String username = SecurityUtils.getLoginUser().getUser().getNickName();
        IsTicketOperLog isTicketOperLog = new IsTicketOperLog();
        isTicketOperLog.setTicketId(jobId);
        isTicketOperLog.setTicketName(jobName);
        isTicketOperLog.setOperationType(2);
        isTicketOperLog.setOperationTime(date);
        isTicketOperLog.setOperationUserId(userId);
        isTicketOperLog.setOperationUserName(username);
        isTicketOperLog.setOperationContent("⼈员变动：" + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, date) + "[" + username +"]已" + type + lockType + "[" +lockUser + "]。");
        save(isTicketOperLog);
        WebSocketJobTicketLog.sendMessage(String.valueOf(jobId), isTicketOperLog.toString());
        return true;
    }

    @Override
    public Boolean addLog4(Long jobId, String jobName) {
        Date date = new Date();
        Long userId = SecurityUtils.getUserId();
        String username = SecurityUtils.getLoginUser().getUser().getNickName();
        IsTicketOperLog isTicketOperLog = new IsTicketOperLog();
        isTicketOperLog.setTicketId(jobId);
        isTicketOperLog.setTicketName(jobName);
        isTicketOperLog.setOperationType(3);
        isTicketOperLog.setOperationTime(date);
        isTicketOperLog.setOperationUserId(userId);
        isTicketOperLog.setOperationUserName(username);
        isTicketOperLog.setOperationContent("启动上锁：" + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, date) + "[" + username +"]启动上锁。");
        save(isTicketOperLog);
        WebSocketJobTicketLog.sendMessage(String.valueOf(jobId), isTicketOperLog.toString());
        return true;
    }

    @Override
    public Boolean addLog5(Long jobId, String jobName, String pointNames) {
        Date date = new Date();
        Long userId = SecurityUtils.getUserId();
        String username = SecurityUtils.getLoginUser().getUser().getNickName();
        IsTicketOperLog isTicketOperLog = new IsTicketOperLog();
        isTicketOperLog.setTicketId(jobId);
        isTicketOperLog.setTicketName(jobName);
        isTicketOperLog.setOperationType(4);
        isTicketOperLog.setOperationTime(date);
        isTicketOperLog.setOperationUserId(userId);
        isTicketOperLog.setOperationUserName(username);
        isTicketOperLog.setOperationContent("完成上锁：" + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, date) + "[" + username +"]完成上锁，" + pointNames + "已上锁。");
        save(isTicketOperLog);
        WebSocketJobTicketLog.sendMessage(String.valueOf(jobId), isTicketOperLog.toString());
        return true;
    }

    @Override
    public Boolean addLog6(Long jobId, String jobName, String colockUser) {
        Date date = new Date();
        Long userId = SecurityUtils.getUserId();
        String username = SecurityUtils.getLoginUser().getUser().getNickName();
        IsTicketOperLog isTicketOperLog = new IsTicketOperLog();
        isTicketOperLog.setTicketId(jobId);
        isTicketOperLog.setTicketName(jobName);
        isTicketOperLog.setOperationType(6);
        isTicketOperLog.setOperationTime(date);
        isTicketOperLog.setOperationUserId(userId);
        isTicketOperLog.setOperationUserName(username);
        isTicketOperLog.setOperationContent("添加共锁：" + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, date) + "[" + colockUser + "]已添加共锁。");
        save(isTicketOperLog);
        WebSocketJobTicketLog.sendMessage(String.valueOf(jobId), isTicketOperLog.toString());
        return true;
    }

    @Override
    public Boolean addLog7(Long jobId, String jobName, String colockUser) {
        Date date = new Date();
        Long userId = SecurityUtils.getUserId();
        String username = SecurityUtils.getLoginUser().getUser().getNickName();
        IsTicketOperLog isTicketOperLog = new IsTicketOperLog();
        isTicketOperLog.setTicketId(jobId);
        isTicketOperLog.setTicketName(jobName);
        isTicketOperLog.setOperationType(7);
        isTicketOperLog.setOperationTime(date);
        isTicketOperLog.setOperationUserId(userId);
        isTicketOperLog.setOperationUserName(username);
        isTicketOperLog.setOperationContent("解除共锁：" + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, date) + "[" + colockUser + "]已解除共锁。");
        save(isTicketOperLog);
        WebSocketJobTicketLog.sendMessage(String.valueOf(jobId), isTicketOperLog.toString());
        return true;
    }

    @Override
    public Boolean addLog8(Long jobId, String jobName) {
        Date date = new Date();
        Long userId = SecurityUtils.getUserId();
        String username = SecurityUtils.getLoginUser().getUser().getNickName();
        IsTicketOperLog isTicketOperLog = new IsTicketOperLog();
        isTicketOperLog.setTicketId(jobId);
        isTicketOperLog.setTicketName(jobName);
        isTicketOperLog.setOperationType(5);
        isTicketOperLog.setOperationTime(date);
        isTicketOperLog.setOperationUserId(userId);
        isTicketOperLog.setOperationUserName(username);
        isTicketOperLog.setOperationContent("启动解锁：" + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, date) + "[" + username +"]启动解锁。");
        save(isTicketOperLog);
        WebSocketJobTicketLog.sendMessage(String.valueOf(jobId), isTicketOperLog.toString());
        return true;
    }

    @Override
    public Boolean addLog9(Long jobId, String jobName, String pointNames) {
        Date date = new Date();
        Long userId = SecurityUtils.getUserId();
        String username = SecurityUtils.getLoginUser().getUser().getNickName();
        IsTicketOperLog isTicketOperLog = new IsTicketOperLog();
        isTicketOperLog.setTicketId(jobId);
        isTicketOperLog.setTicketName(jobName);
        isTicketOperLog.setOperationType(8);
        isTicketOperLog.setOperationTime(date);
        isTicketOperLog.setOperationUserId(userId);
        isTicketOperLog.setOperationUserName(username);
        isTicketOperLog.setOperationContent("完成解锁：" + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, date) + "[" + username +"]完成解锁，" + pointNames + "已解锁。");
        save(isTicketOperLog);
        WebSocketJobTicketLog.sendMessage(String.valueOf(jobId), isTicketOperLog.toString());
        return true;
    }

    @Override
    public Boolean addLog11(Long jobId, String jobName) {
        Date date = new Date();
        Long userId = SecurityUtils.getUserId();
        String username = SecurityUtils.getLoginUser().getUser().getNickName();
        IsTicketOperLog isTicketOperLog = new IsTicketOperLog();
        isTicketOperLog.setTicketId(jobId);
        isTicketOperLog.setTicketName(jobName);
        isTicketOperLog.setOperationType(9);
        isTicketOperLog.setOperationTime(date);
        isTicketOperLog.setOperationUserId(userId);
        isTicketOperLog.setOperationUserName(username);
        isTicketOperLog.setOperationContent("结束作业：" + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, date) + "[" + username +"]已完成作业[" + jobName + "]。");
        save(isTicketOperLog);
        WebSocketJobTicketLog.sendMessage(String.valueOf(jobId), isTicketOperLog.toString());
        return true;
    }

    @Override
    public Boolean addLog10(Long jobId, String jobName) {
        Date date = new Date();
        Long userId = SecurityUtils.getUserId();
        String username = SecurityUtils.getLoginUser().getUser().getNickName();
        IsTicketOperLog isTicketOperLog = new IsTicketOperLog();
        isTicketOperLog.setTicketId(jobId);
        isTicketOperLog.setTicketName(jobName);
        isTicketOperLog.setOperationType(10);
        isTicketOperLog.setOperationTime(date);
        isTicketOperLog.setOperationUserId(userId);
        isTicketOperLog.setOperationUserName(username);
        isTicketOperLog.setOperationContent("取消作业：" + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, date) + "[" + username +"]已取消作业[" + jobName + "]。");
        save(isTicketOperLog);
        WebSocketJobTicketLog.sendMessage(String.valueOf(jobId), isTicketOperLog.toString());
        return true;
    }

}
