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.page.TableDataPageInfo;
import com.ruoyi.common.utils.SecurityUtils;
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.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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
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);
        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);
    }



}
