package com.hzs.system.base.provider;

import com.hzs.common.core.domain.R;
import com.hzs.common.domain.system.base.BdArea;
import com.hzs.common.domain.system.base.ext.AreaCache;
import com.hzs.common.domain.system.base.ext.BdProductStorehouseExt;
import com.hzs.system.base.IAreaServiceApi;
import com.hzs.system.base.dto.AreaDTO;
import com.hzs.system.base.service.IBdAreaService;
import com.hzs.system.base.service.IBdProductStorehouseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

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

/**
 * 行政区划dubbo服务
 */
@Slf4j
@DubboService
public class AreaServiceProvider implements IAreaServiceApi {

    @Autowired
    private IBdAreaService iBdAreaService;

    @Autowired
    private IBdProductStorehouseService iBdProductStorehouseService;

    @Override
    public R<Map<Integer, String>> getAreaMap(Integer pkCountry) {
        Map<Integer, String> resultMap = new HashMap<>();
        List<AreaCache> list = iBdAreaService.getAreaList(pkCountry);
        for (AreaCache areaCache : list) {
            resultMap.put(areaCache.getPkId(), areaCache.getName());
        }
        return R.ok(resultMap);
    }

    @Override
    public R<Map<Integer, String>> getAllAreaNoCache(Integer pkCountry) {
        Map<Integer, String> resultMap = new HashMap<>();
        List<BdArea> list = iBdAreaService.getAllAreaNoCache(pkCountry);
        for (BdArea bdArea : list) {
            resultMap.put(bdArea.getPkId(), bdArea.getName());
        }
        return R.ok(resultMap);
    }

    @Override
    public R<BdArea> getArea(Integer pkId) {
        return R.ok(iBdAreaService.getById(pkId));
    }

    @Override
    public R<?> updateArea(List<AreaDTO> provinceDTOList) {
        try {
            String str = iBdAreaService.updateArea(provinceDTOList);
            if (null == str) {
                return R.ok();
            }
            return R.fail(str);
        } catch (Exception e) {
            log.error("更新行政区划异常：{}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @Override
    public R<List<BdProductStorehouseExt>> queryBdProductStorehouseByCondition(List<Integer> productId, Integer province) {
        return R.ok(iBdProductStorehouseService.queryBdProductStorehouseByCondition(productId, province));
    }

}
