package com.cpxt.web.task;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cpxt.biz.domain.BizCustomerShop;
import com.cpxt.biz.domain.BizCustomerWarehouse;
import com.cpxt.biz.domain.BizOrder;
import com.cpxt.biz.domain.BizOrderTask;
import com.cpxt.biz.mapper.*;
import com.cpxt.common.utils.DateUtils;
import com.cpxt.common.utils.Helper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component("orderTask")
public class OrderTask {

    private static OrderTask orderTask;

    @Autowired
    private BizOrderMapper orderMapper;
    @Autowired
    private BizOrderTaskMapper orderTaskMapper;
    @Autowired
    private BizCustomerRouteShopMapper routeShopMapper;
    @Autowired
    private BizCustomerWarehouseMapper customerWarehouseMapper;
    @Autowired
    private BizCustomerShopMapper customerShopMapper;


    @PostConstruct
    public void init(){
        orderTask = this;

        orderTask.orderMapper = this.orderMapper;
        orderTask.orderTaskMapper = this.orderTaskMapper;
        orderTask.routeShopMapper = this.routeShopMapper;
        orderTask.customerShopMapper = this.customerShopMapper;
    }

    public void generateOrder(String code){
        LambdaQueryWrapper<BizOrderTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BizOrderTask::getCode, code);
        BizOrderTask task = orderTask.orderTaskMapper.selectOne(queryWrapper);

        Long routeId = task.getRouteId();
        if (Helper.FInt(routeId)>0){
            List<Long> shopIds = orderTask.routeShopMapper.selectShopIdListByRouteId(routeId);

            Long warehouseId = task.getWarehouseId();
            BizCustomerWarehouse warehouse = orderTask.customerWarehouseMapper.selectById(warehouseId);

            for (Long shopId : shopIds ) {
                BizOrder order = new BizOrder();
                order.setOrderSn(getNewOrderSn());
                if (task.getOrderDateMode()==2){
                    Calendar calendar = Calendar.getInstance();
                    calendar.add(Calendar.DAY_OF_YEAR, 1);
                    Date tomorrow = calendar.getTime();
                    order.setOrderDate(tomorrow);
                }else{
                    order.setOrderDate(new Date());
                }

                order.setCustomerId(task.getCustomerId());
                order.setCustomerName(task.getCustomerName());

                order.setRouteId(routeId);
                order.setRouteName(task.getRouteName());

                order.setWarehouseId(warehouse.getId());
                order.setWarehouseName(warehouse.getName());
                order.setSenderName(warehouse.getName());
                order.setSenderLinkman(warehouse.getLinkman());
                order.setSenderPhone(warehouse.getLinkphone());
                order.setSenderAddress(warehouse.getAddress());
                order.setSenderLat(warehouse.getLat());
                order.setSenderLng(warehouse.getLng());

                BizCustomerShop shop = orderTask.customerShopMapper.selectById(shopId);
                order.setShopId(shopId);
                order.setShopName(shop.getName());
                order.setReceiverName(shop.getName());
                order.setReceiverLinkman(shop.getLinkman());
                order.setReceiverPhone(shop.getLinkphone());
                order.setReceiverAddress(shop.getAddress());
                order.setReceiverLat(shop.getLat());
                order.setReceiverLng(shop.getLng());

                order.setDriverId(task.getDriverId());
                order.setDriverName(task.getDriverName());
                if (Helper.FInt(task.getCopilotId())>0) {
                    order.setCopilotId(task.getCopilotId());
                    order.setCopilotName(task.getCopilotName());
                }

                order.setGoodsType("");
                order.setGoodsName("");
                orderTask.orderMapper.insert(order);
            }
        }
    }

    // 订单号生成规则
    private String getNewOrderSn() {
        String format = DateUtil.format(new Date(), "yyyyMMdd"); // 20241220
        String orderSn;
        BizOrder order = orderTask.orderMapper.selectLastestOrder();
        if (order == null || !order.getOrderSn().substring(1, 9).equals(format)){
            orderSn = "Y" + format + "1001";
        }else {
            String sn = order.getOrderSn();
            String substring = sn.substring(9); // 1001
            long aLong = Long.parseLong(substring);
            aLong += 1L;
            orderSn = "Y" + format + aLong;
        }
        return orderSn;
    }
}
