package com.kiisoo.ic.activity.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kiisoo.ic.activity.entity.PmnActivityPromotion;
import com.kiisoo.ic.activity.entity.PmnActivityPromotionParam;
import com.kiisoo.ic.activity.service.IPmnActivityPromotionParamService;
import com.kiisoo.ic.activity.service.IPmnActivityPromotionService;
import com.kiisoo.ic.activity.bean.ActivityScheduleVO;
import com.kiisoo.ic.activity.entity.PmnActivitySchedule;
import com.kiisoo.ic.activity.mapper.PmnActivityScheduleDOMapper;
import com.kiisoo.ic.activity.service.IPmnActivityScheduleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 活动规划表 服务实现类
 * </p>
 *
 * @author zheng
 * @since 2020-05-19
 */
@Service
public class PmnActivityScheduleServiceImpl extends ServiceImpl<PmnActivityScheduleDOMapper, PmnActivitySchedule> implements IPmnActivityScheduleService {

    private IPmnActivityPromotionService activityPromotionService;

    private IPmnActivityPromotionParamService pmnActivityPromotionParamService;

    @Autowired
    PmnActivityScheduleServiceImpl(IPmnActivityPromotionService activityPromotionService, IPmnActivityPromotionParamService pmnActivityPromotionParamService) {
        this.activityPromotionService = activityPromotionService;
        this.pmnActivityPromotionParamService = pmnActivityPromotionParamService;
    }

    @Transactional
    public boolean saveVO(ActivityScheduleVO activityScheduleVO) {
        // TODO 创建活动池的时候，自动关联一个 activity_promotion 一对一关系
        // TODO 活动类型参数，默认值必须填，在活动实例中作为参数多选
        // TODO 一对多的存储，都是有ID时修改，无ID时新增
        // TODO APPID，URI，活动类型，都存储到 activity_promotion_params 表里面，类型不一样
        if (activityScheduleVO.getParams().size() <= 0) return false;
        //新建活动池
        PmnActivitySchedule activitySchedule = new PmnActivitySchedule();
        activitySchedule.setCategoryId(0);
        activitySchedule.setCreateTime(new Date());
        activitySchedule.setDescription(activityScheduleVO.getDescription());
        activitySchedule.setId(activityScheduleVO.getId());
        activitySchedule.setName(activityScheduleVO.getName());
        activitySchedule.setLogo(activityScheduleVO.getLogo());
        activitySchedule.setCategoryId(activityScheduleVO.getCategoryId());
        activitySchedule.setCategoryCode(activityScheduleVO.getCategoryCode());
        activitySchedule.setStatus(1);
        activitySchedule.setUpdateTime(new Date());
        boolean saveActivitySchedule = saveOrUpdate(activitySchedule);
        //保存activity_promotion
        PmnActivityPromotion pmnActivityPromotion = PmnActivityPromotion
                .builder()
                .content(activityScheduleVO.getDescription())
                .createTime(new Date())
                .name(activityScheduleVO.getName())
                .status(1)
                .scheduleId(activitySchedule.getId())
                .uri(activityScheduleVO.getUri())
                .updateTime(new Date())
                .type(1)
                .build();
        PmnActivityPromotion oldPmnActivityPromotion = activityPromotionService.getOne(Wrappers.<PmnActivityPromotion>lambdaQuery().eq(PmnActivityPromotion::getScheduleId, activitySchedule.getId()));
        if (oldPmnActivityPromotion != null) {
            pmnActivityPromotion.setId(oldPmnActivityPromotion.getId());
        }
        boolean saveActivityPromotion = activityPromotionService.saveOrUpdate(pmnActivityPromotion);
        boolean saveAppId = true;
        boolean saveUri = true;
        if (saveActivitySchedule && saveActivityPromotion) {
            //保存appid和uri, attr = 1
            PmnActivityPromotionParam appId = new PmnActivityPromotionParam();
            appId.setName("appId");
            appId.setKey(activityScheduleVO.getAppId());
            appId.setAttr(1);
            appId.setDescription(activityScheduleVO.getDescription());
            appId.setIsRequired(1);
            appId.setPromotionId(pmnActivityPromotion.getId());
            appId.setType(2);
            appId.setDefaultVal(activityScheduleVO.getAppId());
            PmnActivityPromotionParam uri = new PmnActivityPromotionParam();
            uri.setName("uri");
            uri.setKey(activityScheduleVO.getUri());
            uri.setAttr(1);
            uri.setDescription(activityScheduleVO.getDescription());
            uri.setIsRequired(1);
            uri.setPromotionId(pmnActivityPromotion.getId());
            uri.setType(2);
            uri.setDefaultVal(activityScheduleVO.getUri());
            saveAppId = pmnActivityPromotionParamService.saveOrUpdate(appId, Wrappers.<PmnActivityPromotionParam>lambdaQuery().eq(PmnActivityPromotionParam::getPromotionId, pmnActivityPromotion.getId()).eq(PmnActivityPromotionParam::getAttr, 1).eq(PmnActivityPromotionParam::getName, "appId"));
            saveUri = pmnActivityPromotionParamService.saveOrUpdate(uri, Wrappers.<PmnActivityPromotionParam>lambdaQuery().eq(PmnActivityPromotionParam::getPromotionId, pmnActivityPromotion.getId()).eq(PmnActivityPromotionParam::getAttr, 1).eq(PmnActivityPromotionParam::getName, "uri"));
            List<Long> paramIds = new ArrayList<>();
            for (PmnActivityPromotionParam param : activityScheduleVO.getParams()) {
                param.setPromotionId(pmnActivityPromotion.getId());
                param.setAttr(2);
                boolean saveParam = pmnActivityPromotionParamService.saveOrUpdate(param);
                if(param.getId() != null && param.getId() > 0) {
                    paramIds.add(param.getId());
                }
                if (!saveParam) throw new RuntimeException("活动类型保存失败");
            }
            if(paramIds.size() > 0) {
                pmnActivityPromotionParamService.remove(Wrappers.<PmnActivityPromotionParam>lambdaQuery().eq(PmnActivityPromotionParam::getPromotionId, pmnActivityPromotion.getId()).notIn(PmnActivityPromotionParam::getId, paramIds).eq(PmnActivityPromotionParam::getAttr, 2));
            }
        }
        return saveActivitySchedule && saveActivityPromotion && saveAppId && saveUri;
    }

    @Override
    public ActivityScheduleVO getVO(Long id) {
        PmnActivitySchedule activitySchedule = getById(id);
        ActivityScheduleVO activityScheduleVO = new ActivityScheduleVO();
        activityScheduleVO.setId(activitySchedule.getId());
        activityScheduleVO.setLogo(activitySchedule.getLogo());
        activityScheduleVO.setName(activitySchedule.getName());
        activityScheduleVO.setDescription(activitySchedule.getDescription());
        activityScheduleVO.setCategoryId(activitySchedule.getCategoryId());
        activityScheduleVO.setCategoryCode(activitySchedule.getCategoryCode());
        PmnActivityPromotion activityPromotion = activityPromotionService.getOne(Wrappers.<PmnActivityPromotion>lambdaQuery().eq(PmnActivityPromotion::getScheduleId, activitySchedule.getId()));
        if(activityPromotion != null) {
            List<PmnActivityPromotionParam> activityPromotionParams = new ArrayList<>();
            List<PmnActivityPromotionParam> activityPromotionParamsAll = pmnActivityPromotionParamService.list(Wrappers.<PmnActivityPromotionParam>lambdaQuery().eq(PmnActivityPromotionParam::getPromotionId, activityPromotion.getId()));
            for (PmnActivityPromotionParam activityPromotionParam : activityPromotionParamsAll) {
                if (activityPromotionParam.getName().equals("appId")) {
                    activityScheduleVO.setAppId(activityPromotionParam.getKey());
                } else if (activityPromotionParam.getName().equals("uri")) {
                    activityScheduleVO.setUri(activityPromotionParam.getKey());
                } else {
                    activityPromotionParams.add(activityPromotionParam);
                }
            }
            activityScheduleVO.setParams(activityPromotionParams);
        }
        return activityScheduleVO;
    }
}
