package com.ruoyi.ibs.grid.controller;

import com.github.pagehelper.Page;
import com.ruoyi.common.annotation.Anonymous;
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.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ibs.grid.domain.dto.GridCustListDTO;
import com.ruoyi.ibs.grid.domain.dto.RegionGridListDTO;
import com.ruoyi.ibs.grid.domain.entity.RegionGrid;
import com.ruoyi.ibs.grid.domain.vo.RegionCustUserVO;
import com.ruoyi.ibs.grid.domain.vo.RegionGridGroupVO;
import com.ruoyi.ibs.grid.domain.vo.RegionGridListVO;
import com.ruoyi.ibs.grid.mapper.RegionGridMapper;
import com.ruoyi.ibs.grid.service.RegionGridListService;
import com.ruoyi.ibs.grid.service.RegionGridUpdateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Author 吴凯程
 * @Date 2025/1/7
 **/
@Api(tags = "地理网格列表接口")
@RestController
@RequestMapping("/grid/region")
public class RegionGridListController extends BaseController {

    @Resource
    private RegionGridListService regionGridListService;

    @Resource
    private RegionGridMapper regionGridMapper;

    @Resource
    private RegionGridUpdateService regionGridUpdateService;

    @ApiOperation("分页查询网格内客户列表")
    @Log(title = "地理网格-分页查询网格内客户列表" )
    @GetMapping("/cust/list")
    public TableDataPageInfo<RegionCustUserVO> selectCustFromGrid(@Valid GridCustListDTO gridCustListDTO) {
        gridCustListDTO.setUserName(SecurityUtils.getUsername());
        gridCustListDTO.setDeptId(SecurityUtils.getDeptId());
        RegionGrid regionGrid = regionGridMapper.selectById(gridCustListDTO.getGridId());
        Page<Object> page = startPage();
        List<RegionCustUserVO> retailCust;
        if (regionGrid.getOpsDept().equals("0") && gridCustListDTO.getCustType().equals("2")) {
            return getDataTable(null, page);
        }
        if (regionGrid.getOpsDept().equals("1") && (gridCustListDTO.getCustType().equals("0") || gridCustListDTO.getCustType().equals("1"))) {
            return getDataTable(null, page);
        }
        retailCust = regionGridListService.selectCustFromGrid(gridCustListDTO, regionGrid);
//        if (SecurityUtils.userRole().equals("manager")) {
//            retailCust = regionGridListService.selectCustFromGridByManager(gridCustListDTO, regionGrid);
//        } else {
//            retailCust = regionGridListService.selectCustFromGrid(gridCustListDTO, regionGrid);
//        }
        return getDataTable(retailCust, page);
    }

    @ApiOperation("分页查询网格列表")
    @Log(title = "地理网格-分页查询网格列表" )
    @GetMapping("/list")
    public TableDataPageInfo<RegionGridListVO> getTopGridList(@Valid RegionGridListDTO regionGridListDTO) {
        regionGridListDTO.setUserName(SecurityUtils.getUsername());
        regionGridListDTO.setDeptId(SecurityUtils.getDeptId());
        List<RegionGridListVO> gridListVOS = new ArrayList<>();
        Page<Object> page = startPage();
        if (regionGridListDTO.getGridLevel().equals("1")) {
            gridListVOS = regionGridListService.getTopGridList(regionGridListDTO);
        } else if (regionGridListDTO.getGridLevel().equals("2")) {
            gridListVOS = regionGridListService.getSecGridList(regionGridListDTO);
        }
        return getDataTable(gridListVOS, page);
    }


    @ApiOperation("导出管护列表")
    @Log(title = "地理网格-导出管护列表" )
    @PostMapping("/cust/export")
    public void exportData(HttpServletResponse response, @RequestBody List<RegionCustUserVO> regionCustUserVOS) {
        ExcelUtil<RegionCustUserVO> util = new ExcelUtil<RegionCustUserVO>(RegionCustUserVO.class);
        util.exportExcel(response, regionCustUserVOS, "管户列表");
    }

    @Anonymous
    @ApiOperation("全量管护更新")
    @PostMapping("/update/all")
    public void updateCurrentGrid() {
        regionGridUpdateService.updateCurrentGrid();
    }

    @Anonymous
    @ApiOperation("全量管护更新")
    @GetMapping("/update/dept")
    public AjaxResult updateCurrentGridByDept(@RequestParam String dept) {
        regionGridUpdateService.updateCurrentGrid(dept);
        return AjaxResult.success("开始更新行社所有网格");
    }

    /**
     * 查询地理网格列表（专用于客群创建）
     */
    @ApiOperation("查询地理网格列表（专用于客群创建）")
    @Log(title = "地理网格-查询地理网格列表（客群创建用）")
    @GetMapping("/groupList")
    public AjaxResult getRegionGridListForGroup(RegionGridListDTO dto) {
        return AjaxResult.success(regionGridListService.getRegionGridListForGroup(dto));
    }

}
