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.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ibs.grid.domain.dto.RegionDataRemarkDTO;
import com.ruoyi.ibs.grid.domain.dto.RegionDataSelectDTO;
import com.ruoyi.ibs.grid.domain.entity.RegionDataRemark;
import com.ruoyi.ibs.grid.domain.vo.GridRegionVO;
import com.ruoyi.ibs.grid.domain.vo.RegionCustVO;
import com.ruoyi.ibs.grid.domain.vo.RegionDataVO;
import com.ruoyi.ibs.grid.domain.vo.RegionMapVO;
import com.ruoyi.ibs.grid.service.AddressAnalyseService;
import com.ruoyi.ibs.grid.service.RegionCustService;
import com.ruoyi.ibs.grid.service.RegionDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author 吴凯程
 * @Date 2024/10/14
 **/

@Api(tags = "行政区划数据展示接口")
@RestController
@RequestMapping("/region")
public class RegionDataController extends BaseController {

    @Resource
    private RegionDataService regionDataService;

    @Resource
    private RegionCustService regionCustService;

    @Resource
    private AddressAnalyseService adminDivisionService;


    @ApiOperation("获取机构关联行政区划列表")
    @GetMapping("/list")
    @Log(title = "行政区划数据展示-获取机构关联行政区划列表")
    public TableDataPageInfo<RegionDataVO> getRegionDataList(RegionDataSelectDTO regionDataSelectDTO) {
        List<String> regionList = null;
        if (SecurityUtils.isHead() || SecurityUtils.userRole().equals("head")) {
            regionList = regionDataService.getRegionListByHead();
        } else if (SecurityUtils.userRole().equals("branch") || SecurityUtils.userRole().equals("outlet")) {
            regionList = regionDataService.getRegionDataListByBranch();
        } else if (SecurityUtils.userRole().equals("manager")) {
            regionList = regionDataService.getRegionDataListByManager();
        } else if (SecurityUtils.userRole().equals("admin")){
            regionList = new ArrayList<>();
        }
        Page<Object> page = startPage();
        List<RegionDataVO> regionDataList = new ArrayList<>();
        if (SecurityUtils.userRole().equals("admin")){
            regionDataList = regionDataService.getRegionDataListByAdmin(regionDataSelectDTO);
            return getDataTable(regionDataList, page);
        }
        if (Objects.isNull(regionList)) {
            return getDataTable(null, page);
        }
        regionDataList = regionDataService.getRegionDataList(regionDataSelectDTO, regionList);
        return getDataTable(regionDataList, page);
    }

    @ApiOperation("获取区域围栏")
    @Log(title = "行政区划数据展示-获取区域围栏")
    @GetMapping("/coordinate")
    public R<RegionMapVO> getPointListByCode(String code) {
        return R.ok(adminDivisionService.getPointListByCode(code));
    }

    @ApiOperation("批量获取区域围栏")
    @Log(title = "行政区划数据展示-批量获取区域围栏")
    @PostMapping("/coordinate")
    public R<List<RegionMapVO>> getPointListByCode(@RequestBody @NotEmpty List<String> codeList) {
        List<RegionMapVO> output = new ArrayList<>();
        codeList.forEach(code -> {
            RegionMapVO regionMapVO = adminDivisionService.getPointListByCode(code);
            if (Objects.nonNull(regionMapVO)) {
                output.add(regionMapVO);
            }
        });
        return R.ok(output);
    }

    @ApiOperation("更新备注")
    @Log(title = "行政区划数据展示-更新备注")
    @PostMapping("/remark/update")
    public R<String> updateRegionRemark(@RequestBody @Valid RegionDataRemarkDTO regionDataRemarkDTO) {
        if (!SecurityUtils.isHead()) {
            throw new ServiceException("当前用户没有权限进行备注修改！");
        }
        return R.ok(regionDataService.updateRegionRemark(regionDataRemarkDTO));
    }

    @ApiOperation("获取备注")
    @Log(title = "行政区划数据展示-获取备注")
    @GetMapping("/remark")
    public R<RegionDataRemark> getRegionDataRemarkByCode(String code) {
        return R.ok(regionDataService.getRegionDataRemarkByCode(code));
    }

    @ApiOperation("获取单个网格地图围栏")
    @Log(title = "行政区划数据展示-获取单个网格地图围栏")
    @GetMapping("/grid")
    public R<GridRegionVO> getRegionDataRemarkByCode(Long gridId) {
        return R.ok(regionDataService.getGridRegionFence(gridId));
    }

    @ApiOperation("网格地图围栏")
    @Log(title = "行政区划数据展示-网格地图围栏")
    @PostMapping("/grid")
    public R<List<GridRegionVO>> getRegionDataRemarkByCode(@RequestBody @NotNull @NotEmpty List<Long> gridIds) {
        return R.ok(regionDataService.queryGridRegionFence(gridIds));
    }

    @ApiOperation("查看行政区划客户列表")
    @Log(title = "行政区划数据展示-查看行政区划客户列表")
    @GetMapping("/cust/list")
    public TableDataPageInfo<RegionCustVO> getRegionCustList(@RequestParam String code, @RequestParam String custType) {
        Page<Object> page = startPage();
        List<RegionCustVO> regionCustList = regionCustService.getRegionCustList(code, custType);
        return getDataTable(regionCustList, page);
    }


}
