package easydo.technology.system.rest;

import easydo.technology.annotation.WebLog;
import easydo.technology.components.JdbcClient;
import easydo.technology.system.domain.SysDept;
import easydo.technology.system.dto.SysDeptDto;
import easydo.technology.utils.SecurityUtils;
import easydo.technology.utils.StringUtil;
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 javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.util.List;
import java.util.Map;


/**
 * @author Zheng Jie
 * @date 2019-03-25
 */
@RestController
@RequestMapping("/dept")
public class DeptController {
    @Resource
    JdbcClient jdbcClient;
    @Resource
    DataSource dataSource;

    @WebLog
    @RequestMapping("/getList")
    public Object getList(@RequestBody Map<String, Object> map) throws Exception {
        List<SysDept> deptList = jdbcClient.getJdbcList(map, SysDept.class);
        return new ResponseEntity<>(deptList, HttpStatus.OK);
    }

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

            model.setDeptId((long) 0);
            String username = SecurityUtils.getCurrentUsername();
            model.setCreateBy(username);
            model.setUpdateBy(username);
            jdbcClient.jdbcInsert(model, connection);

            if (StringUtil.isNotEmpty(model.getPid())) {
                SysDept pDept = new SysDept();
                pDept.setDeptId(model.getPid());
                pDept = jdbcClient.getJdbcModel(pDept, connection);
                pDept.setSubCount(pDept.getSubCount() + 1);
                jdbcClient.jdbcUpdateById(pDept, connection);
            }

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

    @WebLog
    @RequestMapping("/update")
    public Object update(@RequestBody SysDept dept) throws Exception {
        String username = SecurityUtils.getCurrentUsername();
        dept.setUpdateBy(username);
        jdbcClient.jdbcUpdateById(dept);
        return new ResponseEntity<>(dept, HttpStatus.OK);
    }

    @WebLog
    @RequestMapping("/remove")
    public Object remove(@RequestBody SysDept model) throws Exception {
        Connection connection = null;
        try {
            connection = dataSource.getConnection();

            SysDept dept = new SysDept();
            dept.setPid(model.getDeptId());
            int count = jdbcClient.getJdbcCount(dept, connection);
            if (count > 0) {
                throw new  Exception("该组织存在下级组织，请勿删除");
            }
            jdbcClient.jdbcRemoveById(model,connection);
            return new ResponseEntity<>(model, HttpStatus.OK);
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        } finally {
            jdbcClient.finallyExecute(connection);
        }
    }
}