package cn.iocoder.yudao.module.order.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
import cn.iocoder.yudao.module.customer.dal.dataobject.customer.CustomerDO;
import cn.iocoder.yudao.module.customer.dal.dataobject.customerContacts.CustomerContactsDO;
import cn.iocoder.yudao.module.customer.service.customer.CustomerService;
import cn.iocoder.yudao.module.order.dal.dataobject.order.OrderDO;
import cn.iocoder.yudao.module.order.dal.dataobject.orderConsignee.OrderConsigneeDO;
import cn.iocoder.yudao.module.order.dal.dataobject.orderConsignor.OrderConsignorDO;
import cn.iocoder.yudao.module.order.service.order.OrderConsigneeService;
import cn.iocoder.yudao.module.order.service.order.OrderConsignorService;
import cn.iocoder.yudao.module.order.service.order.OrderQueryService;
import cn.iocoder.yudao.module.order.service.order.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

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

/**
 * 创建收货人客户任务
 *  添加注释 创建收货人客户
 */
@Component
@TenantJob
@Slf4j
public class CreateConsigneeCustomerJob implements JobHandler {

    @Resource
    private CustomerService customerService;

    @Resource
    private OrderConsignorService orderConsignorService;

    @Resource
    private OrderConsigneeService orderConsigneeService;

    @Resource
    private OrderService orderService;

    @Resource
    private OrderQueryService orderQueryService;

    /**
     * 每天定点执行一次，扫描收货人无客户档案且订单创建时间-当前>14天的web订单，
     * 若存在，则新建收货人客户档案进入待分配.
     *
     * @param param 参数
     * @return
     * @throws Exception
     */
    @Override
    public String execute(String param) throws Exception {
        //获取14天未入库的订单的收货人
        List<OrderDO> noConsigeeCustomerOrderList =
                orderQueryService.getNoConsigeeCustomerOrderList(14);

        if (CollectionUtil.isNotEmpty(noConsigeeCustomerOrderList)) {

            for (OrderDO orderDO : noConsigeeCustomerOrderList) {

                //订单id和订单编号
                Long orderId = orderDO.getOrderId();
                String orderNo = orderDO.getOrderNo();

                //订单发货人
                OrderConsignorDO orderConsignorDO =
                        orderConsignorService.getOrderConsignorByOrderId(orderId);
                //发货人不为空
                if (orderConsignorDO != null) {
                    CustomerDO consignorCustomerDO =
                            customerService.getCustomer(orderConsignorDO.getCustomerId());

                    //订单收货人
                    OrderConsigneeDO orderConsigneeDO =
                            orderConsigneeService.getOrderConsigneeByOrderId(orderId);

                    //创建收货人
                    CustomerContactsDO consigeeCustomer =
                            customerService.createConsigeeCustomer(
                                    orderConsigneeDO.getName(),
                                    orderConsigneeDO.getCountryCode(),
                                    orderConsigneeDO.getPhone(),
                                    orderConsigneeDO.getCompany(),
                                    orderConsigneeDO.getEmail(),
                                    null,
                                    orderNo);

                    if (consigeeCustomer != null) {
                        orderConsigneeDO.setCustomerId(consigeeCustomer.getCustomerId());
                        orderConsigneeDO.setCustomerContactsId(consigeeCustomer.getId());
                        orderConsigneeService.updateById(orderConsigneeDO);
                    }

                    //并更新其他收货人
                    List<OrderConsigneeDO> otherConsigneeDOList =
                            orderConsigneeService.selectList(new LambdaQueryWrapperX<OrderConsigneeDO>()
                                    .eq(OrderConsigneeDO::getCountryCode, orderConsigneeDO.getCountryCode())
                                    .eq(OrderConsigneeDO::getPhone, orderConsigneeDO.getPhone())
                                    .apply("deleted = 0 and order_id != " + orderId + " and (customer_id is null or customer_id = 0 or customer_contacts_id is null or customer_contacts_id = 0)"));

                    if (CollectionUtil.isNotEmpty(otherConsigneeDOList)) {
                        for (OrderConsigneeDO consigneeDO : otherConsigneeDOList) {
                            consigneeDO.setCustomerId(consigeeCustomer.getCustomerId());
                            consigneeDO.setCustomerContactsId(consigeeCustomer.getId());
                            orderConsigneeService.updateById(consigneeDO);
                        }
                    }
                }
            }

        }
        return "";
    }
}
