package com.yanzhu.system.controller;

import com.yanzhu.common.core.constant.CacheConstants;
import com.yanzhu.common.core.utils.poi.ExcelUtil;
import com.yanzhu.common.core.web.controller.BaseController;
import com.yanzhu.common.core.web.domain.AjaxResult;
import com.yanzhu.common.log.annotation.Log;
import com.yanzhu.common.log.enums.BusinessType;
import com.yanzhu.common.redis.service.RedisService;
import com.yanzhu.common.security.annotation.RequiresPermissions;
import com.yanzhu.system.domain.SysArea;
import com.yanzhu.system.domain.vo.TreeSelect;
import com.yanzhu.system.service.ISysAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 地址管理Controller
 * 
 * @author JiangYuQi
 * @date 2024-07-15
 */
@RestController
@RequestMapping("/sysArea")
public class SysAreaController extends BaseController
{
    @Autowired
    private RedisService redisService;

    @Autowired
    private ISysAreaService sysAreaService;

    /**
     * 查询地址管理列表
     */
    @RequiresPermissions("system:sysArea:list")
    @GetMapping("/list")
    public AjaxResult list(SysArea sysArea)
    {
        List<SysArea> list = sysAreaService.selectSysAreaList(sysArea);
        return success(list);
    }

    /**
     * 刷新地址管理缓存
     */
    @RequiresPermissions("system:sysArea:list")
    @GetMapping("/refreshCache")
    public AjaxResult refreshCache()
    {
        sysAreaService.loadingSysAreaCache();
        return success();
    }

    /**
     * 导出地址管理列表
     */
    @RequiresPermissions("system:sysArea:export")
    @Log(title = "地址管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysArea sysArea)
    {
        List<SysArea> list = sysAreaService.selectSysAreaList(sysArea);
        ExcelUtil<SysArea> util = new ExcelUtil<SysArea>(SysArea.class);
        util.exportExcel(response, list, "地址管理数据");
    }

    /**
     * 获取地址管理详细信息
     */
    @RequiresPermissions("system:sysArea:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(sysAreaService.selectSysAreaById(id));
    }

    /**
     * 新增地址管理
     */
    @RequiresPermissions("system:sysArea:add")
    @Log(title = "地址管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysArea sysArea)
    {
        return toAjax(sysAreaService.insertSysArea(sysArea));
    }

    /**
     * 修改地址管理
     */
    @RequiresPermissions("system:sysArea:edit")
    @Log(title = "地址管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysArea sysArea)
    {
        return toAjax(sysAreaService.updateSysArea(sysArea));
    }

    /**
     * 删除地址管理
     */
    @RequiresPermissions("system:sysArea:remove")
    @Log(title = "地址管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(sysAreaService.deleteSysAreaByIds(ids));
    }

    /**
     * 查询区域地址列表
     */
    @GetMapping("/findAll")
    public AjaxResult findAll()
    {
        // 基础数据访问频繁，加入到缓存
        String key = CacheConstants.SYS_AREA_ALL;
        Object obj = redisService.getCacheObject(key);
        if (obj != null) {
            return success(obj);
        }else{
            // 重新加载缓存信息
            sysAreaService.loadingSysAreaCache();
            obj = redisService.getCacheObject(key);
            return success(obj);
        }
    }

    /**
     * 查询城市地址列表
     */
    @GetMapping("/findAllCity")
    public AjaxResult findAllCity()
    {
        // 基础数据访问频繁，加入到缓存
        String key = CacheConstants.SYS_AREA_CITY;
        Object obj = redisService.getCacheObject(key);
        if (obj != null) {
            return success(obj);
        }else{
            // 重新加载缓存信息
            SysArea area = new SysArea();
            area.setCtypeEvent(3);
            List<TreeSelect> treeList = sysAreaService.selectAreaTreeList(area);
            redisService.setCacheObject(key,treeList,CacheConstants.EXPIRATION, TimeUnit.MINUTES);
            return success(treeList);
        }
    }
}
