package com.jjt.calc.service.impl;

import com.jjt.calc.domain.TwinFormulaInfo;
import com.jjt.calc.mapper.TwinFormulaInfoMapper;
import com.jjt.calc.service.ITwinFormulaInfoService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 设备配方信息Service业务层处理
 *
 * @author wukai
 * @date 2025-01-18
 */
@Service
public class TwinFormulaInfoServiceImpl implements ITwinFormulaInfoService {
    @Resource
    private TwinFormulaInfoMapper twinFormulaInfoMapper;
    @Resource
    private SqlSessionFactory factory;

    /**
     * 查询设备配方信息
     *
     * @param specInfoId 设备配方信息主键
     * @return 设备配方信息
     */
    @Override
    public TwinFormulaInfo selectTwinFormulaInfoBySpecInfoId(Long specInfoId) {
        return twinFormulaInfoMapper.selectTwinFormulaInfoBySpecInfoId(specInfoId);
    }

    /**
     * 查询设备配方信息列表
     *
     * @param twinFormulaInfo 设备配方信息
     * @return 设备配方信息
     */
    @Override
    public List<TwinFormulaInfo> selectTwinFormulaInfoList(TwinFormulaInfo twinFormulaInfo) {
        return twinFormulaInfoMapper.selectTwinFormulaInfoList(twinFormulaInfo);
    }

    /**
     * 新增设备配方信息
     *
     * @param twinFormulaInfo 设备配方信息
     * @return 结果
     */
    @Override
    public int insertTwinFormulaInfo(TwinFormulaInfo twinFormulaInfo) {
        return twinFormulaInfoMapper.insertTwinFormulaInfo(twinFormulaInfo);
    }

    /**
     * 修改设备配方信息
     *
     * @param twinFormulaInfo 设备配方信息
     * @return 结果
     */
    @Override
    public int updateTwinFormulaInfo(TwinFormulaInfo twinFormulaInfo) {
        return twinFormulaInfoMapper.updateTwinFormulaInfo(twinFormulaInfo);
    }

    /**
     * 批量删除设备配方信息
     *
     * @param specInfoIds 需要删除的设备配方信息主键
     * @return 结果
     */
    @Override
    public int deleteTwinFormulaInfoBySpecInfoIds(Long[] specInfoIds) {
        return twinFormulaInfoMapper.deleteTwinFormulaInfoBySpecInfoIds(specInfoIds);
    }

    /**
     * 删除设备配方信息信息
     *
     * @param specInfoId 设备配方信息主键
     * @return 结果
     */
    @Override
    public int deleteTwinFormulaInfoBySpecInfoId(Long specInfoId) {
        return twinFormulaInfoMapper.deleteTwinFormulaInfoBySpecInfoId(specInfoId);
    }

    /**
     * 根据列表更新
     *
     * @param formulaInfos 列表
     */
    @Override
    public void update(List<TwinFormulaInfo> formulaInfos) {
        List<TwinFormulaInfo> oldList = selectTwinFormulaInfoList(new TwinFormulaInfo());
        List<Long> idList = oldList.stream().map(TwinFormulaInfo::getSpecInfoId).collect(Collectors.toList());
        List<TwinFormulaInfo> insertList = new ArrayList<>();
        List<TwinFormulaInfo> updateList = new ArrayList<>();
        for (TwinFormulaInfo info : formulaInfos) {
            if (idList.contains(info.getSpecInfoId())) {
                updateList.add(info);
            } else {
                insertList.add(info);
            }
        }
        try (SqlSession sqlSession = factory.openSession(ExecutorType.BATCH, false)) {
            TwinFormulaInfoMapper mapper = sqlSession.getMapper(TwinFormulaInfoMapper.class);
            if (insertList.size() > 0) {
                insertList.forEach(mapper::insertTwinFormulaInfo);
            }
            if (updateList.size() > 0) {
                updateList.forEach(mapper::updateTwinFormulaInfo);
            }
            sqlSession.commit();
        }
    }
}
