package com.fjhx.victoriatourist.controller.jd;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.victoriatourist.entity.jd.dto.JdOrderDto;
import com.fjhx.victoriatourist.entity.jd.dto.JdOrderSelectDto;
import com.fjhx.victoriatourist.entity.jd.po.JdOrder;
import com.fjhx.victoriatourist.entity.jd.po.JdOrderDetails;
import com.fjhx.victoriatourist.entity.jd.vo.JdOrderVo;
import com.fjhx.victoriatourist.entity.logistics.vo.LogisticsInfosVo;
import com.fjhx.victoriatourist.entity.stock.dto.StockTransferDto;
import com.fjhx.victoriatourist.service.jd.JdOrderDetailsService;
import com.fjhx.victoriatourist.service.jd.JdOrderService;
import com.ruoyi.common.core.domain.BaseSelectDto;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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


/**
 * <p>
 * 京东订单 前端控制器
 * </p>
 *
 * @author
 * @since 2023-04-20
 */
@RestController
@RequestMapping("/jdOrder")
public class JdOrderController {

    @Autowired
    private JdOrderService jdOrderService;
    @Resource
    private JdOrderDetailsService jdOrderDetailsService;

    /**
     * 京东订单分页
     */
    @PostMapping("/page")
    public Page<JdOrderVo> page(@RequestBody JdOrderSelectDto dto) {
        return jdOrderService.getPage(dto);
    }

    /**
     * 京东订单接收分页
     */
    @PostMapping("/receivePage")
    public Page<JdOrderVo> receivePage(@RequestBody JdOrderSelectDto dto) {
        return jdOrderService.receivePage(dto);
    }

    /**
     * 京东订单明细
     */
    @PostMapping("/detail")
    public JdOrderVo detail(@RequestBody BaseSelectDto dto) {
        return jdOrderService.detail(dto.getId());
    }

    /**
     * 京东订单新增
     */
    @PostMapping("/add")
    public void add(@Validated @RequestBody JdOrderDto jdOrderDto) {
        jdOrderService.add(jdOrderDto);
    }

    /**
     * 获取配送中心
     */
    @PostMapping("/getDistributionCenter")
    public List<String> getDistributionCenter() {
        return jdOrderService.getDistinctList(JdOrder::getDistributionCenter);
    }

    /**
     * excel导入
     */
    @PostMapping("/excelImport")
    public void excelImport(@RequestParam("file") MultipartFile file, @RequestParam("customerId") Long customerId) {
        jdOrderService.excelImport(file, customerId);
    }

    /**
     * 京东订单出库
     */
    @PostMapping("/outbound")
    public void outbound(@RequestBody JdOrderDto jdOrderDto) {
        jdOrderService.outbound(jdOrderDto);
    }

    /**
     * 京东订单接收
     */
    @PostMapping("/receive")
    public void outbound(@RequestBody StockTransferDto stockTransferDto) {
        jdOrderService.receive(stockTransferDto);
    }

    /**
     * 京东订单接收 excel导入
     */
    @PostMapping("/receiveExcelImport")
    public void receiveExcelImport(@RequestParam("file") MultipartFile file) {
        jdOrderService.receiveExcelImport(file);
    }

    /**
     * 获取物流信息
     */
    @PostMapping("/getLogisticsInfo")
    public List<LogisticsInfosVo> getLogisticsInfo(@RequestBody BaseSelectDto dto) {
        return jdOrderService.getLogisticsInfo(dto.getId());
    }

    /**
     * 确认采购单
     */
    @PostMapping("/confirmBusinessStatus")
    public void confirmBusinessStatus(@RequestBody JdOrderDto jdOrderDto) {
        if(!NumberUtil.isValidNumber(jdOrderDto.getId()) || !NumberUtil.isValidNumber(jdOrderDto.getBusinessStatus())){
            throw new RuntimeException("参数错误");
        }
        //校验明细内的商品是否都有productId
        jdOrderDetailsService.findListByJdOrderId(jdOrderDto.getId()).forEach(jdOrderDetails -> {
            if(ObjectUtil.isNull(jdOrderDetails.getProductId())){
                throw new RuntimeException("采购单明细内商品："+jdOrderDetails.getWareId()+"没有关联到产品，无法进行确认");
            }
        });
        jdOrderService.confirmBusinessStatus(jdOrderDto);
    }


    /**
     * 订单回告
     * @author hj
     * @date 2023/12/16 23:37
     */
    @PostMapping("/tellBackOrer")
    public void tellBackOrer(@RequestBody JdOrderDto jdOrderDto){
        //参数校验
        if(ObjectUtil.isNull(jdOrderDto.getOrderId()) || ObjectUtil.isNull(jdOrderDto.getId())){
            throw new RuntimeException("参数错误");
        }
        if(ObjectUtil.isNull(jdOrderDto.getDeliveryTime())){
            throw new RuntimeException("预计送达时间不能为空");
        }
        List<JdOrderDetails> jdOrderDetailsList = jdOrderDto.getJdOrderDetailsList();
        if(CollectionUtil.isEmpty(jdOrderDetailsList)){
            throw new RuntimeException("采购单明细不能为空");
        }
        for (JdOrderDetails jdOrderDetails : jdOrderDetailsList) {
            if(ObjectUtil.isNull(jdOrderDetails.getWareId())){
                throw new RuntimeException("京东商品编号为空");
            }
            if(ObjectUtil.isNull(jdOrderDetails.getConfirmNum())){
                throw new RuntimeException("京东商品编号："+jdOrderDetails.getWareId()+"确认数量为空");
            }
        }
        jdOrderService.tellBackOrer(jdOrderDto);
    }
}
