package easydo.technology.controller;

import com.alibaba.fastjson.JSONArray;

import easydo.technology.annotation.WebLog;
import easydo.technology.components.JdbcClient;
import easydo.technology.components.ZeroSpringFolderComponent;
import easydo.technology.enums.ChengfaEnum;
import easydo.technology.exception.BizException;
import easydo.technology.model.BusApprove;
import easydo.technology.model.Purchase;
import easydo.technology.model.PurchaseMatter;
import easydo.technology.model.Supplier;
import easydo.technology.model.SupplierBank;
import easydo.technology.model.vo.GLDApproveVo;
import easydo.technology.service.FlowNoService;
import easydo.technology.service.GLDApproveService;
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.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/purchaseMatter")
public class PurchaseMatterController {
    @Resource
    JdbcClient jdbcClient;
    @Resource
    DataSource dataSource;
    @Resource
    GLDApproveService gldApproveService;
    @Resource
    FlowNoService flowNoService;

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

            if (StringUtil.isEmpty(model.getId())) {
                model.setBusinessNo(flowNoService.getFlowNo(ChengfaEnum.FLOWNO_TYPE_PURCHASE_MATTER.getValue(), connection));
                jdbcClient.jdbcInsert(model, connection);
            } else {
                checkData(model, connection);
                jdbcClient.jdbcUpdateById(model, connection);
            }

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

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

            if (StringUtil.isEmpty(model.getId())) {
                model.setBusinessNo(flowNoService.getFlowNo(ChengfaEnum.FLOWNO_TYPE_PURCHASE_MATTER.getValue(), connection));
                jdbcClient.jdbcInsert(model, connection);
            } else {
                checkData(model, connection);
                jdbcClient.jdbcUpdateById(model, connection);
            }

            Purchase purchase = new Purchase();
            purchase.setId(model.getPurchaseId());
            purchase = jdbcClient.getJdbcModelById(purchase, connection);

            GLDApproveVo vo = new GLDApproveVo();
            vo.setTargetId(0);
            vo.setTargetType(ChengfaEnum.BUS_APPROVE_TYPE_PURCHASE_MATTER.getValue());
            vo.setTemplateId(model.getTemplateId());
            String taskId = gldApproveService.startApprove(vo);
            vo.setTaskId(taskId);
            vo = gldApproveService.getApproveInfo(vo);

            model.setProcessInstanceId(vo.getInstanceId());
            model.setProcessState(vo.getState());
            model.setProcessTaskId(vo.getTaskId());
            model.setProcessTemplateId(vo.getTemplateId());

            purchase.setStep(ChengfaEnum.PURCHASE_STEP_PURCHASE_MATTER.getValue());
            purchase.setStatus(ChengfaEnum.PURCHASE_STATUS_APPROVE.getValue());
            jdbcClient.jdbcUpdateById(purchase, connection);

            model.setStatus(ChengfaEnum.PURCHASE_STATUS_APPROVE.getValue());
            jdbcClient.jdbcUpdateById(model, connection);

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

    @WebLog
    @RequestMapping(value = "/getById")
    public Object getById(@RequestBody PurchaseMatter model) throws Exception {
        Map<String, Object> result = new HashMap<>();
        Connection connection = null;
        try {
            connection = dataSource.getConnection();

            model = jdbcClient.getJdbcModelById(model, connection);
            ZeroSpringFolderComponent.build(dataSource).fetch(model);

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

    public Purchase checkData(PurchaseMatter model, Connection connection) throws Exception {
        Purchase purchase = new Purchase();
        purchase.setId(model.getPurchaseId());
        purchase = jdbcClient.getJdbcModelById(purchase, connection);
        if (ChengfaEnum.PURCHASE_STEP_PURCHASE_MATTER.getValue().equals(purchase.getStep())) {
            throw new BizException("此事项已经发起事项审批，请勿操作");
        }

        BusApprove busApprove = new BusApprove();
        busApprove.setBusId(model.getId());
        busApprove.setBusType(ChengfaEnum.BUS_APPROVE_TYPE_PURCHASE_MATTER.getValue());
        busApprove.setApproveStatus(ChengfaEnum.BUS_APPROVE_APPROVE_STATUS_DONE.getValue());
        busApprove.setApproveResult(ChengfaEnum.BUS_APPROVE_APPROVE_RESULT_Y.getValue());
        int count = jdbcClient.getJdbcCount(busApprove, connection);
        if (count > 0) {
            throw new BizException("此事项已经发起事项审批，请勿重复操作");
        }
        return purchase;
    }
}
