package com.huigou.topsun.technology.application.impl;

import cn.hutool.core.bean.BeanUtil;
import com.huigou.data.query.model.QueryDescriptor;
import com.huigou.topsun.technology.application.ProcessApplication;
import com.huigou.topsun.technology.application.ProcessResourceApplication;
import com.huigou.topsun.technology.application.TechnologyProcessApplication;
import com.huigou.topsun.technology.domain.model.ProcessResource;
import com.huigou.topsun.technology.domain.model.TechnologyProcess;
import com.huigou.topsun.technology.repository.TechnologyProcessRepository;
import com.huigou.topsun.technology.vo.ProcessVo;
import com.huigou.topsun.technology.vo.TechnologyProcessVo;
import com.huigou.uasp.bmp.common.application.BaseApplication;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
* @author 16508
* @description 针对表【technology_process(工艺工序关系表)】的数据库操作Service实现
* @createDate 2023-11-22 11:04:03
*/
@Service
public class TechnologyProcessApplicationImpl extends BaseApplication implements TechnologyProcessApplication {
    @Resource
    private TechnologyProcessRepository technologyProcessRepository;
    @Resource
    private ProcessApplication processApplication;
    @Resource
    private ProcessResourceApplication processResourceApplication;

    @Override
    public List<Map<String, Object>> slicedTechnologyProcessList(String technologyId) {
        QueryDescriptor queryDescriptor = this.sqlExecutorDao.getQuery(QUERY_XML_FILE_PATH, "slicedTechnologyProcessList");
        String sql = queryDescriptor.getSql();
        return this.sqlExecutorDao.queryToListMap(sql,technologyId);
    }

    @Override
    public void saveTechnologyProcessVoList(String technologyId, List<TechnologyProcessVo> technologyProcessList) {
        technologyProcessList.forEach(technologyProcessVo -> {

            //保存工艺工序关系
            TechnologyProcess technologyProcess = new TechnologyProcess();
            technologyProcess.setTechnologyId(technologyId);
            technologyProcess.setProcessId(technologyProcessVo.getProcessId());
            technologyProcessRepository.saveAndFlush(technologyProcess);

            //保存工序资源
            ProcessResource processResource = new ProcessResource();
            processResource.setTechnologyProcessId(technologyProcess.getTechnologyProcessId());
            processResource.setResourceId(technologyProcessVo.getResourceId());
            processResourceApplication.saveProcessResource(processResource);
        });
    }

    @Override
    public void updateTechnologyProcessVoList(String technologyId, List<TechnologyProcessVo> processList) {
        //先删除再保存
        List<TechnologyProcess> technologyProcessList = technologyProcessRepository.findByTechnologyId(technologyId);
        this.deleteTechnologyProcessIds(technologyProcessList);

        this.saveTechnologyProcessVoList(technologyId,processList);
    }

    @Override
    public List<TechnologyProcess> findByTechnologyId(String technologyId) {
        return technologyProcessRepository.findByTechnologyId(technologyId);
    }

    @Override
    public void deleteByTechnologyProcessIds(List<String> technologyProcessIds) {
        List<TechnologyProcess> technologyProcessList = technologyProcessRepository.findAll(technologyProcessIds);
        this.deleteTechnologyProcessIds(technologyProcessList);
    }

    public void deleteTechnologyProcessIds(List<TechnologyProcess> technologyProcessList){
        technologyProcessList.forEach(technologyProcess -> {
            //删除工序资源
            processResourceApplication.deleteByTechnologyProcessId(technologyProcess.getTechnologyProcessId());
        });
        //删除工艺工序关系表
        technologyProcessRepository.delete(technologyProcessList);
    }
}




