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

import java.math.BigDecimal;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.cf.imes.framework.common.exception.ServiceException;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.util.object.BeanUtils;
import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cf.imes.framework.mybatis.core.mapper.BaseMapperX;
import com.cf.imes.module.executor.controller.admin.plan.vo.*;
import com.cf.imes.module.executor.dal.dataobject.orderGroup.OrderGroupDO;
import com.cf.imes.module.executor.dal.dataobject.orderParts.OrderPartsDO;
import com.cf.imes.module.executor.dal.dataobject.plate.PlateDO;
import org.apache.ibatis.annotations.Mapper;
import com.cf.imes.module.executor.controller.admin.plate.vo.*;
import org.apache.ibatis.annotations.Param;

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

/**
 * 生产单板件 Mapper
 *
 * @author 晨丰科技
 */
@Mapper
public interface PlateMapper extends BaseMapperX<PlateDO> {

    default PageResult<PlateDO> selectPage(PlatePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<PlateDO>()
                .eq(PlateDO::getOrganId,getUserOrganId())
                .eqIfPresent(PlateDO::getOrderId, reqVO.getOrderId())
                .likeIfPresent(PlateDO::getName, reqVO.getName())
                .eqIfPresent(PlateDO::getType, reqVO.getType())
                .eqIfPresent(PlateDO::getGoodsId, reqVO.getGoodsId())
                .eqIfPresent(PlateDO::getWidth, reqVO.getWidth())
                .eqIfPresent(PlateDO::getHeight, reqVO.getHeight())
                .eqIfPresent(PlateDO::getThickness, reqVO.getThickness())
                .eqIfPresent(PlateDO::getSplitWidth, reqVO.getSplitWidth())
                .eqIfPresent(PlateDO::getSplitHeight, reqVO.getSplitHeight())
                .eqIfPresent(PlateDO::getSplitThickness, reqVO.getSplitThickness())
                .eqIfPresent(PlateDO::getSealLeft, reqVO.getSealLeft())
                .eqIfPresent(PlateDO::getSealRight, reqVO.getSealRight())
                .eqIfPresent(PlateDO::getSealUp, reqVO.getSealUp())
                .eqIfPresent(PlateDO::getSealDown, reqVO.getSealDown())
                .eqIfPresent(PlateDO::getArea, reqVO.getArea())
                .eqIfPresent(PlateDO::getTexture, reqVO.getTexture())
                .eqIfPresent(PlateDO::getHoleFace, reqVO.getHoleFace())
                .eqIfPresent(PlateDO::getHoleArrange, reqVO.getHoleArrange())
                .eqIfPresent(PlateDO::getUnregularPointCount, reqVO.getUnregularPointCount())
                .eqIfPresent(PlateDO::getFrontHoleCount, reqVO.getFrontHoleCount())
                .eqIfPresent(PlateDO::getBackHoleCount, reqVO.getBackHoleCount())
                .eqIfPresent(PlateDO::getSideHoleCount, reqVO.getSideHoleCount())
                .eqIfPresent(PlateDO::getFrontModelCount, reqVO.getFrontModelCount())
                .eqIfPresent(PlateDO::getBackModelCount, reqVO.getBackModelCount())
                .eqIfPresent(PlateDO::getIsDoor, reqVO.getIsDoor())
                .eqIfPresent(PlateDO::getOpenDoorType, reqVO.getOpenDoorType())
                .eqIfPresent(PlateDO::getOffsetX, reqVO.getOffsetX())
                .eqIfPresent(PlateDO::getOffsetY, reqVO.getOffsetY())
                .eqIfPresent(PlateDO::getIsArcAcross, reqVO.getIsArcAcross())
                .eqIfPresent(PlateDO::getModuleTypeId, reqVO.getModuleTypeId())
                .eqIfPresent(PlateDO::getFilterType, reqVO.getFilterType())
                .eqIfPresent(PlateDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(PlateDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(PlateDO::getId));
    }

//    IPage<PlatePage> selectPlatePage(@Param("page") IPage<PlatePage> page, @Param("orderId")Long organId, @Param("goodsId") String goodsId);
    List<PlatePage> selectPlatePage(/*@Param("page") IPage<PlatePage> page,*/ @Param("orderId")Long orderId,@Param("organId") Long organId/*, @Param("goodsId") String goodsId*/);

    IPage<PlateResList> selectPlateByPlanId(@Param("page") IPage<PlateResList> page, @Param(Constants.WRAPPER) Wrapper<PlateDO> wrapper);

    List<PlateParam> selectPlateList(Long planId);


    IPage<PlateRespVO> selectProductList(@Param("page") IPage<PlateRespVO> page , @Param("orderId")Long orderId, @Param("roomId")Long roomId , @Param("bodyId")Long bodyId,@Param("organId") Long organId);


    Set<Long> selectBatchOrderIdsAndGoodsIds(@Param("itemList") List<PlanSaveReqVO.Item> itemList, @Param("organId") Long organId );

//    PlateDetailRespVO selectPlateNumList(PlateDetailReqVO reqVO);

    List<PlateDetialRespVO> selectPlateDetialList(@Param("orderId") Long orderId,@Param("organId") Long organId);


    List<PlateDetialRespVO> selectPlateDetialListByIds(@Param("orderIds") List<Long> orderIds,@Param("organId") Long organId);


    List<PlateGoodsRespVO> selectPlateGoodsList(@Param("orderId") Long orderId, @Param("organId") Long organId, @Param("deleted") Integer deleted); // 明细

    List<PlateGoodsRespVO> selectPlateGoodsListSummary(@Param("orderId") Long orderId, @Param("organId") Long organId, @Param("deleted") Integer deleted); // 汇总


    // 单个修改删除状态
    default int updateOrderDeleted(Long id, Integer deleted, Long organId) {
        LambdaUpdateWrapper<PlateDO> wrapper = new LambdaUpdateWrapper<PlateDO>()
                .set(PlateDO::getDeleted, deleted)
                .eq(PlateDO::getOrderId, id)
                .eq(PlateDO::getOrganId, organId);
        if (deleted == 0)
            wrapper.eq(PlateDO::getDeleted, 1);
        return update(wrapper);
    }

    void updateDeletedById(@Param("bodyId") Long bodyId, @Param("deleted") Integer deleted, @Param("organId") Long organId);

    int countPlateByOrderId(@Param("orderId") Long orderId, @Param("organId") Long organId, @Param("deleted") Integer deleted);


    BigDecimal selectPlateSize( @Param("orderId") Long orderId);
}