package com.mortals.xhx.busiz.web;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.ICacheService;
import com.mortals.xhx.busiz.req.ComplexHandleReq;
import com.mortals.xhx.busiz.req.ComplexImplementationReq;
import com.mortals.xhx.busiz.req.accept.ComplexAcceptReq;
import com.mortals.xhx.busiz.rsp.ApiResp;
import com.mortals.xhx.busiz.rsp.ComplexApiResp;
import com.mortals.xhx.busiz.rsp.ImplementEventInfo;
import com.mortals.xhx.busiz.rsp.implement.ComplexImplementDetailInfo;
import com.mortals.xhx.busiz.rsp.material.ComplexMaterialInfo;
import com.mortals.xhx.busiz.rsp.situation.ComplexStituationInfo;
import com.mortals.xhx.busiz.rsp.work.ComplexWorkInfo;
import com.mortals.xhx.common.code.ApiRespCodeEnum;
import com.mortals.xhx.protocol.complex.ComplexApiRest;
import com.mortals.xhx.protocol.complex.matter.model.rsp.DictInfo;
import com.mortals.xhx.protocol.complex.matter.model.rsp.MatterListInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.math3.Field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;

import static com.mortals.xhx.common.key.Constant.HTTP_TIMEOUT;
import static com.mortals.xhx.common.key.RedisKey.*;

/**
 * 综窗对接api
 *
 * @author: zxfei
 * @date: 2023/11/30 14:17
 */
@RestController
@Slf4j
@RequestMapping("api")
public class ComplexApiController {

    @Autowired
    private ICacheService cacheService;

    /**
     * 事项列表查询
     *
     * @param req
     * @return
     */
    @PostMapping("event-implementation/list")
    @UnAuth
    public String eventImplementationList(@RequestBody ComplexImplementationReq req) {
        log.info("【事项列表查询】【请求体】-->  " + JSONObject.toJSONString(req));
        req.setUrlPath("eventImplementation/list");
        ApiResp<String> rsp = new ApiResp<>();
        rsp.setMsg(ApiRespCodeEnum.SUCCESS.getLabel());
        rsp.setCode(ApiRespCodeEnum.SUCCESS.getValue());
        try {
            req.setNonce(RandomUtil.randomNumbers(6));
            cacheService.lpush(KEY_COMPLEX_REQ, JSONObject.toJSONString(req));
            String rest = cacheService.blpop(KEY_EVENT_IMPLEMENTATION_RESP + req.getNonce(), HTTP_TIMEOUT, String.class);
            if (ObjectUtil.isEmpty(rest)) {
                throw new AppException("请求访问超时");
            }
            Rest<ImplementEventInfo> eventListRest = JSON.parseObject(rest, new TypeReference<Rest<ImplementEventInfo>>() {
            });
            return JSON.toJSONString(eventListRest);
        } catch (Exception e) {
            log.error("接收数据失败", e);
            rsp.setCode(ApiRespCodeEnum.FAILED.getValue());
            rsp.setMsg(e.getMessage());
            return JSON.toJSONString(rsp);
        }
    }

    /**
     * 事项详细查询
     *
     * @param req
     * @return
     */
    @PostMapping("event-implementation/get-by-id")
    @UnAuth
    public String implementationGetById(@RequestBody ComplexImplementationReq req) {
        log.info("【事项详细查询】【请求体】-->  " + JSONObject.toJSONString(req));
        req.setUrlPath("event-implementation/get-by-id");
        ApiResp<String> rsp = new ApiResp<>();
        rsp.setMsg(ApiRespCodeEnum.SUCCESS.getLabel());
        rsp.setCode(ApiRespCodeEnum.SUCCESS.getValue());
        try {
            req.setNonce(RandomUtil.randomNumbers(6));
            cacheService.lpush(KEY_COMPLEX_REQ, JSONObject.toJSONString(req));
            String rest = cacheService.blpop(KEY_EVENT_IMPLEMENTATION_RESP + req.getNonce(), HTTP_TIMEOUT, String.class);
            if (ObjectUtil.isEmpty(rest)) {
                throw new AppException("请求访问超时");
            }
            Rest<ComplexImplementDetailInfo> eventRest = JSON.parseObject(rest, new TypeReference<Rest<ComplexImplementDetailInfo>>() {
            });
            return JSON.toJSONString(eventRest);
        } catch (Exception e) {
            log.error("接收数据失败", e);
            rsp.setCode(ApiRespCodeEnum.FAILED.getValue());
            rsp.setMsg(e.getMessage());
            return JSON.toJSONString(rsp);
        }
    }

    /**
     * 查询办理事项情形
     *
     * @param req
     * @return
     */
    @PostMapping("bus-situation-material/getSituationMaterialTreeByEventIds")
    @UnAuth
    public String getSituationMaterialTreeByEventIds(@RequestBody ComplexImplementationReq req) {
        log.info("【查询办理事项情形】【请求体】-->  " + JSONObject.toJSONString(req));
        req.setUrlPath("bus-situation-material/getSituationMaterialTreeByEventIds");
        ApiResp<String> rsp = new ApiResp<>();
        rsp.setMsg(ApiRespCodeEnum.SUCCESS.getLabel());
        rsp.setCode(ApiRespCodeEnum.SUCCESS.getValue());
        try {
            req.setNonce(RandomUtil.randomNumbers(6));
            cacheService.lpush(KEY_COMPLEX_REQ, JSONObject.toJSONString(req));
            String rest = cacheService.blpop(KEY_EVENT_IMPLEMENTATION_RESP + req.getNonce(), HTTP_TIMEOUT, String.class);
            if (ObjectUtil.isEmpty(rest)) {
                throw new AppException("请求访问超时");
            }
            Rest<ComplexStituationInfo> eventRest = JSON.parseObject(rest, new TypeReference<Rest<ComplexStituationInfo>>() {
            });
            return JSON.toJSONString(eventRest);
        } catch (Exception e) {
            log.error("接收数据失败", e);
            rsp.setCode(ApiRespCodeEnum.FAILED.getValue());
            rsp.setMsg(e.getMessage());
            return JSON.toJSONString(rsp);
        }
    }

    /**
     * 根据事项与情形查询办理事项材料
     *
     * @param req
     * @return
     */
    @PostMapping("consulting-service/getMaterialByEventAndSituation")
    @UnAuth
    public String getMaterialByEventAndSituation(@RequestBody ComplexImplementationReq req) {
        log.info("【根据事项与情形查询办理事项材料】【请求体】-->  " + JSONObject.toJSONString(req));
        req.setUrlPath("consulting-service/getMaterialByEventAndSituation");
        ApiResp<String> rsp = new ApiResp<>();
        rsp.setMsg(ApiRespCodeEnum.SUCCESS.getLabel());
        rsp.setCode(ApiRespCodeEnum.SUCCESS.getValue());
        try {
            req.setNonce(RandomUtil.randomNumbers(6));
            cacheService.lpush(KEY_COMPLEX_REQ, JSONObject.toJSONString(req));
            String rest = cacheService.blpop(KEY_EVENT_IMPLEMENTATION_RESP + req.getNonce(), HTTP_TIMEOUT, String.class);
            if (ObjectUtil.isEmpty(rest)) {
                throw new AppException("请求访问超时");
            }
            Rest<List<ComplexMaterialInfo>> eventRest = JSON.parseObject(rest, new TypeReference<Rest<List<ComplexMaterialInfo>>>() {
            });
            return JSON.toJSONString(eventRest);
        } catch (Exception e) {
            log.error("接收数据失败", e);
            rsp.setCode(ApiRespCodeEnum.FAILED.getValue());
            rsp.setMsg(e.getMessage());
            return JSON.toJSONString(rsp);
        }
    }

    /**
     * 根据事项查询办理事项材料
     *
     * @param req
     * @return
     */
    @PostMapping("material/findList")
    @UnAuth
    public String materialFindList(@RequestBody ComplexImplementationReq req) {
        log.info("【根据事项查询办理事项材料】【请求体】-->  " + JSONObject.toJSONString(req));
        req.setUrlPath("material/findList");
        ApiResp<String> rsp = new ApiResp<>();
        rsp.setMsg(ApiRespCodeEnum.SUCCESS.getLabel());
        rsp.setCode(ApiRespCodeEnum.SUCCESS.getValue());
        try {
            req.setNonce(RandomUtil.randomNumbers(6));
            cacheService.lpush(KEY_COMPLEX_REQ, JSONObject.toJSONString(req));
            String rest = cacheService.blpop(KEY_EVENT_IMPLEMENTATION_RESP + req.getNonce(), HTTP_TIMEOUT, String.class);
            if (ObjectUtil.isEmpty(rest)) {
                throw new AppException("请求访问超时");
            }
            Rest<List<ComplexMaterialInfo>> eventRest = JSON.parseObject(rest, new TypeReference<Rest<List<ComplexMaterialInfo>>>() {
            });
            return JSON.toJSONString(eventRest);
        } catch (Exception e) {
            log.error("接收数据失败", e);
            rsp.setCode(ApiRespCodeEnum.FAILED.getValue());
            rsp.setMsg(e.getMessage());
            return JSON.toJSONString(rsp);
        }
    }


    /**
     * 获取办件要素
     *
     * @param req
     * @return
     */
    @PostMapping("event-implementation/getDynamicFormByEventIds")
    @UnAuth
    public String getDynamicFormByEventIds(@RequestBody ComplexImplementationReq req) {
        log.info("【获取办件要素】【请求体】-->  " + JSONObject.toJSONString(req));
        req.setUrlPath("event-implementation/getDynamicFormByEventIds");
        ApiResp<String> rsp = new ApiResp<>();
        rsp.setMsg(ApiRespCodeEnum.SUCCESS.getLabel());
        rsp.setCode(ApiRespCodeEnum.SUCCESS.getValue());
        try {
            req.setNonce(RandomUtil.randomNumbers(6));
            cacheService.lpush(KEY_COMPLEX_REQ, JSONObject.toJSONString(req));
            String rest = cacheService.blpop(KEY_EVENT_IMPLEMENTATION_RESP + req.getNonce(), HTTP_TIMEOUT, String.class);
            if (ObjectUtil.isEmpty(rest)) {
                throw new AppException("请求访问超时");
            }
            Rest<ComplexWorkInfo> eventRest = JSON.parseObject(rest, new TypeReference<Rest<ComplexWorkInfo>>() {
            });
            return JSON.toJSONString(eventRest);
        } catch (Exception e) {
            log.error("接收数据失败", e);
            rsp.setCode(ApiRespCodeEnum.FAILED.getValue());
            rsp.setMsg(e.getMessage());
            return JSON.toJSONString(rsp);
        }
    }

    /**
     * 查询字典值
     *
     * @param req
     * @return
     */
    @PostMapping("common/queryDict")
    @UnAuth
    public String queryDict(@RequestBody ComplexImplementationReq req) {
        log.info("【查询字典值】【请求体】-->  " + JSONObject.toJSONString(req));
        req.setUrlPath("common/queryDict");
        ApiResp<String> rsp = new ApiResp<>();
        rsp.setMsg(ApiRespCodeEnum.SUCCESS.getLabel());
        rsp.setCode(ApiRespCodeEnum.SUCCESS.getValue());
        try {
            req.setNonce(RandomUtil.randomNumbers(6));
            cacheService.lpush(KEY_COMPLEX_REQ, JSONObject.toJSONString(req));
            String rest = cacheService.blpop(KEY_EVENT_IMPLEMENTATION_RESP + req.getNonce(), HTTP_TIMEOUT, String.class);
            if (ObjectUtil.isEmpty(rest)) {
                throw new AppException("请求访问超时");
            }
            Rest<List<DictInfo>> eventRest = JSON.parseObject(rest, new TypeReference<Rest<List<DictInfo>>>() {
            });
            return JSON.toJSONString(eventRest);
        } catch (Exception e) {
            log.error("接收数据失败", e);
            rsp.setCode(ApiRespCodeEnum.FAILED.getValue());
            rsp.setMsg(e.getMessage());
            return JSON.toJSONString(rsp);
        }
    }


    /**
     * 自助设备接件
     *
     * @param req
     * @return
     */
    @PostMapping("self-device-info/acceptHandling")
    @UnAuth
    public String acceptHandling(@RequestBody ComplexAcceptReq req) {
        log.info("【自助设备接件】【请求体】-->  " + JSONObject.toJSONString(req));
        req.setUrlPath("self-device-info/acceptHandling");
        ApiResp<String> rsp = new ApiResp<>();
        rsp.setMsg(ApiRespCodeEnum.SUCCESS.getLabel());
        rsp.setCode(ApiRespCodeEnum.SUCCESS.getValue());
        try {
            req.setNonce(RandomUtil.randomNumbers(6));
            cacheService.lpush(KEY_COMPLEX_REQ, JSONObject.toJSONString(req));
            String rest = cacheService.blpop(KEY_EVENT_IMPLEMENTATION_RESP + req.getNonce(), 30, String.class);
            if (ObjectUtil.isEmpty(rest)) {
                throw new AppException("请求访问超时");
            }
            Rest<List<DictInfo>> eventRest = JSON.parseObject(rest, new TypeReference<Rest<List<DictInfo>>>() {
            });
            return JSON.toJSONString(eventRest);
        } catch (Exception e) {
            log.error("接收数据失败", e);
            rsp.setCode(ApiRespCodeEnum.FAILED.getValue());
            rsp.setMsg(e.getMessage());
            return JSON.toJSONString(rsp);
        }
    }


    /**
     * 查询代办列表
     *
     * @param req
     * @return
     */
    @PostMapping("handling/findPageWithPermission")
    @UnAuth
    public String findPageWithPermission(@RequestBody ComplexHandleReq req) {
        log.info("【查询代办列表】【请求体】-->  " + JSONObject.toJSONString(req));
        req.setUrlPath("self-device-info/acceptHandling");
        ApiResp<String> rsp = new ApiResp<>();
        rsp.setMsg(ApiRespCodeEnum.SUCCESS.getLabel());
        rsp.setCode(ApiRespCodeEnum.SUCCESS.getValue());
        try {
            req.setNonce(RandomUtil.randomNumbers(6));
            cacheService.lpush(KEY_COMPLEX_REQ, JSONObject.toJSONString(req));
            String rest = cacheService.blpop(KEY_EVENT_IMPLEMENTATION_RESP + req.getNonce(), 30, String.class);
            if (ObjectUtil.isEmpty(rest)) {
                throw new AppException("请求访问超时");
            }
            Rest<List<DictInfo>> eventRest = JSON.parseObject(rest, new TypeReference<Rest<List<DictInfo>>>() {
            });
            return JSON.toJSONString(eventRest);
        } catch (Exception e) {
            log.error("接收数据失败", e);
            rsp.setCode(ApiRespCodeEnum.FAILED.getValue());
            rsp.setMsg(e.getMessage());
            return JSON.toJSONString(rsp);
        }
    }

    /**
     * 查询办理事项情形
     *
     * @param req
     * @return
     */
    @PostMapping("bus-situation-material/judgmentExist")
    @UnAuth
    public String judgmentExist(@RequestBody ComplexImplementationReq req) {
        log.info("【查询办理事项情形】【请求体】-->  " + JSONObject.toJSONString(req));
        req.setUrlPath("bus-situation-material/judgmentExist");
        ApiResp<String> rsp = new ApiResp<>();
        rsp.setMsg(ApiRespCodeEnum.SUCCESS.getLabel());
        rsp.setCode(ApiRespCodeEnum.SUCCESS.getValue());
        try {
            req.setNonce(RandomUtil.randomNumbers(6));
            cacheService.lpush(KEY_COMPLEX_REQ, JSONObject.toJSONString(req));
            String rest = cacheService.blpop(KEY_EVENT_IMPLEMENTATION_RESP + req.getNonce(), HTTP_TIMEOUT, String.class);
            if (ObjectUtil.isEmpty(rest)) {
                throw new AppException("请求访问超时");
            }
            Rest<ComplexStituationInfo> eventRest = JSON.parseObject(rest, new TypeReference<Rest<ComplexStituationInfo>>() {
            });
            return JSON.toJSONString(eventRest);
        } catch (Exception e) {
            log.error("接收数据失败", e);
            rsp.setCode(ApiRespCodeEnum.FAILED.getValue());
            rsp.setMsg(e.getMessage());
            return JSON.toJSONString(rsp);
        }
    }

    public static void main(String[] args) {

        File file = new File("E:\\pic\\1.png");

        String encode = Base64.encode(file);
        System.out.println(encode);

    }


}
