package com.gree.mall.miniapp.logic.acitivy;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.miniapp.bean.activity.PromotionActivityBean;
import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
import com.gree.mall.miniapp.commonmapper.CommonMapper;
import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.logic.common.CommonLogic;
import com.gree.mall.miniapp.plus.entity.PromotionActivity;
import com.gree.mall.miniapp.plus.entity.PromotionActivityItem;
import com.gree.mall.miniapp.plus.entity.PromotionQuestionnaire;
import com.gree.mall.miniapp.plus.service.PromotionActivityItemService;
import com.gree.mall.miniapp.plus.service.PromotionActivityService;
import com.gree.mall.miniapp.plus.service.PromotionQuestionnaireService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class PromotionActivityLogic {
    private final CommonLogic commonLogic;
    private final CommonMapper commonMapper;
    private final PromotionActivityService promotionActivityService;
    private final PromotionActivityItemService promotionActivityItemService;
    private final PromotionQuestionnaireService promotionQuestionnaireService;

    public List<PromotionActivityBean> list(Integer pageNum, Integer pageSize, String promotionQuestionnaireId) {
        final CurrentCompanyWechat wechat = commonLogic.getCurrentCompanyWechat();
//        return promotionActivityService.lambdaQuery()
//                .eq(StringUtils.isNotBlank(promotionQuestionnaireId), PromotionActivity::getPromotionQuestionnaireId, promotionQuestionnaireId)
//                .eq(PromotionActivity::getUserId, wechat.getUser().getUserId())
//                .eq(PromotionActivity::getCompanyWechatId, wechat.getUser().getCompanyWechatId())
//                .list();
        List<PromotionActivityBean> list = commonMapper.promotionActivity(new Page<>(pageNum, pageSize), wechat.getUser().getCompanyWechatId(), wechat.getUser().getUserId(), promotionQuestionnaireId);
        return list;
    }

    public PromotionActivityBean detail(String promotionActivityId) {
        final PromotionActivity activity = promotionActivityService.getById(promotionActivityId);
        PromotionActivityBean bean = new PromotionActivityBean();
        BeanUtils.copyProperties(activity, bean);

        final List<PromotionActivityItem> items = promotionActivityItemService.lambdaQuery()
                .eq(PromotionActivityItem::getPromotionActivityId, promotionActivityId)
                .list();
        bean.setItems(items);
        return bean;
    }

    public void add(HttpServletRequest request, PromotionActivityBean bean) {
        final CurrentCompanyWechat wechat = commonLogic.getCurrentCompanyWechat();

        if (StringUtils.isBlank(bean.getPromotionQuestionnaireId())) {
            throw new RemoteServiceException("设置活动id不能为空");
        }

        if (StringUtils.isBlank(bean.getUserName())) {
            throw new RemoteServiceException("姓名不能为空");
        }

        if (StringUtils.isBlank(bean.getUserMobile())) {
            throw new RemoteServiceException("电话不能为空");
        }

        if (StringUtils.isBlank(bean.getUserAddress())) {
            throw new RemoteServiceException("地址不能为空");
        }

        if (CollectionUtil.isEmpty(bean.getItems())) {
            throw new RemoteServiceException("报名信息不能为空");
        }

        final PromotionQuestionnaire questionnaire = promotionQuestionnaireService.getById(bean.getPromotionQuestionnaireId());

        if (DateUtil.date().getTime() < questionnaire.getStartTime().getTime()) {
            throw new RemoteServiceException("活动未生效");
        }

        if (questionnaire.getIsStop() || DateUtil.date().getTime() > questionnaire.getEndTime().getTime()) {
            throw new RemoteServiceException("活动已失效");
        }

        if (questionnaire.getSubmitLimit() > -1) {
            final Integer count = promotionActivityService.lambdaQuery()
                    .eq(PromotionActivity::getPromotionQuestionnaireId, bean.getPromotionQuestionnaireId())
                    .eq(PromotionActivity::getUserId, wechat.getUser().getUserId())
                    .count();
            if (count >= questionnaire.getSubmitLimit()) {
                throw new RemoteServiceException("活动提交次数限制, 已提交" + count + "次");
            }
        }

        final String id = commonLogic.generateNo("", "PromotionActivity", 13);
        bean.setCompanyWechatId(wechat.getUser().getCompanyWechatId())
                .setCompanyWechatName(wechat.getUser().getCompanyName())
                .setPromotionActivityId(id)
                .setUserId(wechat.getUser().getUserId())
                .setLinkName(bean.getUserName());

        bean.insert();

        for (PromotionActivityItem item : bean.getItems()) {
            if (StringUtils.isBlank(item.getId())) {
                throw new RemoteServiceException("活动答案源明细id不能为空");
            }
            item.setPromotionActivityItemId(null)
                    .setPromotionActivityId(bean.getPromotionActivityId())
                    .setPromotionQuestionnaireId(bean.getPromotionQuestionnaireId());
        }

        promotionActivityItemService.saveBatch(bean.getItems());
    }
}
