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.cpxt.biz.domain.BizCustomer;
import com.cpxt.biz.domain.BizCustomerRouteShop;
import com.cpxt.biz.mapper.BizCustomerRouteShopMapper;
import com.cpxt.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cpxt.biz.mapper.BizCustomerRouteMapper;
import com.cpxt.biz.domain.BizCustomerRoute;
import com.cpxt.biz.service.IBizCustomerRouteService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 路线Service业务层处理
 * 
 * @author YIN
 * @date 2024-12-16
 */
@Service
public class BizCustomerRouteServiceImpl implements IBizCustomerRouteService 
{
    @Autowired
    private BizCustomerRouteMapper bizCustomerRouteMapper;

    @Autowired
    private BizCustomerRouteShopMapper bizCustomerRouteShopMapper;

    /**
     * 查询路线
     * 
     * @param id 路线主键
     * @return 路线
     */
    @Override
    public BizCustomerRoute selectBizCustomerRouteById(Long id)
    {
        BizCustomerRoute bizCustomerRoute = bizCustomerRouteMapper.selectById(id);
        // 根据路线id查询关联的店铺id
        List<Long> ids = bizCustomerRouteShopMapper.selectShopIdListByRouteId(id);
        bizCustomerRoute.setShops(ids);
        return bizCustomerRoute;
    }

    /**
     * 查询路线列表
     * 
     * @param bizCustomerRoute 路线
     * @return 路线
     */
    @Override
    public List<BizCustomerRoute> selectBizCustomerRouteList(BizCustomerRoute bizCustomerRoute)
    {
        LambdaQueryWrapper<BizCustomerRoute> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(bizCustomerRoute.getCustomerId()))
            queryWrapper.eq(BizCustomerRoute::getCustomerId, bizCustomerRoute.getCustomerId());
        queryWrapper.eq(BizCustomerRoute::getStatus,1);
        return bizCustomerRouteMapper.selectList(queryWrapper);
    }

    /**
     * 新增路线
     * 
     * @param bizCustomerRoute 路线
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBizCustomerRoute(BizCustomerRoute bizCustomerRoute)
    {
        int insert = bizCustomerRouteMapper.insert(bizCustomerRoute);
        // 新增完路线将路线和店铺关联起来入关联表
        List<Long> shops = bizCustomerRoute.getShops();
        for (int i = 0; i < shops.size(); i++) {
            BizCustomerRouteShop bizCustomerRouteShop = new BizCustomerRouteShop();
            bizCustomerRouteShop.setRouteId(bizCustomerRoute.getId());
            bizCustomerRouteShop.setShopId(shops.get(i));
            bizCustomerRouteShop.setSort(i);
            bizCustomerRouteShopMapper.insert(bizCustomerRouteShop);
        }
        return insert;
    }

    /**
     * 修改路线
     * 
     * @param bizCustomerRoute 路线
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBizCustomerRoute(BizCustomerRoute bizCustomerRoute)
    {
        // 修改完路线同步修改到路线和店铺的关联表
        List<Long> shops = bizCustomerRoute.getShops();
        // 先删除再新增
        bizCustomerRouteShopMapper.deleteByRouteId(bizCustomerRoute.getId());
        for (int i = 0; i < shops.size(); i++) {
            BizCustomerRouteShop bizCustomerRouteShop = new BizCustomerRouteShop();
            bizCustomerRouteShop.setRouteId(bizCustomerRoute.getId());
            bizCustomerRouteShop.setShopId(shops.get(i));
            bizCustomerRouteShop.setSort(i);
            bizCustomerRouteShopMapper.insert(bizCustomerRouteShop);
        }
        return bizCustomerRouteMapper.updateById(bizCustomerRoute);
    }

    /**
     * 批量删除路线
     * 
     * @param ids 需要删除的路线主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBizCustomerRouteByIds(Long[] ids)
    {
        // 同步删除路线和店铺的关联表
        for (Long id : ids) {
            bizCustomerRouteShopMapper.deleteByRouteId(id);
        }
        UpdateWrapper<BizCustomerRoute> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", (Object[]) ids);
        updateWrapper.set("status","2"); //状态改为删除
        return bizCustomerRouteMapper.update(null,updateWrapper);
    }

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