package com.gxwebsoft.common.system.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.Area;
import com.gxwebsoft.common.system.param.AreaParam;
import com.gxwebsoft.common.system.service.AreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 控制器
 *
 * @author 科技小王子
 * @since 2023-07-06 15:08:15
 */
@Api(tags = "管理")
@RestController
@RequestMapping("/api/common/area")
public class AreaController extends BaseController {
    @Resource
    private AreaService areaService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    
    @ApiOperation("分页查询")
    @PostMapping("/page")
    public ApiResult<PageResult<Area>> page(AreaParam param) {
        PageParam<Area, AreaParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(areaService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(areaService.pageRel(param));
    }

    @ApiOperation("查询全部")
    @PostMapping("/area-info-by-name")
    public ApiResult<Area> areaInfoByName(@RequestBody AreaParam param) {
        return success(areaService.areaInfoByName(param.getName(), null));
    }

    @PostMapping("/region-info-with-province")
    public ApiResult<Area> regionInfoWithProvince(@RequestBody AreaParam param) {
        return success(areaService.regionInfoWithProvince(param.getProvinceName(), param.getCityName(), param.getName()));
    }

    @PostMapping()
    public ApiResult<List<Area>> list() {
        return success(areaService.list());
        // 使用关联查询
        //return success(areaService.listRel(param));
    }

    @ApiOperation("查询全部")
    @PostMapping("/list-by-level")
    public ApiResult<?> listByLevel(@RequestBody AreaParam areaParam) {
        Integer level = areaParam.getLevelId();
        String str = "area-list-by-level";
        if (level != null) str += "-" + level;
        List<Area> topList;
        String topListString = redisTemplate.opsForValue().get(str);
        if (topListString == null) {
            topList = areaService.list(new LambdaQueryWrapper<Area>().eq(Area::getLevelId, 1));
            for (Area top : topList) {
                List<Area> secondList = areaService.list(new LambdaQueryWrapper<Area>().eq(Area::getLevelId, 2).eq(Area::getFid, top.getId()));
                if (level == null || level == 3) {
                    for (Area second : secondList) {
                        List<Area> thirdList = areaService.list(new LambdaQueryWrapper<Area>().eq(Area::getLevelId, 3).eq(Area::getFid, second.getId()));
                        second.setChildren(thirdList);
                    }
                }
                top.setChildren(secondList);
            }
            redisTemplate.opsForValue().set(str, JSON.toJSONString(topList));
        } else topList = JSON.parseArray(topListString, Area.class);
        return success(topList);
        // 使用关联查询
        //return success(areaService.listRel(param));
    }

    @PostMapping("/city-list")
    public ApiResult<?> cityList() {
        List<Area> topList;
        String topListString = redisTemplate.opsForValue().get("area-city-list");
        if (topListString == null || topListString.equals("[]")) {
            topList = areaService.list(new LambdaQueryWrapper<Area>().eq(Area::getLevelId, 1));
            for (Area top : topList) {
                List<Area> secondList = areaService.list(new LambdaQueryWrapper<Area>().eq(Area::getLevelId, 2).eq(Area::getFid, top.getId()));
                top.setChildren(secondList);
            }
            redisTemplate.opsForValue().set("area-city-list", JSON.toJSONString(topList));
        } else topList = JSON.parseArray(topListString, Area.class);
        return success(topList);
        // 使用关联查询
        //return success(areaService.listRel(param));
    }

    @ApiOperation("根据id查询")
    @PostMapping("/{id}")
    public ApiResult<Area> get(@PathVariable("id") Integer id) {
        return success(areaService.getById(id));
        // 使用关联查询
        //return success(areaService.getByIdRel(id));
    }
}
