package com.vctgo.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vctgo.common.core.utils.poi.ExcelUtil;
import com.vctgo.common.core.web.controller.BaseController;
import com.vctgo.common.core.web.domain.AjaxResult;
import com.vctgo.common.security.annotation.Logical;
import com.vctgo.common.security.annotation.RequiresPermissions;
import com.vctgo.system.api.domain.dto.SysAreaSettingDto;
import com.vctgo.system.domain.SysAreaSetting;
import com.vctgo.system.service.ISysAreaSettingService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 区域设置Controller
 *
 * @author wanglonghua
 * @date 2023-05-30
 */
@RestController
@RequestMapping("/sysAreaSetting")
public class SysAreaSettingController extends BaseController
{
    @Autowired
    private ISysAreaSettingService sysAreaSettingService;

    /**
     * 查询区域设置列表
     */
    @RequiresPermissions(value = {"system:sysAreaSetting:list",
            "toy:kindergarten:sysAreaSetting:list",
            "toy:info:sysAreaSetting:list"}, logical = Logical.OR)
    @GetMapping("/list")
    public AjaxResult list(SysAreaSetting sysAreaSetting) {
        IPage<SysAreaSetting> list = sysAreaSettingService.selectSysAreaSettingPage(sysAreaSetting);
        return AjaxResult.success(list);
    }

    /**
     * 获取全系统所有的区域列表
     */
    @GetMapping("/list/sys")
    public AjaxResult getSysAreaList() {
        return AjaxResult.success(sysAreaSettingService.selectSysAreaSettingList(new SysAreaSetting()));
    }

    /**
     * 获取当前租户所有的区域列表
     */
    @GetMapping("/list/all")
    public AjaxResult getTenantAreaList(@SpringQueryMap SysAreaSettingDto sysAreaSettingDto) {
        SysAreaSetting sysAreaSetting = new SysAreaSetting();
        BeanUtils.copyProperties(sysAreaSettingDto, sysAreaSetting);
        return AjaxResult.success(sysAreaSettingService.selectTenantAreaSettingList(sysAreaSetting));
    }

    /**
     * 导出区域设置列表
     */
    @RequiresPermissions("system:sysAreaSetting:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysAreaSetting sysAreaSetting)
    {
        List<SysAreaSetting> list = sysAreaSettingService.selectSysAreaSettingList(sysAreaSetting);
        ExcelUtil<SysAreaSetting> util = new ExcelUtil<SysAreaSetting>(SysAreaSetting.class);
        util.exportExcel(response, list, "区域设置数据");
    }

    /**
     * 获取区域设置详细信息
     */
    @RequiresPermissions("system:sysAreaSetting:query")
    @GetMapping(value = "/{areaId}")
    public AjaxResult getInfo(@PathVariable("areaId") Long areaId)
    {
        return success((sysAreaSettingService.selectSysAreaSettingByAreaId(areaId)));
    }

    @GetMapping(value = {"/list/{areaName}/","/list/{areaName}/{areaId}"})
    public AjaxResult repeatAreaNameList(@PathVariable("areaName") String areaName,
                                         @PathVariable(value = "areaId", required = false) String areaId)
    {
        Map<String, Object> params = new HashMap<>();
        params.put("areaName", areaName);
        if(StringUtils.isNotEmpty(areaId) && StringUtils.isNumeric(areaId)){
            params.put("areaId", Long.parseLong(areaId));
        }
        List<SysAreaSetting> areaSettingList = sysAreaSettingService.selectAreaListByParams(params);
        return AjaxResult.success(areaSettingList.size());
    }

    /**
     * 新增区域设置
     */
    @RequiresPermissions("system:sysAreaSetting:add")
    @PostMapping
    public AjaxResult add(@RequestBody SysAreaSetting sysAreaSetting)
    {
        sysAreaSetting.setAreaType(StringUtils.join(sysAreaSetting.getAreaTypeArr(),","));
        return toAjax(sysAreaSettingService.insertSysAreaSetting(sysAreaSetting));
    }

    /**
     * 修改区域设置
     */
    @RequiresPermissions("system:sysAreaSetting:edit")
    @PutMapping
    public AjaxResult edit(@RequestBody SysAreaSetting sysAreaSetting)
    {
        sysAreaSetting.setAreaType(StringUtils.join(sysAreaSetting.getAreaTypeArr(),","));
        return toAjax(sysAreaSettingService.updateSysAreaSetting(sysAreaSetting));
    }

    /**
     * 逻辑删除区域设置
     */
    @RequiresPermissions("system:sysAreaSetting:abandon")
    @PutMapping("/abandon/{areaIds}")
    public AjaxResult logicalDelete(@PathVariable Long[] areaIds)
    {
        return toAjax(sysAreaSettingService.logicalDeleteByAreaIds(areaIds));
    }

    /**
     * 删除区域设置
     */
    @RequiresPermissions("system:sysAreaSetting:remove")
	@DeleteMapping("/{areaIds}")
    public AjaxResult remove(@PathVariable Long[] areaIds)
    {
        return toAjax(sysAreaSettingService.deleteSysAreaSettingByAreaIds(areaIds));
    }

    @ApiOperation(value = "通过位置查询区域")
    @GetMapping("/getAreaSettingInfo")
    public AjaxResult getAreaSettingInfo(String locationIds,String attribute) {
        List<SysAreaSetting> list = sysAreaSettingService.getAreaSettingInfo(locationIds,attribute);
        return AjaxResult.success(list);
    }

    /**
     * 获取当前租户所有的区域列表
     */
    @GetMapping("/list/getAreaNameByIds")
    public AjaxResult getAreaNameByIds(@RequestParam("areaIdStr") String areaIdStr) {
        List<Long> areaIds = new ArrayList<>();
        if(areaIdStr!=null){
            if(areaIdStr.contains(",")){
                areaIds = Arrays.stream(areaIdStr.split(","))
                        .map(Long::parseLong)
                        .collect(Collectors.toList());
            } else {
                Long areaId = Long.parseLong(areaIdStr);
                areaIds.add(areaId);
            }

        }
        List<String> list = sysAreaSettingService.getAreaNameByIds(areaIds);
        return AjaxResult.success(list);
    }

    @GetMapping("/list/getAreaListByIds")
    public AjaxResult getAreaListByIds(@RequestParam("areaIdList") List<Long> areaIdList) {
        List<SysAreaSetting> list = sysAreaSettingService.selectAreaNameByIds(areaIdList);
        return AjaxResult.success(list);
    }
}
