package com.iformall.service.invest.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.iformall.domain.po.invest.InvestRemindEntity;
import com.iformall.domain.vo.invest.InvestUserContext;
import com.iformall.enums.EnumInvestRemindStatus;
import com.iformall.enums.EnumNegotiationType;
import com.iformall.mapper.InvestRemindMapper;
import com.iformall.service.invest.InvestRemindService;
import com.iformall.utils.DateUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@Service
public class InvestRemindServiceImpl extends InvestBaseServiceImpl<InvestRemindMapper, InvestRemindEntity> implements InvestRemindService {

    @Override
    public boolean save(InvestRemindEntity entity) {
        return super.saveAction(entity, this::checkBeforeSaveOrUpdate);
    }

    @Override
    public boolean update(InvestRemindEntity entity, Wrapper<InvestRemindEntity> updateWrapper) {
        return super.updateActon(entity, updateWrapper, this::checkBeforeSaveOrUpdate);
    }

    @Override
    public boolean saveOrUpdate(InvestRemindEntity entity) {
        return super.saveOrUpdateAction(entity, this::checkBeforeSaveOrUpdate);
    }

    @Override
    public boolean updateById(InvestRemindEntity entity) {
        return super.updateByIdAction(entity, this::checkBeforeSaveOrUpdate);
    }

    @Override
    public boolean removeAll() {
        LambdaUpdateWrapper<InvestRemindEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(InvestRemindEntity::getTenantId, InvestUserContext.getUser().getTenantId());
        wrapper.eq(InvestRemindEntity::getOwner, InvestUserContext.getUserId());
        return super.remove(wrapper);
    }

    @Override
    public List<InvestRemindEntity> findUnRemindList() {
        LambdaUpdateWrapper<InvestRemindEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(InvestRemindEntity::getStatus, EnumInvestRemindStatus.UN_REMINDED);
        wrapper.gt(InvestRemindEntity::getBeginDate, DateUtils.formatDateTime(new Date()));
        return this.list(wrapper);
    }

    private void checkBeforeSaveOrUpdate(InvestRemindEntity input) {
        input.setOwner(InvestUserContext.getUserId());
        if (Objects.nonNull(input.getContent())) {
            input.setContent(stringToJson(input.getContent()));
        }
        Map contentMap = JSON.parseObject(input.getContent(), Map.class);
        if (MapUtils.isNotEmpty(contentMap)) {
            Object customerId = contentMap.get(InvestRemindEntity.KEY_CUSTOMER);
            input.setCustomerId(Objects.isNull(customerId) ? null : Long.parseLong((String) customerId));
            Object negotiationType = contentMap.get(InvestRemindEntity.KEY_NEGOTIATIONTYPE);
            input.setNegotiationType(EnumNegotiationType.getEnum(Integer.valueOf((String) negotiationType)));
        }
    }
}
