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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSONArray;
import com.czg.account.dto.freeding.FreeDineConfigEditDTO;
import com.czg.account.entity.FreeDineConfig;
import com.czg.account.service.FreeDineConfigService;
import com.czg.account.service.ShopConfigService;
import com.czg.account.vo.FreeDineConfigVO;
import com.czg.exception.CzgException;
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.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.ArrayList;

/**
 * 霸王餐配置信息表 服务层实现。
 *
 * @author zs
 * @since 2025-02-21
 */
@DubboService
public class FreeDineConfigServiceImpl extends ServiceImpl<FreeDineConfigMapper, FreeDineConfig> implements FreeDineConfigService{

    @DubboReference
    private ShopConfigService shopConfigService;

    @Override
    public FreeDineConfigVO 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));
        }
        FreeDineConfigVO dto = BeanUtil.copyProperties(freeDineConfig, FreeDineConfigVO.class, "shopIdList");
        if (StrUtil.isNotBlank(freeDineConfig.getShopIdList())) {
            dto.setShopIdList(JSONArray.parseArray(freeDineConfig.getShopIdList()).toList(Long.class));
        }

        if (StrUtil.isNotBlank(freeDineConfig.getUseType())) {
            dto.setUseType(JSONArray.parseArray(freeDineConfig.getUseType()).toList(String.class));
        }
        return dto;
    }

    @Override
    public Boolean edit(long shopId, FreeDineConfigEditDTO freeDineConfigEditDTO) {
        FreeDineConfig config = getOne(new QueryWrapper().eq(FreeDineConfig::getShopId, shopId));
        if (config == null) {
            throw new CzgException("霸王餐信息未配置");
        }
        BeanUtil.copyProperties(freeDineConfigEditDTO, config, "useType");
        if ("part".equals(freeDineConfigEditDTO.getUseShopType()) && freeDineConfigEditDTO.getShopIdList() != null) {
            config.setShopIdList(JSONArray.toJSONString(freeDineConfigEditDTO.getShopIdList()));
        }

        shopConfigService.editStatusByShopIdList(config.getShopId(), freeDineConfigEditDTO.getEnable() ? 1 : 0,true, "is_account_pay",
                freeDineConfigEditDTO.getUseShopType(), freeDineConfigEditDTO.getShopIdList() == null ? new ArrayList<>() : freeDineConfigEditDTO.getShopIdList());
        if (freeDineConfigEditDTO.getUseType() != null) {
            config.setUseType(JSONObject.toJSONString(freeDineConfigEditDTO.getUseType()));
        }
        return updateById(config);
    }
}
