package easydo.technology.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import easydo.technology.annotation.WebLog;
import easydo.technology.components.JdbcClient;
import easydo.technology.model.ApproveInfo;
import easydo.technology.model.GldDept;
import easydo.technology.model.GldMember;
import easydo.technology.model.SysUser;
import easydo.technology.service.GLDApproveService;
import easydo.technology.service.GLDMasterService;
import easydo.technology.utils.StringUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/gldMaster")
public class GLDMasterController {
    @Resource
    GLDMasterService gldMasterService;
    @Resource
    GLDApproveService gldApproveService;
    @Value("${GLD.APPROVE.mgtGroupId}")
    String mgtGroupId;
    @Value("${tenantId_gld_master}")
    String tenantId;
    @Value("${GLD.host}")
    String host;
    @Resource
    DataSource dataSource;
    @Resource
    JdbcClient jdbcClient;

    @WebLog
    @RequestMapping("/getToken")
    public Object getToken() throws Exception {
        Map<String, Object> result = new HashMap<>();
        String token = gldMasterService.getGetToken();
        result.put("mgtGroupId", mgtGroupId);
        result.put("tenantId", tenantId);
        result.put("token", token);
        result.put("host", host);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    @WebLog
    @RequestMapping("/getFormId")
    public Object getFormId(@RequestBody ApproveInfo approveInfo) throws Exception {
        Connection connection = null;
        try {
            connection = dataSource.getConnection();

            approveInfo = jdbcClient.getJdbcModel(approveInfo, connection);
            return new ResponseEntity<>(approveInfo.getFormId(), HttpStatus.OK);
        } catch (Exception e) {
            throw e;
        } finally {
            jdbcClient.finallyExecute(connection);
        }
    }


    @WebLog
    @GetMapping("/removeToken")
    public Object removeToken() throws Exception {
        gldMasterService.removeToken();
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @WebLog
    @RequestMapping("/getUserDeptList")
    public Object getUserDeptList(@RequestBody SysUser user) throws Exception {
        if (StringUtil.isEmpty(user.getPhone())) {
            throw new Exception("参数错误");
        }

        Connection connection = null;
        try {
            connection = dataSource.getConnection();

            GldMember gldMember = new GldMember();
            gldMember.setPhone(user.getPhone());
            gldMember.setActive(true);
            gldMember.setEnabled(true);
            gldMember = jdbcClient.getJdbcModel(gldMember, connection);

            if (StringUtil.isEmpty(gldMember.getId())) {
                throw new Exception("手机号参数错误或缺失");
            }

            JSONArray list = gldApproveService.getUserDeptList(gldMember.getUserId());
            List<GldDept> deptList = new ArrayList<>();
            for (Object object : list) {
                JSONObject obj = (JSONObject) object;
                Long deptId = obj.getLong("deptId");
                GldDept gldDept = new GldDept();
                gldDept.setId(deptId.toString());
                gldDept = jdbcClient.getJdbcModel(gldDept, connection);
                deptList.add(gldDept);
            }
            deptList = deptList.stream().filter(model->!"内部单位".equals(model.getName())).collect(Collectors.toList());
            return new ResponseEntity<>(deptList, HttpStatus.OK);
        } catch (Exception e) {
            throw e;
        } finally {
            jdbcClient.finallyExecute(connection);
        }
    }
}
