package easydo.technology.controller;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import easydo.technology.annotation.WebLog;
import easydo.technology.components.JdbcClient;
import easydo.technology.exception.BizException;
import easydo.technology.system.domain.SysJob;
import easydo.technology.system.domain.SysUsersJobs;

@RestController
@RequestMapping("/job")
public class JobController {
    @Resource
    JdbcClient jdbcClient;
    @Resource
    DataSource dataSource;

    @WebLog
    @RequestMapping(value = "/getPage")
    public Object getPage(@RequestBody Map<String, Object> map) throws Exception{
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            Map<String, Object> page = jdbcClient.getJdbcPage(map, SysJob.class, connection);
            return new ResponseEntity<>(page, HttpStatus.OK);
        } catch (Exception e) {
            throw e;
        } finally {
            jdbcClient.finallyExecute(connection);
        }
    }

    @WebLog
    @RequestMapping(value = "/getList")
    public Object getList(@RequestBody Map<String, Object> map) throws Exception {
        Connection connection = null;
        try {
            connection = dataSource.getConnection();

            List<SysJob> list = jdbcClient.getJdbcList(map, SysJob.class, connection);
            return new ResponseEntity<>(list, HttpStatus.OK);
        } catch (Exception e) {
            throw e;
        } finally {
            jdbcClient.finallyExecute(connection);
        }
    }

    @WebLog
    @RequestMapping(value = "/save")
    public Object save(@RequestBody SysJob model) throws Exception {
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            connection.setAutoCommit(false);

            jdbcClient.jdbcInsert(model, connection);

            connection.commit();
            return new ResponseEntity<>(model, HttpStatus.OK);
        } catch (Exception e) {
            connection.rollback();
            throw e;
        } finally {
            jdbcClient.finallyExecute(connection);
        }
    }

    @WebLog
    @RequestMapping(value = "/update")
    public Object update(@RequestBody SysJob model) throws Exception {
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            connection.setAutoCommit(false);

            jdbcClient.jdbcUpdateById(model, connection);
            connection.commit();
            return new ResponseEntity<>(model, HttpStatus.OK);
        } catch (Exception e) {
            connection.rollback();
            throw e;
        } finally {
            jdbcClient.finallyExecute(connection);
        }
    }

    @WebLog
    @RequestMapping(value = "/remove")
    public Object remove(@RequestBody SysJob model) throws Exception {

        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            connection.setAutoCommit(false);
            SysUsersJobs usersJobs = new SysUsersJobs();
            usersJobs.setJobId(model.getId());
            int count = jdbcClient.getJdbcCount(usersJobs, connection);
            if (count > 0) {
                throw new BizException("此岗位已被引用，请勿删除");
            }

            jdbcClient.jdbcRemoveById(model, connection);

            connection.commit();
            return new ResponseEntity<>(model, HttpStatus.OK);
        } catch (Exception e) {
            connection.rollback();
            throw e;
        } finally {
            jdbcClient.finallyExecute(connection);
        }
    }

}
