package com.gree.mall.manager.controller.goods;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.goods.GoodsCodeAccVO;
import com.gree.mall.manager.bean.goods.GoodsSaleLedgerVO;
import com.gree.mall.manager.bean.goods.GoodsStockVO;
import com.gree.mall.manager.bean.order.OrderInfoDetailBean;
import com.gree.mall.manager.bean.order.RefundOrderBody;
import com.gree.mall.manager.bean.order.SendOrderBody;
import com.gree.mall.manager.bean.websit.WorkerSettlementLineVO;
import com.gree.mall.manager.constant.Constant;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.goods.JxGoodsLogic;
import com.gree.mall.manager.logic.order.OrderLogic;
import com.gree.mall.manager.logic.order.OrderRefundLogic;
import com.gree.mall.manager.plus.entity.*;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@RestController
@Api(value = "进销台账列表", tags ={"进销台账列表"} )
@RequestMapping(value = "/jx/goods", produces = "application/json; charset=utf-8")
public class JxGoodsController {

    @Autowired
    JxGoodsLogic jxGoodsLogic;

    @Autowired
    OrderLogic orderLogic;

    @Autowired
    RedisLockRegistry redisLockRegistry;

    @Autowired
    OrderRefundLogic orderRefundLogic;

    @PostMapping("/importCode")
    @ApiOperation("初始化条码导入")
    public ResponseHelper importCode(@RequestParam MultipartFile file


    ) throws IOException {
        jxGoodsLogic.importCode(file);
        return ResponseHelper.success();
    }



    @ZfireList
    @PostMapping("/list/pageStock")
    @ApiOperation(value = "商品库存列表")
    public ResponseHelper<IPage<GoodsStockVO>> pageStock(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<GoodsStockVO> page = jxGoodsLogic.pageStock(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<GoodsStockVO>() {});
    }

    @PostMapping("/list/pageStockExport")
    @ApiOperation(value = "商品库存列表导出")
    public void pageStock(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<GoodsStockVO> page = jxGoodsLogic.pageStock(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }


    @ZfireList
    @PostMapping("/list/pageCode")
    @ApiOperation(value = "商品条码台账")
    public ResponseHelper<IPage<GoodsCodeAccVO>> pageCode(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<GoodsCodeAccVO> page = jxGoodsLogic.pageCode(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<GoodsCodeAccVO>() {});
    }

    @PostMapping("/list/pageCodeExport")
    @ApiOperation(value = "商品条码台账导出")
    public void pageCode(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<GoodsCodeAccVO> page = jxGoodsLogic.pageCode(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }


    @ZfireList
    @PostMapping("/list/pageSale")
    @ApiOperation(value = "商品销售台账列表")
    public ResponseHelper<IPage<GoodsSaleLedgerVO>> pageSale(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<GoodsSaleLedgerVO> page = jxGoodsLogic.pageSale(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<GoodsSaleLedgerVO>() {});
    }

    @PostMapping("/list/pageSaleExport")
    @ApiOperation(value = "商品销售台账列表导出")
    public void pageSale(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<GoodsSaleLedgerVO> page = jxGoodsLogic.pageSale(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }


    @PostMapping("/goods/deliverV2")
    @ApiOperation("发货")
    public ResponseHelper deliverV2(
            @RequestBody SendOrderBody sendOrderBody
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_ORDER + sendOrderBody.getOrderId());
        if(!obtain.tryLock(10, TimeUnit.SECONDS)){
            return ResponseHelper.error("系统繁忙，请稍后再试");
        }
        try {
            orderLogic.deliverV2(sendOrderBody);
        }finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/save")
    @ApiOperation("保存发货条码")
    public ResponseHelper<List<OrderDetailCode>> save(
            @RequestBody List<OrderDetailCode> orderDetailCodes
    ){
        jxGoodsLogic.save(orderDetailCodes);
        return ResponseHelper.success();
    }

    @PostMapping("/remove")
    @ApiOperation("删除发货条码")
    public ResponseHelper remove(
            @RequestParam String  orderDetailCodeId
    ){
        jxGoodsLogic.remove(orderDetailCodeId);
        return ResponseHelper.success();
    }


    @PostMapping("/detail")
    @ApiOperation("发货条码详情")
    public ResponseHelper<List<OrderDetailCode>> detail(
            @RequestParam String orderId
    ){
        List<OrderDetailCode> orderDetailCode = jxGoodsLogic.detail(orderId);
        return ResponseHelper.success(orderDetailCode);
    }


    @PostMapping("/import")
    @ApiOperation("导入发货条码")
    public ResponseHelper importData(@RequestParam MultipartFile file,
                                     @ApiParam(value = "订单明细id",required = true) @RequestParam String orderDetailId

    ) throws IOException {
        jxGoodsLogic.importData(file,orderDetailId);
        return ResponseHelper.success();
    }

    @PostMapping("/goods/refundV2")
    @ApiOperation("退款调用")
    public ResponseHelper refundV2(
            @RequestBody RefundOrderBody refundOrderBody
    ) throws Exception {

        orderRefundLogic.examineRefund(refundOrderBody);

        return ResponseHelper.success();
    }


    @PostMapping("/importRefund")
    @ApiOperation("导入退货条码")
    public ResponseHelper importRefund(@RequestParam MultipartFile file,
                                       @ApiParam(value = "订单明细id",required = true) @RequestParam String orderDetailId
    ) throws IOException {
        jxGoodsLogic.importRefund(file,orderDetailId);
        return ResponseHelper.success();
    }


    @PostMapping("/saveRefund")
    @ApiOperation("保存退货条条码")
    public ResponseHelper saveRefund(
            @RequestBody List<OrderRefundCode> orderDetailCodes
    ){
        jxGoodsLogic.saveRefund(orderDetailCodes);
        return ResponseHelper.success();
    }


    @PostMapping("/detailRefund")
    @ApiOperation("退货条条码详情")
    public ResponseHelper<List<OrderRefundCode>> detailRefund(
            @RequestParam String orderId
    ){
        List<OrderRefundCode> orderDetailCode = jxGoodsLogic.detailRefund(orderId);
        return ResponseHelper.success(orderDetailCode);
    }

    @PostMapping("/removeRefund")
    @ApiOperation("删除退货条码")
    public ResponseHelper removeRefund(
            @RequestParam String  orderDetailCodeId
    ){
        jxGoodsLogic.removeRefund(orderDetailCodeId);
        return ResponseHelper.success();
    }




}
