package com.ruoyi.ibs.draw.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.R;
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.draw.domain.dto.grid.*;
import com.ruoyi.ibs.draw.domain.vo.DrawGridCustVO;
import com.ruoyi.ibs.draw.domain.vo.DrawGridListVO;
import com.ruoyi.ibs.draw.domain.vo.DrawGridVO;
import com.ruoyi.ibs.draw.service.DrawGridCustService;
import com.ruoyi.ibs.draw.service.DrawGridService;
import com.ruoyi.ibs.grid.domain.dto.DeleteDTO;
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 javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 吴凯程
 * @Date 2024/12/26
 **/
@Api(tags = "特色网格接口")
@RestController
@RequestMapping("/draw/grid")
public class DrawGridController extends BaseController {

    @Resource
    private DrawGridService drawGridService;

    @Resource
    private DrawGridCustService drawGridCustService;


    @ApiOperation("网格信息")
    @Log(title = "特色网格-特色网格信息")
    @GetMapping("/{gridId}")
    public R<DrawGridVO> getDrawGrid(@PathVariable Long gridId) {
        return R.ok(drawGridService.getDrawGrid(gridId));
    }

    @ApiOperation("分页获取网格列表")
    @Log(title = "特色网格-分页获取网格列表")
    @GetMapping("/list")
    public TableDataPageInfo<DrawGridListVO> drawGridCustService(DrawGridListDTO drawGridListDTO) {
        Page<Object> page = startPage();
        List<DrawGridListVO> gridList = drawGridService.getGridList(drawGridListDTO);
        return getDataTable(gridList, page);
    }

    @ApiOperation("分页获取网格内客户列表")
    @Log(title = "特色网格-分页获取网格内客户列表")
    @GetMapping("/cust/list")
    public TableDataPageInfo<DrawGridCustVO> getCustListByGrid(DrawGridCustListDTO drawGridCustListDTO) {
        drawGridCustListDTO.setUserName(SecurityUtils.getUsername());
        Page<Object> page = startPage();
        List<DrawGridCustVO> custListByGrid = drawGridCustService.getCustListByGrid(drawGridCustListDTO);
        return getDataTable(custListByGrid, page);
    }


    @ApiOperation("获取预计客户数")
    @Log(title = "特色网格-获取预计客户数")
    @GetMapping("/cust/count")
    public R<CustCountDTO> getExpectedCustCount(@Valid @NotNull @NotEmpty Long[] shapeIds) {
        CustCountDTO custCountDTO = new CustCountDTO();
        List<CustCountDTO> collect = Arrays.stream(shapeIds).map(shapeId -> drawGridCustService.countCustByShape(shapeId)).collect(Collectors.toList());
        custCountDTO.accumulate(collect);
        return R.ok(custCountDTO);
    }

}
