/**
   * @功能描述:修改工单数据
   *
   * @author gel @2015年8月31日
   * @param
   * @version
   * @throws Exception
   */
  @RequestMapping(value = "updateDataWn", method = RequestMethod.POST)
  @ResponseBody
  public void updateDataDirectory(
      @RequestBody WnVo vo, HttpServletRequest request, HttpServletResponse response)
      throws Exception {
    // 判断vo
    if (vo == null) {
      throw new MbvException("传入参数为空!");
    }

    Map<String, Object> map = new HashMap<String, Object>();

    try {
      log.info("WnController.updateDataWn -> entity: " + vo.getEntity());
      WnEntity entity = vo.getEntity();
      HttpSession session = request.getSession();
      entity.setLastModifiedUser((String) session.getAttribute(MbvConstant.USER_CODE));
      entity.setLastModifiedDate(new Date());

      boolean updateResult = wnService.updateByPrimaryKeySelective(entity);
      log.info(
          "WnController.updateDataWn -> docType: "
              + entity.getDocType()
              + ", docState: "
              + entity.getDocState()
              + ",id:"
              + entity.getId());

      // 0缺货 1提交状态
      if (updateResult == true
          && MbvConstant.MBV_WN_DOC_TYPE.equals(entity.getDocType())
          && MbvConstant.MBV_WN_DOC_STATE.equals(entity.getDocState())) {
        /** 调用消息队列 */
        //				wnService.sendOrder(wnJsonStr(entity));
        //				mqWnService.sendOrder(wnJsonStr(entity));
        wnService.sendOrder(entity);
      }

      map.put("result", updateResult);
      map.put("success", true);
    } catch (MbvException e) {
      throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP);
    } catch (RuntimeException e) {
      throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP);
    }

    // 保存成功
    returnSuccess(response, map);
  }
  /**
   * @功能描述:删除工单
   *
   * @author gel @2015年8月31日
   * @param
   * @version
   * @throws Exception
   */
  @RequestMapping(value = "deleteDataWn", method = RequestMethod.POST)
  @ResponseBody
  public void deleteDataWn(@RequestBody WnVo vo, HttpServletResponse response) throws Exception {
    // 判断vo
    if (vo == null) {
      throw new MbvException("传入参数为空!");
    }
    Map<String, Object> map = new HashMap<String, Object>();

    try {
      List<Integer> deleteIds = vo.getDeleteIds();
      // 根据id 查询数据字典
      List<WnEntity> dataWns = new ArrayList<WnEntity>();
      for (int id : deleteIds) {
        WnEntity d = new WnEntity();
        d.setId((long) id);
        dataWns.add(wnService.selectByPrimaryKey((long) id));
      }
      boolean deleteResult = wnService.deleteDataDirectory(dataWns);

      map.put("result", deleteResult);
      map.put("success", true);
    } catch (MbvException e) {
      throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP);
    } catch (RuntimeException e) {
      throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP);
    }

    // 保存成功
    returnSuccess(response, map);
  }
  /**
   * @功能描述:保存工单
   *
   * @author henry @2015年8月31日
   * @param
   * @version
   * @throws Exception
   */
  @RequestMapping(value = "saveDataWn", method = RequestMethod.POST)
  @ResponseBody
  public void addSysParam(
      @RequestBody WnVo vo, HttpServletRequest request, HttpServletResponse response)
      throws Exception {
    // 判断vo
    if (vo == null) {
      throw new MbvException("传入参数为空!");
    }
    Map<String, Object> map = new HashMap<String, Object>();

    try {
      WnEntity entity = vo.getEntity();
      entity.setDocDate(new Date());
      // entity.setDocState(MbvConstant.WN_STATUS_INPUT);//录入中

      HttpSession session = request.getSession();
      String user = (String) session.getAttribute(MbvConstant.USER_CODE);
      String unitCode = (String) session.getAttribute(MbvConstant.UNIT_CODE);

      entity.setCreateUser(user);
      entity.setCreateDate(new Date());
      entity.setLastModifiedUser(user);
      entity.setLastModifiedDate(new Date());
      entity.setUnitCode(unitCode);

      // doccode
      String docCode = wnService.selectDocCodeSeq(MbvConstant.MBV_WN_SEQ_NAME);
      entity.setDocCode(docCode);

      boolean result = wnService.insertSelective(entity);

      log.info(
          "WnController.saveDataWn -> docType: "
              + entity.getDocType()
              + ", docState: "
              + entity.getDocState());

      // 0缺货 1提交状态
      if (MbvConstant.MBV_WN_DOC_TYPE.equals(entity.getDocType())
          && MbvConstant.MBV_WN_DOC_STATE.equals(entity.getDocState())) {
        /** 调用消息队列 */
        //				wnService.sendOrder(wnJsonStr(entity));
        WnEntity wnEntity = wnService.selectByDocCode(docCode);
        //				mqWnService.sendOrder(wnJsonStr(wnEntity));
        wnService.sendOrder(wnEntity);
      }

      map.put("success", result);
    } catch (MbvException e) {
      throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP);
    } catch (RuntimeException e) {
      throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP);
    }

    // 保存成功
    returnSuccess(response, map);
  }
  /**
   * @功能描述:根据id查询工单
   *
   * @author gel @2015年8月31日
   * @param
   * @version
   */
  @RequestMapping(value = "findDataWnById", method = RequestMethod.POST)
  @ResponseBody
  public void findDataDirectory(@RequestBody WnVo vo, HttpServletResponse response)
      throws MbvException {
    // 判断vo
    if (vo == null) {
      throw new MbvException("传入参数为空!");
    }

    Map<String, Object> map = new HashMap<String, Object>();
    try {
      WnEntity entity = wnService.selectByPrimaryKey(vo.getEntity().getId());
      log.info("WnController.findDataWnById -> id: " + vo.getEntity().getId());

      map.put("data", entity);
      map.put("success", true);
    } catch (MbvException e) {
      throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP);
    } catch (RuntimeException e) {
      throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP);
    }
    // 保存成功
    returnSuccess(response, map);
  }
  /**
   * @功能描述:查询库存表 @2015年9月6日
   *
   * @param
   * @version
   * @throws MbvException
   */
  @RequestMapping(value = "/queryByParams", method = RequestMethod.POST)
  @ResponseBody
  public JqGridBaseEntityVo<WnBean> queryByParams(
      JqGridBaseEntityVo<WnBean> entity,
      WnVo vo,
      HttpServletRequest request,
      HttpServletResponse response)
      throws MbvException {
    // 如果vo为空,则抛出异常
    if (vo == null || vo.getBean() == null) {
      throw new MbvException("传入参数有误!");
    }
    try {
      List<WnBean> list = new ArrayList<WnBean>();
      // 查询总条数
      WnBean bean = vo.getBean();

      String weekFrom = bean.getWeekFrom();
      String weekTo = bean.getWeekTo();
      log.info("WnController.queryByParams -> weekFrom: " + weekFrom + ", weekTo:" + weekTo);

      // date
      if (StringUtils.isNotEmpty(weekFrom) && StringUtils.isNotEmpty(weekTo)) {
        bean.setWeekFrom(weekFrom);
        bean.setWeekTo(weekTo);
        bean.setDay("");
        bean.setMonth("");
      } else if ((StringUtils.isNotEmpty(weekFrom) && weekFrom.trim().length() == 10)
          || (StringUtils.isNotEmpty(weekTo) && weekTo.trim().length() == 10)) {
        bean.setWeekFrom("");
        bean.setWeekTo("");
        bean.setMonth("");
        bean.setDay(weekFrom + weekTo);
      } else if ((StringUtils.isNotEmpty(weekFrom) && weekFrom.trim().length() == 7)
          || (StringUtils.isNotEmpty(weekTo) && weekTo.trim().length() == 7)) {
        bean.setWeekFrom("");
        bean.setWeekTo("");
        bean.setDay("");
        bean.setMonth(weekFrom + weekTo);
      } else {
        bean.setWeekFrom("");
        bean.setWeekTo("");
        bean.setDay("");
        bean.setMonth("");
      }

      HttpSession session = request.getSession();
      String user = (String) session.getAttribute(MbvConstant.USER_CODE);
      String unitCode = (String) session.getAttribute(MbvConstant.UNIT_CODE);
      bean.setUnitCode(unitCode);

      int totalCount = wnService.queryByParamsCount(bean);
      log.info("totalCount:" + totalCount);
      // 如果查询不到,择返回
      if (totalCount > 0) {
        // 组装分页数据
        int offset = (entity.getPage() - 1) * entity.getRows();
        int limit = entity.getRows();
        // 获取数据
        list = wnService.queryByParams(vo.getBean(), offset, limit);
        // 设置分页条数
        JqGridPagesUtils.setPagingParams(entity, list, totalCount);
      }
    } catch (MbvException e) {
      throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP);
    } catch (RuntimeException e) {
      throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP);
    }
    // 返回查询结果
    return entity;
  }