package com.ruoyi.ibs.grid.controller;


import com.github.pagehelper.Page;
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.page.TableDataPageInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ibs.grid.domain.dto.*;
import com.ruoyi.ibs.grid.domain.dto.virtual.VirtualGridCustExport;
import com.ruoyi.ibs.grid.domain.dto.virtual.VirtualGridSelectInfo;
import com.ruoyi.ibs.grid.service.GridInfoService;
import com.ruoyi.ibs.grid.service.IGridSelectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 网格查询 控制类
 *
 * @author ruoyi
 */
@Api(tags = "网格查询管理")
@RestController
@RequestMapping("/system/gridSelect")
public class GridSelectController extends BaseController {
    private static Logger logger= LoggerFactory.getLogger(GridSelectController.class);

    @Autowired
    private IGridSelectService gridSelectService;

    @Resource
    private GridInfoService gridInfoService;

    @GetMapping("/info")
    @Log(title = "网格查询-获取网格查询数据")
    @ApiOperation("获取网格查询数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tabGridType", value = "网格类型tab 1行政2特色3虚拟", dataType = "String", dataTypeClass = String.class, required = true)
    })
    public AjaxResult getGridInfo(@RequestParam String tabGridType) {
        if (SecurityUtils.userRole().equals("manager")) {
            return success(gridInfoService.getGirdInfoVoByManager(tabGridType));
        } else {
            return success(gridInfoService.getGirdInfoVo(tabGridType));
        }
    }


    /**
     * 获取机构数量、可选机构
     */
//    @PreAuthorize("@ss.hasPermi('system:getGridDeptNum')")
    @GetMapping("/list/griddeptnum")
    @ApiOperation("获取机构网格数量")
    @Log(title = "网格查询-获取机构网格数量")
    public AjaxResult getInfo(GridDeptNumSelect gridDeptNumSelect){
        return success(gridSelectService.getGridDeptNum(gridDeptNumSelect));
    }

    /**
     * 获取行政或特色网格列表
     */
//    @PreAuthorize("@ss.hasPermi('system:gridLevelSelect:list')")
    @GetMapping("/list/grid")
    @ApiOperation("获取行政或特色网格列表")
    @Log(title = "网格查询-获取行政或特色网格列表")
    public TableDataPageInfo<GridSelectInfo> list(GridSelectInfo gridSelectInfo){
        Page<Object> page = startPage();
        List<GridSelectInfo> gridSelectInfolist = gridSelectService.selectgridLevelInfoList(gridSelectInfo);
        return getDataTable(gridSelectInfolist,page);
    }

    /**
     * 获取虚拟网格列表
     */
//    @PreAuthorize("@ss.hasPermi('system:virtualGridSelect:list')")
    @GetMapping("/list/virtualgrid")
    @ApiOperation("获取虚拟网格列表")
    @Log(title = "网格查询-获取虚拟网格列表")
    public TableDataPageInfo<VirtualGridSelectInfo> list(VirtualGridSelectInfo virtualGridSelectInfo){
        Page<Object> page = startPage();
        List<VirtualGridSelectInfo> virtualGridSelectInfolist = gridSelectService.selectVirtualInfoList(virtualGridSelectInfo);
        return getDataTable(virtualGridSelectInfolist,page);
    }

    /**
     * 管护列表查询
     */
//    @PreAuthorize("@ss.hasPermi('system:custManage:list')")
    @GetMapping("/list/custManage")
    @ApiOperation("获取管护列表")
    @Log(title = "网格查询-获取管护列表")
    public TableDataPageInfo<CustManageInfo> list(CustManageInfo custManageSelect){
        Page<Object> page = startPage();
        List<CustManageInfo> custManageSelectlist = gridSelectService.custManageSelect(custManageSelect);
        return getDataTable(custManageSelectlist,page);
    }


    /**
     * 管护列表批量分配
     */
//    @PreAuthorize("@ss.hasPermi('system:custManage:listadd')")
    @Log(title = "网格查询-管护分配", businessType = BusinessType.UPDATE)
    @PostMapping("/list/upanddel")
    @ApiOperation("管护列表分配")
    public AjaxResult custManageaddlist(@RequestBody CustManageUpDTO custManageUpDTO)
    {
        return toAjax(gridSelectService.custManageListUpdate(custManageUpDTO));
    }

    /**
     * 管护列表解除前查询
     */
    @GetMapping("/list/selectManage/{gridId}{gridPattern}{custIds}{userName}")
    @ApiOperation("管护列表分配前查询")
    public TableDataPageInfo<CustManageSelectDTO> selectCustManagelist(@PathVariable("gridId") String gridId,@PathVariable("gridPattern") String gridPattern,@PathVariable("custIds") String[] custIds,@PathVariable("userName") String userName)
    {
        Page<Object> page = startPage();
        List<CustManageSelectDTO> custManageSelectDTOList = gridSelectService.selectCustManage(gridId,gridPattern,custIds,userName);
        return getDataTable(custManageSelectDTOList,page);
    }


//    @PreAuthorize("@ss.hasPermi('system:listexport')")
    @Log(title = "网格查询-管户列表，导出客户信息", businessType = BusinessType.IMPORT)
    @PostMapping("/exportList")
    @ApiOperation("管户列表，导出客户信息")
    public void exportData(HttpServletResponse response , CustManageInfo custManageSelect)
    {        String gridPattern = custManageSelect.getGridPattern();
        List<CustManageInfo> custManageInfoList = gridSelectService.custManageSelect(custManageSelect);
        if(gridPattern.equals("1")){
            List<RegionGridCustExport> regionGridCustExportList = new ArrayList<>();
            for(int i = 0 ; i < custManageInfoList.size() ; i++){
                logger.info(custManageInfoList.get(i).toString());
                RegionGridCustExport regionGridCustExport = new RegionGridCustExport();
                BeanUtils.copyProperties(custManageInfoList.get(i),regionGridCustExport);
                regionGridCustExportList.add(regionGridCustExport);
            }
            ExcelUtil<RegionGridCustExport> util = new ExcelUtil<RegionGridCustExport>(RegionGridCustExport.class);
            util.exportExcel(response, regionGridCustExportList, "管户列表");
        }else{
            List<VirtualGridCustExport> virtualGridCustExportList = new ArrayList<>();
            for(int i = 0 ; i < custManageInfoList.size() ; i++){
                VirtualGridCustExport virtualGridCustExport = new VirtualGridCustExport();
                BeanUtils.copyProperties(custManageInfoList.get(i),virtualGridCustExport);
                virtualGridCustExportList.add(virtualGridCustExport);
            }
            ExcelUtil<VirtualGridCustExport> util = new ExcelUtil<VirtualGridCustExport>(VirtualGridCustExport.class);
            util.exportExcel(response, virtualGridCustExportList, "管户列表");
        }
    }
}
