package com.hai.goods.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.dao.GoodsRegionFreightMapper;
import com.hai.entity.GoodsRegionFreight;
import com.hai.entity.GoodsRegionFreightExample;
import com.hai.entity.SecRegion;
import com.hai.goods.service.GoodsRegionFreightService;
import com.hai.service.CommonService;
import com.hai.service.SecConfigService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service("goodsRegionFreightService")
public class GoodsRegionFreightServiceImpl implements GoodsRegionFreightService {

    @Resource
    private GoodsRegionFreightMapper regionFreightMapper;

    @Resource
    private SecConfigService secConfigService;

    @Resource
    private CommonService commonService;

    @Override
    public void insertRegionFreight(GoodsRegionFreight regionFreight) {
        regionFreightMapper.insert(regionFreight);
    }

    @Override
    public void updateRegionFreight(GoodsRegionFreight regionFreight) {
        regionFreightMapper.updateByPrimaryKey(regionFreight);
    }

    @Override
    public List<GoodsRegionFreight> getRegionFreightList(Map<String, Object> map) {

        GoodsRegionFreightExample example = new GoodsRegionFreightExample();
        GoodsRegionFreightExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getString(map, "regionName") != null) {
            criteria.andRegionNameLike("%" +  MapUtils.getString(map, "regionName") + "%");
        }
        if (MapUtils.getString(map, "regionId") != null) {
            criteria.andRegionIdEqualTo(MapUtils.getString(map, "regionId"));
        }
        criteria.andStatusEqualTo(1);

        return regionFreightMapper.selectByExample(example);
    }

    @Override
    public GoodsRegionFreight findRegionFreightById(Long id) {
        return regionFreightMapper.selectByPrimaryKey(id);
    }

    @Override
    public GoodsRegionFreight findRegionFreightByRegionId(String regionId) {

        GoodsRegionFreightExample example = new GoodsRegionFreightExample();
        example.createCriteria().andRegionIdEqualTo(regionId).andStatusEqualTo(1);

        List<GoodsRegionFreight> list = regionFreightMapper.selectByExample(example);

        if (list.size() > 0 ) {
            return list.get(0);
        }

        return null;
    }

    @Override
    public void deleteRegionFreight(Long id) {
        regionFreightMapper.deleteByPrimaryKey(id);
    }

    @Override
    public JSONObject getRegionFreight(String regionId) {

        // 获取默认包邮价格 ， 运费
        BigDecimal freePostPrice = new BigDecimal(secConfigService.findByCodeType("FREE_POST_PRICE").getCodeValue());
        BigDecimal freightPrice = new BigDecimal(secConfigService.findByCodeType("FREIGHT_PRICE").getCodeValue());

        JSONObject jsonObject = new JSONObject();

        // 查询当前登记
        GoodsRegionFreight goodsRegionFreight = findRegionFreightByRegionId(regionId);

        SecRegion region = commonService.getRegionsById(Long.parseLong(regionId));

        if (region == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无效区域编码！");
        }

        if (goodsRegionFreight != null) {
            jsonObject.put("freePostPrice" , goodsRegionFreight.getFreePostPrice());
            jsonObject.put("freightPrice" , goodsRegionFreight.getFreightPrice());
            return jsonObject;
        }

        if (region.getParentId() == null) {
            jsonObject.put("freePostPrice" , freePostPrice);
            jsonObject.put("freightPrice" , freightPrice);
            return jsonObject;
        }

        return getRegionFreight(region.getParentId().toString());
    }

}
