package shop.alien.storeplatform.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import shop.alien.entity.result.R;
import shop.alien.entity.store.StoreDictionary;
import shop.alien.entity.store.vo.StoreDictionaryVo;
import shop.alien.mapper.StoreDictionaryMapper;
import shop.alien.storeplatform.service.BaseDataService;
import shop.alien.storeplatform.util.GaoDeMapApiUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * web端基础数据服务实现类
 *
 * @author ssk
 * @since 2025-01-xx
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class BaseDataServiceImpl implements BaseDataService {

    private final GaoDeMapApiUtil gaoDeMapApiUtil;

    private final StoreDictionaryMapper storeDictionaryMapper;

    @Override
    public R<JSONObject> getAddressPrompt(String addressName, String city) {
        log.info("BaseDataServiceImpl.getAddressPrompt?addressName={}&city={}", addressName, city);
        JSONObject result = gaoDeMapApiUtil.getInputPrompt(addressName, city);
        return R.data(result);
    }

    @Override
    public R<JSONObject> getDistrict(String adCode) {
        log.info("BaseDataServiceImpl.getDistrict?adCode={}", adCode);
        JSONObject result = gaoDeMapApiUtil.getDistrict(adCode);
        return R.data(result);
    }

    @Override
    public R<List<StoreDictionaryVo>> getBusinessSection() {
        log.info("BaseDataServiceImpl.getBusinessSection");
        // 查询经营板块数据（排除父节点dictId=0）
        LambdaQueryWrapper<StoreDictionary> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StoreDictionary::getTypeName, "business_section")
               .ne(StoreDictionary::getDictId, 0);
        List<StoreDictionary> businessSectionList = storeDictionaryMapper.selectList(wrapper);
        
        // 转换为VO
        List<StoreDictionaryVo> voList = new ArrayList<>();
        for (StoreDictionary storeDictionary : businessSectionList) {
            if (storeDictionary != null) {
                StoreDictionaryVo vo = new StoreDictionaryVo();
                BeanUtils.copyProperties(storeDictionary, vo);
                voList.add(vo);
            }
        }
        
        return R.data(voList);
    }

    @Override
    public R<List<StoreDictionaryVo>> getBusinessTypes(String parentId) {
        log.info("BaseDataServiceImpl.getBusinessTypes?parentId={}", parentId);
        
        // 根据parentId查询经营板块信息
        LambdaQueryWrapper<StoreDictionary> sectionWrapper = new LambdaQueryWrapper<>();
        sectionWrapper.eq(StoreDictionary::getTypeName, "business_section")
                     .eq(StoreDictionary::getDictId, parentId);
        StoreDictionary businessSection = storeDictionaryMapper.selectOne(sectionWrapper);
        
        if (businessSection == null) {
            log.warn("BaseDataServiceImpl.getBusinessTypes 未找到经营板块，parentId={}", parentId);
            return R.data(new ArrayList<>());
        }
        
        // 查询该板块下的经营种类
        LambdaQueryWrapper<StoreDictionary> typesWrapper = new LambdaQueryWrapper<>();
        typesWrapper.eq(StoreDictionary::getParentId, businessSection.getId());
        List<StoreDictionary> businessTypesList = storeDictionaryMapper.selectList(typesWrapper);
        
        // 转换为VO
        List<StoreDictionaryVo> voList = new ArrayList<>();
        for (StoreDictionary storeDictionary : businessTypesList) {
            if (storeDictionary != null) {
                StoreDictionaryVo vo = new StoreDictionaryVo();
                BeanUtils.copyProperties(storeDictionary, vo);
                voList.add(vo);
            }
        }
        
        return R.data(voList);
    }
}

