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.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.grid.domain.dto.GridCustListDTO;
import com.ruoyi.ibs.grid.domain.dto.UnbindDTO;
import com.ruoyi.ibs.grid.domain.dto.virtual.VirtualGridListDTO;
import com.ruoyi.ibs.grid.domain.entity.VirtualCust;
import com.ruoyi.ibs.grid.domain.entity.VirtualGrid;
import com.ruoyi.ibs.grid.domain.vo.VirtualGridCustVO;
import com.ruoyi.ibs.grid.domain.vo.VirtualGridListVO;
import com.ruoyi.ibs.grid.domain.vo.VirtualGridVO;
import com.ruoyi.ibs.grid.mapper.VirtualGridMapper;
import com.ruoyi.ibs.grid.service.VirtualGridListService;
import com.ruoyi.ibs.grid.service.VirtualGridService;
import com.ruoyi.system.mapper.SysDeptMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;

/**
 * @Author 吴凯程
 * @Date 2024/9/20
 **/
@Api(tags = "虚拟网格接口")
@RestController
@RequestMapping("/grid/virtual")
public class VirtualGridListController extends BaseController {

    @Resource
    private VirtualGridService virtualGridService;

    @Resource
    private SysDeptMapper sysDeptMapper;

    @Resource
    private VirtualGridMapper virtualGridMapper;


    @Resource
    private VirtualGridListService virtualGridListService;


    @ApiOperation("分页获取网格内客户列表")
    @Log(title = "虚拟网格-分页获取网格内客户列表")
    @GetMapping("/cust/list")
    public TableDataPageInfo<VirtualGridCustVO> getCustListByGrid(GridCustListDTO gridCustListDTO) {
        VirtualGrid virtualGrid = virtualGridMapper.selectById(gridCustListDTO.getGridId());
        Page<Object> page = startPage();
        List<VirtualGridCustVO> custList;
        if (Objects.nonNull(gridCustListDTO.getIsUnrelate()) && gridCustListDTO.getIsUnrelate()) {
            custList = virtualGridListService.getUnrelateCustList(gridCustListDTO);
        } else {
            custList = virtualGridListService.getCustList(gridCustListDTO, virtualGrid);
        }
        return getDataTable(custList, page);
    }

    @ApiOperation("客户信息导出")
    @Log(title = "虚拟网格-客户信息导出", businessType = BusinessType.EXPORT)
    @PostMapping("/cust/export")
    public void export(HttpServletResponse response, @RequestBody List<VirtualGridCustVO> virtualGridCustVOS) {
        ExcelUtil<VirtualGridCustVO> util = new ExcelUtil<>(VirtualGridCustVO.class);
        util.exportExcel(response, virtualGridCustVOS, "虚拟网格客户信息");
    }

    @ApiOperation("变动客户与客户经理绑定关系")
    @Log(title = "虚拟网格-变动客户与客户经理绑定关系")
    @PostMapping("/cust/unbind")
    public R<String> unbindCustUser(@RequestBody UnbindDTO unbindDTO) {
        return R.ok(virtualGridListService.unbindCustUser(unbindDTO));
    }

    @ApiOperation("添加客户与客户经理关系")
    @Log(title = "虚拟网格-添加客户与客户经理关系")
    @PostMapping("/cust/add")
    public R<String> addCustUser(@RequestBody List<UnbindDTO> unbindDTOS) {
        return R.ok(virtualGridListService.addCustUser(unbindDTOS));
    }

    @ApiOperation("获取网格列表")
    @Log(title = "虚拟网格-获取网格列表")
    @GetMapping("/list")
    public TableDataPageInfo<VirtualGridListVO> getGridList(VirtualGridListDTO virtualGridListDTO) {
        virtualGridListDTO.setUserName(SecurityUtils.getUsername());
        virtualGridListDTO.setDeptId(SecurityUtils.getDeptId());
        List<VirtualGridListVO> gridList;
        Page<Object> page = startPage();
        if (SecurityUtils.userRole().equals("manager")) {
            gridList = virtualGridListService.getGridListByManager(virtualGridListDTO);
        } else {
            gridList = virtualGridListService.getGridList(virtualGridListDTO);
        }
        return getDataTable(gridList, page);
    }

    @ApiOperation("分页查询网格内客户列表，手动绑定关系用")
    @Log(title = "虚拟网格-分页查询网格内客户列表，手动绑定关系用")
    @GetMapping("/cust/search")
    public R<List<VirtualCust>> selectCustFromGrid(@RequestParam @NotNull Long gridId, String searchParam) {
        return R.ok(virtualGridListService.selectCustFromGrid(gridId, searchParam));
    }


    @ApiOperation("分配规则查询虚拟网格")
    @Log(title = "虚拟网格-分配规则查询虚拟网格")
    @GetMapping("/rule/search")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "opsDept", value = "业务归属", dataType = "String", dataTypeClass = String.class, required = true),
            @ApiImplicitParam(name = "gridName", value = "网格名称", dataType = "String", dataTypeClass = String.class),
    })
    public R<List<VirtualGridVO>> searchVirtualGrid(@RequestParam String opsDept, String gridName) {
        return R.ok(virtualGridListService.searchVirtualGrid(opsDept, gridName));
    }
}
