package com.mortals.xhx.module.error.web;

import cn.hutool.core.util.IdUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.ErrorLogPdu;
import com.mortals.framework.model.OrderCol;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.module.error.model.ErrorLogQuery;
import com.mortals.xhx.system.MessageProducer;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.mortals.framework.model.Context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.module.error.model.ErrorLogEntity;
import com.mortals.xhx.module.error.service.ErrorLogService;
import org.apache.commons.lang3.ArrayUtils;
import com.mortals.framework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import static com.mortals.framework.ap.SysConstains.*;

/**
 * 异常日志
 *
 * @author zxfei
 * @date 2022-08-24
 */
@RestController
@RequestMapping("error/log")
public class ErrorLogController extends BaseCRUDJsonBodyMappingController<ErrorLogService, ErrorLogEntity, Long> {

    @Autowired
    private ParamService paramService;

    @Autowired
    private MessageProducer messageProducer;


    public ErrorLogController() {
        super.setModuleDesc("异常日志");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "platform", paramService.getParamBySecondOrganize("ErrorLog", "platform"));
        this.addDict(model, "level", paramService.getParamBySecondOrganize("ErrorLog", "level"));

        Map<String, String> map = paramService.getParamBySecondOrganize("BizLog", "appName");

        Map<String, String> map1 = paramService.getParamBySecondOrganize("OperateLog", "platformMark");
        map.putAll(map1);
        this.addDict(model, "appName", map);
        super.init(model, context);
    }

    @Override
    protected void doListBefore(ErrorLogEntity query, Map<String, Object> model, Context context) throws AppException {
        List<OrderCol> orderColList = new ArrayList<>();
        orderColList.add(new OrderCol("id", OrderCol.DESCENDING));
        query.setOrderColList(orderColList);
        super.doListBefore(query, model, context);
    }

    @Override
    @UnAuth
    public String save(@RequestBody ErrorLogEntity entity) {

/*        ErrorLogPdu errorLogPdu = new ErrorLogPdu();
        errorLogPdu.initAttrValue();
        BeanUtils.copyProperties(entity, errorLogPdu, BeanUtil.getNullPropertyNames(entity));
        messageProducer.syncErrorSend(errorLogPdu);*/

        ErrorLogQuery errorLogQuery = new ErrorLogQuery();
        errorLogQuery.setAppName(entity.getAppName());
        errorLogQuery.setPlatform(entity.getPlatform());
        errorLogQuery.setErrorStack(entity.getErrorStack());
        ErrorLogEntity errorLogEntity = this.service.selectOne(errorLogQuery, null);
        if (!ObjectUtils.isEmpty(errorLogEntity)) {
            entity.setId(errorLogEntity.getId());
            entity.setCheckNum(errorLogEntity.getCheckNum() + 1);
            entity.setUpdateTime(new Date());
        }

        return super.save(entity);
    }


    @Override
    @UnAuth
    public String batchSave(@RequestBody List<ErrorLogEntity> list) {
/*        list.forEach(entity -> {
            ErrorLogQuery errorLogQuery = new ErrorLogQuery();
            errorLogQuery.setAppName(entity.getAppName());
            errorLogQuery.setPlatform(entity.getPlatform());
            errorLogQuery.setErrorStack(entity.getErrorStack());
            ErrorLogEntity errorLogEntity = this.service.selectOne(errorLogQuery, null);
            if (!ObjectUtils.isEmpty(errorLogEntity)) {
                entity.setId(errorLogEntity.getId());
                entity.setCheckNum(errorLogEntity.getCheckNum() + 1);
                entity.setUpdateTime(new Date());
            }
        });*/
        return super.batchSave(list);
    }
}