package com.cweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.security.SessionObject;
import com.hai.common.security.UserCenter;
import com.hai.common.utils.DateUtil;
import com.hai.common.utils.IDGenerator;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.config.TuanYouConfig;
import com.hai.entity.*;
import com.hai.model.HighMerchantStoreModel;
import com.hai.model.HighUserModel;
import com.hai.model.ResponseData;
import com.hai.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DeadlockLoserDataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Auther: 胡锐
 * @Description:
 * @Date: 2021/3/26 23:08
 */
@Controller
@RequestMapping(value = "/orderPre")
@Api(value = "预约订单接口")
public class HighOrderPreController {
    private static Logger log = LoggerFactory.getLogger(HighOrderPreController.class);

    @Autowired
    private UserCenter userCenter;

    @Resource
    private HighOrderPreService highOrderPreService;


    @RequestMapping(value = "/getUserOrderPreList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取用户预约订单")
    public ResponseData getUserOrderPreList(@RequestParam(name = "status", required = false) Integer status,
                                            @RequestParam(name = "orderNo", required = false) String orderNo,
                                            @RequestParam(name = "orderPreNo", required = false) String preOrderNo,
                                            @RequestParam(name = "goodsName", required = false) String goodsName,
                                            @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                            @RequestParam(name = "pageSize", required = true) Integer pageSize,
                                            HttpServletRequest request) {
        try {

            // 用户
            SessionObject sessionObject = userCenter.getSessionObject(request);
            HighUserModel userInfoModel = (HighUserModel) sessionObject.getObject();

            Map<String,Object> map = new HashMap<>();
            map.put("preUserId", userInfoModel.getHighUser().getId());
            map.put("status", status);
            map.put("orderNo", orderNo);
            map.put("preOrderNo", preOrderNo);
            map.put("goodsName", goodsName);

            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(highOrderPreService.getListOrderPre(map)));

        } catch (Exception e) {
            log.error("HighOrderController --> getUserPreOrderList() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getOrderPreById", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据预约订单id 查询预约订单详情")
    public ResponseData getOrderPreById(@RequestParam(name = "orderPreId", required = true) Long orderPreId) {
        try {

            return ResponseMsgUtil.success(highOrderPreService.findByOrderId(orderPreId));

        } catch (Exception e) {
            log.error("HighOrderController --> getOrderById() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getPreByOrderIdId", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据订单id 查询预约订单详情")
    public ResponseData getPreByOrderIdId(@RequestParam(name = "orderId", required = true) Long orderId) {
        try {

            return ResponseMsgUtil.success(highOrderPreService.getPreByOrderId(orderId));

        } catch (Exception e) {
            log.error("HighOrderController --> getPreByOrderIdId() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/orderComplete", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "订单完成")
    public ResponseData orderComplete(@RequestBody JSONObject body) {
        try {
            if (body == null || body.getLong("preOrderId") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            highOrderPreService.orderComplete(body.getLong("preOrderId"));

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error("HighOrderController --> orderComplete() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }


}
