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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.czg.constant.TableValueConstant;
import com.czg.exception.CzgException;
import com.czg.market.dto.MkEnableConfigDTO;
import com.czg.market.vo.MkEnableConfigVO;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.market.entity.MkEnableConfig;
import com.czg.market.service.MkEnableConfigService;
import com.czg.service.market.mapper.MkEnableConfigMapper;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * 兑换码明细 服务层实现。
 *
 * @author zs
 * @since 2025-10-21
 */
@Service
@CacheConfig(cacheNames = "enableConfig")
public class MkEnableConfigServiceImpl extends ServiceImpl<MkEnableConfigMapper, MkEnableConfig> implements MkEnableConfigService{

    @Override
    public MkEnableConfigVO detail(Long mainShopId, Long shopId, TableValueConstant.EnableConfig.Type type) {
        MkEnableConfig one = getOne(new QueryWrapper().eq(MkEnableConfig::getMainShopId, mainShopId).eq(MkEnableConfig::getType, type.getCode()));
        if (one == null) {
            one = new MkEnableConfig();
            one.setMainShopId(mainShopId);
            one.setIsEnable(1);
            one.setShopId(shopId);
            one.setType(type.getCode());
//            save(one);
        }
        MkEnableConfigVO configVO = BeanUtil.copyProperties(one, MkEnableConfigVO.class);
        if (StrUtil.isNotBlank(one.getShopIdList())) {
            configVO.setShopIdList(JSONArray.parseArray(one.getShopIdList()).toList(Long.class));
        }
        return configVO;
    }

    @Override
    public Boolean upEnable(Long mainShopId, Long shopId, MkEnableConfigDTO dto, TableValueConstant.EnableConfig.Type type) {
        MkEnableConfig one = getOne(new QueryWrapper().eq(MkEnableConfig::getMainShopId, mainShopId).eq(MkEnableConfig::getType, type.getCode()));
        if (one == null) {
            one = new MkEnableConfig();
            one.setMainShopId(mainShopId);
            one.setIsEnable(dto.getIsEnable());
            one.setShopId(shopId);
            one.setType(type.getCode());
            save(one);
        }else {
            one.setIsEnable(dto.getIsEnable());
        }


        if ("part".equals(dto.getUseType()) && dto.getShopIdList() != null) {
            one.setShopIdList(JSONObject.toJSONString(dto.getShopIdList()));
        }
        one.setUseType(dto.getUseType());
        return updateById(one);
    }

    @Override
    public void checkEnable(Long mainShopId, Long shopId, TableValueConstant.EnableConfig.Type type, boolean masterMange) {
        MkEnableConfigVO detail = detail(mainShopId, shopId, type);
        if (detail.getIsEnable() != 1) {
            throw new CzgException("模块未开启");
        }
    }
}
