package com.hzs.system.home.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hzs.common.core.utils.ip.IpLocationUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.system.home.HoRegionDistribute;
import com.hzs.common.domain.system.home.HoRegionDistributeItem;
import com.hzs.system.home.param.HoRegionDistributeParam;
import com.hzs.system.home.service.IHoRegionDistributeItemService;
import com.hzs.system.home.service.IHoRegionDistributeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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


/**
 * @Description:
 * @Author: yuhui
 * @Time: 2022/10/10 11:28
 * @Classname: SaRegionDistributeController
 * @PackageName: com.hzs.system.home.controller.api
 */
@RestController
@RequestMapping("/api/ho-distribute")
@Slf4j
public class ApiRegionDistributeController extends BaseController {

    @Autowired
    private IHoRegionDistributeService regionDistributeService;
    @Autowired
    private IHoRegionDistributeItemService regionDistributeItemService;

    /**
     * 查询全球网络分布
     * @return
     */
    @GetMapping("get-region-distribute")
    public AjaxResult getRegionDistribute(){
        Integer languageType = IpLocationUtils.getIpLocation();
        UpdateWrapper<HoRegionDistribute> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("LANGUAGE_TYPE",languageType);
        List<HoRegionDistribute> list = regionDistributeService.list(updateWrapper);
        List<HoRegionDistributeParam> resultList =new ArrayList<>();
        for (HoRegionDistribute distribute : list) {
            HoRegionDistributeParam regionDistributeParam = new HoRegionDistributeParam();
            regionDistributeParam.setName(distribute.getName());
            regionDistributeParam.setSort(distribute.getSort());
            QueryWrapper<HoRegionDistributeItem> updateWrapperDistri = new QueryWrapper();
            updateWrapperDistri.eq("REGION_DISTRIBUTE_PK",distribute.getPkId());
            updateWrapperDistri.eq("LANGUAGE_TYPE",languageType);
            List<HoRegionDistributeItem> regionDistributeItemList = regionDistributeItemService.list(updateWrapperDistri);
            regionDistributeParam.setItemList(regionDistributeItemList);
            // 查询第一条数据的展示信息
            if (regionDistributeItemList.size() > 0){
                HoRegionDistributeItem regionDistributeItem = regionDistributeItemList.get(0);
                // 这个位置让转化为数据形式传递
                List<HoRegionDistributeItem> firstRegionDistributeItem =new ArrayList<>();
                firstRegionDistributeItem.add(regionDistributeItem);
                regionDistributeParam.setRegionDistributeItem(firstRegionDistributeItem);
            }
            resultList.add(regionDistributeParam);
        }
        return AjaxResult.success(resultList);
    }

    /**
     * 全球网络分布详情
     * @return
     */
    @GetMapping("get-region-detail")
    public AjaxResult getRegionDetail(Long pkId){
        HoRegionDistributeItem regionDistributeItem = regionDistributeItemService.getById(pkId);
        return AjaxResult.success(regionDistributeItem);
    }
}
