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

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.pojo.PageResult;
import com.cf.imes.framework.mybatis.core.mapper.BaseMapperX;
import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cf.imes.module.executor.controller.admin.goods.vo.GoodsPageReqVO;
import com.cf.imes.module.executor.controller.admin.goods.vo.GoodsRespVO;
import com.cf.imes.module.executor.controller.admin.order.vo.order.OrderGoodsPlateIds;
import com.cf.imes.module.executor.controller.admin.order.vo.order.OrderPlanPlateNumAndAreaRespVO;
import com.cf.imes.module.executor.controller.admin.plan.bo.OrderRoomBodyList;
import com.cf.imes.module.executor.controller.admin.plan.vo.GoodsReqVO;
import com.cf.imes.module.executor.controller.admin.plan.vo.PlateInfoVO;
import com.cf.imes.module.executor.controller.admin.plan.vo.PlateResList;
import com.cf.imes.module.executor.dal.dataobject.goods.GoodsDO;
import com.cf.imes.module.executor.dal.dataobject.goods.OrderGoodsPlateNumAndAreaDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.Collection;
import java.util.List;

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

/**
 * 订单商品 Mapper
 *
 * @author 晨丰科技
 */
@Mapper
public interface GoodsMapper extends BaseMapperX<GoodsDO> {

    default PageResult<GoodsDO> selectPage(GoodsPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<GoodsDO>()
                .eq(GoodsDO::getOrganId, getUserOrganId())
                .eqIfPresent(GoodsDO::getOrderId, reqVO.getOrderId())
                .eqIfPresent(GoodsDO::getRawGoodsId, reqVO.getRawGoodsId())
                .eqIfPresent(GoodsDO::getGoodsId, reqVO.getGoodsId())
                .likeIfPresent(GoodsDO::getGoodsName, reqVO.getGoodsName())
                .eqIfPresent(GoodsDO::getMaterial, reqVO.getMaterial())
                .eqIfPresent(GoodsDO::getColor, reqVO.getColor())
                .eqIfPresent(GoodsDO::getWidth, reqVO.getWidth())
                .eqIfPresent(GoodsDO::getHeight, reqVO.getHeight())
                .eqIfPresent(GoodsDO::getThickness, reqVO.getThickness())
                .eqIfPresent(GoodsDO::getPrice, reqVO.getPrice())
                .eqIfPresent(GoodsDO::getBrand, reqVO.getBrand())
                .eqIfPresent(GoodsDO::getSpec, reqVO.getSpec())
                .eqIfPresent(GoodsDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(GoodsDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(GoodsDO::getId));
    }


    List<GoodsReqVO> selectGoodsList(@Param("orderId") Long orderId, @Param("organId") Long organId);


    List<GoodsReqVO> selectGoodsListByOrderIds(@Param("planId") Long planId, @Param("organId") Long organId);


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

    // 通过orderId查找板材
    default List<GoodsDO> selectGoodsByOrderId(Long orderId, Long organId) {
        return selectList(new LambdaQueryWrapperX<GoodsDO>()
                .eq(GoodsDO::getOrderId, orderId)
                .eq(GoodsDO::getDeleted, 0)
                .eq(GoodsDO::getOrganId, organId));
    }

    // 删板材，批量
    void updateDeletedById(@Param("orderId") Long orderId,@Param("ids") List<Long> ids , @Param("organId") Long organId);

    void deletedById(@Param("ids") List<Long> ids, @Param("organId") Long organId);




    default List<GoodsDO> selectListByGoodsId(List<Long> orderIds,List<String> goodsIds, Long organId) {
        return selectList(new LambdaQueryWrapperX<GoodsDO>()
                .eq(GoodsDO::getOrganId, organId)
                .inIfPresent(GoodsDO::getOrderId,orderIds)
                .in(GoodsDO::getGoodsId, goodsIds)
                .eq(GoodsDO::getDeleted, false));

    }


    List<GoodsDO> selectListByGoodsIdList(@Param("goodsId") List<String> goodsId,@Param("organId")  Long organId);



    // 根据订单删除
    default int deleteByOrderId(Long orderId, Long organId) {
        return delete(new LambdaQueryWrapperX<GoodsDO>()
                .eq(GoodsDO::getOrganId, organId)
                .eqIfPresent(GoodsDO::getOrderId, orderId));
    }
    // 批量删除
    default int deleteBatch(Collection<?> orderId) {
        return delete(new LambdaQueryWrapperX<GoodsDO>()
                .in(GoodsDO::getOrderId, orderId));
    }





    List<GoodsDO> selectPlanGoodsDataSource(@Param("filed") String filed,@Param("planId") Long planId ,@Param("organId")  Long organId);



    GoodsDO selectGoodsId(@Param("orderId") Long orderId,@Param("goodsId") String goodsId,@Param("organId") Long organId);


    List<PlateInfoVO> selectOrderGoodsPlateList(@Param("planIds") List<Long> planIds, @Param("organId") Long organId);


    default List<GoodsDO> selectPlanGoods(Long planId, Long organId) {
        return selectList(new LambdaQueryWrapperX<GoodsDO>()
                .eq(GoodsDO::getOrganId, organId)
                .eq(GoodsDO::getDeleted, false)
                .eqIfPresent(GoodsDO::getPlanId,planId));

    }




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

    List<GoodsReqVO> selectGoodsAndRemainPlate(@Param("orderIds") List<Long> orderIds,@Param("goodsIdList") List<String> goodsIdList,@Param("organId") Long organId);

    List<PlateResList> selectPlateListByPlanId(@Param("planIds") List<Long> planIds, @Param("organId") Long organId);


    List<OrderRoomBodyList> selectNoPlanRoomBody(@Param("orderIds") List<Long> orderIds, @Param("organId") Long organId);

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

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

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

    /**
     * 查询订单商品编号下已删除的商品
     *
     * @param orderId
     * @param goodsIds
     * @param organId
     * @return
     */
    default List<GoodsDO> selectGoodsListByStatus(Long orderId, Collection<Long> goodsIds, Long organId) {
        return selectList(new LambdaQueryWrapperX<GoodsDO>()
                .eq(GoodsDO::getOrganId, organId)
                .eq(GoodsDO::getDeleted, true)
                .eqIfPresent(GoodsDO::getOrderId, orderId)
                .inIfPresent(GoodsDO::getId, goodsIds));

    }


    List<GoodsDO> selectNoPlanGoodsList(@Param("orderId") Long orderId,@Param("goodsIds")  List<Long> goodsIds,@Param("organId")  Long organId);



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


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


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

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

    List<OrderGoodsPlateIds> selectPlateGoodIds(@Param("orderIds") List<Long> orderIds, @Param("organId")  Long organId);


    List<GoodsRespVO> selectNoOrderPlanGoods(@Param("ids") List<Long> ids, @Param("organId") Long organId);

    List<Long> selectInsertGoodsIds(@Param("orderIds") List<Long> orderIds,@Param("goodsIds") List<Long> goodsIds,@Param("organId") Long organId);

    // 通过orderId查找板材
    default List<GoodsDO> selectGoodsByOrderId(Long orderId, Long organId, Integer deleted) {
        return selectList(new LambdaQueryWrapperX<GoodsDO>()
                .eq(GoodsDO::getOrderId, orderId)
                .eq(GoodsDO::getDeleted, deleted)
                .eq(GoodsDO::getOrganId, organId));
    }

    /**
     * 查询订单材质下已排单的板件数量和面积
     *
     * @param orderGoodsId
     * @return
     */
    OrderPlanPlateNumAndAreaRespVO selectPlannedPlateNumAndArea(@Param("orderGoodsId") Long orderGoodsId);

    /**
     * 批量统计已排单板件数量和面积
     *
     * @param orderGoodsIds 商品id列表
     * @return 每个商品的已排板件数量和面积
     */
    List<OrderGoodsPlateNumAndAreaDO> selectPlannedPlateNumAndAreaGroup(@Param("orderGoodsIds") Collection<Long> orderGoodsIds);

    /**
     * 查询订单材质下的板件数量和面积
     *
     * @param orderGoodsIds
     * @return
     */
    List<OrderGoodsPlateNumAndAreaDO> selectPlateNumAndArea(@Param("orderGoodsIds") Collection<Long> orderGoodsIds, @Param("delete") Boolean delete);

    /**
     * 根据板材id和订单id查询父订单id
     *
     * @param goodsId
     * @param orderIds
     * @return
     */
    List<Long> getParentOrderIdsByGoodsIds(@Param("goodsIds") List<String> goodsId, @Param("orderIds") List<Long> orderIds);

    /**
     * 物理删除订单材质
     *
     * @return
     */
    int physicalDeleteByOrderIds(@Param("ids") List<Long> ids);
}