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

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.order.vo.order.OrderPageReqVO;
import com.cf.imes.module.executor.controller.admin.plan.vo.OrderRespVOCopy;
import com.cf.imes.module.executor.dal.dataobject.order.OrderDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

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

/**
 * 生产单表 order_{N} Mapper
 *
 * @author 晨丰科技
 */
@Mapper
public interface OrderMapper extends BaseMapperX<OrderDO> {

    default PageResult<OrderDO> selectPage(OrderPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<OrderDO>()
                .eqIfPresent(OrderDO::getParentNo, reqVO.getParentNo())
                .betweenIfPresent(OrderDO::getDeliveryDate, reqVO.getDeliveryDate())
                .betweenIfPresent(OrderDO::getOrderDate, reqVO.getOrderDate())
                .eqIfPresent(OrderDO::getId, reqVO.getId())
                .eqIfPresent(OrderDO::getOrderType, reqVO.getOrderType())
                .eqIfPresent(OrderDO::getOrderSort, reqVO.getOrderSort())
                .eqIfPresent(OrderDO::getDataType, reqVO.getDataType())
                .eqIfPresent(OrderDO::getStatus, reqVO.getStatus())
                .likeIfPresent(OrderDO::getCustomOrderNo, reqVO.getCustomOrderNo())
                .eqIfPresent(OrderDO::getCustomer, reqVO.getCustomer())
                .likeIfPresent(OrderDO::getAddress, reqVO.getAddress())
                .eqIfPresent(OrderDO::getPhoneNumber, reqVO.getPhoneNumber())
                .likeIfPresent(OrderDO::getDealer, reqVO.getDealer())
                .eqIfPresent(OrderDO::getDealerPhoneNumber, reqVO.getDealerPhoneNumber())
                .eqIfPresent(OrderDO::getSalesman, reqVO.getSalesman())
                .eqIfPresent(OrderDO::getSplitter, reqVO.getSplitter())
                .eqIfPresent(OrderDO::getRemark, reqVO.getRemark())
                .eqIfPresent(OrderDO::getOrganId, reqVO.getOrganId())
                .eqIfPresent(OrderDO::getDeleted, reqVO.getDeleted())
                .betweenIfPresent(OrderDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(OrderDO::getId));
    }


    IPage<OrderRespVOCopy> selectOrderPage(@Param("page") IPage page, @Param(Constants.WRAPPER) Wrapper<OrderDO> wrapper);


    Map<String, Object> selectDynamicSqlString(@Param("sqlStr") String sqlStr);


    // 查找生产单
    default List<OrderDO> selectOrder(Long id,Long organId) {
        return selectList(new LambdaQueryWrapperX<OrderDO>()
                .eqIfPresent(OrderDO::getId, id)
                .eqIfPresent(OrderDO::getOrganId, organId));
    }

    // 修改生产单状态 单个
    default int updateOrderStatus(Long id, Integer status,Long organId) {
        return update(new LambdaUpdateWrapper<OrderDO>()
                .set(OrderDO::getStatus, status)
                .eq(OrderDO::getId, id)
                .eq(OrderDO::getDeleted, 0)
                .eq(OrderDO::getOrganId, organId));
    }
    // 查询单个生产单
    default OrderDO selectOrderOne(Long id,Long organId) {
        return selectOne(new LambdaQueryWrapper<OrderDO>()
                .eq(OrderDO::getId, id)
                .eq(OrderDO::getOrganId, organId));
    }

    // 删除状态为条件，查询生产单
    default OrderDO selectOrderOne(Long id,Long organId,Integer deleted) {
        return selectOne(new LambdaQueryWrapper<OrderDO>()
                .eq(OrderDO::getId, id)
                .eq(OrderDO::getOrganId, organId)
                .eq(OrderDO::getDeleted, deleted));
    }
    // 批量查询生产单
    default List<OrderDO> selectOrderList(Collection<Long> ids, Long organId) {
        return selectList(new LambdaQueryWrapper<OrderDO>()
                .in(OrderDO::getId, ids)
                .eq(OrderDO::getOrganId, organId));
    }

    // 修改删除状态
    default int updateOrderDeleted(Long id, Integer deleted,Long organId) {
        return update(new LambdaUpdateWrapper<OrderDO>()
                .set(OrderDO::getDeleted, deleted)
                .eq(OrderDO::getId, id)
                .eq(OrderDO::getOrganId, organId));
    }
}
