/*
 * Copyright (c) 京北方信息技术股份有限公司 Corporation 2021 . All rights reserved.
 *
 */
package net.northking.cctp.platform.api.log.service;

import net.northking.cctp.common.db.BasicService;
import net.northking.cctp.common.db.annotation.DatabaseWriteOnly;
import net.northking.cctp.common.db.excel.AbstractExcelService;
import net.northking.cctp.common.db.excel.ExcelHeader;
import net.northking.cctp.common.security.authentication.NKSecurityContext;
import net.northking.cctp.common.util.UUIDUtil;
import net.northking.cctp.platform.db.entity.CpLog;
import net.northking.cctp.platform.db.entity.CpLogContent;
import net.northking.cctp.platform.db.entity.CpLogContentDetail;
import net.northking.cctp.platform.db.service.CpLogContentDetailService;
import net.northking.cctp.platform.db.service.CpLogContentService;
import net.northking.cctp.platform.db.service.CpLogService;
import net.northking.cctp.platform.dto.log.CpLogAddDto;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 服务实现类
 *
 * @author maven-cctp-plugin
 * @since 1.0
 */

@Service
@ConditionalOnMissingBean(name = "CpLogApiServiceImpl")
public class CpLogApiServiceImpl extends AbstractExcelService<CpLog> implements CpLogApiService {
    /**
     * 日志
     */
    private final static Logger logger = LoggerFactory.getLogger(CpLogApiServiceImpl.class);

    @Autowired
    private CpLogService cpLogService;

    @Autowired
    private CpLogContentService cpLogContentService;

    @Autowired
    private CpLogContentDetailService cpLogContentDetailService;

    @Override
    public BasicService<CpLog> getService() {
        return this.cpLogService;
    }

    @Override
    public ExcelHeader[] excelHeaders() {
        ExcelHeader id = new ExcelHeader("主键", "id", String.class);
        ExcelHeader type = new ExcelHeader("日志类型（1-新增 2-修改 3-删除）", "type", String.class);
        ExcelHeader module = new ExcelHeader("所属功能", "module", String.class);
        ExcelHeader remoteaddr = new ExcelHeader("请求地址", "remoteaddr", String.class);
        ExcelHeader deleted = new ExcelHeader("逻辑删除", "deleted", Boolean.class);
        ExcelHeader createdBy = new ExcelHeader("创建人", "createdBy", String.class);
        ExcelHeader createdTime = new ExcelHeader("创建时间", "createdTime", Date.class);
        ExcelHeader updatedBy = new ExcelHeader("修改人", "updatedBy", String.class);
        ExcelHeader updatedTime = new ExcelHeader("修改时间", "updatedTime", Date.class);
        ExcelHeader tenantId = new ExcelHeader("租户id", "tenantId", String.class);
        return new ExcelHeader[]{};
    }

    /**
     * Excel导入，入库之前的处理
     *
     * @param records 从Excel读取到的记录
     * @return 待入库的记录
     */
    @Override
    protected List<CpLog> beforeInsertBatch(List<CpLog> records) {
        for (CpLog record : records) {
            record.setId(UUIDUtil.create32UUID());
        }
        return records;
    }

    /**
     * 写入Excel之前的处理<br>
     * 数据的特殊处理在此处加工处理
     *
     * @param record 来自数据库记录
     * @return 待写入Excel的记录
     */
    @Override
    protected CpLog beforeWriteExcel(CpLog record) {
        return record;
    }

    @Override
    @DatabaseWriteOnly
    @Transactional
    public String add(CpLogAddDto dto) {
        String uuid = UUIDUtil.create32UUID();
        CpLog entity = new CpLog();
        BeanUtils.copyProperties(dto, entity);
        entity.setId(uuid);
        this.cpLogService.insert(entity);

        //日志内容
        CpLogContent cpLogContent = new CpLogContent();
        cpLogContent.setId(uuid);
        cpLogContent.setContent(dto.getContent());
        cpLogContent.setTenantId(NKSecurityContext.getTenantId());
        this.cpLogContentService.insertCpLogContent(cpLogContent);

        //批量操作 日志内容
        if (CollectionUtils.isNotEmpty(dto.getContentDetails())) {
            dto.getContentDetails().forEach(detail -> {
                CpLogContentDetail cpLogContentDetail = new CpLogContentDetail();
                cpLogContentDetail.setContent(detail);
                cpLogContentDetail.setLogId(uuid);
                cpLogContentDetail.setTenantId(NKSecurityContext.getTenantId());
                cpLogContentDetail.setId(UUIDUtil.create32UUID());
                this.cpLogContentDetailService.insertCpLogContentDetail(cpLogContentDetail);
            });
        }
        return uuid;
    }
}
