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.BizCustomerRoute;
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.BizCustomerShopMapper;
import com.cpxt.biz.domain.BizCustomerShop;
import com.cpxt.biz.service.IBizCustomerShopService;

/**
 * 门店Service业务层处理
 * 
 * @author YIN
 * @date 2024-12-16
 */
@Service
public class BizCustomerShopServiceImpl implements IBizCustomerShopService 
{
    @Autowired
    private BizCustomerShopMapper bizCustomerShopMapper;

    @Autowired
    private SysAreaMapper sysAreaMapper;

    /**
     * 查询门店
     * 
     * @param id 门店主键
     * @return 门店
     */
    @Override
    public BizCustomerShop selectBizCustomerShopById(Long id)
    {
        BizCustomerShop bizCustomerShop = bizCustomerShopMapper.selectById(id);
        if (ObjectUtil.isNotEmpty(bizCustomerShop.getArea())){
            SysArea sysArea = sysAreaMapper.selectById(bizCustomerShop.getArea());
            if (sysArea != null){
                bizCustomerShop.setAreaFullName(sysArea.getFullName());
            }
        }
        return bizCustomerShop;
    }

    /**
     * 查询门店列表（分页）
     *
     * @param bizCustomerShop 门店
     * @return 门店
     */
    @Override
    public Page<BizCustomerShop> selectBizCustomerShopPage(BizCustomerShop bizCustomerShop, int pageNum, int pageSize) {
        LambdaQueryWrapper<BizCustomerShop> queryWrapper = buildQueryWrapper(bizCustomerShop);
        return bizCustomerShopMapper.selectPage(new Page<>(pageNum,pageSize),queryWrapper);
    }

    /**
     * 查询门店列表
     * 
     * @param bizCustomerShop 门店
     * @return 门店
     */
    @Override
    public List<BizCustomerShop> selectBizCustomerShopList(BizCustomerShop bizCustomerShop)
    {
        LambdaQueryWrapper<BizCustomerShop> queryWrapper = buildQueryWrapper(bizCustomerShop);

        return bizCustomerShopMapper.selectList(queryWrapper);
    }

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

    /**
     * 新增门店
     * 
     * @param bizCustomerShop 门店
     * @return 结果
     */
    @Override
    public int insertBizCustomerShop(BizCustomerShop bizCustomerShop)
    {
        return bizCustomerShopMapper.insert(bizCustomerShop);
    }

    /**
     * 修改门店
     * 
     * @param bizCustomerShop 门店
     * @return 结果
     */
    @Override
    public int updateBizCustomerShop(BizCustomerShop bizCustomerShop)
    {
        return bizCustomerShopMapper.updateById(bizCustomerShop);
    }

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

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


}
