package com.cf.imes.module.executor.dal.mysql.plan;

import java.util.*;

import cn.hutool.core.util.StrUtil;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cf.imes.framework.mybatis.core.mapper.BaseMapperX;
import com.cf.imes.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.cf.imes.module.executor.dal.dataobject.order.OrderDO;
import com.cf.imes.module.executor.dal.dataobject.orderItem.OrderItemDO;
import com.cf.imes.module.executor.dal.dataobject.plan.PlanDO;
import com.cf.imes.module.executor.dal.dataobject.planitem.PlanItemDO;
import org.apache.ibatis.annotations.Mapper;
import com.cf.imes.module.executor.controller.admin.plan.vo.*;
import org.apache.ibatis.annotations.Param;

import static com.cf.imes.framework.security.core.util.SecurityFrameworkUtils.getUserOrganId;

/**
 * 生产单开料排单 Mapper
 *
 * @author 晨丰科技
 */
@Mapper
public interface PlanMapper extends BaseMapperX<PlanDO> {

    default PageResult<PlanDO> selectPage(PlanPageReqVO reqVO) {
        if(StrUtil.isNotBlank(reqVO.getCustomer())) {
            return selectJoinPage(reqVO, PlanDO.class, new MPJLambdaWrapperX<PlanDO>()
                    .eq(PlanDO::getOrganId,getUserOrganId())
                    .eqIfPresent(PlanDO::getPlanNo, reqVO.getPlanNo())
                    .eqIfPresent(PlanDO::getSort, reqVO.getSort())
                    .eqIfPresent(PlanDO::getType, reqVO.getType())
                    .eqIfPresent(PlanDO::getStatus, reqVO.getStatus())
                    .eqIfPresent(PlanDO::getIsPay, reqVO.getIsPay())
                    .eqIfPresent(PlanDO::getMachineId, reqVO.getMachineId())
                    .betweenIfPresent(PlanDO::getPlanTime, reqVO.getPlanTime())
                    .eqIfPresent(PlanDO::getOrderNos, reqVO.getOrderNos())
                    .eqIfPresent(PlanDO::getRemark, reqVO.getRemark())
                    .betweenIfPresent(PlanDO::getCreateTime, reqVO.getCreateTime())
                    .eqIfPresent(PlanDO::getOperator, reqVO.getOperator())
                    .betweenIfPresent(PlanDO::getProduceTime, reqVO.getProduceTime())
                    .leftJoin(PlanItemDO.class, PlanItemDO::getPlanId, PlanDO::getId)
                    .leftJoin(OrderItemDO.class, OrderItemDO::getId, PlanItemDO::getItemId)
                    .leftJoin(OrderDO.class, OrderDO::getId, OrderItemDO::getOrderId)
                    .like(OrderDO::getCustomer, reqVO.getCustomer())
                    .orderByAsc(PlanDO::getSort)
                    .orderByDesc(PlanDO::getId)
            );
        }

        return selectPage(reqVO, new LambdaQueryWrapperX<PlanDO>()
                .eq(PlanDO::getOrganId,getUserOrganId())
                .eqIfPresent(PlanDO::getPlanNo, reqVO.getPlanNo())
                .eqIfPresent(PlanDO::getSort, reqVO.getSort())
                .eqIfPresent(PlanDO::getType, reqVO.getType())
                .eqIfPresent(PlanDO::getStatus, reqVO.getStatus())
                .eqIfPresent(PlanDO::getIsPay, reqVO.getIsPay())
                .eqIfPresent(PlanDO::getMachineId, reqVO.getMachineId())
                .betweenIfPresent(PlanDO::getPlanTime, reqVO.getPlanTime())
                .eqIfPresent(PlanDO::getOrderNos, reqVO.getOrderNos())
                .eqIfPresent(PlanDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(PlanDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(PlanDO::getOperator, reqVO.getOperator())
                .betweenIfPresent(PlanDO::getProduceTime, reqVO.getProduceTime())
                .orderByDesc(PlanDO::getSort));
//                .orderByDesc(PlanDO::getId));
    }

    List<PlateOptimize> selectPlateListByPlanId(@Param("planId") Long planId, @Param("organId") Long organId);

    OptimizeParamRespVO getOptimizePlanParam(Long planId);


//
//    default PlanDO selectByOrderId(Long orderId) {
//            return selectOne(new MPJLambdaWrapperX<PlanDO>()
//                    .like(PlanDO::getOrderNos, orderId));
//
//        }
}