package com.czg.service.account.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.czg.account.dto.area.ShopAreaAddDTO;
import com.czg.account.dto.area.ShopAreaEditDTO;
import com.czg.exception.CzgException;
import com.czg.sa.StpKit;
import com.czg.utils.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.account.entity.ShopTableArea;
import com.czg.account.service.ShopTableAreaService;
import com.czg.service.account.mapper.ShopAreaMapper;
import org.springframework.stereotype.Service;

/**
 * 店铺区域 服务层实现。
 *
 * @author zs
 * @since 2025-02-18
 */
@Service
public class ShopTableAreaServiceImpl extends ServiceImpl<ShopAreaMapper, ShopTableArea> implements ShopTableAreaService {

    @Override
    public Page<ShopTableArea> pageInfo(Long shopId, String name) {
        QueryWrapper queryWrapper = new QueryWrapper().eq(ShopTableArea::getShopId, shopId);

        if (StrUtil.isNotBlank(name)) {
            queryWrapper.like(ShopTableArea::getName, name);
        }
        return page(PageUtil.buildPage(), queryWrapper);
    }

    @Override
    public boolean edit(Long shopId, ShopAreaEditDTO shopAreaEditDTO) {
        ShopTableArea tableArea = queryChain().eq(ShopTableArea::getShopId, shopId).eq(ShopTableArea::getId, shopAreaEditDTO.getId()).one();
        if (tableArea == null) {
            throw new CzgException("区域不存在");
        }

        BeanUtil.copyProperties(shopAreaEditDTO, tableArea);
        return updateById(tableArea);
    }

    @Override
    public Boolean add(ShopAreaAddDTO shopAreaAddDTO) {
        ShopTableArea shopTableArea = BeanUtil.copyProperties(shopAreaAddDTO, ShopTableArea.class);
        shopTableArea.setShopId(StpKit.USER.getShopId());
        return save(shopTableArea);
    }
}
