package com.jsowell.api.uniapp;

import com.alibaba.fastjson2.JSON;
import com.google.common.collect.ImmutableMap;
import com.jsowell.common.annotation.Anonymous;
import com.jsowell.common.core.controller.BaseController;
import com.jsowell.common.core.page.PageResponse;
import com.jsowell.common.enums.ykc.ReturnCodeEnum;
import com.jsowell.common.exception.BusinessException;
import com.jsowell.common.response.RestApiResponse;
import com.jsowell.common.util.StringUtils;
import com.jsowell.pile.domain.OrderPileOccupy;
import com.jsowell.pile.dto.QueryOccupyOrderDTO;
import com.jsowell.pile.service.IPileBillingTemplateService;
import com.jsowell.pile.service.OrderPileOccupyService;
import com.jsowell.pile.vo.uniapp.OccupyOrderDetailVO;
import com.jsowell.pile.vo.web.BillingTemplateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 占桩订单controller
 *
 * @author Lemon
 * @Date 2023/8/18 8:55
 */
@Anonymous
@RestController
@RequestMapping("/uniapp/occupyOrder")
public class OccupyOrderController extends BaseController {
    @Autowired
    private OrderPileOccupyService orderPileOccupyService;

    @Autowired
    private IPileBillingTemplateService pileBillingTemplateService;

    /**
     * 查询站点占桩费率
     * https://api.jsowellcloud.com/uniapp/occupyOrder/getStationOccupyFee/{stationId}
     * @param stationId
     * @return
     */
    @GetMapping("/getStationOccupyFee/{stationId}")
    public RestApiResponse<?> getStationOccupyFee(@PathVariable("stationId") String stationId) {
        logger.info("查询站点占桩费率 params:{}", stationId);
        RestApiResponse<?> response = null;
        try {
            BillingTemplateVO vo = pileBillingTemplateService.queryUsedBillingTemplate(stationId);
            response = new RestApiResponse<>(vo);
        } catch (Exception e) {
            logger.error("查询站点占桩费率 error,", e);
            response = new RestApiResponse<>(e);
        }
        logger.info("查询站点占桩费率 result:{}", response);
        return response;
    }

    /**
     * 查询占桩订单列表页
     * https://api.jsowellcloud.com/uniapp/occupyOrder/getOccupyOrderInfo
     * @param request
     * @return
     */
    @PostMapping("/getOccupyOrderInfo")
    public RestApiResponse<?> getOccupyOrderInfo(HttpServletRequest request, @RequestBody QueryOccupyOrderDTO dto) {
        // 获取memberId
        String memberId = getMemberIdByAuthorization(request);
        if (StringUtils.isEmpty(memberId)) {
            throw new BusinessException(ReturnCodeEnum.CODE_TOKEN_ERROR);
        }
        logger.info("查询占桩订单列表页 params:{}", JSON.toJSONString(dto));
        RestApiResponse<?> response = null;
        dto.setMemberId(memberId);
        try {
            PageResponse pageInfo = orderPileOccupyService.getOccupyOrderInfo(dto);
            response = new RestApiResponse<>(pageInfo);
        } catch (Exception e) {
            logger.error("查询占桩订单列表页 error, ", e);
            response = new RestApiResponse<>(e);
        }
        logger.info("查询占桩订单列表页 result:{}", response);
        return response;
    }


    /**
     * 查询占桩订单详情页
     * https://api.jsowellcloud.com/uniapp/occupyOrder/getOccupyOrderDetail/{occupyCode}
     * @param occupyCode
     * @return
     */
    @GetMapping("/getOccupyOrderDetail/{occupyCode}")
    public RestApiResponse<?> getOccupyOrderDetail(@PathVariable("occupyCode") String occupyCode) {
        logger.info("查询占桩订单详情页 param:{}", occupyCode);
        RestApiResponse<?> response = null;
        try {
            OccupyOrderDetailVO vo = orderPileOccupyService.getOccupyOrderDetail(occupyCode);
            response = new RestApiResponse<>(vo);
        } catch (Exception e) {
            logger.error("查询占桩订单详情页 error,", e);
            response = new RestApiResponse<>(e);
        }
        logger.info("查询占桩订单详情页 result:{}", response);
        return response;
    }

    /**
     * 查询用户是否有未支付的占桩订单
     * https://api.jsowellcloud.com/uniapp/occupyOrder/getUnPayOccupyOrder
     * @param request
     * @return
     */
    @GetMapping("/getUnPayOccupyOrder")
    public RestApiResponse<?> getUnPayOccupyOrder(HttpServletRequest request) {
        RestApiResponse<?> response = null;
        try {
            String memberId = getMemberIdByAuthorization(request);
            logger.info("查询用户: {} 是否有未支付的占桩订单", memberId);
            OrderPileOccupy orderPileOccupy = orderPileOccupyService.queryUnPayOrderByMemberId(memberId);
            if (orderPileOccupy != null) {
                response = new RestApiResponse<>(ImmutableMap.of("occupyOrder", orderPileOccupy.getOccupyCode()));
            }
        } catch (Exception e) {
            logger.error("查询用户是否有未支付的占桩订单 error,", e);
            response = new RestApiResponse<>(e);
        }
        logger.info("查询用户是否有未支付的占桩订单 result:{}", response);
        return response;
    }

}
