package easydo.technology.service.impl;

import easydo.technology.components.JdbcClient;
import easydo.technology.enums.MESEnum;
import easydo.technology.model.Customer;
import easydo.technology.service.CustomerService;
import easydo.technology.service.FlowNoService;
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 CustomerServiceImpl implements CustomerService {

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

    @Override
    public Object getList(Map<String, Object> map) throws Exception {
        List<Customer> list = jdbcClient.getJdbcList(map, Customer.class);
        Connection connection = dataSource.getConnection();
        try {
            for (Customer model : list) {
                jdbcClient.getMinioFile(model, connection);
            }
        } finally {
            connection.close();
        }
        return list;
    }

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

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

    @Override
    public Object update(Customer 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(Customer model) throws Exception {
        Connection connection = dataSource.getConnection();
        try {
            connection.setAutoCommit(false);
            jdbcClient.jdbcRemoveById(model, connection);
            connection.commit();
            return model;
        } catch (Exception e) {
            connection.rollback();
            throw e;
        } finally {
            connection.close();
        }
    }
}

