package org.jk.system.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.jk.common.core.utils.DateUtils;
import org.jk.common.mybatis.core.page.PageQuery;
import org.jk.system.domain.DocumentAuditConfig;
import org.jk.system.domain.bo.DocumentAuditConfigBo;
import org.jk.system.mapper.DocumentAuditConfigMapper;
import org.jk.system.service.IDocumentAuditConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 审核配置Service业务层处理
 *
 * @author takai
 * @date 2025-05-29
 */
@Service
@RequiredArgsConstructor
public class DocumentAuditConfigServiceImpl implements IDocumentAuditConfigService
{
    private final DocumentAuditConfigMapper documentAuditConfigMapper;

    /**
     * 查询审核配置
     *
     * @param id 审核配置主键
     * @return 审核配置
     */
    @Override
    public DocumentAuditConfig selectDocumentAuditConfigById(Long id)
    {
        return documentAuditConfigMapper.selectDocumentAuditConfigById(id);
    }

    /**
     * 查询审核配置列表
     *
     * @param documentAuditConfig 审核配置
     * @return 审核配置
     */
    @Override
    public List<DocumentAuditConfig> selectDocumentAuditConfigList(DocumentAuditConfig documentAuditConfig)
    {
        List<DocumentAuditConfig> list = documentAuditConfigMapper.selectDocumentAuditConfigList(documentAuditConfig);
        if (!list.isEmpty()) {
            for (DocumentAuditConfig documentAuditConfig1 : list) {
                documentAuditConfig1.setApproverList(Arrays.asList(documentAuditConfig1.getApprover().split(",")));
            }
        }
        return list;
    }

    /**
     * 新增审核配置
     *
     * @param documentAuditConfig 审核配置
     * @return 结果
     */
    @Override
    public int insertDocumentAuditConfig(DocumentAuditConfig documentAuditConfig)
    {
        documentAuditConfig.setCreateTime(DateUtils.getNowDate());
        int i = documentAuditConfigMapper.insertDocumentAuditConfig(documentAuditConfig);
        if(i > 0){
            updateChain();
        }
        return i;
    }

    /**
     * 修改审核配置
     *
     * @param documentAuditConfig 审核配置
     * @return 结果
     */
    @Override
    public int updateDocumentAuditConfig(DocumentAuditConfig documentAuditConfig)
    {
        documentAuditConfig.setUpdateTime(DateUtils.getNowDate());
        return documentAuditConfigMapper.updateDocumentAuditConfig(documentAuditConfig);
    }

    /**
     * 批量删除审核配置
     *
     * @param ids 需要删除的审核配置主键
     * @return 结果
     */
    @Override
    public int deleteDocumentAuditConfigByIds(Long[] ids)
    {
        int i = documentAuditConfigMapper.deleteDocumentAuditConfigByIds(ids);
        if(i > 0){
            updateChain();
        }
        return i;
    }

    private void updateChain(){
        // 获取所有审核配置信息
        List<DocumentAuditConfig> list = documentAuditConfigMapper.selectDocumentAuditConfigListByBo(null);
        String chain =list.stream()
                .map(DocumentAuditConfig::getId)
                .map(String::valueOf)
                .collect(Collectors.joining("/"));
        for (int i=0; i< list.size(); i++){
            // 审核节点顺序
            list.get(i).setNodeOrder(i+1);
            // 更新审核链
            list.get(i).setChain(chain);
            documentAuditConfigMapper.updateDocumentAuditConfig(list.get(i));
        }
    }

    /**
     * 删除审核配置信息
     *
     * @param id 审核配置主键
     * @return 结果
     */
    @Override
    public int deleteDocumentAuditConfigById(Long id)
    {
        return documentAuditConfigMapper.deleteDocumentAuditConfigById(id);
    }

    @Override
    public List<DocumentAuditConfig> selectDocumentAuditConfigListByBo(DocumentAuditConfigBo documentAuditConfigBo) {
        return documentAuditConfigMapper.selectDocumentAuditConfigListByBo(documentAuditConfigBo);
    }

    /**
     * 查询审核配置列表
     *
     * @param documentAuditConfig 审核配置
     * @return 审核配置
     */
    @Override
    public Page<DocumentAuditConfig> selectPageList(DocumentAuditConfig documentAuditConfig, PageQuery pageQuery)
    {
        Page<DocumentAuditConfig> list = documentAuditConfigMapper.selectPageList(documentAuditConfig,pageQuery.build());
        if (!list.getRecords().isEmpty()) {
            for (DocumentAuditConfig documentAuditConfig1 : list.getRecords()) {
                documentAuditConfig1.setApproverList(Arrays.asList(documentAuditConfig1.getApprover().split(",")));
            }
        }
        return list;
    }

    @Override
    public Page<DocumentAuditConfig> selectDocumentPageList(DocumentAuditConfigBo documentAuditConfigBo, PageQuery pageQuery) {
        return documentAuditConfigMapper.selectDocumentPageList(documentAuditConfigBo,pageQuery.build());
    }
}
