package com.iformall.service.invest.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.iformall.common.ErrorCode;
import com.iformall.domain.po.invest.InvestTaskEntity;
import com.iformall.enums.EnumFollowType;
import com.iformall.enums.EnumInvestMessageTag;
import com.iformall.enums.EnumInvestMessageType;
import com.iformall.enums.EnumTaskStatus;
import com.iformall.mapper.InvestTaskMapper;
import com.iformall.service.MallUserInfoService;
import com.iformall.service.WxShopService;
import com.iformall.service.invest.InvestHelper;
import com.iformall.service.invest.InvestTaskService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class InvestTaskServiceImpl extends InvestBaseServiceImpl<InvestTaskMapper, InvestTaskEntity> implements InvestTaskService {

    @Autowired
    private WxShopService shopService;
    @Autowired
    private MallUserInfoService userInfoService;

    @Override
    public boolean save(InvestTaskEntity entity) {
        InvestHelper.checkOwner(entity);
        super.saveAction(entity, this::checkBeforeSaveOrUpdate);
        saveMessage(entity);
        return true;
    }

    //@TableLog
    @Override
    public boolean update(InvestTaskEntity entity, Wrapper<InvestTaskEntity> updateWrapper) {
        super.updateActon(entity, updateWrapper, this::checkBeforeSaveOrUpdate);
        saveMessage(entity);
        return true;
    }

    //@TableLog
    @Override
    public boolean updateById(InvestTaskEntity entity) {
        super.updateByIdAction(entity, this::checkBeforeSaveOrUpdate);
        saveMessage(entity);
        return true;
    }

    private void saveMessage(InvestTaskEntity entity) {
        String owner = entity.getOwner();
        if (StringUtils.isNotBlank(owner)) {
            List<Long> ownerArray = JSONArray.parseArray(owner, Long.class);
            this.saveBatchMessage(ownerArray, EnumInvestMessageType.TASK_CREATE.getInfo(),
                    EnumInvestMessageTag.TASK_CREATE, EnumFollowType.TASK, entity.getId(),entity);
        }
    }

    private void checkBeforeSaveOrUpdate(InvestTaskEntity entity) {
        // 1 check shop 状态
        //checkShop(shopService, entity);
        // 2 check user
        InvestHelper.notNull(entity.getLastTime(),"lastTime");
        checkUserExit(userInfoService, entity.getOwner());
        if (Objects.nonNull(entity.getOwner())) {
            entity.setOwner(stringToJson(entity.getOwner()));
        }
        if (Objects.nonNull(entity.getContent())) {
            entity.setContent(stringToJson(entity.getContent()));
        }
    }

    @Deprecated
    @Override
    public boolean saveOrUpdate(InvestTaskEntity entity) {
        notSupport();
        return false ;
    }

    @Deprecated
    @Override
    public boolean update(Wrapper<InvestTaskEntity> updateWrapper) {
        notSupport();
        return false ;
    }

    @Override
    public List<InvestTaskEntity> listByStatusAndTime(List<EnumTaskStatus> status, Date start, Date end) {
        LambdaQueryWrapper<InvestTaskEntity> taskQueryWrapper = new LambdaQueryWrapper<>();
        taskQueryWrapper.in(InvestTaskEntity::getStatus, status);
        taskQueryWrapper.ge(InvestTaskEntity::getLastTime, start);
        taskQueryWrapper.le(InvestTaskEntity::getLastTime, end);
        taskQueryWrapper.isNotNull(InvestTaskEntity::getOwner);
        return this.list(taskQueryWrapper);
    }
}
