package com.huiju.estateagents.redpack.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.common.StringUtils;
import com.huiju.estateagents.redpack.entity.TaRedPacket;
import com.huiju.estateagents.redpack.entity.TaRedPacketHelp;
import com.huiju.estateagents.redpack.mapper.TaRedPacketHelpMapper;
import com.huiju.estateagents.redpack.service.ITaRedPacketHelpService;
import com.huiju.estateagents.redpack.service.ITaRedPacketService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * <p>
 * 红包助力设置  服务实现类
 * </p>
 *
 * @author fxf
 * @since 2020-06-03
 */
@Service
public class TaRedPacketHelpServiceImpl extends ServiceImpl<TaRedPacketHelpMapper, TaRedPacketHelp> implements ITaRedPacketHelpService {

    private Logger logger = LoggerFactory.getLogger(TaRedPacketServiceImpl.class);

    @Autowired
    private TaRedPacketHelpMapper taRedPacketHelpMapper;

    @Autowired
    private ITaRedPacketService iTaRedPacketService;

    @Override
    public ResponseBean updateRedPackHelp(TaRedPacketHelp taRedPacketHelp) {
        ResponseBean responseBean = new ResponseBean();

        // 查询红包(活动)开始时间
        TaRedPacket taRedPacket = iTaRedPacketService.getById(taRedPacketHelp.getActivityId());
        if (taRedPacket == null) {
            responseBean.addError("fail");
            return responseBean;
        }

        // 校验活动是否开始
        if (taRedPacket.getStartDate().toInstant(ZoneOffset.of("+8")).toEpochMilli() > LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli()
                && (taRedPacketHelp.getHelpSuccesPersons() != null || taRedPacketHelp.getLimitNumPerDay() != null
                || taRedPacketHelp.getLimitNumPerPerson() != null || taRedPacketHelp.getLimitNumForEachPerson() != null)) {
            responseBean.addError("活动已开始，不可修改");
            return responseBean;
        }

        // 更行助力配置表
        Boolean updateResult = updateById(taRedPacketHelp);
        if (!updateResult) {
            responseBean.addError("fail");
        }

        // 更新红包(活动)基础信息
        if (taRedPacket.getStartDate() != null || taRedPacket.getEndDate() != null || !StringUtils.isEmpty(taRedPacket.getActivityEndTip())) {
            TaRedPacket record = new TaRedPacket();
            record.setActivityId(taRedPacket.getActivityId());
            record.setStartDate(taRedPacketHelp.getStartDate());
            record.setEndDate(taRedPacketHelp.getEndDate());
            record.setActivityEndTip(taRedPacketHelp.getActivityEndTip());
            iTaRedPacketService.updateById(record);
        }

        return responseBean;
    }

    @Override
    public ResponseBean getRedPacketHelpById(String activityId) {
        ResponseBean responseBean = new ResponseBean();
        TaRedPacketHelp taRedPacketHelp = getById(activityId);

        // 获取红包基础信息
        TaRedPacket taRedPacket = iTaRedPacketService.getById(activityId);
        if (taRedPacket != null) {
            taRedPacketHelp.setStartDate(taRedPacket.getStartDate());
            taRedPacketHelp.setEndDate(taRedPacket.getEndDate());
            taRedPacketHelp.setActivityEndTip(taRedPacket.getActivityEndTip());
        }
        responseBean.addSuccess(taRedPacket);
        return responseBean;
    }
}
