package com.cpxt.biz.service.impl;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cpxt.biz.domain.BizCustomerUser;
import com.cpxt.biz.domain.SysArea;
import com.cpxt.biz.mapper.SysAreaMapper;
import com.cpxt.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cpxt.biz.mapper.BizCustomerWarehouseMapper;
import com.cpxt.biz.domain.BizCustomerWarehouse;
import com.cpxt.biz.service.IBizCustomerWarehouseService;

/**
 * 仓库Service业务层处理
 * 
 * @author YIN
 * @date 2024-12-16
 */
@Service
public class BizCustomerWarehouseServiceImpl implements IBizCustomerWarehouseService 
{
    @Autowired
    private BizCustomerWarehouseMapper bizCustomerWarehouseMapper;

    @Autowired
    private SysAreaMapper sysAreaMapper;

    /**
     * 查询仓库
     * 
     * @param id 仓库主键
     * @return 仓库
     */
    @Override
    public BizCustomerWarehouse selectBizCustomerWarehouseById(Long id)
    {
        BizCustomerWarehouse bizCustomerWarehouse = bizCustomerWarehouseMapper.selectById(id);
        if (ObjectUtil.isNotEmpty(bizCustomerWarehouse.getArea())){
            SysArea sysArea = sysAreaMapper.selectById(bizCustomerWarehouse.getArea());
            if (sysArea != null){
                bizCustomerWarehouse.setAreaFullName(sysArea.getFullName());
            }
        }
        return bizCustomerWarehouse;
    }

    /**
     * 查询仓库列表（分页）
     *
     * @param bizCustomerWarehouse 仓库
     * @return 仓库
     */
    @Override
    public Page<BizCustomerWarehouse> selectBizCustomerWarehousePage(BizCustomerWarehouse bizCustomerWarehouse, int pageNum, int pageSize) {
        LambdaQueryWrapper<BizCustomerWarehouse> queryWrapper = buildQueryWrapper(bizCustomerWarehouse);
        return bizCustomerWarehouseMapper.selectPage(new Page<>(pageNum,pageSize),queryWrapper);
    }

    /**
     * 查询仓库列表
     * 
     * @param bizCustomerWarehouse 仓库
     * @return 仓库
     */
    @Override
    public List<BizCustomerWarehouse> selectBizCustomerWarehouseList(BizCustomerWarehouse bizCustomerWarehouse)
    {
        LambdaQueryWrapper<BizCustomerWarehouse> queryWrapper = buildQueryWrapper(bizCustomerWarehouse);
        return bizCustomerWarehouseMapper.selectList(queryWrapper);
    }

    private LambdaQueryWrapper<BizCustomerWarehouse> buildQueryWrapper(BizCustomerWarehouse bizCustomerWarehouse) {
        LambdaQueryWrapper<BizCustomerWarehouse> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(bizCustomerWarehouse.getCustomerId())){
            queryWrapper.eq(BizCustomerWarehouse::getCustomerId, bizCustomerWarehouse.getCustomerId());
        }
        if (ObjectUtil.isNotEmpty(bizCustomerWarehouse.getCustomerName())){
            queryWrapper.like(BizCustomerWarehouse::getCustomerName, bizCustomerWarehouse.getCustomerName());
        }
        if (ObjectUtil.isNotEmpty(bizCustomerWarehouse.getName())){
            queryWrapper.like(BizCustomerWarehouse::getName, bizCustomerWarehouse.getName());
        }
        if (ObjectUtil.isNotEmpty(bizCustomerWarehouse.getLinkman())){
            queryWrapper.like(BizCustomerWarehouse::getLinkman, bizCustomerWarehouse.getLinkman());
        }
        if (ObjectUtil.isNotEmpty(bizCustomerWarehouse.getLinkphone())){
            queryWrapper.like(BizCustomerWarehouse::getLinkphone, bizCustomerWarehouse.getLinkphone());
        }
        if (ObjectUtil.isNotEmpty(bizCustomerWarehouse.getAddress())){
            queryWrapper.like(BizCustomerWarehouse::getAddress, bizCustomerWarehouse.getAddress());
        }
        queryWrapper.eq(BizCustomerWarehouse::getStatus,1);
        return queryWrapper;
    }

    /**
     * 新增仓库
     * 
     * @param bizCustomerWarehouse 仓库
     * @return 结果
     */
    @Override
    public int insertBizCustomerWarehouse(BizCustomerWarehouse bizCustomerWarehouse)
    {
        return bizCustomerWarehouseMapper.insert(bizCustomerWarehouse);
    }

    /**
     * 修改仓库
     * 
     * @param bizCustomerWarehouse 仓库
     * @return 结果
     */
    @Override
    public int updateBizCustomerWarehouse(BizCustomerWarehouse bizCustomerWarehouse)
    {
        return bizCustomerWarehouseMapper.updateById(bizCustomerWarehouse);
    }

    /**
     * 批量删除仓库
     * 
     * @param ids 需要删除的仓库主键
     * @return 结果
     */
    @Override
    public int deleteBizCustomerWarehouseByIds(Long[] ids)
    {
        UpdateWrapper<BizCustomerWarehouse> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", (Object[]) ids);
        updateWrapper.set("status","2"); //状态改为删除
        return bizCustomerWarehouseMapper.update(null,updateWrapper);
    }

    /**
     * 删除仓库信息
     * 
     * @param id 仓库主键
     * @return 结果
     */
    @Override
    public int deleteBizCustomerWarehouseById(Long id)
    {
        UpdateWrapper<BizCustomerWarehouse> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("status","2"); //状态改为删除
        return bizCustomerWarehouseMapper.update(null,updateWrapper);
    }


}
