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

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.plate.vo.PlateGoodsRespVO;
import com.cf.imes.module.executor.controller.admin.plate.vo.PlateRespVO;
import com.cf.imes.module.executor.dal.dataobject.goods.GoodsDO;
import com.cf.imes.module.executor.dal.dataobject.orderParts.OrderPartsDO;
import org.apache.ibatis.annotations.Mapper;
import com.cf.imes.module.executor.controller.admin.orderParts.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.formula.functions.T;

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

/**
 * 生产单配件表 order_parts_{N} Mapper
 *
 * @author 晨丰科技
 */
@Mapper
public interface OrderPartsMapper extends BaseMapperX<OrderPartsDO> {

    default PageResult<OrderPartsDO> selectPage(OrderPartsPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<OrderPartsDO>()
                .eq(OrderPartsDO::getOrganId,getUserOrganId())
                .eqIfPresent(OrderPartsDO::getOrderId, reqVO.getOrderId())
                .eqIfPresent(OrderPartsDO::getGoodsId, reqVO.getGoodsId())
                .likeIfPresent(OrderPartsDO::getName, reqVO.getName())
                .eqIfPresent(OrderPartsDO::getMaterial, reqVO.getMaterial())
                .eqIfPresent(OrderPartsDO::getType, reqVO.getType())
                .eqIfPresent(OrderPartsDO::getModel, reqVO.getModel())
                .eqIfPresent(OrderPartsDO::getSpec, reqVO.getSpec())
                .eqIfPresent(OrderPartsDO::getBrand, reqVO.getBrand())
                .eqIfPresent(OrderPartsDO::getFactory, reqVO.getFactory())
                .eqIfPresent(OrderPartsDO::getUnit, reqVO.getUnit())
                .eqIfPresent(OrderPartsDO::getPrice, reqVO.getPrice())
                .eqIfPresent(OrderPartsDO::getIsComposite, reqVO.getIsComposite())
                .eqIfPresent(OrderPartsDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(OrderPartsDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(OrderPartsDO::getId));
    }

    default OrderPartsImportRespVO importOrderPartsList(List<OrderPartsSaveReqVO> importOrderParts) {
        OrderPartsImportRespVO respVO = OrderPartsImportRespVO.builder().createOrderParts(new ArrayList<OrderPartsSaveReqVO>())
                .updateOrderParts(new ArrayList<>()).failureOrderParts(new LinkedHashMap<>()).build();
        importOrderParts.forEach(orderParts -> {
            try{
                OrderPartsDO orderPartsDO = BeanUtils.toBean(orderParts, OrderPartsDO.class);
                insert(orderPartsDO);
                orderParts.setId(orderPartsDO.getId());
                respVO.getCreateOrderParts().add(orderParts);
            }catch (ServiceException ex){
                respVO.getFailureOrderParts().put(orderParts.getName(), ex.getMessage());
            }
        });
        System.err.println("OrderPartsImportRespVO " + respVO);
        return respVO;
    }

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

    List<PartGoodsRespVO> selectPartDetail(@Param("orderId") Long orderId, @Param("organId") Long organId, @Param("deleted") Integer deleted);// 配件明细

    List<PartGoodsRespVO> selectPartAll(@Param("orderId") Long orderId, @Param("organId") Long organId, @Param("deleted") Integer deleted);// 配件汇总


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

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