package com.czg.service.market.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.czg.account.service.ShopInfoService;
import com.czg.constants.ShopSwitchTypeEnum;
import com.czg.market.dto.GbWareDTO;
import com.czg.market.dto.GbWareQueryParamDTO;
import com.czg.market.entity.GbWare;
import com.czg.market.service.GbWareService;
import com.czg.sa.StpKit;
import com.czg.service.market.mapper.GbWareMapper;
import com.czg.utils.AssertUtil;
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 org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * 拼团商品 服务层实现。
 *
 * @author ww
 * @since 2025-12-15
 */
@Service
public class GbWareServiceImpl extends ServiceImpl<GbWareMapper, GbWare> implements GbWareService {
    @DubboReference
    private ShopInfoService shopInfoService;


    @Override
    public Page<GbWare> getGbWarePage(GbWareQueryParamDTO param, Long shopId, boolean isAdmin) {
        if (!isAdmin && !shopInfoService.checkSwitch(shopId, ShopSwitchTypeEnum.GROUP_BUY)) {
            return PageUtil.emptyPage();
        }
        Long mainShopId = shopInfoService.getMainIdByShopId(shopId);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(GbWare::getIsDel, 0)
                .eq(GbWare::getOnlineStatus, param.getOnlineStatus())
                .eq(GbWare::getGroupPeopleNum, param.getGroupPeopleNum())
                .like(GbWare::getWareName, param.getWareName())
                .orderBy(GbWare::getCreateTime).desc();
        if (StrUtil.isNotBlank(param.getStartTime()) || StrUtil.isNotBlank(param.getEndTime())) {
            queryWrapper.between(GbWare::getCreateTime, param.getStartTime(), param.getEndTime());
        }

        queryWrapper.and(q -> {
            q.eq(GbWare::getShopId, shopId).or(q1 -> {
                q1.eq(GbWare::getUseShopType, "all").eq(GbWare::getShopId, mainShopId);
            }).or(q2 -> {
                q2.eq(GbWare::getUseShopType, "custom").and("FIND_IN_SET( " + shopId + ", use_shops ) > 0");
            });
        });
        Page<GbWare> page = page(Page.of(param.getPage(), param.getSize()), queryWrapper);
        if (!isAdmin) {
            page.getRecords().forEach(item -> item.setShopId(shopId));
        }
        return page;
    }

    @Override
    public boolean addGbWare(GbWareDTO param) {
        if ("custom".equals(param.getUseShopType())) {
            AssertUtil.isBlank(param.getUseShops(), "请配置可用门店");
        }
        GbWare gbWare = BeanUtil.toBean(param, GbWare.class);
        gbWare.setShopId(StpKit.USER.getShopId());
        return save(gbWare);
    }


    @Override
    public boolean updateGbWareById(GbWareDTO param) {
        checkStatus(param.getId());
        if ("custom".equals(param.getUseShopType())) {
            AssertUtil.isBlank(param.getUseShops(), "请配置可用门店");
        }
        GbWare gbWare = BeanUtil.toBean(param, GbWare.class);
        return updateById(gbWare);
    }

    @Override
    public boolean deleteGbWare(Long id) {
        checkStatus(id);
        return remove(query().eq(GbWare::getId, id).eq(GbWare::getShopId, StpKit.USER.getShopId()));
    }

    @Override
    public void upGbWareGroupedNum(Long id, Integer num) {
        GbWare ware = getById(id);
        if (ware != null) {
            GbWare upWare = new GbWare();
            upWare.setGroupedNum(ware.getGroupedNum() == null ? num : ware.getGroupedNum() + num);
            update(upWare, query().eq(GbWare::getId, id));
        }
    }

    private void checkStatus(Long id) {
        GbWare ware = getById(id);
        AssertUtil.isNotEqual(ware.getOnlineStatus(), 0, "操作失败,请下架后,重试");
    }
}
