package com.siwei.rsmonitoring.service.impl;

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

import com.siwei.rsmonitoring.cache.AuditFlowCacheUtil;
import com.siwei.rsmonitoring.domain.TDkAuditflowStep;
import com.siwei.rsmonitoring.service.ITDkAuditflowStepService;
import com.siwei.system.api.domain.SysDept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.siwei.rsmonitoring.mapper.TDkAuditflowTypeMapper;
import com.siwei.rsmonitoring.domain.TDkAuditflowType;
import com.siwei.rsmonitoring.service.ITDkAuditflowTypeService;

/**
 * 地块的审核流程类型Service业务层处理
 *
 * @author siwei
 * @date 2025-01-20
 */
@Service
public class TDkAuditflowTypeServiceImpl implements ITDkAuditflowTypeService {
    @Autowired
    private TDkAuditflowTypeMapper tDkAuditflowTypeMapper;
    @Autowired
    private ITDkAuditflowStepService tDkAuditflowStepService;

    /**
     * 查询地块的审核流程类型
     *
     * @param id 地块的审核流程类型主键
     * @return 地块的审核流程类型
     */
    @Override
    public TDkAuditflowType selectTDkAuditflowTypeById(String id) {
        return tDkAuditflowTypeMapper.selectTDkAuditflowTypeById(id);
    }

    /**
     * 查询地块的审核流程类型列表
     *
     * @param tDkAuditflowType 地块的审核流程类型
     * @return 地块的审核流程类型
     */
    @Override
    public List<TDkAuditflowType> selectTDkAuditflowTypeList(TDkAuditflowType tDkAuditflowType) {
        return tDkAuditflowTypeMapper.selectTDkAuditflowTypeList(tDkAuditflowType);
    }

    /**
     * 新增地块的审核流程类型
     *
     * @param tDkAuditflowType 地块的审核流程类型
     * @return 结果
     */
    @Override
    public int insertTDkAuditflowType(TDkAuditflowType tDkAuditflowType) {
        return tDkAuditflowTypeMapper.insertTDkAuditflowType(tDkAuditflowType);
    }

    /**
     * 修改地块的审核流程类型
     *
     * @param tDkAuditflowType 地块的审核流程类型
     * @return 结果
     */
    @Override
    public int updateTDkAuditflowType(TDkAuditflowType tDkAuditflowType) {
        return tDkAuditflowTypeMapper.updateTDkAuditflowType(tDkAuditflowType);
    }

    /**
     * 批量删除地块的审核流程类型
     *
     * @param ids 需要删除的地块的审核流程类型主键
     * @return 结果
     */
    @Override
    public int deleteTDkAuditflowTypeByIds(String[] ids) {
        return tDkAuditflowTypeMapper.deleteTDkAuditflowTypeByIds(ids);
    }

    /**
     * 删除地块的审核流程类型信息
     *
     * @param id 地块的审核流程类型主键
     * @return 结果
     */
    @Override
    public int deleteTDkAuditflowTypeById(String id) {
        return tDkAuditflowTypeMapper.deleteTDkAuditflowTypeById(id);
    }

    /**
     * 緩存抵抗的審核流程類型
     */
    @Override
    public void cacheTDkAuditflowType() {
        AuditFlowCacheUtil cacheUtil = AuditFlowCacheUtil.getInstance();
        List<TDkAuditflowType> typeList = tDkAuditflowTypeMapper.selectTDkAuditflowTypeList(null);
        for (TDkAuditflowType typeBean : typeList) {
            TDkAuditflowStep queryStep = new TDkAuditflowStep();
            queryStep.setAuditflowCode(typeBean.getAuditflowCode());
            List<TDkAuditflowStep> stepList = tDkAuditflowStepService.selectTDkAuditflowStepList(queryStep);
            cacheUtil.addCodeCacheData(typeBean.getAuditflowCode(), stepList);
        }
    }

    @Override
    public TDkAuditflowType selectTDkAuditflowTypeDefault() {
        List<TDkAuditflowType> typeList = tDkAuditflowTypeMapper.selectTDkAuditflowTypeList(null);
        if (typeList.isEmpty()) {
            return null;
        }
        return typeList.get(0);
    }

    @Override
    public List<TDkAuditflowType> selectTDkAuditflowTypeTree() {
        List<TDkAuditflowType> typeList = tDkAuditflowTypeMapper.selectTDkAuditflowTypeList(null);
        for (TDkAuditflowType typeBean : typeList) {
            TDkAuditflowStep queryStep = new TDkAuditflowStep();
            queryStep.setAuditflowCode(typeBean.getAuditflowCode());
            List<TDkAuditflowStep> stepList = tDkAuditflowStepService.selectTDkAuditflowStepList(queryStep);
            typeBean.setChildren(stepList);
        }
        return typeList;
    }
}
