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.CommConstant;
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.TaOrgOrderMapper;
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;
import java.util.Map;

/**
 * <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;

    @Autowired
    private TaOrgOrderMapper taOrgOrderMapper;

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

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

        // 查询h5 help数据
        TaRedPacketHelp redPacketHelp = taRedPacketHelpMapper.selectById(taRedPacketHelp.getActivityId());
        if (redPacketHelp == null) {
            responseBean.addError("fail");
            return responseBean;
        }

        // 校验活动是否开始
        if (taRedPacket.getStartDate().toInstant(ZoneOffset.of("+8")).toEpochMilli() < LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli()) {

            // 活动开始，配置人数和次数限制不可修改
            if ((taRedPacketHelp.getHelpSuccesPersons() != redPacketHelp.getHelpSuccesPersons() ||
                    taRedPacketHelp.getLimitNumPerDay() != redPacketHelp.getLimitNumPerDay()
                    || taRedPacketHelp.getLimitNumPerPerson() != redPacketHelp.getLimitNumPerPerson() ||
                    taRedPacketHelp.getLimitNumForEachPerson() != redPacketHelp.getLimitNumForEachPerson())) {
                responseBean.addError("活动已开始，不可修改");
                return responseBean;
            }

            // 活动开始，开始时间不可修改
            if (taRedPacketHelp.getStartDate().toInstant(ZoneOffset.of("+8")).toEpochMilli() > taRedPacket.getStartDate().toInstant(ZoneOffset.of("+8")).toEpochMilli() ) {
                responseBean.addError("活动已开始，开始时间不可修改");
                return responseBean;
            }
        }

        // 校验结束时间是否已经到达
        if (taRedPacketHelp.getEndDate() != null && taRedPacket.getEndDate().toInstant(ZoneOffset.of("+8")).toEpochMilli() < LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli()) {
            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);
        }
        responseBean.addSuccess(taRedPacket);
        return responseBean;
    }

    @Override
    public ResponseBean getRedPacketHelpById(String activityId, Integer orgId) {
        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(taRedPacketHelp);
        return responseBean;
    }
}
