package com.liangjian.dataplatform.member.controller;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.liangjian.dataplatform.member.entity.po.secondary.MemIngOrders;
import com.liangjian.dataplatform.member.entity.po.secondary.MemIngProduct;
import com.liangjian.dataplatform.member.entity.po.secondary.WeixinUserInfo;
import com.liangjian.dataplatform.member.entity.po.secondary.UserAddOrders;
import com.liangjian.dataplatform.member.entity.response.ResultObject;
import com.liangjian.dataplatform.member.entity.token.JwtTokenObject;
import com.liangjian.dataplatform.member.entity.token.WeixinJwtTokenGetter;
import com.liangjian.dataplatform.member.service.IUserIngOrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/weixin/userOrdersInfo")
@Api(value = "用户订单记录控制器", tags = "用户订单记录控制器", description = "用户订单记录控制器")
public class UserOrdersController {

    @Autowired
    private IUserIngOrdersService iUserOrdersService;

    private static final Cache<String, Object> CACHES = CacheBuilder.newBuilder()
            // 最大缓存
            .maximumSize(100)
            // 设置过期
            .expireAfterWrite(10, TimeUnit.SECONDS)
            .build();

    @GetMapping("/productInfo")
    @ApiOperation(value = "获取商品详情")
    public ResultObject<MemIngProduct> productInfo(@ApiParam(value = "商品ID", required = true) @RequestParam String productCode,@ApiParam(value = "用户手机号(等级价格)", required = false) @RequestParam String secretPhone) {
            if(productCode!=null){
                MemIngProduct order = iUserOrdersService.getProductInfo(productCode,secretPhone);
                if(order == null) {
                    return ResultObject.success("没有记录",null);
                } else {
                    return ResultObject.success(order);
                }
            }else{
                return ResultObject.success("参数错误",null);
            }

    }

    @GetMapping("/orderInfo")
    @ApiOperation(value = "获取订单详情")
    public ResultObject<MemIngOrders> orderInfo(@ApiParam(value = "订单ID", required = true) @RequestParam String ordersCode) {
        JwtTokenObject<WeixinUserInfo> jwtTokenObject = WeixinJwtTokenGetter.UserInfo.get();
        if(jwtTokenObject==null) {
            return ResultObject.success("暂未登陆！",null);
        } else {
            MemIngOrders order = iUserOrdersService.queryByAccountPhoneInfo(ordersCode);
            if(order == null) {
                return ResultObject.success("没有记录",null);
            } else {
                return ResultObject.success(order);
            }
        }

    }
    @ApiModelProperty(hidden=true)
    @JsonIgnore
    @GetMapping("/orderList")
    @ApiOperation(value = "获取订单列表")
    public Page<MemIngOrders> getLastLog(@ApiParam(value = "页数", required = true) @RequestParam Integer page,
                                                  @ApiParam(value = "每页数量", required = true) @RequestParam Integer size,
                                                  @ApiParam(value = "订单状态", required = false)Integer state) {
        JwtTokenObject<WeixinUserInfo> jwtTokenObject = WeixinJwtTokenGetter.UserInfo.get();
        if(jwtTokenObject==null) {
            return null;
        } else {
            if(page-1<=0){
                page=1;
            }
            return iUserOrdersService.queryByAccountPhone(jwtTokenObject.getObject().getSecretPhone(),state,page-1,size);
        }

    }

    @PostMapping("/addOrder")
    @ApiOperation(value = "保存订单",notes="200:成功,其他：系统错误，缺少必须参数,用户暂无积分记录,积分余额不足,请勿重复提交")
    public ResultObject<Object> addOrder(@ApiParam(value = "下单参数", required = true) @RequestBody UserAddOrders or) {
        if (CACHES.getIfPresent("order") != null && CACHES.getIfPresent("order").equals(or.getProductCode())) {
            return ResultObject.success("请勿重复提交！",null);
        }else{
            CACHES.put("order",or.getProductCode());
        }
        JwtTokenObject<WeixinUserInfo> jwtTokenObject = WeixinJwtTokenGetter.UserInfo.get();

        if(jwtTokenObject==null) {
            return ResultObject.success("暂未登陆！",null);
        } else {
            if(or.getProductCode()!=null){
                MemIngOrders orders=new MemIngOrders();
                orders.setAccountId(jwtTokenObject.getObject().getSecretPhone());
                orders.setaConsigneePhone(or.getaConsigneePhone());
                orders.setConsignee(or.getConsignee());
                orders.setaDeliveryAddress(or.getaDeliveryAddress());
                orders.setAddress(or.getAddress());
                orders.setBuyRemark(or.getBuyRemark());
                orders.setAmount((or.getAmount()==null||or.getAmount()>=0)?1:or.getAmount());
                orders.setOpenid(jwtTokenObject.getObject().getOpenid());
                String suc=iUserOrdersService.addOrders(jwtTokenObject.getObject(),orders,or.getProductCode());
                return ResultObject.success(suc,null);
            }else{
                return ResultObject.success("参数错误",null);
            }
        }

    }

}
