package easydo.technology.service.impl;

import easydo.technology.components.JdbcClient;
import easydo.technology.enums.MESEnum;
import easydo.technology.exception.BizException;
import easydo.technology.model.MinioFile;
import easydo.technology.model.ProcessRouteDetail;
import easydo.technology.model.ProcessStage;
import easydo.technology.service.FlowNoService;
import easydo.technology.service.ProcessStageService;
import easydo.technology.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.util.List;
import java.util.Map;

@Service
public class ProcessStageServiceImpl implements ProcessStageService {

    @Resource
    JdbcClient jdbcClient;
    @Resource
    DataSource dataSource;
    @Resource
    FlowNoService flowNoService;

    @Override
    public Object getList(Map<String, Object> map) throws Exception {
        List<ProcessStage> list = jdbcClient.getJdbcList(map, ProcessStage.class);
        return list;
    }

    @Override
    public Object getPage(Map<String, Object> map) throws Exception {
        Connection connection = dataSource.getConnection();
        try {
            Map<String, Object> recordsPage = jdbcClient.getJdbcPage(map, ProcessStage.class, connection);
            List<ProcessStage> records = (List<ProcessStage>) recordsPage.get("records");
            for (ProcessStage model : records) {
                jdbcClient.getMinioFile(model, connection);
            }
            return recordsPage;
        } catch (Exception e) {
            throw e;
        } finally {
            connection.close();
        }
    }

    @Override
    public Object save(ProcessStage model) throws Exception {
        Connection connection = dataSource.getConnection();
        try {
            connection.setAutoCommit(false);
            model.setStatus(MESEnum.PROCESS_OF_STATUS_ENABLE.getValue());
            model.setCreateId(SecurityUtils.getCurrentUserId());
            model.setCode(flowNoService.generateProcessStageCode(model, connection));
            jdbcClient.jdbcInsert(model, connection);
            connection.commit();
            return model;
        } catch (Exception e) {
            connection.rollback();
            throw e;
        } finally {
            connection.close();
        }
    }

    @Override
    public Object update(ProcessStage model) throws Exception {
        Connection connection = dataSource.getConnection();
        try {
            connection.setAutoCommit(false);
            model.setUpdateId(SecurityUtils.getCurrentUserId());
            jdbcClient.jdbcUpdateById(model, connection);
            connection.commit();
            return model;
        } catch (Exception e) {
            connection.rollback();
            throw e;
        } finally {
            connection.close();
        }
    }

    @Override
    public Object remove(ProcessStage model) throws Exception {
        Connection connection = dataSource.getConnection();
        try {
            connection.setAutoCommit(false);

            ProcessRouteDetail routeDetail = new ProcessRouteDetail();
            routeDetail.setStageId(model.getId());
            int count = jdbcClient.getJdbcCount(routeDetail, connection);
            if (count > 0) {
                throw new BizException("当前工序已被绑定到工艺路线，请勿删除");
            }

            jdbcClient.jdbcRemoveById(model, connection);

            connection.commit();
            return model;
        } catch (Exception e) {
            connection.rollback();
            throw e;
        } finally {
            connection.close();
        }
    }
}

