package com.tmerclub.cloud.order.manager;

import com.tmerclub.cloud.order.bo.mongo.MongoAllotOrderBO;
import com.tmerclub.cloud.order.bo.mongo.MongoAllotOrderItemBO;
import com.tmerclub.cloud.order.bo.mongo.MongoPurchaseOrderBO;
import com.tmerclub.cloud.order.constant.AllotOrderStatusEnum;
import com.tmerclub.cloud.order.dto.AllotOrderDTO;
import com.tmerclub.cloud.order.mapper.AllotOrderMapper;
import com.tmerclub.cloud.order.vo.AllotOrderItemVO;
import com.tmerclub.cloud.order.vo.AllotOrderVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 调拨订单
 * @author gaozijie
 * @since 2023-11-24
 */
@Component
public class MongoAllotOrderManager {

    private static final Logger logger = LoggerFactory.getLogger(MongoAllotOrderManager.class);

    @Autowired
    private AllotOrderMapper allotOrderMapper;

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 新增调拨订单并返回
     * @param allotOrderId 调拨订单id
     * @return 调拨订单mongo数据
     */
    public MongoAllotOrderBO saveAllotOrder(Long allotOrderId) {
        AllotOrderDTO allotOrderDTO = new AllotOrderDTO();
        allotOrderDTO.setAllotOrderId(allotOrderId);
        AllotOrderVO allotOrderVO = allotOrderMapper.getAllotOrder(allotOrderDTO);
        // 转成mongo数据
        MongoAllotOrderBO mongoAllotOrderBO = new MongoAllotOrderBO();
        BeanUtils.copyProperties(allotOrderVO, mongoAllotOrderBO);
        List<MongoAllotOrderItemBO> mongoItems = new ArrayList<>(allotOrderVO.getAllotOrderItemVOList().size());
        for (AllotOrderItemVO allotOrderItemVO : allotOrderVO.getAllotOrderItemVOList()) {
            MongoAllotOrderItemBO mongoItem = new MongoAllotOrderItemBO();
            BeanUtils.copyProperties(allotOrderItemVO, mongoItem);
            mongoItems.add(mongoItem);
        }
        mongoAllotOrderBO.setAllotOrderItemBOList(mongoItems);
        mongoTemplate.save(mongoAllotOrderBO);
        return mongoAllotOrderBO;
    }

    /**
     * 统计未完成的调拨订单数量
     * @param warehouseId 仓库id
     * @return 未完成数
     */
    public Long countUnFinishAllot(Long warehouseId) {
        Criteria criteria = new Criteria()
                .orOperator(Criteria.where("inWarehouseId").is(warehouseId),
                        Criteria.where("outWarehouseId").is(warehouseId))
                .and("status").nin(AllotOrderStatusEnum.COMPLETION.getNum(), AllotOrderStatusEnum.VOIDED.getNum());
        Query query = new Query(criteria);
        logger.info("countUnFinishAllot()的mongodb查询语句： {}", query);
        return mongoTemplate.count(query, MongoAllotOrderBO.class);
    }

    /**
     * 获取调拨订单列表
     * @param allotOrderDTO 调拨订单dto
     * @return 调拨订单集合
     */
    public List<MongoAllotOrderBO> listAllotOrder(AllotOrderDTO allotOrderDTO) {
        Query query = new Query(this.buildListCriteria(allotOrderDTO));
        logger.info("listAllotOrder()的mongodb查询语句： {}", query);
        return mongoTemplate.find(query, MongoAllotOrderBO.class);
    }

    /**
     * 构建list查询条件
     * @param allotOrderDTO 调拨订单dto
     * @return 查询条件
     */
    private Criteria buildListCriteria(AllotOrderDTO allotOrderDTO) {
        Criteria criteria = new Criteria();
        if (!Objects.isNull(allotOrderDTO.getWarehouseId())) {
            criteria.orOperator(Criteria.where("inWarehouseId").is(allotOrderDTO.getWarehouseId()),
                    Criteria.where("outWarehouseId").is(allotOrderDTO.getWarehouseId()));
        }
        if (!CollectionUtils.isEmpty(allotOrderDTO.getStatuses())) {
            criteria.and("status").in(allotOrderDTO.getStatuses());
        }
        return criteria;
    }
}
