private void btnSave(Long[] ids, Long[] sortIds) {
    if (null == ids || null == sortIds || ids.length < 1 || sortIds.length < 1) {
      return;
    }

    for (int i = 0; i < ids.length; i++) {
      Long id = ids[i];

      TdCoupon e = tdCouponService.findOne(id);

      if (null != e) {
        if (sortIds.length > i) {
          e.setSortId(sortIds[i]);
          tdCouponService.save(e);
        }
      }
    }
  }
  @RequestMapping(value = "/distributed/list")
  public String distributedList(
      Integer page,
      Integer size,
      String __EVENTTARGET,
      String __EVENTARGUMENT,
      String __VIEWSTATE,
      Long[] listId,
      Integer[] listChkId,
      Long[] listSortId,
      ModelMap map,
      HttpServletRequest req) {

    String username = (String) req.getSession().getAttribute("manager");

    if (null == username) {
      return "redirect:/Verwalter/login";
    }

    if (null != __EVENTTARGET) {
      if (__EVENTTARGET.equalsIgnoreCase("btnPage")) {
        if (null != __EVENTARGUMENT) {
          page = Integer.parseInt(__EVENTARGUMENT);
        }
      } else if (__EVENTTARGET.equalsIgnoreCase("btnDelete")) {
        btnDelete(listId, listChkId);
        tdManagerLogService.addLog("delete", "删除优惠券", req);
      } else if (__EVENTTARGET.equalsIgnoreCase("btnSave")) {
        btnSave(listId, listSortId);
        tdManagerLogService.addLog("edit", "修改优惠券", req);
      }
    }

    if (null == page || page < 0) {
      page = 0;
    }

    if (null == size || size <= 0) {
      size = SiteMagConstant.pageSize;
      ;
    }

    map.addAttribute("page", page);
    map.addAttribute("size", size);
    map.addAttribute("__EVENTTARGET", __EVENTTARGET);
    map.addAttribute("__EVENTARGUMENT", __EVENTARGUMENT);
    map.addAttribute("__VIEWSTATE", __VIEWSTATE);

    Page<TdCoupon> couponPage = null;

    couponPage = tdCouponService.findByIsDistributtedTrueOrderBySortIdAsc(page, size);

    map.addAttribute("coupon_page", couponPage);

    return "/site_mag/coupon_distributed_list";
  }
  private void btnDelete(Long[] ids, Integer[] chkIds) {
    if (null == ids || null == chkIds || ids.length < 1 || chkIds.length < 1) {
      return;
    }

    for (int chkId : chkIds) {
      if (chkId >= 0 && ids.length > chkId) {
        Long id = ids[chkId];

        tdCouponService.delete(id);
      }
    }
  }
  @ModelAttribute
  public void getModel(
      @RequestParam(value = "couponTypeId", required = false) Long couponTypeId,
      @RequestParam(value = "couponId", required = false) Long couponId,
      Model model) {
    if (null != couponTypeId) {
      model.addAttribute("tdCouponType", tdCouponTypeService.findOne(couponTypeId));
    }

    if (null != couponId) {
      model.addAttribute("tdCoupon", tdCouponService.findOne(couponId));
    }
  }
  @RequestMapping(value = "/edit")
  public String orderEdit(Long id, String __VIEWSTATE, ModelMap map, HttpServletRequest req) {
    String username = (String) req.getSession().getAttribute("manager");
    if (null == username) {
      return "redirect:/Verwalter/login";
    }

    map.addAttribute("__VIEWSTATE", __VIEWSTATE);

    map.addAttribute("diy_site_list", tdDiySiteService.findByIsEnableTrue());

    List<TdCouponType> couponTypeList = null;

    couponTypeList = tdCouponTypeService.findAllOrderBySortIdAsc();

    map.addAttribute("coupon_type_list", couponTypeList);

    if (null != id) {
      map.addAttribute("coupon", tdCouponService.findOne(id));
      return "/site_mag/coupon_edit_hasId";
    }
    return "/site_mag/coupon_edit";
  }
  @RequestMapping(value = "/save")
  public String orderEdit(
      TdCoupon tdCoupon,
      String __VIEWSTATE,
      Long[] leftNumbers,
      Long typeId,
      ModelMap map,
      HttpServletRequest req) {
    String username = (String) req.getSession().getAttribute("manager");
    if (null == username) {
      return "redirect:/Verwalter/login";
    }

    map.addAttribute("__VIEWSTATE", __VIEWSTATE);

    if (null == tdCoupon.getId()) {
      tdManagerLogService.addLog("add", "用户修改优惠券", req);

      List<TdDiySite> tdDiySiteList = tdDiySiteService.findByIsEnableTrue();

      if (null != tdDiySiteList
          && tdDiySiteList.size() > 0
          && null != leftNumbers
          && leftNumbers.length > 0
          && null != typeId) {
        /** @author lc @注释:如果不是免费洗车券和免费打蜡券就不存在同盟店 */
        TdCouponType tdCouponType = tdCouponTypeService.findOne(typeId);

        if (tdCouponType.getTitle().equals("免费打蜡券") || tdCouponType.getTitle().equals("免费洗车券")) {
          for (int i = 0; i < tdDiySiteList.size(); i++) {
            TdDiySite tds = tdDiySiteList.get(i);

            if (null != tds && leftNumbers.length > i) {
              TdCoupon coupon =
                  tdCouponService.findTopByTypeIdAndDiySiteIdAndIsDistributtedFalse(
                      typeId, tds.getId());

              if (null == coupon) {
                coupon = new TdCoupon();
                coupon.setDiySiteId(tds.getId());
                coupon.setLeftNumber(leftNumbers[i]);
                coupon.setTypeId(typeId);
                coupon.setSortId(99L);
                coupon.setPrice(tdCouponType.getPrice());
              } else {
                coupon.setLeftNumber(coupon.getLeftNumber() + leftNumbers[i]);
              }

              tdCouponService.save(coupon);
            }
          }
        } else {
          TdCoupon coupon = tdCouponService.findByTypeId(typeId);

          if (null == coupon) {
            coupon = new TdCoupon();
            coupon.setLeftNumber(leftNumbers[0]);
            coupon.setTypeId(typeId);
            coupon.setSortId(99L);
            coupon.setPrice(tdCouponType.getPrice());
          } else {
            coupon.setLeftNumber(coupon.getLeftNumber() + leftNumbers[0]);
          }

          tdCouponService.save(coupon);
        }
      }

    } else {
      tdManagerLogService.addLog("edit", "用户修改优惠券", req);
      tdCouponService.save(tdCoupon);
    }

    return "redirect:/Verwalter/coupon/list";
  }