package com.zhyc.module.feed.service.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Objects;

import com.zhyc.common.annotation.DataScope;
import com.zhyc.module.feed.domain.SgFormulaList;
import com.zhyc.module.feed.mapper.SgFormulaListMapper;
import org.springframework.stereotype.Service;
import com.zhyc.module.feed.mapper.SgFormulaManagementMapper;
import com.zhyc.module.feed.domain.SgFormulaManagement;
import com.zhyc.module.feed.service.ISgFormulaManagementService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 配方管理Service业务层处理
 *
 * @author HashMap
 * @date 2025-08-09
 */
@Service
public class SgFormulaManagementServiceImpl implements ISgFormulaManagementService {
    private final SgFormulaManagementMapper sgFormulaManagementMapper;
    private final SgFormulaListMapper sgFormulaListMapper;

    public SgFormulaManagementServiceImpl(SgFormulaManagementMapper sgFormulaManagementMapper, SgFormulaListMapper sgFormulaListMapper) {
        this.sgFormulaManagementMapper = sgFormulaManagementMapper;
        this.sgFormulaListMapper = sgFormulaListMapper;
    }

    /**
     * 查询配方管理
     *
     * @param formulaId 配方管理主键
     * @return 配方管理
     */
    @Override
    public SgFormulaManagement selectSgFormulaManagementByFormulaId(String formulaId) {
        return sgFormulaManagementMapper.selectSgFormulaManagementByFormulaId(formulaId);
    }

    /**
     * 查询配方管理列表
     *
     * @param sgFormulaManagement 配方管理
     * @return 配方管理
     */
    @Override
    @DataScope(deptAlias = "sg_formula_management_alias", userAlias = "sg_formula_management_alias")
    public List<SgFormulaManagement> selectSgFormulaManagementList(SgFormulaManagement sgFormulaManagement) {
        List<SgFormulaManagement> sgFormulaManagements =
                sgFormulaManagementMapper.selectSgFormulaManagementList(sgFormulaManagement);

        Iterator<SgFormulaManagement> iterator = sgFormulaManagements.iterator();
        while (iterator.hasNext()) {
            SgFormulaManagement formulaManagement = iterator.next();
            // 子查询中去除重复的父项
            if (formulaManagement != null
                    && sgFormulaManagement.getQueryType() != null
                    && sgFormulaManagement.getQueryType().equals("Sub")
                    && Objects.equals(formulaManagement.getBatchId(), "0")) {
                iterator.remove(); // 安全删除
                continue; // 删除后跳过本次循环
            }
            // 绑定配方列表
            if (formulaManagement != null
                    && formulaManagement.getFormulaId() != null
                    && formulaManagement.getBatchId() != null) {
                SgFormulaList sgFormulaList = new SgFormulaList();
                sgFormulaList.setFormulaId(formulaManagement.getFormulaId());
                sgFormulaList.setBatchId(formulaManagement.getBatchId());
                List<SgFormulaList> formulaLists =
                        sgFormulaListMapper.selectSgFormulaListList(sgFormulaList);
                formulaManagement.setSgFormulaList(formulaLists);
            }
        }

        return sgFormulaManagements;
    }


    /**
     * 新增配方管理
     *
     * @param sgFormulaManagement 配方管理
     * @return 结果
     */
    @Override
    public int insertSgFormulaManagement(SgFormulaManagement sgFormulaManagement) {
        return sgFormulaManagementMapper.insertSgFormulaManagement(sgFormulaManagement);
    }

    /**
     * 修改配方管理
     *
     * @param sgFormulaManagement 配方管理
     * @return 结果
     */
    @Override
    public int updateSgFormulaManagement(SgFormulaManagement sgFormulaManagement) {
        return sgFormulaManagementMapper.updateSgFormulaManagement(sgFormulaManagement);
    }

    /**
     * 批量删除配方管理
     *
     * @param formulaIds 需要删除的配方管理主键
     * @return 结果
     */
    @Override
    public int deleteSgFormulaManagementByFormulaIds(String[] formulaIds) {
        return sgFormulaManagementMapper.deleteSgFormulaManagementByFormulaIds(formulaIds);
    }

    /**
     * 删除配方管理信息
     *
     * @param formulaId 配方管理主键
     * @return 结果
     */
    @Override
    public int deleteSgFormulaManagementByFormulaId(String formulaId) {
        return sgFormulaManagementMapper.deleteSgFormulaManagementByFormulaId(formulaId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteSgFormulaManagement(SgFormulaManagement sgFormulaManagement) {
        // 删除关联配方表
        SgFormulaList sgFormulaList = new SgFormulaList();
        sgFormulaList.setFormulaId(sgFormulaManagement.getFormulaId());
        sgFormulaList.setBatchId(sgFormulaManagement.getBatchId());
        sgFormulaListMapper.deleteSgFormulaListByFormulaIdAndBatchId(sgFormulaList);
        return sgFormulaManagementMapper.deleteSgFormulaManagement(sgFormulaManagement);
    }
}
