package com.nbport.zgwl.contractlog.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nbport.zgwl.common.PageResult;
import com.nbport.zgwl.contractlaunch.utils.ContractLaunchConstants;
import com.nbport.zgwl.contractlog.dto.ContractLogDto;
import com.nbport.zgwl.contractlog.dto.ContractLogQueryDto;
import com.nbport.zgwl.contractlog.mapper.ContractLogMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class ContractLogServiceImpl implements ContractLogService {

    private static final Map<String, String> STATUS_LABEL_MAP = Map.ofEntries(
            Map.entry(ContractLaunchConstants.STATUS_DRAFT, "草稿"),
            Map.entry(ContractLaunchConstants.STATUS_REVIEWING, "审批中"),
            Map.entry(ContractLaunchConstants.STATUS_REJECTED, "审批驳回"),
            Map.entry(ContractLaunchConstants.STATUS_PENDING_SIGN, "待签署"),
            Map.entry(ContractLaunchConstants.STATUS_SIGNING, "签署中"),
            Map.entry(ContractLaunchConstants.STATUS_PENDING_COLLECT, "待采集"),
            Map.entry(ContractLaunchConstants.STATUS_PENDING_ARCHIVE, "待归档"),
            Map.entry(ContractLaunchConstants.STATUS_COMPLETED, "已完成"),
            Map.entry(ContractLaunchConstants.STATUS_TERMINATED, "已终止")
    );

    private static final Map<String, String> ACTION_TYPE_LABEL_MAP = Map.ofEntries(
            Map.entry(ContractLaunchConstants.ACTION_SAVE, "保存"),
            Map.entry(ContractLaunchConstants.ACTION_COPY, "复制"),
            Map.entry(ContractLaunchConstants.ACTION_CHANGE, "变更"),
            Map.entry(ContractLaunchConstants.ACTION_SUBMIT, "提交法务"),
            Map.entry(ContractLaunchConstants.ACTION_APPROVE, "审批通过"),
            Map.entry(ContractLaunchConstants.ACTION_REJECT, "审批驳回"),
            Map.entry(ContractLaunchConstants.ACTION_SEAL_START, "开始签署"),
            Map.entry(ContractLaunchConstants.ACTION_SEAL_FINISH, "签署完成"),
            Map.entry(ContractLaunchConstants.ACTION_COLLECT, "合同采集"),
            Map.entry(ContractLaunchConstants.ACTION_ARCHIVE, "合同归档"),
            Map.entry(ContractLaunchConstants.ACTION_DELETE, "删除合同"),
            Map.entry(ContractLaunchConstants.ACTION_TERMINATE, "终止合同"),
            Map.entry(ContractLaunchConstants.ACTION_SEAL_CREATE, "创建签署流程"),
            Map.entry(ContractLaunchConstants.ACTION_SEAL_SIGN, "签署确认"),
            Map.entry(ContractLaunchConstants.ACTION_SEAL_COMPLETE, "签署流程完结"),
            Map.entry(ContractLaunchConstants.ACTION_SEAL_CANCEL, "取消签署"),
            Map.entry(ContractLaunchConstants.ACTION_SEAL_TIMESTAMP, "时间戳签章"),
            Map.entry(ContractLaunchConstants.ACTION_SEAL_COMMENT, "批注签章"),
            Map.entry(ContractLaunchConstants.ACTION_SEAL_CALLBACK, "签章回调通知"),
            Map.entry(ContractLaunchConstants.ACTION_SEAL_UPLOAD, "上传线下签署文件")
    );

    private static final Map<String, String> ACTION_STATUS_LABEL_MAP = Map.of(
            ContractLaunchConstants.ACTION_STATUS_SUCCESS, "成功",
            ContractLaunchConstants.ACTION_STATUS_FAILED, "失败",
            ContractLaunchConstants.ACTION_STATUS_PENDING, "处理中"
    );

    private final ContractLogMapper contractLogMapper;

    @Override
    public PageResult<ContractLogDto> queryPage(ContractLogQueryDto queryDto) {
        Page<ContractLogDto> page = new Page<>(queryDto.getPageNo(), queryDto.getPageSize());
        Page<ContractLogDto> resultPage = (Page<ContractLogDto>) contractLogMapper.selectLogPage(page, queryDto);
        return new PageResult<>(resultPage);
    }

    @Override
    public List<ContractLogDto> queryList(ContractLogQueryDto queryDto) {
        return contractLogMapper.selectLogList(queryDto);
    }

    @Override
    public byte[] exportExcel(ContractLogQueryDto queryDto) {
        List<ContractLogDto> list = queryList(queryDto);
        list.forEach(dto -> {
            dto.setContractStatus(STATUS_LABEL_MAP.getOrDefault(dto.getContractStatus(), dto.getContractStatus()));
            dto.setActionType(ACTION_TYPE_LABEL_MAP.getOrDefault(dto.getActionType(), dto.getActionType()));
            dto.setActionStatus(ACTION_STATUS_LABEL_MAP.getOrDefault(dto.getActionStatus(), dto.getActionStatus()));
        });
        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            EasyExcel.write(outputStream, ContractLogDto.class)
                    .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                    .sheet("合同日志")
                    .doWrite(list);
            return outputStream.toByteArray();
        } catch (Exception ex) {
            throw new RuntimeException("导出日志失败", ex);
        }
    }
}
