package com.ruoyi.clock.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.clock.domain.Agent;
import com.ruoyi.clock.exception.AgentExceptionEnum;
import com.ruoyi.clock.exception.ShopExceptionEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.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.springframework.stereotype.Service;
import com.ruoyi.clock.domain.bo.ShopBo;
import com.ruoyi.clock.domain.vo.ShopVo;
import com.ruoyi.clock.domain.Shop;
import com.ruoyi.clock.mapper.ShopMapper;
import com.ruoyi.clock.service.IShopService;

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

/**
 * 门店信息Service业务层处理
 *
 * @author pengc
 * @date 2026-03-04
 */
@RequiredArgsConstructor
@Service
public class ShopServiceImpl implements IShopService {

    private final ShopMapper baseMapper;

    /**
     * 查询门店信息分页
     *
     * @param bo 门店信息
     * @return 门店信息
     */
    @Override
    public TableDataInfo<ShopVo> queryPageList(ShopBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Shop> lqw = buildQueryWrapper(bo);
        Page<ShopVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询门店信息列表
     *
     * @param bo 门店信息
     * @return 门店信息
     */
    @Override
    public List<ShopVo> queryList(ShopBo bo) {
        LambdaQueryWrapper<Shop> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Shop> buildQueryWrapper(ShopBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Shop> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getAgentId() != null, Shop::getAgentId, bo.getAgentId());
        lqw.like(StringUtils.isNotBlank(bo.getShopName()), Shop::getShopName, bo.getShopName());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), Shop::getAddress, bo.getAddress());
        lqw.like(StringUtils.isNotBlank(bo.getContact()), Shop::getContact, bo.getContact());
        lqw.eq(StringUtils.isNotBlank(bo.getPhonenumber()), Shop::getPhonenumber, bo.getPhonenumber());
        return lqw;
    }

    /**
     * 查询门店信息
     *
     * @param shopId 门店信息主键
     * @return 门店信息
     */
    @Override
    public ShopVo queryById(Long shopId){
        return baseMapper.selectVoById(shopId);
    }

    /**
     * 详情门店信息
     *
     * @param shopId 门店信息主键
     * @return 门店信息
     */
    @Override
    public Shop loadById(Long shopId, Boolean tw){
        Shop info = this.baseMapper.selectById(shopId);
        if(ObjectUtil.isEmpty(info) && tw){
            throw new ServiceException(ShopExceptionEnum.SHOP_IS_NOT_EXISTS);
        }
        return info;
    }

    /**
     * 新增门店信息
     *
     * @param bo 门店信息
     * @return 结果
     */
    @Override
    public Boolean insertByBo(ShopBo bo) {
        validEntityBeforeSave(bo);
        Shop add = BeanUtil.toBean(bo, Shop.class);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setShopId(add.getShopId());
        }
        return flag;
    }

    /**
     * 修改门店信息
     *
     * @param bo 门店信息
     * @return 结果
     */
    @Override
    public Boolean updateByBo(ShopBo bo) {
        validEntityBeforeSave(bo);
        Shop shop = baseMapper.selectById(bo.getShopId());
        Shop update = BeanCopyUtils.copy(bo, shop);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param bo 实体类数据
     */
    private void validEntityBeforeSave(ShopBo bo){
        //TODO 做一些数据校验,如唯一约束
        Shop shop = getShopByName(bo.getShopName());
        if(ObjectUtil.isNotNull(shop)&& !shop.getShopId().equals(bo.getShopId())){
            throw new ServiceException(ShopExceptionEnum.SHOP_REPEAT);
        }
    }

    public Shop getShopByName(String shopName) {
        return baseMapper.selectOne(new LambdaQueryWrapper<Shop>().eq(Shop::getShopName,shopName)
            .last("limit 1"));
    }


    /**
     * 批量删除门店信息
     *
     * @param ids 需要删除的门店信息主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

}
