package com.ruoyi.web.controller.zhdd;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.zhdd.domain.vo.ResourceVo;
import com.ruoyi.zhdd.domain.bo.ResourceBo;
import com.ruoyi.zhdd.service.IResourceService;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 应急资源Controller
 *
 * @author xintong
 * @date 2021-10-27
 */
@Validated
@Api(value = "应急资源控制器", tags = {"应急资源管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/zhdd/resource")
public class ResourceController extends BaseController {

    private final IResourceService iResourceService;

    /**
     * 查询应急资源列表
     */
    @ApiOperation("查询应急资源列表")
//    @PreAuthorize("@ss.hasPermi('zhdd:resource:list')")
    @GetMapping("/list")
    public TableDataInfo<ResourceVo> list(@Validated(QueryGroup.class) ResourceBo bo) {
        return iResourceService.queryPageList(bo);
    }

    /**
     * 导出应急资源列表
     */
    @ApiOperation("导出应急资源列表")
//    @PreAuthorize("@ss.hasPermi('zhdd:resource:export')")
    @Log(title = "应急资源", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(@Validated ResourceBo bo, HttpServletResponse response) {
        List<ResourceVo> list = iResourceService.queryList(bo);
        ExcelUtil.exportExcel(list, "应急资源", ResourceVo.class, response);
    }

    /**
     * 获取应急资源详细信息
     */
    @ApiOperation("获取应急资源详细信息")
//    @PreAuthorize("@ss.hasPermi('zhdd:resource:query')")
    @GetMapping("/{id}")
    public AjaxResult<ResourceVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") String id) {
        return AjaxResult.success(iResourceService.queryById(id));
    }

    /**
     * 新增应急资源
     */
    @ApiOperation("新增应急资源")
//    @PreAuthorize("@ss.hasPermi('zhdd:resource:add')")
    @Log(title = "应急资源", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody ResourceBo bo) {
        return toAjax(iResourceService.insertResource(bo) ? 1 : 0);
    }

    /**
     * 修改应急资源
     */
    @ApiOperation("修改应急资源")
//    @PreAuthorize("@ss.hasPermi('zhdd:resource:edit')")
    @Log(title = "应急资源", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody ResourceBo bo) {
        return toAjax(iResourceService.updateResource(bo) ? 1 : 0);
    }

    /**
     * 删除应急资源
     */
    @ApiOperation("删除应急资源")
//    @PreAuthorize("@ss.hasPermi('zhdd:resource:remove')")
    @Log(title = "应急资源" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable String[] ids) {
        return toAjax(iResourceService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    @ApiOperation("查询所有应急资源信息")
    @GetMapping("/location")
    public AjaxResult<Map<String,List<ResourceVo>>> queryResourceLocation(ResourceBo bo) {
        List<ResourceVo> list = iResourceService.queryList(bo);
        Map<Integer, List<ResourceVo>> collect = list.stream().collect(Collectors.groupingBy(ResourceVo::getResourceType, Collectors.mapping(a -> a, Collectors.toList())));
        Map<String,List<ResourceVo>> result = new HashMap<>();
        result.put("应急仓库", CollUtil.defaultIfEmpty(collect.get(1), new ArrayList<>()));
        result.put("应急队伍", CollUtil.defaultIfEmpty(collect.get(2), new ArrayList<>()));
        result.put("应急车队", CollUtil.defaultIfEmpty(collect.get(3), new ArrayList<>()));
        return AjaxResult.success(result);
    }
}
