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

import cn.hutool.core.bean.BeanUtil;
import com.czg.BaseQueryParam;
import com.czg.account.entity.ShopInfo;
import com.czg.account.service.ShopInfoService;
import com.czg.market.dto.MkProductSmartSuggestDTO;
import com.czg.market.entity.MkProductSmartSuggest;
import com.czg.utils.AssertUtil;
import com.czg.utils.CzgStrUtils;
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.market.service.MkProductSmartSuggestService;
import com.czg.service.market.mapper.MkProductSmartSuggestMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

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

/**
 * 点单智能推荐 服务层实现。
 *
 * @author ww
 * @since 2025-10-21
 */
@Service
public class MkProductSmartSuggestServiceImpl extends ServiceImpl<MkProductSmartSuggestMapper, MkProductSmartSuggest> implements MkProductSmartSuggestService {

    @DubboReference
    private ShopInfoService shopInfoService;

    @Override
    public Page<MkProductSmartSuggestDTO> getProductSmartSuggestPage(BaseQueryParam param, Long shopId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(MkProductSmartSuggest::getShopId, shopId)
                .eq(MkProductSmartSuggest::getIsDel, 0)
                .orderBy(MkProductSmartSuggest::getUpdateTime).desc();
        return pageAs(PageUtil.buildPage(), queryWrapper, MkProductSmartSuggestDTO.class);
    }

    @Override
    public Map<String, Object> getProductSmartSuggestByShopId(Long shopId) {
        ShopInfo shopInfo = shopInfoService.getById(shopId);
        AssertUtil.isNull(shopInfo, "店铺不存在");
        if (shopInfo.getIsProductSuggest() == null || shopInfo.getIsProductSuggest() == 0) {
            return null;
        }
        List<MkProductSmartSuggest> suggests = mapper.selectListByShopId(shopId, CzgStrUtils.getStrWeek());
        return Map.of("suggestTime", shopInfo.getSuggestTime(), "list", suggests);
    }

    @Override
    public void addProductSmartSuggest(MkProductSmartSuggestDTO param) {
        param.setIsDel(false);
        MkProductSmartSuggest activity = BeanUtil.toBean(param, MkProductSmartSuggest.class);
        save(activity);
    }

    @Override
    public void updateProductSmartSuggestById(MkProductSmartSuggestDTO param) {
        param.setIsDel(false);
        MkProductSmartSuggest suggest = BeanUtil.toBean(param, MkProductSmartSuggest.class);
        updateById(suggest, true);
    }

    @Override
    public void deleteProductSmartSuggest(Long id) {
        MkProductSmartSuggest suggest = new MkProductSmartSuggest();
        suggest.setId(id);
        suggest.setIsDel(true);
        updateById(suggest, true);
    }
}
