package com.zswl.cloud.springBtach.server.core.controller;

import cn.hutool.json.JSONObject;
import com.github.microservice.auth.client.constant.AuthConstant;
import com.github.microservice.auth.client.content.ResultContent;
import com.github.microservice.auth.security.annotations.ResourceAuth;
import com.github.microservice.auth.security.helper.AuthHelper;
import com.github.microservice.auth.security.type.AuthType;
import com.zhongshu.payment.client.model.order.*;
import com.zhongshu.payment.client.model.order.v2.CreateOrder2Model;
import com.zhongshu.payment.client.model.order.v2.OrderResult2Model;
import com.zhongshu.payment.client.model.payment.CreateResponseModel;
import com.zhongshu.payment.client.type.JobFlowMap;
import com.zhongshu.payment.client.type.TransferOwner;
import com.zhongshu.payment.client.type.TransferType;
import com.zswl.cloud.shop.client.service.GoodsService;
import com.zswl.cloud.shop.client.vo.life.GoodsInfoVo;
import com.zswl.cloud.springBtach.server.core.service.OrderServiceImp;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

@Log4j2
@RestController
@RequestMapping("order")
public class OrderController {


    @Autowired
    AuthHelper authHelper;

    @Autowired
    OrderServiceImp orderServiceImp;

    @Autowired
    GoodsService goodsService;


    @ApiOperation("创建订单")
    @ResourceAuth(value = AuthConstant.User, type = AuthType.User)
    @RequestMapping(value = "creat", method = RequestMethod.POST)
    public ResultContent creat(@RequestBody @Valid AddOrderParamModel orderParamModel) {
        String userId = authHelper.getCurrentUser().getUserId();


        CreateOrder2Model createOrder2Model = new CreateOrder2Model();
        createOrder2Model.setUserId(userId);
        createOrder2Model.setShopId(orderParamModel.getShopId());

        for (String goodsId : orderParamModel.getGoodsList()) {
            com.zswl.cloud.shop.client.ret.ResultContent<GoodsInfoVo> detail = goodsService.detail(goodsId, 1, userId);
            String goodsType = detail.getContent().getGoodsType();

            CreateOrder2Model.Goods goods1 = new CreateOrder2Model.Goods();
            goods1.setGoodsId(goodsId);
            switch (goodsType) {
                case "801":
                    goods1.setJobFlowMap(JobFlowMap.P801);
                    break;
                case "802":
                    goods1.setJobFlowMap(JobFlowMap.P802);
                    break;
                case "803":
                    goods1.setJobFlowMap(JobFlowMap.P803);
                    break;
                case "804":
                    goods1.setJobFlowMap(JobFlowMap.P804);
                    break;
                case "805":
                    goods1.setJobFlowMap(JobFlowMap.P805);
                    break;
                case "806":
                    goods1.setJobFlowMap(JobFlowMap.P806);
                    break;
                case "807":
                    goods1.setJobFlowMap(JobFlowMap.P807);
                    break;
                case "808":
                    goods1.setJobFlowMap(JobFlowMap.P808);
                    break;


            }


            createOrder2Model.setGoodsList(List.of(goods1));
        }

        return orderServiceImp.creat(createOrder2Model);
    }


    @ApiOperation("生成二维码")
    @ResourceAuth(value = AuthConstant.User, type = AuthType.User)
    @RequestMapping(value = "qrCode/{id}", method = RequestMethod.GET, consumes = {MediaType.ALL_VALUE})
    public ResultContent<String> qrCode(@PathVariable("id") String id) {
        Assert.hasText(id, "id不能为空");
        return orderServiceImp.qrCode(id);
    }


    @ApiOperation("创建扫码订单")
    @ResourceAuth(value = "user", type = AuthType.User)
    @RequestMapping(value = "creatScanOrder", method = RequestMethod.POST)
    public ResultContent<CreateResponseModel> creatScanOrder(@Valid @RequestBody ScanModel scanModel) {

        String userId = authHelper.getCurrentUser().getUserId();

        // 订单信息
        CreateOrder2Model createOrder2Model = new CreateOrder2Model();
        createOrder2Model.setShopId(scanModel.getShopId());
        createOrder2Model.setUserId(userId);
        // todo 店铺信息
        createOrder2Model.setShopInfo(new JSONObject());
        createOrder2Model.setTotalAmount(scanModel.getPrice());
        createOrder2Model.setDiscountAmount(BigDecimal.ZERO);
        createOrder2Model.setPayAmount(scanModel.getPrice());


        // 商品信息
        CreateOrder2Model.Goods goods = new CreateOrder2Model.Goods();
        goods.setGoodsId("二维码");
        goods.setJobFlowMap(JobFlowMap.Pcod);
        goods.setPayAmount(scanModel.getPrice());
        goods.setTransferType(TransferType.RATIO);


        goods.setGoodsInfo(new JSONObject().set("goodsName", "二维码商品"));

        // todo 店铺分账规则
        // 平台分账
        CreateOrder2Model.Owner plat = new CreateOrder2Model.Owner();
        plat.setOwner(TransferOwner.PLATFORM);
        plat.setValue(BigDecimal.ZERO);

        // 商品数据
        CreateOrder2Model.Owner shop = new CreateOrder2Model.Owner();
        shop.setOwner(TransferOwner.BUY_SHOP);
        shop.setValue(BigDecimal.valueOf(100));
        shop.setShopId(createOrder2Model.getShopId());

        goods.setTransferRrule(List.of(plat, shop));
        goods.setExtend(scanModel.getExtend());
        createOrder2Model.setGoodsList(List.of(goods));


        createOrder2Model.setGoodsList(List.of(goods));

        return orderServiceImp.creat(createOrder2Model);
    }


    @ApiOperation("商家验券")
    @ResourceAuth(value = "checkCoupon", type = AuthType.Enterprise)
    @ResourceAuth(value = "checkCoupon", type = AuthType.Project)
    @RequestMapping(value = "shop/verification/{token}", method = RequestMethod.GET, consumes = {MediaType.ALL_VALUE})
    public ResultContent verification(@PathVariable("token") String token) {

        Assert.hasText(token, "token不能为空");
        return orderServiceImp.verification(token);
    }


    @ApiOperation("app_订单查询列表")
    @RequestMapping(value = "user/search", method = RequestMethod.POST)
    @ResourceAuth(value = AuthConstant.User, type = AuthType.User)
    public ResultContent<Page<OrderResultModel>> userSearch(@RequestBody UserSeachOrderModel userSeachOrderModel) {

        return orderServiceImp.userSearch(userSeachOrderModel);
    }

    @ApiOperation("app_商品订单详情")
    @RequestMapping(value = "user/goodsDetail/{id}", method = RequestMethod.GET)
    @ResourceAuth(value = AuthConstant.User, type = AuthType.User)
    public ResultContent<OrderResultModel> goodsDetail(@PathVariable("id") String id) {

        return orderServiceImp.goodsDetail(id);
    }

    @ApiOperation("app_订单详细")
    @RequestMapping(value = "user/orderDetail/{orderNo}", method = RequestMethod.GET)
    @ResourceAuth(value = AuthConstant.User, type = AuthType.User)
    public ResultContent<OrderResultModel> orderDetail(@PathVariable("orderNo") String orderNo) {

        return orderServiceImp.orderDetails(orderNo);
    }



    /*                                                 商家接口                                                 */


    @ApiOperation("商家_订单查询列表")
    @RequestMapping(value = "shop/search", method = RequestMethod.POST)
    @ResourceAuth(value = "admin", type = AuthType.Enterprise)
    @ResourceAuth(value = "admin", type = AuthType.Project)
    public ResultContent<Page<OrderResult2Model>> shopSearch(@RequestBody AdminSeachOrderModel seachOrderModel) {
        return orderServiceImp.shopSearch(seachOrderModel);
    }

    @ApiOperation("商家_订单详情")
    @RequestMapping(value = "shop/orderDetail/{orderNo}", method = RequestMethod.GET)
    @ResourceAuth(value = "admin", type = AuthType.Enterprise)
    @ResourceAuth(value = "admin", type = AuthType.Project)
    public ResultContent shopOrderDetail(@PathVariable("orderNo") String orderNo) {
        return orderServiceImp.shopOrderDetail(orderNo);
    }


    // 平台接口

    @ApiOperation("平台_订单查询列表")
    @RequestMapping(value = "platform/search", method = RequestMethod.POST)
    @ResourceAuth(value = AuthConstant.SuperAdmin, type = AuthType.Platform)
    public ResultContent<Page<OrderResult2Model>> platformSearch(@RequestBody AdminSeachOrderModel seachOrderModel) {

        return orderServiceImp.adminSearch(seachOrderModel);
    }

    @ApiOperation("平台_订单详情")
    @RequestMapping(value = "platform/orderDetail/{orderNo}", method = RequestMethod.GET)
    @ResourceAuth(value = AuthConstant.SuperAdmin, type = AuthType.Platform)
    @ResourceAuth(value = "admin", type = AuthType.Enterprise)
    @ResourceAuth(value = "admin", type = AuthType.Project)
    public ResultContent adminOrderDetail(@PathVariable("orderNo") String orderNo) {

        return orderServiceImp.adminOrderDetail(orderNo);
    }


}
