package com.bs.tm.controller;

import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bs.common.core.controller.BaseController;
import com.bs.common.core.page.TableDataInfo;
import com.bs.tm.domain.TmCargoSource;
import com.bs.tm.domain.TmOrder;
import com.bs.tm.service.ITmCargoDetailService;
import com.bs.tm.service.ITmCargoSourceService;
import com.bs.tm.service.ITmOrderCargoService;
import com.bs.tm.service.ITmOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@Api(tags = "托运/承运订单")
@RestController
@RequestMapping("/tm/allOrder")
public class TmAllOrderController extends BaseController {
    @Resource
    private ITmCargoSourceService tmCargoSourceService;

    @Resource
    private ITmCargoDetailService tmCargoDetailService;
    @Resource
    private ITmOrderService tmOrderService;
    @Resource
    private ITmOrderCargoService tmOrderCargoService;
    @ApiOperation("分页查询托运、承运订单列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(TmOrder tmOrder) {
        startPage();
        List<TmOrder> list = new ArrayList<>();
        List<TmCargoSource> lsCargo = new ArrayList<>();
        LambdaQueryWrapper<TmOrder> queryWrapper = new LambdaQueryWrapper();
        LambdaQueryWrapper<TmCargoSource> cargoWrapper = new LambdaQueryWrapper<>();
        if (Validator.isNotEmpty(tmOrder.getKeyword())) {
            queryWrapper.and(wp ->{
                wp.like(TmOrder::getOrderNo, tmOrder.getKeyword()).or();
                wp.like(TmOrder::getCustName, tmOrder.getKeyword());
            });
            cargoWrapper.and(wp ->{
                wp.like(TmCargoSource::getOrderNo, tmOrder.getKeyword()).or();
                wp.like(TmCargoSource::getCustName, tmOrder.getKeyword());
            });
        }
        if ("0".equals(tmOrder.getIsBalance())) {
            queryWrapper.notInSql(TmOrder::getId, "select order_id from cw_amortize_cost where order_type='1' and del_flag='0'");
            cargoWrapper.notInSql(TmCargoSource::getId, "select order_id from cw_amortize_cost where order_type='0' and del_flag='0'");
        } else if ("1".equals(tmOrder.getIsBalance())) {
            queryWrapper.inSql(TmOrder::getId, "select order_id from cw_amortize_cost where order_type='1' and del_flag='0'");
            cargoWrapper.inSql(TmCargoSource::getId, "select order_id from cw_amortize_cost where order_type='0' and del_flag='0'");
        }
        if (Validator.isNotEmpty(tmOrder.getBeginDate())) {
            queryWrapper.ge(TmOrder::getOrderDate, tmOrder.getBeginDate());
            cargoWrapper.ge(TmCargoSource::getOrderDate, tmOrder.getBeginDate());
        }
        if (Validator.isNotEmpty(tmOrder.getEndDate())) {
            queryWrapper.le(TmOrder::getOrderDate, tmOrder.getEndDate());
            cargoWrapper.ge(TmCargoSource::getOrderDate, tmOrder.getEndDate());
        }
        if (Validator.isNotEmpty(tmOrder.getStatus())) {
            queryWrapper.eq(TmOrder::getStatus, tmOrder.getStatus());
            cargoWrapper.eq(TmCargoSource::getStatus, tmOrder.getStatus());
        }
        list = tmOrderService.list(queryWrapper);
        lsCargo = tmCargoSourceService.list(cargoWrapper);
        if (Validator.isNotEmpty(tmOrder.getOrderType())) {
            if ("0".equals(tmOrder.getOrderType())) {
                // 0表示托运订单,把承运订单清空
                list = new ArrayList<>();
            } else if ("1".equals(tmOrder.getOrderType())) {
                // 1表示承运订单，把托运订单清空
                lsCargo = new ArrayList<>();
            }
        }
        if (list.size() > 0) {
            list.forEach(ele -> {
                ele.setOrderType("1");
            });
        }
        if (lsCargo.size() > 0) {
            for (TmCargoSource cargo : lsCargo) {
                TmOrder order = new TmOrder();
                //orderNo、cargoName、custType、freightRates、custName、custId
                order.setOrderNo(cargo.getOrderNo());
                order.setCargoInfo(cargo.getCargoInfo());
                order.setOrderType("0");
                order.setFreightRates(cargo.getFreightRates());
                order.setCustName(cargo.getCustName());
                order.setCustId(cargo.getCustId());
                order.setId(cargo.getId());
                order.setCurrency(cargo.getCurrency());
                list.add(order);
            }
        }
        return getDataTable(list);
    }
}
