package easydo.technology.service.impl;

import easydo.technology.components.JdbcClient;
import easydo.technology.enums.MESEnum;
import easydo.technology.exception.BizException;
import easydo.technology.model.ProductBom;
import easydo.technology.model.ProductMaterial;
import easydo.technology.service.FlowNoService;
import easydo.technology.service.ProductMaterialService;
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.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductMaterialServiceImpl implements ProductMaterialService {

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

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

    @Override
    public Object getPage(Map<String, Object> map) throws Exception {
        Connection connection = dataSource.getConnection();
        try {
            return jdbcClient.getJdbcPage(map, ProductMaterial.class, connection);
        } finally {
            connection.close();
        }
    }

    @Override
    public Object save(ProductMaterial 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.generateProductMaterialCode(model, connection));
            jdbcClient.jdbcInsert(model, connection);
            connection.commit();
            return model;
        } catch (Exception e) {
            connection.rollback();
            throw e;
        } finally {
            connection.close();
        }
    }

    @Override
    public Object update(ProductMaterial model) throws Exception {
        Connection connection = dataSource.getConnection();
        try {
            connection.setAutoCommit(false);
            model.setUpdateId(SecurityUtils.getCurrentUserId());
            jdbcClient.jdbcUpdateById(model, connection);

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("materialCode", model.getCode());
            ProductBom bom = new ProductBom();
            bom.setMaterialName(model.getName());
            jdbcClient.jdbcUpdate(bom, paramMap, connection);

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

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

            ProductMaterial exist = jdbcClient.getJdbcModelById(model, connection);
            if (exist == null) {
                throw new BizException("物料不存在");
            }

            ProductBom bom = new ProductBom();
            bom.setMaterialCode(exist.getCode());
            int count = jdbcClient.getJdbcCount(bom, connection);
            if (count > 0) {
                throw new BizException("该物料在bom管理中存在，无法删除");
            }

            jdbcClient.jdbcRemoveById(exist, connection);

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

