package com.yc.ship.module.trade.controller.admin.supplier;

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.framework.web.core.util.WebFrameworkUtils;
import com.yc.ship.module.resource.service.supplier.ProductSupplierService;
import com.yc.ship.module.trade.controller.admin.order.vo.check.TradeCheckReqVO;
import com.yc.ship.module.trade.controller.admin.order.vo.check.TradeCheckRespVO;
import com.yc.ship.module.trade.controller.admin.supplier.vo.AuthRespVO;
import com.yc.ship.module.trade.controller.admin.supplier.vo.SupplierRespVO;
import com.yc.ship.module.trade.controller.app.pda.vo.SupplierCheckOrderPageVO;
import com.yc.ship.module.trade.enums.CheckTypeEnum;
import com.yc.ship.module.trade.service.order.TradeCheckService;
import com.yc.ship.module.trade.service.supplier.TradeSupplierOrderRepositoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.API;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.CHECK;


@Validated
@RestController
@RequestMapping("/supplier/trade")
@Tag(name = "小程序商家核销服务")
public class SupplierTradeOrderController {

    @Resource
    private ProductSupplierService productSupplierService;
    @Resource
    private TradeCheckService tradeCheckService;

    @Resource
    private TradeSupplierOrderRepositoryService tradeSupplierOrderRepositoryService;

    @GetMapping("/getAuthInfo")
    @Operation(summary = "获取登录用户认证信息")
    public CommonResult<AuthRespVO> getAuthInfo() {
        AuthRespVO authRespVO = new AuthRespVO();
        Long userId = WebFrameworkUtils.getLoginUserId();
        List<SupplierRespVO> supplierRespVOs = tradeSupplierOrderRepositoryService.selectSupplierByUserId(userId);
        if(supplierRespVOs!=null && !supplierRespVOs.isEmpty()){
            authRespVO.setSupplier(true);
            authRespVO.setSupplierRespVO(supplierRespVOs);
            return success(authRespVO);
        }else{
            authRespVO.setSupplier(false);
        }
        return success(authRespVO);
    }

    @PostMapping("check/query-order-info")
    @Operation(summary = "查询供应商订单信息")
    @OperateLog(type = API)
    public CommonResult<?> queryOrderInfo(@Valid @RequestBody SupplierCheckOrderPageVO reqVO){
        Long userId = WebFrameworkUtils.getLoginUserId();
        reqVO.setUserId(userId);
//        return productSupplierService.queryOrderInfo(reqVO);
        return null;
    }

    @PostMapping("check/supplier")
    @Operation(summary = "小程序核销(商家)")
    @OperateLog(type = CHECK)
    public CommonResult<List<TradeCheckRespVO>> checkSupplier(@Valid @RequestBody TradeCheckReqVO checkReqVO) {
//        checkReqVO.setType(CheckTypeEnum.Merchant.getType());
//        checkReqVO.setIsPreCheck(0);
//        if(checkReqVO.getSupplierId()==null) {
//            Long userId = WebFrameworkUtils.getLoginUserId();
//            List<SupplierRespVO> supplierRespVOs = productSupplierService.selectSupplierByUserId(userId);
//            if (supplierRespVOs != null && !supplierRespVOs.isEmpty()) {
//                checkReqVO.setSupplierId(supplierRespVOs.get(0).getId());
//            }
//        }
//        if(checkReqVO.getSupplierId()==null) {
//            return error(500,"供应商信息不能为空");
//        }
//        return tradeCheckService.checkProductSupplier(checkReqVO);
        return null;
    }


}
