/**
  * 删除线路模板
  *
  * @param id
  * @return
  */
 @AuthorityPolicy(authorityTypes = {Right.DELETE_LINE_TEMPLATE})
 @RequestMapping(value = "/deletetemplate.htm")
 @ResponseBody
 public JsonResult delete(Long id) {
   boolean bool = lineService.deleteTravelLine(id);
   return JsonResultUtils.success(null, bool ? "删除成功!" : "删除失败!");
 }
 /**
  * 修改交通模板
  *
  * @param traffic
  * @return
  */
 @AuthorityPolicy(authorityTypes = {Right.MODIFY_TRAFFIC_TEMPLATE})
 @RequestMapping(value = "/updatetraffic.htm")
 @ResponseBody
 public JsonResult updateTraffic(TravelTrafficDO traffic) {
   return JsonResultUtils.success(
       null, lineService.updateTravelTraffic(traffic) ? "修改成功!" : "修改失败!");
 }
 /**
  * 添加线路模板
  *
  * @param travelLineDO
  * @param mv
  * @param result
  * @return
  */
 @AuthorityPolicy(authorityTypes = {Right.CREATE_LINE_TEMPLATE})
 @RequestMapping(value = "/addtemplate.htm")
 @ResponseBody
 public JsonResult addtemplat(TravelLineVO travelLineVO, BindingResult result) {
   Result rs = showErrors(result);
   if (rs.isFailed()) {
     return JsonResultUtils.error(rs.getMessage());
   }
   // 添加线路模板
   travelLineVO.setlTemplateState(LineTemplateEnum.Template.getValue());
   TravelLineDO travelLineDO = new TravelLineDO();
   BeanUtils.copyProperties(travelLineDO, travelLineVO);
   // 设置添加线路的用户id
   travelLineDO.setmId(WebUserTools.getMid());
   travelLineDO.setcId(WebUserTools.getCid());
   int linebo = lineService.addTravelLine(travelLineDO);
   int routebo = 0;
   // 添加行程
   if (travelLineVO.getRoutelist() != null || travelLineVO.getRoutelist().size() != 0) {
     for (TravelRouteDO route : travelLineVO.getRoutelist()) {
       route.setlId(travelLineDO.getlId());
       routebo = lineService.addTravelRoute(route);
     }
   }
   boolean bool = linebo != 0 && routebo != 0;
   return bool ? JsonResultUtils.success("添加成功!") : JsonResultUtils.error(null, "添加失败!");
 }
  /**
   * 根据条件查询公司
   *
   * @return
   */
  @RequestMapping(value = "/queryCompanyByConditions.htm", produces = "application/json")
  @ResponseBody
  public JsonResult queryCompanyByConditions(TravelCompanyQuery query, Integer limit) {
    List<TravelCompanyDO> list = companyService.listQuery(query);

    List<Map<String, ?>> mapList = CollectionUtils.toMapList(list, "cId", "cName", "cSpell");
    // StringBuilder sb = new StringBuilder();
    String cond = query.getQ() == null ? StringUtils.EMPTY : query.getQ();
    cond = cond.toLowerCase();
    // String temp;
    int maxSize = getLimit(limit);
    int size = 0;
    List<Map<String, ?>> result = new LinkedList<Map<String, ?>>();
    String property = cond.matches("[a-zA-Z]+") ? "cSpell" : "cName";
    for (Map<String, ?> map : mapList) {
      Object cName = null;
      for (Entry<String, ?> entry : map.entrySet()) {
        if (StringUtils.equals(entry.getKey(), property)) {
          cName = entry.getValue();
        }
      }
      if (cond.matches("[a-zA-Z]+")
          ? StringUtils.startsWith((String) cName, cond)
          : StringUtils.containsIgnoreCase((String) cName, cond)) {
        result.add(map);
        size++;
        if (size > maxSize) {
          break;
        }
      }
    }
    return JsonResultUtils.success(result);
  }
 /**
  * 添加交通模板页面
  *
  * @param traffic
  * @return
  */
 @AuthorityPolicy(authorityTypes = {Right.CREATE_TRAFFIC_TEMPLATE})
 @RequestMapping(value = "/addtraffic.htm", method = RequestMethod.POST)
 @ResponseBody
 public JsonResult addTraffic(TravelTrafficDO traffic) {
   traffic.setcId(WebUserTools.getCid());
   int i = lineService.insertTravelTraffic(traffic);
   return JsonResultUtils.success(null, i != 0 ? "添加成功!" : "添加失败!");
 }
 /**
  * 修改线路模板
  *
  * @param trDo
  * @return
  */
 @AuthorityPolicy(authorityTypes = {Right.MODIFY_LINE_TEMPLATE})
 @RequestMapping(value = "/updatetemplate.htm")
 @ResponseBody
 public JsonResult updateTemplate(TravelLineVO trDo) {
   // 设置最后修改人ID
   trDo.setlEditUserId(WebUserTools.getMid().intValue());
   boolean bool = lineService.updateTravelLine(trDo);
   // 删除行程
   lineService.deleteTravelRouteByLineid(trDo.getlId());
   // 添加行程
   boolean boo = true;
   if (trDo.getRoutelist() != null || trDo.getRoutelist().size() != 0) {
     for (TravelRouteDO route : trDo.getRoutelist()) {
       route.setlId(trDo.getlId());
       int roid = lineService.addTravelRoute(route);
       if (roid == 0) {
         boo = false;
       }
     }
   }
   return bool && boo ? JsonResultUtils.success(null, "修改成功!") : JsonResultUtils.error("修改失败!");
 }
  @RequestMapping(
      value = "/addGiftOrder.htm",
      produces = "application/json",
      method = RequestMethod.POST)
  @ResponseBody
  public JsonResult addGiftOrder(ModelAndView mav, final TravelGiftOrderDO giftOrderDO) {
    TravelIntegralQuery integralQuery = new TravelIntegralQuery();
    integralQuery.setcId(WebUserTools.getCid());
    integralQuery.setmId(WebUserTools.getMid());
    TravelIntegralDO integralDO = integralService.queryBala(integralQuery);
    if (integralDO == null) {
      return JsonResultUtils.error(giftOrderDO, "你暂时还没有积分,赶快去消费吧!");
    }
    mav.addObject("integralDO", integralDO);
    if (giftOrderDO.getGoIntegralCount() > integralDO.getiBalance())
      return JsonResultUtils.error(giftOrderDO, "积分不够,请改变兑换数量!");

    giftOrderDO.setcId(WebUserTools.getCid());
    giftOrderDO.setmId(WebUserTools.getMid());
    giftOrderDO.setGoState(0);
    Integer i = integralService.addTravelGiftOrder(giftOrderDO);
    if (i == 0) {
      return JsonResultUtils.error(giftOrderDO, "下单失败!");
    } else {
      TravelIntegralDO newACC = new TravelIntegralDO();
      newACC.setcId(WebUserTools.getCid());
      newACC.setmId(WebUserTools.getMid());
      newACC.setiSource(IntegralSourceEnum.consumer.value);
      newACC.setiAddintegral(0 - giftOrderDO.getGoIntegralCount());
      newACC.setiBalance(integralDO.getiBalance() + newACC.getiAddintegral());
      newACC.setiFrozen(integralDO.getiFrozen());
      newACC.setiAltogether(newACC.getiBalance() + newACC.getiFrozen());
      newACC.setiRemark("兑换积分产品");
      integralService.addTravelIntegral(newACC);
      return JsonResultUtils.success(giftOrderDO, "下单成功!");
    }
  }
 /**
  * 删除交通模板
  *
  * @param id
  * @return
  */
 @AuthorityPolicy(authorityTypes = {Right.DELETE_TRAFFIC_TEMPLATE})
 @RequestMapping(value = "/deletetraffic.htm", method = RequestMethod.POST)
 @ResponseBody
 public JsonResult deleteTraffic(Long id) {
   return JsonResultUtils.success(null, lineService.deleteTravelTraffic(id) ? "删除成功!" : "删除失败!");
 }