package easydo.technology.controller;

import easydo.technology.annotation.WebLog;
import easydo.technology.components.JdbcClient;
import easydo.technology.exception.BizException;
import easydo.technology.model.SupplierType;
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;

@RestController
@RequestMapping("/supplierType")
public class SupplierTypeController {
    @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, SupplierType.class, connection);
            List<SupplierType> list = (List<SupplierType>) page.get("records");
            for (SupplierType model : list) {
                SupplierType supplierTypeParm = new SupplierType();
                supplierTypeParm.setPid(model.getId());
                int count = jdbcClient.getJdbcCount(supplierTypeParm, connection);
                if (count > 0) {
                    model.setHasChildren(true);
                } else {
                    model.setHasChildren(false);
                }
            }
            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<SupplierType> list = jdbcClient.getJdbcList(map, SupplierType.class, connection);
            for (SupplierType model : list) {
                SupplierType supplierTypeParm = new SupplierType();
                supplierTypeParm.setPid(model.getId());
                int count = jdbcClient.getJdbcCount(supplierTypeParm, connection);
                if (count > 0) {
                    model.setHasChildren(true);
                } else {
                    model.setHasChildren(false);
                }
            }
            return new ResponseEntity<>(list, HttpStatus.OK);
        } catch (Exception e) {
            throw e;
        } finally {
            jdbcClient.finallyExecute(connection);
        }
    }

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

            SupplierType param = new SupplierType();
            param.setName(model.getName());
            int count = jdbcClient.getJdbcCount(param, connection);
            if (count > 0) {
                throw new BizException("供应商类别名称不能重复");
            }
            param = new SupplierType();
            param.setNumber(model.getNumber());
            count = jdbcClient.getJdbcCount(param, connection);
            if (count > 0) {
                throw new BizException("供应商类别编号不能重复");
            }
            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 SupplierType model) throws Exception {
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            connection.setAutoCommit(false);

            SupplierType oldModel = jdbcClient.getJdbcModelById(model);
            if (!oldModel.getName().equals(model.getName())) {
                SupplierType param = new SupplierType();
                param.setName(model.getName());
                int count = jdbcClient.getJdbcCount(param, connection);
                if (count > 0) {
                    throw new BizException("供应商类别名称不能重复");
                }
            }
            if (!oldModel.getNumber().equals(model.getNumber())) {
                SupplierType param = new SupplierType();
                param.setNumber(model.getNumber());
                int count = jdbcClient.getJdbcCount(param, connection);
                if (count > 0) {
                    throw new BizException("供应商类别编号不能重复");
                }
            }
            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 SupplierType model) throws Exception {
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            connection.setAutoCommit(false);

            SupplierType param = new SupplierType();
            param.setPid(model.getId());
            int count = jdbcClient.getJdbcCount(param, 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);
        }
    }
}
