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

import cn.hutool.core.util.StrUtil;
import com.czg.BaseQueryParam;
import com.czg.market.entity.MkPointsConfig;
import com.czg.market.entity.MkPointsGoods;
import com.czg.market.entity.MkPointsUser;
import com.czg.market.entity.ShopCoupon;
import com.czg.market.service.MkPointsConfigService;
import com.czg.market.service.MkPointsGoodsService;
import com.czg.market.service.MkPointsUserService;
import com.czg.market.service.ShopCouponService;
import com.czg.service.market.mapper.MkPointsGoodsMapper;
import com.czg.utils.PageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 积分商品设置 服务层实现。
 *
 * @author ww
 * @since 2025-12-08
 */
@Service
public class MkPointsGoodsServiceImpl extends ServiceImpl<MkPointsGoodsMapper, MkPointsGoods> implements MkPointsGoodsService {
    @Resource
    private MkPointsConfigService mkPointsConfigService;
    @Resource
    private MkPointsUserService pointsUserService;
    @Resource
    private ShopCouponService shopCouponService;

    @Override
    public Page<MkPointsGoods> getPointsGoodsPage(BaseQueryParam param, Long shopId) {
        boolean exists = mkPointsConfigService.exists(query().eq(MkPointsConfig::getShopId, shopId).eq(MkPointsConfig::getEnablePointsMall, 1));
        if (!exists) {
            return PageUtil.emptyPage();
        }
        Page<MkPointsGoods> page = page(Page.of(param.getPage(), param.getSize()),
                query()
                        .eq(MkPointsGoods::getShopId, shopId)
                        .eq(MkPointsGoods::getDelFlag, 0)
                        .orderBy(MkPointsGoods::getSort, false)
                        .orderBy(MkPointsGoods::getId, false)
        );
        page.getRecords().forEach(goods -> {
            if ("优惠券".equals(goods.getGoodsCategory())) {
                ShopCoupon one = shopCouponService.getOne(query().eq(ShopCoupon::getId, goods.getCouponId())
                        .eq(ShopCoupon::getShopId, shopId)
//                        .eq(ShopCoupon::getStatus, 1)
                        .eq(ShopCoupon::getIsDel, 0));
                goods.setCouponInfo(one);
            }
        });
        return page;
    }

    @Override
    public Map<String, Object> getPointsGoodsPageByUser(Integer page, Integer size, Long shopId, String goodsCategory, Long userId) {
        Map<String, Object> result = new HashMap<>(2);
        MkPointsUser pointsUser = pointsUserService.getPointsUser(shopId, null, userId);
        List<MkPointsGoods> list;
        boolean exists = mkPointsConfigService.exists(query().eq(MkPointsConfig::getShopId, shopId).eq(MkPointsConfig::getEnablePointsMall, 1));
        if (exists) {
            PageHelper.startPage(page, size);
            list = mapper.getPointsGoodsPageByUser(shopId, userId, StrUtil.trim(goodsCategory));
            list.forEach(goods -> {
                if ("优惠券".equals(goods.getGoodsCategory())) {
                    ShopCoupon one = shopCouponService.getOne(query().eq(ShopCoupon::getId, goods.getCouponId())
                            .eq(ShopCoupon::getShopId, shopId)
                            .eq(ShopCoupon::getStatus, 1)
                            .eq(ShopCoupon::getIsDel, 0));
                    goods.setCouponInfo(one);
                }
            });
            result.put("pointsGoods", PageUtil.convert(new PageInfo<>(list)));
        }
        result.put("pointsUser", pointsUser == null ? "" : pointsUser);
        return result;
    }

    @Override
    public boolean upNumberById(Long id, Integer quantity, Integer totalExchangeCount) {
        MkPointsGoods goodUp = new MkPointsGoods();
        goodUp.setId(id);
        goodUp.setQuantity(quantity);
        goodUp.setTotalExchangeCount(totalExchangeCount);
        return updateById(goodUp);

    }

    @Override
    public void updateGoodsCouponStatus(Long couponId) {
        MkPointsGoods upMkPointsGoods = new MkPointsGoods();
        upMkPointsGoods.setStatus(0);
        update(upMkPointsGoods, query().eq(MkPointsGoods::getCouponId, couponId));
    }
}
