/** 晒单列表记录 */
  @RequestMapping("/orderInputList.json")
  @ResponseBody
  public Page<Map<String, Object>> orderInputList(
      String userName, String groupName, HttpServletRequest request) {
    logger.info("晒单录入列表记录: {}, {}", userName, groupName);

    Page<Map<String, Object>> page = PageUtil.getPage(request);
    // 统计总记录数
    page.setTotal(
        userDynamicInfoService.count(
            userName,
            UserDynamicInfoTypeEnum.S.getCode(),
            null,
            null,
            null,
            null,
            UserDynamicInfoIsInputEnum.N.getCode()));
    // 分页记录
    List<Map<String, Object>> list =
        userDynamicInfoService.find(
            userName,
            UserDynamicInfoTypeEnum.S.getCode(),
            null,
            null,
            null,
            null,
            UserDynamicInfoIsInputEnum.N.getCode(),
            page.getFirst(),
            page.getPageSize());
    page.setRows(list);
    return page;
  }
  /** 晒单录入操作 */
  @RequestMapping("/orderInput.json")
  @ResponseBody
  public JsonResult orderInput(
      Long dynamicId,
      Long userId,
      @RequestParam(value = "stockCode") String[] stockCodes,
      @RequestParam(value = "num") Long[] nums,
      @RequestParam(value = "tradeType") String[] tradeTypes,
      @RequestParam(value = "tradePrice") Double[] tradePrices,
      String auditDesc,
      Long parentGroupId,
      HttpServletRequest request) {
    // 查询晒单的动态记录
    UserDynamicInfo userDynamicInfo = userDynamicInfoService.findById(dynamicId);
    userDynamicInfo.setAuditDesc(auditDesc);

    List<Trade> trades = new ArrayList<Trade>();
    for (int i = 0; i < stockCodes.length; i++) {
      String stockCode = stockCodes[i];
      Trade trade = new Trade();
      trade.setUserId(userId);

      Stock stock = stockService.findByStockCode(stockCode);
      trade.setStockId(stock.getStockId());
      trade.setStockCode(stockCode);
      trade.setStockName(stock.getStockName());
      if (tradePrices != null && tradePrices.length > i) {
        trade.setTradePrice(tradePrices[i]);
      }
      if (nums != null && nums.length > i) {
        trade.setTradeNumber(nums[i]);
      }
      trade.setTurnoverAmount(
          MathUtil.mul(
              (trade.getTradePrice() == null ? 0 : trade.getTradePrice()), trade.getTradeNumber()));
      trade.setTradeType(tradeTypes[i]);
      trade.setIsRevoke(TradeIsRevokeEnum.N.getCode());
      trade.setTradeDate(userDynamicInfo.getCreateDate());
      trade.setTradeTime(userDynamicInfo.getCreateDate());
      Date currDate = new java.util.Date();
      trade.setCreateDate(currDate);
      trade.setUpdateDate(currDate);
      trade.setTradeDate(currDate);
      trade.setTradeTime(currDate);
      trade.setShareholderCode(stock.getStockCode());
      trades.add(trade);
    }
    // 添加晒单
    userDynamicInfoService.addOrder(userDynamicInfo, trades, userId, parentGroupId);

    // return jsonResultHelper.buildFailJsonResult(UserDynamicInfoResultCode.SIGN_ERROR);
    return jsonResultHelper.buildSuccessJsonResult(true);
  }
  /** 晒单作废操作 */
  @RequestMapping("/orderInputCancel.json")
  @ResponseBody
  public JsonResult orderInputCancel(Long dynamicId, String auditDesc) {
    // 查询晒单的动态记录
    UserDynamicInfo userDynamicInfo = userDynamicInfoService.findById(dynamicId);
    userDynamicInfo.setAuditDesc(auditDesc);

    // 将此条记录修改成审批通过
    userDynamicInfo.setAuditDate(new java.util.Date());
    userDynamicInfo.setAuditName(AuthUtil.getUserName());
    userDynamicInfo.setAuditStatus(UserDynamicInfoAuditStatusEnum.D.getCode());
    userDynamicInfo.setIsInput(UserDynamicInfoIsInputEnum.Y.getCode());
    userDynamicInfoService.update(userDynamicInfo);
    return jsonResultHelper.buildSuccessJsonResult(null);
  }
  /**
   * 晒单信息屏蔽
   *
   * @param dynamic_id
   * @return
   */
  @RequestMapping("/orderCancel.json")
  @ResponseBody
  public String orderCancel(Long dynamic_id, String auditDesc) {
    logger.info("晒单信息屏蔽: dynamic_id", dynamic_id);
    String status = "1";
    try {
      UserDynamicInfo userDynamicInfo = userDynamicInfoService.findById(dynamic_id);
      userDynamicInfo.setAuditStatus(UserDynamicInfoAuditStatusEnum.D.getCode());
      userDynamicInfo.setAuditDesc(auditDesc);
      userDynamicInfo.setAuditDate(new Date());
      Long userId = AuthUtil.getUserId();
      UUser user = uUserService.findById(userId);
      userDynamicInfo.setAuditName(user.getFullname());
      userDynamicInfoService.update(userDynamicInfo);
    } catch (Exception e) {
      e.printStackTrace();
      status = "2";
      logger.error("晒单信息屏蔽异常: dynamic_id=" + dynamic_id, e);
    }

    return status;
  }
  /** 晒单信息管理列表记录 */
  @RequestMapping("/orderList.json")
  @ResponseBody
  public Page<Map<String, Object>> orderList(
      String userName, String groupName, String auditStatus, HttpServletRequest request) {
    logger.info("晒单信息管理列表记录: userName,groupName", userName, groupName);

    Page<Map<String, Object>> page = PageUtil.getPage(request);
    // 统计总记录数
    page.setTotal(
        userDynamicInfoService.orderCount(
            userName, groupName, UserDynamicInfoTypeEnum.S.getCode(), auditStatus, null));
    // 分页记录
    List<Map<String, Object>> list =
        userDynamicInfoService.find(
            userName,
            groupName,
            UserDynamicInfoTypeEnum.S.getCode(),
            auditStatus,
            null,
            page.getFirst(),
            page.getPageSize());
    page.setRows(list);
    return page;
  }
  /** 晒单录入页面 */
  @RequestMapping("/orderInput.html")
  public String orderInput(Long dynamicId, Model model) {
    logger.info("晒单录入界面: {}", dynamicId);
    // 查询晒单的动态记录
    UserDynamicInfo userDynamicInfo = userDynamicInfoService.findById(dynamicId);
    // 查询用户信息
    User user = userService.findById(userDynamicInfo.getUserId());
    // 查询用户的持仓
    UserStockPosition userStockPositionParam = new UserStockPosition();
    userStockPositionParam.setUserId(user.getUserId());
    List<UserStockPosition> stockPositions = userStockPositionService.find(userStockPositionParam);

    String[] paths = userDynamicInfo.getImagePath().split(";");
    model.addAttribute("paths", paths);
    model.addAttribute("sail_web_url", sail_web_url);
    model.addAttribute("userDynamicInfo", userDynamicInfo);
    model.addAttribute("user", user);
    model.addAttribute("stockPositions", stockPositions);

    if (UserTypeEnum.getByCode(user.getType()).equals(UserTypeEnum.CATTLE)) {
      return "/dynamic/order/orderInput";
    } else {
      // 查询跟投的组合
      MyGroup voteGroup = myGroupService.findById(userDynamicInfo.getGroupId());
      // 查询组合的用户
      User voteGroupUser = userService.findById(voteGroup.getUserId());
      // 查询跟投的组合的股票明细
      List<Map<String, Object>> voteGroupDetails =
          myGroupDetailService.findAndCurrPrice(voteGroup.getGroupId());

      model.addAttribute("voteGroup", voteGroup);
      model.addAttribute("voteGroupUser", voteGroupUser);
      model.addAttribute("voteGroupDetails", voteGroupDetails);
      return "/dynamic/order/orderInputVote";
    }
  }