package com.xxfc.platform.order.rest;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.github.wxiaoqi.security.admin.feign.UserFeign;
import com.github.wxiaoqi.security.admin.feign.dto.AppUserDTO;
import com.github.wxiaoqi.security.auth.client.annotation.IgnoreClientToken;
import com.github.wxiaoqi.security.auth.client.annotation.IgnoreUserToken;
import com.github.wxiaoqi.security.auth.client.config.UserAuthConfig;
import com.github.wxiaoqi.security.common.context.BaseContextHandler;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.github.wxiaoqi.security.common.rest.BaseController;
import com.xxfc.platform.order.biz.OrderTourDetailBiz;
import com.xxfc.platform.order.biz.OrderTourReceivedStatisticsBiz;
import com.xxfc.platform.order.entity.BaseOrder;
import com.xxfc.platform.order.entity.OrderTourDetail;
import com.xxfc.platform.order.entity.OrderTourReceivedStatistics;
import com.xxfc.platform.order.pojo.order.add.AddTourDTO;
import com.xxfc.platform.order.pojo.order.TourBO;
import com.xxfc.platform.order.pojo.price.TourPriceVO;
import com.xxfc.platform.order.service.OrderTourService;
import com.xxfc.platform.tour.entity.TourGood;
import com.xxfc.platform.tour.feign.TourFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

@Controller
@RequestMapping("orderTour")
@IgnoreClientToken
@Api(value = "旅游订单", tags = {"旅游订单"})
public class OrderTourController extends BaseController<OrderTourDetailBiz, OrderTourDetail> {

    @Autowired
    OrderTourService orderTourService;

    @Autowired
    OrderTourReceivedStatisticsBiz orderTourReceivedStatisticsBiz;

    @Autowired

    TourFeign tourFeign;

    @Autowired
    protected UserAuthConfig userAuthConfig;

    @Autowired
    UserFeign userFeign;

    public AppUserDTO getUserInfo() {
        return userFeign.userDetailByToken(userAuthConfig.getToken(request)).getData();
    }

    @RequestMapping(value = "/orderTourStatistics", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "旅游订单统计")
     public ObjectRestResponse<OrderTourReceivedStatistics> orderTourStatistics() {
        Date today = new Date();//获取今天的日期
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        c.add(Calendar.DAY_OF_MONTH, 1);
        Date tomorrow = c.getTime();//这是明天
        c.setTime(today);
        c.add(Calendar.DAY_OF_MONTH, -1);
        Date yesterday = c.getTime();//这是昨天
        orderTourReceivedStatisticsBiz.orderTourInsertStatisticsNew(yesterday, tomorrow,null
        );
        return ObjectRestResponse.succ();
    }


    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "确认旅游订单")
    public ObjectRestResponse<BaseOrder> add(@RequestBody AddTourDTO vo) {
        TourBO bo = BeanUtil.toBean(vo, TourBO.class);
        bo.setAppUserDTO(userFeign.userDetailByToken(BaseContextHandler.getToken()).getData());
        bo.setTickerNo(StrUtil.isNotBlank(vo.getTickerNos()) ? Arrays.asList(vo.getTickerNos().split(",")) : null);
        orderTourService.createOrder(bo);
        return ObjectRestResponse.succ(bo.getOrder());
    }

    @RequestMapping(value = "/calculate-price", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "价格计算")
    @IgnoreUserToken
    public ObjectRestResponse<TourPriceVO> calculatePrice(CalculatePriceVO vo) {
        TourBO bo = BeanUtil.toBean(vo, TourBO.class);
        bo.setAppUserDTO(getUserInfo());
        bo.setAdultNum(vo.getNumber());
        bo.setChildNum(vo.getChildNumber());
        TourGood tourGood = tourFeign.usableGet(bo.getGoodId()).getData();
        bo.setTourGood(tourGood);
        bo.setTickerNo(StrUtil.isNotBlank(vo.getTickerNos()) ? Arrays.asList(vo.getTickerNos().split(",")) : null);
        return ObjectRestResponse.succ(orderTourService.calculatePrice(bo));
    }

    @Data
    public class CalculatePriceVO {
        //商品id
        @ApiModelProperty(value = "商品id")
        private Integer goodId;

        //商品规格ids
        @ApiModelProperty(value = "商品规格ids")
        private String speIds;

        //具体商品（商品项）id
        @ApiModelProperty(value = "具体商品（商品项）id")
        private String spePriceId;

        @ApiModelProperty(value = "成人个数")
        Integer number;

        @ApiModelProperty(value = "儿童个数")
        Integer childNumber;

        @ApiModelProperty(value = "是否保险标记 1--要保险；0--不需要")
        Integer hasInsure;

        /**
         * 优惠券*
         */
        @ApiModelProperty(value = "优惠卷卷号")
        private String tickerNos;

    }
}