package com.liquidnet.service.slime.service.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.liquidnet.common.exception.LiquidnetServiceException;
import com.liquidnet.commons.lang.util.*;
import com.liquidnet.service.base.ErrorMapping;
import com.liquidnet.service.base.SqlMapping;
import com.liquidnet.service.base.constant.MQConst;
import com.liquidnet.service.slime.dto.param.SlimeFieldApplyParam;
import com.liquidnet.service.slime.dto.vo.SlimeFieldAppliesVo;
import com.liquidnet.service.slime.dto.vo.SlimeFieldsVo;
import com.liquidnet.service.slime.service.ISlimeFieldAppliesService;
import com.liquidnet.service.slime.service.SlimeMongoService;
import com.liquidnet.service.slime.service.SlimeRdmService;
import com.liquidnet.service.slime.util.QueueUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.IntStream;

@Slf4j
@Service
public class SlimeFieldAppliesServiceImpl implements ISlimeFieldAppliesService {

    @Autowired
    SlimeRdmService slimeRdmService;

    @Autowired
    SlimeMongoService slimeMongoService;

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    QueueUtil queueUtil;

    @Override
    public String apply(String uid, SlimeFieldApplyParam parameter) {
        LinkedList<String> toMqSqls = CollectionUtil.linkedListString();
        LinkedList<Object[]> fieldUpdateObjs = CollectionUtil.linkedListObjectArr();
        LinkedList<Object[]> fieldApplyInsertObjs = CollectionUtil.linkedListObjectArr();

        // 若认领，查询验证场地
        SlimeFieldsVo fieldsVo = null;
        if (null != parameter.getFieldId() && !parameter.getFieldId().isEmpty()) {
            fieldsVo = slimeRdmService.getFieldsVoByFieldId(parameter.getFieldId());
            if (null == fieldsVo) {
                ErrorMapping.ErrorMessage errorMessage = ErrorMapping.get("13101");
                throw new LiquidnetServiceException(errorMessage.getCode(), errorMessage.getMessage());
            }
            if (0 != fieldsVo.getClaimStatus()) {
                ErrorMapping.ErrorMessage errorMessage = ErrorMapping.get("13102");
                throw new LiquidnetServiceException(errorMessage.getCode(), errorMessage.getMessage());
            }
        }

        List<SlimeFieldAppliesVo> fieldAppliesVos = slimeRdmService.getFieldAppliesVosByUid(uid);

        LocalDateTime now = LocalDateTime.now();
        long s = System.currentTimeMillis();
        if (null != fieldsVo) {
            // 场地 vo 更改场地状态 及 认领的人
            fieldsVo.setClaimStatus(2);
            fieldsVo.setUid(uid);
            fieldsVo.setUpdatedAt(now);

            // 场地 redis
            slimeRdmService.setFieldsVoByFieldId(fieldsVo.getFieldId(), fieldsVo);
            log.debug("#RDS耗时:{}ms", System.currentTimeMillis() - s);

            // 场地 mongo
            s = System.currentTimeMillis();
            Query query = Query.query(Criteria.where("fieldId").is(fieldsVo.getFieldId()));
            Update update = Update.update("claimStatus", fieldsVo.getClaimStatus()).set("uid", fieldsVo.getUid()).set("updatedAt", fieldsVo.getUpdatedAt());
            mongoTemplate.updateFirst(query, update, SlimeFieldsVo.class, SlimeFieldsVo.class.getSimpleName());
            log.debug("#MONGO耗时:{}ms", System.currentTimeMillis() - s);
        }

        // 申请场地 vo
        SlimeFieldAppliesVo fieldAppliesVo = SlimeFieldAppliesVo.getNew();
        BeanUtils.copyProperties(parameter, fieldAppliesVo);
        fieldAppliesVo.setFieldApplyId(IDGenerator.nextSnowId());
        fieldAppliesVo.setApplyStatus(0);
        fieldAppliesVo.setReject("");
        fieldAppliesVo.setUid(uid);
        fieldAppliesVo.setCreatedAt(now);
        if (null != fieldsVo) {
            fieldAppliesVo.setApplyType("claim");
            fieldAppliesVo.setFieldId(fieldsVo.getFieldId());
        } else {
            fieldAppliesVo.setApplyType("create");
            fieldAppliesVo.setFieldId("");
        }
        LocalDate builtDate = LocalDate.parse(parameter.getBuiltDate(), DateTimeFormatter.ofPattern(DateUtil.DATE_SMALL_STR));
        fieldAppliesVo.setBuiltDate(builtDate);

        // 身份证检查
        String respStr = IdentityUtils.aliThird(fieldAppliesVo.getLegalName(), fieldAppliesVo.getLegalIdentity());;
        JsonNode respJNode = JsonUtils.fromJson(respStr, JsonNode.class);
        if (null == respJNode || !"0".equals(respJNode.get("error_code").asText())) {
            log.info("###实名认证失败[{}]", respStr);
            ErrorMapping.ErrorMessage errorMessage = ErrorMapping.get("13001");
            throw new LiquidnetServiceException(errorMessage.getCode(), errorMessage.getMessage());
        }

        // 申请场地 redis
        s = System.currentTimeMillis();
        slimeRdmService.addFieldAppliesVoByUid(uid, fieldAppliesVos, fieldAppliesVo);
        log.debug("#RDS耗时:{}ms", System.currentTimeMillis() - s);

        // 申请场地 mongo
        s = System.currentTimeMillis();
        slimeMongoService.addFieldAppliesVo(fieldAppliesVo);
        log.debug("#MONGO耗时:{}ms", System.currentTimeMillis() - s);

        if (null != fieldsVo) {
            // 场地 sql
            toMqSqls.add(SqlMapping.get("slime_fields.update_claim_status_and_uid"));
            fieldUpdateObjs.add(new Object[]{
                    fieldsVo.getClaimStatus(), fieldsVo.getUid(), fieldsVo.getUpdatedAt(), fieldsVo.getFieldId()
            });
        }
        // 申请场地 sql
        toMqSqls.add(SqlMapping.get("slime_field_applies.insert"));
        fieldApplyInsertObjs.add(new Object[]{
                fieldAppliesVo.getFieldApplyId(), fieldAppliesVo.getApplyStatus(), fieldAppliesVo.getApplyType(), fieldAppliesVo.getReject(), fieldAppliesVo.getUid(), fieldAppliesVo.getFieldId(),
                fieldAppliesVo.getName(), fieldAppliesVo.getLogo(), fieldAppliesVo.getBackground(), fieldAppliesVo.getDescription(), fieldAppliesVo.getBuiltDate(),
                fieldAppliesVo.getProvinceId(), fieldAppliesVo.getProvinceName(), fieldAppliesVo.getCityId(), fieldAppliesVo.getCityName(), fieldAppliesVo.getDistrictId(), fieldAppliesVo.getDistrictName(), fieldAppliesVo.getAddress(), fieldAppliesVo.getLongitude(), fieldAppliesVo.getLatitude(),
                fieldAppliesVo.getContactName(), fieldAppliesVo.getContactEmail(),
                fieldAppliesVo.getCompanyName(), fieldAppliesVo.getLicenseCode(), fieldAppliesVo.getLicenseImg(),
                fieldAppliesVo.getLegalName(), fieldAppliesVo.getLegalIdentity(), fieldAppliesVo.getLegalIdentityObverse(), fieldAppliesVo.getLegalIdentityReverse(), fieldAppliesVo.getCreatedAt()
        });

        // mq
        s = System.currentTimeMillis();
        if (null != fieldsVo) {
            queueUtil.sendMsgByRedis(
                    MQConst.SlimeQueue.SQL_SLIME_FIELD.getKey(),
                    SqlMapping.gets(toMqSqls, fieldUpdateObjs, fieldApplyInsertObjs)
            );
        } else {
            queueUtil.sendMsgByRedis(
                    MQConst.SlimeQueue.SQL_SLIME_FIELD.getKey(),
                    SqlMapping.gets(toMqSqls, fieldApplyInsertObjs)
            );
        }
        log.debug("#MQ耗时:{}ms", System.currentTimeMillis() - s);

        return fieldAppliesVo.getFieldApplyId();
    }

    @Override
    public void applyDel(String uid, String fieldApplyId) {
        // 查找场地申请，是否存在且已经驳回
        List<SlimeFieldAppliesVo> fieldAppliesVos = slimeRdmService.getFieldAppliesVosByUid(uid);
        if (CollectionUtils.isEmpty(fieldAppliesVos)) {
            ErrorMapping.ErrorMessage errorMessage = ErrorMapping.get("13112");
            throw new LiquidnetServiceException(errorMessage.getCode(), errorMessage.getMessage());
        }
        int idx = IntStream.range(0, fieldAppliesVos.size())
                .filter(i -> fieldAppliesVos.get(i).getFieldApplyId().equals(fieldApplyId))
                .findFirst()
                .orElse(-1);
        if (idx < 0) {
            ErrorMapping.ErrorMessage errorMessage = ErrorMapping.get("13112");
            throw new LiquidnetServiceException(errorMessage.getCode(), errorMessage.getMessage());
        }
        SlimeFieldAppliesVo fieldAppliesVo = fieldAppliesVos.get(idx);
        if (fieldAppliesVo.getApplyStatus() != 2) {
            ErrorMapping.ErrorMessage errorMessage = ErrorMapping.get("13113");
            throw new LiquidnetServiceException(errorMessage.getCode(), errorMessage.getMessage());
        }

        LocalDateTime now = LocalDateTime.now();
        fieldAppliesVo.setUpdatedAt(now);
        fieldAppliesVo.setDeletedAt(now);

        // 申请场地 redis
        long s = System.currentTimeMillis();
        slimeRdmService.delFieldAppliesVoByUid(uid, fieldAppliesVos, fieldAppliesVo);
        log.debug("#RDS耗时:{}ms", System.currentTimeMillis() - s);

        // 申请场地 mongo
        s = System.currentTimeMillis();
        slimeMongoService.delFieldAppliesVoByFieldApplyId(fieldAppliesVo.getFieldApplyId());
        log.debug("#MONGO耗时:{}ms", System.currentTimeMillis() - s);

        // 申请场地 sql
        LinkedList<String> toMqSqls = CollectionUtil.linkedListString();
        LinkedList<Object[]> fieldAppliesUpdateObjs = CollectionUtil.linkedListObjectArr();
        toMqSqls.add(SqlMapping.get("slime_field_applies.update"));
        fieldAppliesUpdateObjs.add(new Object[]{
                fieldAppliesVo.getUpdatedAt(), fieldAppliesVo.getDeletedAt(), fieldAppliesVo.getFieldApplyId()
        });

        // mq
        s = System.currentTimeMillis();
        queueUtil.sendMsgByRedis(
                MQConst.SlimeQueue.SQL_SLIME_FIELD.getKey(),
                SqlMapping.gets(toMqSqls, fieldAppliesUpdateObjs)
        );
        log.debug("#MQ耗时:{}ms", System.currentTimeMillis() - s);
    }

    @Override
    public SlimeFieldAppliesVo applyInfo(String uid, String fieldApplyId) {
        // 查找场地申请，是否存在
        List<SlimeFieldAppliesVo> fieldAppliesVos = slimeRdmService.getFieldAppliesVosByUid(uid);
        if (CollectionUtils.isEmpty(fieldAppliesVos)) {
            ErrorMapping.ErrorMessage errorMessage = ErrorMapping.get("13112");
            throw new LiquidnetServiceException(errorMessage.getCode(), errorMessage.getMessage());
        }
        int idx = IntStream.range(0, fieldAppliesVos.size())
                .filter(i -> fieldAppliesVos.get(i).getFieldApplyId().equals(fieldApplyId))
                .findFirst()
                .orElse(-1);
        if (idx < 0) {
            ErrorMapping.ErrorMessage errorMessage = ErrorMapping.get("13112");
            throw new LiquidnetServiceException(errorMessage.getCode(), errorMessage.getMessage());
        }

        SlimeFieldAppliesVo fieldAppliesVo = fieldAppliesVos.get(idx);
        if (null != fieldAppliesVo.getFieldId() && !fieldAppliesVo.getFieldId().isEmpty()) {
            SlimeFieldsVo fieldsVo = slimeRdmService.getFieldsVoByFieldId(fieldAppliesVo.getFieldId());
            fieldAppliesVo.setFieldVo(fieldsVo);
        }

        return fieldAppliesVos.get(idx);
    }

}
