/**
   *
   *
   * <pre>
   * 编辑(新增和修改)活动接口
   * 测试数据:
   * 1.1 public数据:状态是进行中和未开始:public  和 publicforver
   *     添加:openapi/s/partner/act/save?createOper=1rzwgd0p&actType=PUBLIC_FOREVER&actTitle=ceshi2
   *     修改:openapi/s/partner/act/save?actId=3177&createOper=1rzwgd0p&actType=PUBLIC_FOREVER&actTitle=ceshi27
   * 1.2 private 需要开始时间
   *     修改 openapi/s/partner/act/save?actId=3180&createOper=1rzwgd0p&actType=PRIVATE&actTitle=ceshi34&actStartTime=2784441632
   * </pre>
   */
  @RequestMapping("/partner/act/save")
  @ResponseBody
  public ResponseObject<Activity> save(
      @ModelAttribute XQActivityVO pai, @RequestHeader("Domain") String domain) {
    // 数据有效性验证
    actCreateChk(pai, domain);
    // 初始化activity
    Activity initAct = transFormActInfo(pai);

    // 执行添加/修改
    if (StringUtils.isBlank(pai.getActId())) {
      return new ResponseObject<Activity>(activityService.saveActivity(initAct));
    }

    return new ResponseObject<Activity>(
        activityService.update(initAct, initAct.getCreatorId(), pai.getShopId()));
  }
  /**
   * 初始化活动
   *
   * @param pai
   * @return
   */
  private Activity transFormActInfo(XQActivityVO pai) {
    Activity activity = new Activity();
    activity.setId(pai.getActId());
    activity.setName(pai.getActTitle());
    activity.setDetails(pai.getActDesc());
    activity.setApplyDesc(pai.getApplyDesc());
    activity.setCreatedAt(new Date());
    activity.setType(pai.getActType());
    activity.setImg(xq_default_img);
    activity.setBanner(xq_default_banner);
    activity.setUrl(pai.getActUrl());
    activity.setTagImage(pai.getActTagImage());
    activity.setActTagType(pai.getActTagType());
    activity.setCreatorId(pai.getCreateOper());

    // 时间初始化
    if (pai.getActStartTime() != null) {
      activity.setStartTime(new Date(pai.getActStartTime()));
    }

    if (pai.getActEndTime() != null) {
      activity.setEndTime(new Date(pai.getActEndTime()));
    }

    if (pai.getApplyStartTime() != null) {
      activity.setApplyStartTime(new Date(pai.getApplyStartTime()));
    }

    if (pai.getApplyEndTime() != null) {
      activity.setApplyEndTime(new Date(pai.getApplyEndTime()));
    }

    // 活动所属
    if (ActivityChannel.XIANGQU.equals(pai.getActChannel())) {
      activity.setChannel(ActivityChannel.XIANGQU);
    }

    // 活动状态
    if (ActivityType.PUBLIC_FOREVER.equals(pai.getActType())) {
      activity.setStatus(ActivityStatus.IN_PROGRESS);
    } else {
      activity.setStatus(ActivityStatus.NOT_STARTED);
    }

    return activity;
  }
  /**
   * 检查活动是否合法 创建的时候:创建人,活动类型,标题
   *
   * @param pai
   * @param domain
   */
  private void actCreateChk(XQActivityVO pai, String domain) {
    if (pai == null) {
      throw new BizException(GlobalErrorCode.INVALID_ARGUMENT, "活动不能为空");
    }
    domain = StringUtils.defaultIfBlank(domain, "xiangqu");

    // 创建人
    if (StringUtils.isBlank(pai.getCreateOper())) {
      throw new BizException(GlobalErrorCode.INVALID_ARGUMENT, "创建人不能为空");
    } else {
      User user = null;
      if (!StringUtils.isBlank(pai.getExtUid())) {
        user = userService.loadExtUser(domain, pai.getExtUid());
      } else if (!StringUtils.isBlank(pai.getCreateOper())) {
        user = userService.loadExtUser(domain, pai.getCreateOper());
      } else {
        user = userService.loadByAdmin(pai.getCreateOper());
      }

      if (user == null || !domain.equals(user.getPartner())) {
        log.debug("用户不存在");
        throw new BizException(GlobalErrorCode.INVALID_ARGUMENT, "用户不存在");
      }

      if (StringUtils.isBlank(pai.getShopId())) {
        pai.setShopId(user.getShopId());
      }
    }

    if (StringUtils.isBlank(pai.getActId())) {
      // 活动名称
      if (StringUtils.isBlank(pai.getActTitle())) {
        throw new BizException(GlobalErrorCode.THIRDPLANT_BUZERROR, "活动名称不能为空");
      }

      // 活动永久的,不需要时间信息
      if (ActivityType.PUBLIC_FOREVER.equals(pai.getActType())) {
        pai.setActState(ActivityStatus.IN_PROGRESS);
        return;
      }

      pai.setActState(ActivityStatus.NOT_STARTED);
      if (pai.getActStartTime() == null || pai.getActEndTime() == null) {
        throw new BizException(GlobalErrorCode.THIRDPLANT_BUZERROR, "活动开始或结束时间不能为空");
      }

      // 活动报名时间
      if (pai.getApplyEndTime() != null
          && pai.getApplyStartTime() != null
          && pai.getApplyEndTime() > pai.getActStartTime()) {
        throw new BizException(GlobalErrorCode.THIRDPLANT_BUZERROR, "活动报名 截止时间不能大于开始时间");
      }
    }
  }