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

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
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.module.executor.controller.admin.plan.vo.GoodsReqVO;
import com.cf.imes.module.executor.dal.dataobject.goods.GoodsDO;
import com.cf.imes.module.executor.dal.dataobject.orderGroup.OrderGroupDO;
import org.apache.ibatis.annotations.Mapper;
import com.cf.imes.module.executor.controller.admin.goods.vo.*;
import org.apache.ibatis.annotations.Param;

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("orderIds") List<Long> orderIds, @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);
    }
}