package org.dromara.zhishu.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Service;
import org.dromara.zhishu.domain.bo.DepotOrderBo;
import org.dromara.zhishu.domain.vo.DepotOrderVo;
import org.dromara.zhishu.domain.DepotOrder;
import org.dromara.zhishu.mapper.DepotOrderMapper;
import org.dromara.zhishu.service.IDepotOrderService;

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

/**
 * 仓库订单信息Service业务层处理
 *
 * @author yxy
 * @date 2025-06-03
 */
@RequiredArgsConstructor
@Service
public class DepotOrderServiceImpl implements IDepotOrderService {

   private final DepotOrderMapper baseMapper;

    /**
     * 查询仓库订单信息
     *
     * @param id 主键
     * @return 仓库订单信息
     */
    @Override
    public DepotOrderVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询仓库订单信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 仓库订单信息分页列表
     */
    @Override
    public TableDataInfo<DepotOrderVo> queryPageList(DepotOrderBo bo, PageQuery pageQuery) {



        bo.setPageNum(pageQuery.getPageNum() - 1);
        bo.setPageSize(pageQuery.getPageSize());
        bo.setDelFlag("0");
        //LoginHelper.getUserId().toString()
        bo.setUserId(Long.valueOf(LoginHelper.getUserId().toString()));
        List<DepotOrderVo> depotOrderVoList = baseMapper.selectListPage(bo);


        Page<DepotOrderVo> result = new Page<>();
        result.setCurrent(pageQuery.getPageNum());
        result.setSize(pageQuery.getPageSize());
        result.setTotal(baseMapper.selectTotal(bo));
        result.setRecords(depotOrderVoList);


        return TableDataInfo.build(result);
    }

    @Override
    public DepotOrderVo selectDepotOrderById(String id){
        return baseMapper.selectDepotOrderById(id);
    }

    /**
     * 查询符合条件的仓库订单信息列表
     *
     * @param bo 查询条件
     * @return 仓库订单信息列表
     */
    @Override
    public List<DepotOrderVo> queryList(DepotOrderBo bo) {
        LambdaQueryWrapper<DepotOrder> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DepotOrder> buildQueryWrapper(DepotOrderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DepotOrder> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(DepotOrder::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getShopOrderId()), DepotOrder::getShopOrderId, bo.getShopOrderId());
        lqw.eq(bo.getUserId() != null, DepotOrder::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getItemList()), DepotOrder::getItemList, bo.getItemList());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), DepotOrder::getStatus, bo.getStatus());
        lqw.eq(bo.getCreateBy() != null, DepotOrder::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, DepotOrder::getCreateTime, bo.getCreateTime());
        return lqw;
    }

    /**
     * 新增仓库订单信息
     *
     * @param bo 仓库订单信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(DepotOrderBo bo) {
        DepotOrder add = MapstructUtils.convert(bo, DepotOrder.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改仓库订单信息
     *
     * @param bo 仓库订单信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(DepotOrderBo bo) {
        DepotOrder update = MapstructUtils.convert(bo, DepotOrder.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DepotOrder entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除仓库订单信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
