package com.ruoyi.ibs.grid.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ibs.grid.domain.dto.*;
import com.ruoyi.ibs.grid.domain.excel.RegionSecGridExcelDTO;
import com.ruoyi.ibs.grid.domain.excel.RegionSecGridExcelImportVO;
import com.ruoyi.ibs.grid.domain.excel.RegionTopGridExcelDTO;
import com.ruoyi.ibs.grid.domain.vo.*;
import com.ruoyi.ibs.grid.service.RegionGridService;
import com.ruoyi.ibs.grid.service.RegionGridUtilsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author 吴凯程
 * @Date 2024/9/18
 **/
@Api(tags = "地理网格接口")
@RestController
@RequestMapping("/grid/region")
public class RegionGridController extends BaseController {
    @Resource
    private RegionGridService regionGridService;

    @Resource
    private RegionGridUtilsService regionGridUtilsService;

    @ApiOperation("新增一级网格")
    @Log(title = "地理网格-新增一级网格", businessType = BusinessType.INSERT)
    @PostMapping("/top/create")
    public R<String> createTopRegionGrid(@RequestBody @Valid TopRegionGridDTO topRegionGridDTO){
        return R.ok(regionGridService.createTopRegionGrid(topRegionGridDTO)) ;
    }

    @ApiOperation("新增二级网格")
    @Log(title = "地理网格-新增二级网格", businessType = BusinessType.INSERT)
    @PostMapping("/sec/create")
    public R<String> createTopRegionGrid(@RequestBody @Valid SecRegionGridDTO secRegionGridDTO){
        return R.ok(regionGridService.createSecRegionGrid(secRegionGridDTO)) ;
    }

    @ApiOperation("获取一级网格列表")
    @Log(title = "地理网格-获取一级网格列表")
    @GetMapping("/top/list")
    public R<List<TopGridSelectVO>> getTopGridList(){
        return R.ok(regionGridUtilsService.getTopGridList());
    }

    @ApiOperation("获取一级网格归属行政区划地址树")
    @Log(title = "地理网格-获取一级网格归属行政区划地址树")
    @GetMapping("/top/address/tree")
    R<List<AddressTreeVO>> getTopGridAddressTree(Long gridId){
        String gridOpsDept = regionGridUtilsService.getGridOpsDept(gridId);
        List<String> selectedRegionCodes = regionGridUtilsService.getSelectedRegionCodes("2", gridOpsDept);
        return R.ok(regionGridUtilsService.getTopGridAddressTree(gridId, selectedRegionCodes));
    }


    @ApiOperation("编辑一级网格")
    @Log(title = "地理网格-编辑一级网格", businessType = BusinessType.UPDATE)
    @PostMapping("/top/update")
    public R<String> updateRegionGrid(@RequestBody @Valid TopGridUpdateDTO topGridUpdateDTO){
        return R.ok(regionGridService.updateTopRegionGrid(topGridUpdateDTO));
    }
    @ApiOperation("编辑二级网格")
    @Log(title = "地理网格-编辑二级网格", businessType = BusinessType.UPDATE)
    @PostMapping("/sec/update")
    public R<String> updateRegionGrid(@RequestBody @Valid SecGridUpdateDTO secGridUpdateDTO){
        return R.ok(regionGridService.updateSecRegionGrid(secGridUpdateDTO));
    }

    @ApiOperation("获取网格详细信息")
    @Log(title = "地理网格-获取网格详细信息")
    @GetMapping("/{gridId}")
    public R<RegionGridVO> getRegionGridDetail(@PathVariable Long gridId){
        return R.ok(regionGridService.getRegionGridDetail(gridId));
    }

    @ApiOperation("删除网格")
    @Log(title = "地理网格-删除网格", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    public R<String> deleteRegionGrid(@RequestBody @Valid DeleteDTO deleteDTO){
        return R.ok(regionGridService.deleteRegionGrid(deleteDTO));
    }

    @ApiOperation("获取网格关联机构用户")
    @Log(title = "地理网格-获取网格关联机构用户")
    @GetMapping("/user/list")
    public R<List<GridUserVO>> getUserListByGridId(Long gridId){
        return R.ok(regionGridUtilsService.getUserListByGridId(gridId));
    }

    @ApiOperation("获取预计客户数")
    @Log(title = "地理网格-获取预计客户数")
    @PostMapping("/cust/count")
    public R<Long> getUserListByGridId(@Valid @RequestBody RegionGridCustCountDTO regionGridCustCountDTO) {
        return R.ok(regionGridUtilsService.getExpectedCustCount(regionGridCustCountDTO.getCollect()));
    }

    @ApiOperation("获取已选择行政区划")
    @Log(title = "地理网格-获取已选择行政区划")
    @GetMapping("/select/code")
    public R<List<String>> getSelectedRegionCodes(String gridLevel) {
        return R.ok(regionGridUtilsService.getSelectedRegionCodes(gridLevel, null));
    }

    @ApiOperation("检查网格名称是否重复")
    @Log(title = "地理网格-检查网格名称是否重复")
    @GetMapping("/check/name")
    public R<Boolean> getSelectedRegionCodes(@ApiParam(value = "网格名称", name = "gridName") String gridName,
                                             @ApiParam(value = "网格等级", name = "gridLevel") String gridLevel) {
        return R.ok(regionGridUtilsService.checkGridNameExist(gridName, gridLevel));
    }

    @ApiOperation("加载每个行政区划的客户数")
    @Log(title = "地理网格-加载每个行政区划的客户数")
    @GetMapping("/cust/count/init")
    public R<String> loadRegionCustCount() {
        regionGridUtilsService.loadRegionCustCount(965000L, 901000L);
        return R.ok("加载每个行政区划的客户数完成");
    }

    @ApiOperation("加载每个行政区划的客户数")
    @Log(title = "地理网格-加载每个行政区划的客户数")
    @GetMapping("/cust/count")
    public R<List<Long>> getRegionCustList(Long gridId) {
        return R.ok(regionGridUtilsService.getGridCustCount(gridId));
    }


    @ApiOperation("导出批量创建一级网格模板")
    @Log(title = "地理网格-导出批量创建一级网格模板")
    @PostMapping("/template/export")
    public void exportTopGridTemplate(HttpServletResponse response) {
        ExcelUtil<RegionTopGridExcelDTO> util = new ExcelUtil<>(RegionTopGridExcelDTO.class);
        util.exportExcel(response, null, "一级网格模板");
    }

    @ApiOperation("导出批量创建一级网格结果")
    @Log(title = "地理网格-导出批量创建一级网格结果")
    @PostMapping("/template/result/export")
    public void exportTopGridTemplate(HttpServletResponse response, @RequestBody List<RegionTopGridExcelVO> regionTopGridExcelVOS) {
        ExcelUtil<RegionTopGridExcelVO> util = new ExcelUtil<>(RegionTopGridExcelVO.class);
        util.exportExcel(response, regionTopGridExcelVOS, "一级网格创建结果");
    }

    @ApiOperation("导入批量创建一级网格信息")
    @Log(title = "地理网格-导入批量创建一级网格信息")
    @PostMapping("/template/import")
    public R<List<RegionTopGridExcelDTO>> importTopGridTemplate(MultipartFile file) throws Exception {
        ExcelUtil<RegionTopGridExcelDTO> util = new ExcelUtil<>(RegionTopGridExcelDTO.class);
        List<RegionTopGridExcelDTO> regionTopGridExcelDTOS = util.importExcel(file.getInputStream());
        regionTopGridExcelDTOS.forEach(regionTopGridExcelDTO -> {
            if (!regionTopGridExcelDTO.isValid()) {
                throw new ServiceException("模板存在异常空数据！");
            }
        });
        return R.ok(regionTopGridExcelDTOS);
    }


    @ApiOperation("提交创建一级网格模板")
    @Log(title = "地理网格-提交创建一级网格模板")
    @PostMapping("/template/create")
    public TemplateResultVO importTopGridData(@RequestBody @Valid RegionTopGridExcelDTO regionTopGridExcelDTO) {
        TemplateResultVO templateResultVO = new TemplateResultVO();
        try {
            String result = regionGridService.createTopRegionGridByTemplate(regionTopGridExcelDTO);
            templateResultVO.setResultStatus(Boolean.TRUE);
            templateResultVO.setMsg(result);
            return templateResultVO;
        } catch (Exception e) {
            templateResultVO.setResultStatus(Boolean.FALSE);
            templateResultVO.setMsg(e.getMessage());
            return templateResultVO;
        }
    }

    @ApiOperation("导出批量创建二级网格模板")
    @Log(title = "地理网格-导出批量创建二级网格模板")
    @PostMapping("/template/sec/export")
    public void exportSecGridTemplate(HttpServletResponse response)  {
        regionGridUtilsService.updateTopGridNameLabelToRedis();
        ExcelUtil<RegionSecGridExcelDTO> util = new ExcelUtil<>(RegionSecGridExcelDTO.class);
        util.exportExcel(response, null, "二级网格模板");
    }

    @ApiOperation("导入二级网格模板")
    @Log(title = "地理网格-导入二级网格模板")
    @PostMapping("/template/sec/import")
    public R<List<RegionSecGridExcelImportVO>> importSecGridTemplate(MultipartFile file) throws Exception {
        ExcelUtil<RegionSecGridExcelImportVO> util = new ExcelUtil<>(RegionSecGridExcelImportVO.class);
        List<RegionSecGridExcelImportVO> regionSecGridExcelDTOS = util.importExcel(file.getInputStream());
        regionSecGridExcelDTOS.forEach(regionSecGridExcelDTO -> {
            if (!regionSecGridExcelDTO.isValid()) {
                throw new ServiceException("模板存在异常空数据！");
            }
        });
        return R.ok(regionSecGridExcelDTOS);
    }

    @ApiOperation("根据二级网格模板创建")
    @Log(title = "地理网格-根据二级网格模板创建")
    @PostMapping("/template/sec/create")
    public TemplateResultVO exportSecGridTemplate(@RequestBody RegionSecGridExcelDTO regionSecGridExcelDTO)  {
        TemplateResultVO templateResultVO = new TemplateResultVO();
        try {
            String result = regionGridService.createSecRegionGridByTemplate(regionSecGridExcelDTO);
            templateResultVO.setResultStatus(Boolean.TRUE);
            templateResultVO.setMsg(result);
            return templateResultVO;
        } catch (Exception e) {
            templateResultVO.setResultStatus(Boolean.FALSE);
            templateResultVO.setMsg(e.getMessage());
            return templateResultVO;
        }
    }

    @ApiOperation("导出批量创建二级网格结果")
    @Log(title = "地理网格-导出批量创建二级网格结果")
    @PostMapping("/template/sec/result/export")
    public void exportSecGridTemplate(HttpServletResponse response, @RequestBody List<RegionSecGridExcelVO> regionSecGridExcelVOS) {
        ExcelUtil<RegionSecGridExcelVO> util = new ExcelUtil<>(RegionSecGridExcelVO.class);
        util.exportExcel(response, regionSecGridExcelVOS, "二级网格创建结果");
    }

}
