package com.trs.ggzyexchange.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.trs.ggzyexchange.api.vo.Result;
import com.trs.ggzyexchange.entity.AreaPlatform;
import com.trs.ggzyexchange.entity.TRegion;
import com.trs.ggzyexchange.service.AreaPlatformService;
import com.trs.ggzyexchange.service.TRegionService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 行政区划表 前端控制器
 * </p>
 *
 * @author trs
 * @since 2023-02-17
 */
@RestController
@RequestMapping("/t-region")
public class TRegionController extends BaseController {

    @Autowired
    private TRegionService tRegionService;

    @Autowired
    private AreaPlatformService areaPlatformService;

    //查询盟市列表
    @GetMapping("/getTRegionCityList")
    public Result<List<TRegion>> getTRegionCityList(){
        QueryWrapper<TRegion> queryWrapper = new QueryWrapper<TRegion>();
        queryWrapper.eq("parent_code","150000");
        queryWrapper.eq("is_delete","N");
        queryWrapper.orderByAsc("order_num");
        List<TRegion> listReturn = new ArrayList<TRegion>();
        List<TRegion> list = tRegionService.list(queryWrapper);
        for (TRegion tRegion : list) {
            String code = tRegion.getCode();
            QueryWrapper<AreaPlatform> platformQueryWrapper = new QueryWrapper<AreaPlatform>();
            platformQueryWrapper.eq("AREA_CODE",code);
            AreaPlatform areaPlatform = areaPlatformService.getOne(platformQueryWrapper);
            if(!ObjectUtils.isEmpty(areaPlatform)){
                tRegion.setPlatformCode(areaPlatform.getPlatformCode());
            }
            String substringCode = code.substring(0,6);
            tRegion.setCodePram(substringCode);
        }
        TRegion tRegion = new TRegion();
        tRegion.setCode("150000000000");
        tRegion.setCodePram("150000");
        tRegion.setPlatformCode("121500004600236091");
        tRegion.setName("自治区本级");
        tRegion.setOrderNum(0);
        listReturn.add(tRegion);
        listReturn.addAll(list);
        return Result.OK(listReturn);
    }

    //查询盟市对应的旗县区
    @GetMapping("/getTRegionDistrictList")
    public Result<List<TRegion>> getTRegionDistrictList(@RequestParam(name="parentCode",required=true) String parentCode){
        QueryWrapper<TRegion> queryWrapper = new QueryWrapper<TRegion>();
        queryWrapper.eq("parent_code",parentCode);
        queryWrapper.eq("is_delete","N");
        queryWrapper.orderByAsc("code");
        List<TRegion> list = tRegionService.list(queryWrapper);
        for (TRegion tRegion : list) {
            String code = tRegion.getCode();
            String substringCode = code.substring(0,6);
            tRegion.setCodePram(substringCode);
        }
        return Result.OK(list);
    }
}
