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

import cn.hutool.core.bean.BeanUtil;
import com.czg.account.dto.freeding.FreeDineConfigEditDTO;
import com.czg.account.entity.FreeDineConfig;
import com.czg.account.service.FreeDineConfigService;
import com.czg.exception.ApiNotPrintException;
import com.czg.service.account.mapper.FreeDineConfigMapper;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.DubboService;

/**
 * 霸王餐配置信息表 服务层实现。
 *
 * @author zs
 * @since 2025-02-21
 */
@DubboService
public class FreeDineConfigServiceImpl extends ServiceImpl<FreeDineConfigMapper, FreeDineConfig> implements FreeDineConfigService{
    @Override
    public FreeDineConfig getConfig(long shopId) {
        FreeDineConfig freeDineConfig = getOne(new QueryWrapper().eq(FreeDineConfig::getShopId, shopId));
        if (freeDineConfig == null) {
            freeDineConfig = new FreeDineConfig();
            freeDineConfig.setShopId(shopId);
            save(freeDineConfig);
            freeDineConfig = getOne(new QueryWrapper().eq(FreeDineConfig::getShopId, shopId));
        }

        return freeDineConfig;
    }

    @Override
    public Boolean edit(long shopId, FreeDineConfigEditDTO freeDineConfigEditDTO) {
        FreeDineConfig config = getConfig(shopId);
        if (config == null) {
            throw new ApiNotPrintException("霸王餐信息未配置");
        }
        BeanUtil.copyProperties(freeDineConfigEditDTO, config);
//        if (freeDineConfigEditDTO.getUseType() != null && !freeDineConfigEditDTO.getUseType().isEmpty()) {
//            config.setUseType(JSONObject.toJSONString(freeDineConfigEditDTO.getUseType()));
//        }
        return updateById(config);
    }
}
