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.mybatisplus.aop.TemporaryIgnoreTables;
import com.vctgo.common.security.annotation.Logical;
import com.vctgo.common.security.annotation.RequiresPermissions;
import com.vctgo.common.security.utils.SecurityUtils;
import com.vctgo.system.api.RemoteTenantService;
import com.vctgo.system.api.domain.SysLocationSetting;
import com.vctgo.system.api.domain.SysReLocationClass;
import com.vctgo.system.api.domain.SysTenant;
import com.vctgo.system.api.domain.dto.SysLocationDto;
import com.vctgo.system.api.domain.dto.SysLocationReClassDto;
import com.vctgo.system.api.domain.dto.TemporaryConfig;
import com.vctgo.system.domain.dto.SysLocationAttributeDto;
import com.vctgo.system.service.ISysLocationSettingService;
import com.vctgo.system.util.ConstantUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
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.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 位置设置Controller
 *
 * @author caesar
 * @date 2023-05-30
 */
@Api(tags = "位置设置")
@RestController
@RequestMapping("/sysLocationSetting")
public class SysLocationSettingController extends BaseController {

    @Autowired
    private ISysLocationSettingService sysLocationSettingService;

    @Autowired
    private RemoteTenantService remoteTenantService;

    /**
     * 分页查询位置设置列表--区域组合在一起
     */
    @ApiOperation(value = "分页查询位置设置列表--区域组合在一起")
    @RequiresPermissions(value = {"system:sysLocationSetting:page",
            "toy:kindergarten:sysLocationSetting:page","book:info:kindergarten:query"}, logical = Logical.OR)
    @GetMapping("/page")
    public AjaxResult list(SysLocationSetting sysLocationSetting) {
        IPage<SysLocationSetting> list = sysLocationSettingService.selectSysLocationSettingPage(sysLocationSetting);
        return AjaxResult.success(list);
    }

    /**
     * 分页查询位置设置列表--区域组合在一起
     */
    @ApiOperation(value = "图书投放列表分页查询--区域组合在一起")
    @RequiresPermissions(value = {"book:info:kindergarten:query"}, logical = Logical.OR)
    @GetMapping("/book/page")
    public AjaxResult bookList(SysLocationSetting sysLocationSetting) {
        IPage<SysLocationSetting> list = sysLocationSettingService.selectBookSysLocationSettingPage(sysLocationSetting);
        return AjaxResult.success(list);
    }

    /**
     * 分页查询位置设置列表--区域分开，一条记录，一个区域
     */
    @ApiOperation(value = "分页查询位置设置列表--区域分开，一条记录，一个区域")
    @GetMapping("/listDistinguishArea")
    public AjaxResult listDistinguishArea(SysLocationSetting sysLocationSetting) {
        return AjaxResult.success(sysLocationSettingService.listDistinguishArea(sysLocationSetting));
    }

    /**
     * 查询当前幼儿园投放位置的类型列表
     */
    @ApiOperation(value = "查询当前幼儿园投放位置的类型列表")
    @GetMapping("/type/list")
    public AjaxResult listType() {
        return AjaxResult.success(sysLocationSettingService.listType());
    }

    @ApiOperation(value = "")
    @GetMapping("/locationAndAreaList/{tenantId}")
    public AjaxResult locationAndAreaList(@PathVariable String tenantId){
        return AjaxResult.success(sysLocationSettingService.locationAndAreaList(tenantId));
    }

    /**
     * 通过位置类型获取对应的位置列表
     */
    @ApiOperation(value = "通过位置类型获取对应的位置列表")
    @TemporaryIgnoreTables
    //@RequiresPermissions("system:sysLocationSetting:type")
    @GetMapping("/selectLocationListByType/{type}")
    public AjaxResult selectLocationListByType(@PathVariable String type) {
        return AjaxResult.success(sysLocationSettingService.selectLocationListByType(type));
    }

    /**
     * 通过参数获取到所有符合条件的位置id
     */
    @ApiOperation(value = "通过参数获取到所有符合条件的位置id")
    @PostMapping("/locationIdList")
    public AjaxResult locationIdList(@RequestBody SysLocationSetting sysLocationSetting) {
        return AjaxResult.success(sysLocationSettingService.locationIdList(sysLocationSetting));
    }

    /**
     * 通过位置Id列表获取位置信息
     */
    @TemporaryIgnoreTables(temporaryIgnoreTables = "sys_location_setting")
    @ApiOperation(value = "通过位置Id列表获取位置信息")
    @GetMapping("/locationId/list")
    public AjaxResult locationIdList(Long[] locationIdArray) {
        return AjaxResult.success(sysLocationSettingService.locationIdList(locationIdArray));
    }

    /**
     * 全量查询位置设置列表,需要添加空数组参数
     */
    @TemporaryIgnoreTables
    @ApiOperation(value = "全量查询位置设置列表,需要添加空数组参数")
    @GetMapping("/external/page")
    public AjaxResult allList(@SpringQueryMap TemporaryConfig temporaryConfig) {
        if (CollectionUtils.isNotEmpty(temporaryConfig.getIgnoreTables())) {
            SecurityUtils.setTemporaryIgnoreTables(temporaryConfig.getIgnoreTables());
        }
        return AjaxResult.success(sysLocationSettingService.selectSysLocationSettingList(SysLocationSetting.builder().isDeleted("0").build()));
    }

    /**
     * 根据位置信息查询其对应的班级信息
     */
    @ApiOperation(value = "根据位置信息查询其对应的班级信息")
    @GetMapping("/external/class/list")
    public AjaxResult getRelatedClassList(@SpringQueryMap SysLocationReClassDto sysLocationReClassDto) {
        sysLocationReClassDto.setIsDeleted(ConstantUtils.IS_DELETED_0);
        return AjaxResult.success(sysLocationSettingService.getRelatedClassList(sysLocationReClassDto));
    }

    /**
     * 根据位置信息查询其对应的班级信息
     */
    @ApiOperation(value = "查询全系统的位置列表")
    @GetMapping("/sys/location/list")
    public AjaxResult getSysLocationList(@SpringQueryMap SysLocationSetting sysLocationSetting) {
        List<SysLocationSetting> sysLocationSettingList = sysLocationSettingService.getSysLocationList(sysLocationSetting);
        Map<Long, List<SysLocationSetting>> groupByMap = sysLocationSettingList.stream().collect(Collectors.groupingBy(SysLocationSetting::getTenantId));
        List<SysTenant> sysTenantList = remoteTenantService.allTenantList().getData();
        sysTenantList.stream().forEach(obj-> obj.getParams().put("locationList", groupByMap.get(obj.getId())));
        return AjaxResult.success(sysTenantList);
    }

    /**
     * 导出位置设置列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLocationSetting sysLocationSetting) {
        List<SysLocationSetting> list = sysLocationSettingService.selectSysLocationSettingList(sysLocationSetting);
        ExcelUtil<SysLocationSetting> util = new ExcelUtil<>(SysLocationSetting.class);
        util.exportExcel(response, list, "位置设置数据");
    }

    /**
     * 获取位置设置详细信息
     */
    @RequiresPermissions("system:sysLocationSetting:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysLocationSettingService.selectSysLocationSettingById(id));
    }

    /**
     * 根据位置id获取位置名称的集合
     */
    @GetMapping(value = "/name/{locationIdStr}")
    public AjaxResult getLocationNameByLocationId(@PathVariable(value = "locationIdStr") String locationIdStr)
    {
        if(StringUtils.isBlank(locationIdStr)){
            throw new IllegalArgumentException("参数异常，请检查");
        }
        SysLocationSetting location = sysLocationSettingService.selectSysLocationSettingById(Long.valueOf(locationIdStr));
        return AjaxResult.success(location.getName());
    }

    /**
     * 通过名称查询位置是否重复
     */
    @RequiresPermissions("system:sysLocationSetting:check")
    @PostMapping(value = "/repeatCheck")
    public AjaxResult repeatCheck(@RequestBody SysLocationSetting sysLocationSetting) {
        return success((sysLocationSettingService.repeatCheck(sysLocationSetting)));
    }

    /**
     * 新增位置设置
     */
    @RequiresPermissions("system:sysLocationSetting:add")
    @PostMapping
    public AjaxResult add(@RequestBody SysLocationSetting sysLocationSetting) {
        return toAjax(sysLocationSettingService.insertSysLocationSetting(sysLocationSetting));
    }

    /**
     * 修改位置设置
     */
    @RequiresPermissions("system:sysLocationSetting:edit")
    @PutMapping
    public AjaxResult edit(@RequestBody SysLocationSetting sysLocationSetting) {
        return toAjax(sysLocationSettingService.updateSysLocationSetting(sysLocationSetting));
    }

    /**
     * 删除位置设置
     */
    @RequiresPermissions("system:sysLocationSetting:remove")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysLocationSettingService.deleteSysLocationSettingByIds(ids));
    }

    /**
     * 绑定或者解绑班级
     */
    @RequiresPermissions("system:sysLocationSetting:bind")
    @PostMapping("/bindOrUnBindClass")
    public AjaxResult bindOrUnBindClass(@RequestBody SysLocationSetting sysLocationSetting) {
        sysLocationSettingService.bindOrUnBindClass(sysLocationSetting);
        return AjaxResult.success();
    }


    /**
     * 根据位置信息查询其对应的班级信息
     */
    @ApiOperation(value = "根据位置信息查询其对应的班级信息")
    @GetMapping("/external/location/list")
    public AjaxResult getRelatedLocationList(@SpringQueryMap SysLocationReClassDto sysLocationReClassDto) {
        sysLocationReClassDto.setIsDeleted(ConstantUtils.IS_DELETED_0);
        return AjaxResult.success(sysLocationSettingService.getRelatedLocationList(sysLocationReClassDto));
    }

    /**
     * 根据位置信息查询其对应的班级信息
     */
    @ApiOperation(value = "根据位置信息查询其对应的班级信息")
    @GetMapping("/location/class/list/{locationId}")
    public AjaxResult selectClassByLocationId(@PathVariable String locationId) {
        return AjaxResult.success(sysLocationSettingService.selectClassByLocationId(locationId));
    }

    @ApiOperation(value = "通过参数获取到所有符合条件的位置id")
    @GetMapping("/getLocationIdList")
    public AjaxResult getLocationIdList(@SpringQueryMap SysLocationDto locationDto) {
        SysLocationSetting sysLocationSetting = new SysLocationSetting();
        sysLocationSetting.setAreaId(locationDto.getAreaId());
        sysLocationSetting.setClassNumber(locationDto.getClassNumber());
        sysLocationSetting.setGradeIn(locationDto.getGradeIn());
        sysLocationSetting.setName(locationDto.getName());
        sysLocationSetting.setType(locationDto.getType());
        sysLocationSetting.setIsDeleted(ConstantUtils.IS_DELETED_0);
        List<SysLocationDto> sysLocationDtoList = new ArrayList<>();
        List<SysLocationSetting> sysLocationSettingList = sysLocationSettingService.locationIdList(sysLocationSetting);
        sysLocationDtoList = sysLocationSettingList.stream().map(obj->{
            SysLocationDto sysLocationDto = new SysLocationDto();
            BeanUtils.copyProperties(obj,sysLocationDto);
            return sysLocationDto;
        }).collect(Collectors.toList());
        return AjaxResult.success(sysLocationDtoList);
    }

    @ApiOperation(value = "通过参数获取到所有符合条件的位置id")
    @GetMapping("/getLocationSettingInfo")
    public AjaxResult getLocationSettingInfo(String attribute) {
        List<SysLocationSetting> list = sysLocationSettingService.getLocationSettingInfo(attribute);
        return AjaxResult.success(list);
    }

    @ApiOperation(value = "获取位置的树结构")
    @GetMapping("/getLocationSettingTree")
    public AjaxResult getLocationSettingTree() {
        List<SysLocationAttributeDto> list = sysLocationSettingService.getLocationSettingTree();
        return AjaxResult.success(list);
    }

    @GetMapping("/classId/{classId}")
    public AjaxResult getLocationSettingInfoByClassId(@PathVariable String classId) {
        List<SysReLocationClass> list = sysLocationSettingService.getLocationSettingInfoByClassId(classId);
        return AjaxResult.success(list);
    }

    @ApiOperation(value = "通过参数获取到所有符合条件的位置id")
    @GetMapping("/selectLocationSettingInfo/{attribute}")
    public AjaxResult selectLocationSettingInfo(@PathVariable("attribute")String attribute) {
        List<SysLocationSetting> list = sysLocationSettingService.getLocationSettingInfo(attribute);
        return AjaxResult.success(list);
    }

    @GetMapping("/classIds/{classIds}")
    public AjaxResult getLocationSettingInfoByClassIds(@PathVariable String classIds) {
        List<SysReLocationClass> list = sysLocationSettingService.getLocationSettingInfoByClassIds(classIds);
        return AjaxResult.success(list);
    }

}
