package com.ruoyi.ibs.custmap.controller;

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.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ibs.custmap.domain.dto.CustMapCircleDTO;
import com.ruoyi.ibs.custmap.domain.dto.CustMapCircleExcelDTO;
import com.ruoyi.ibs.custmap.domain.dto.CustMapPolygonDTO;
import com.ruoyi.ibs.custmap.domain.dto.CustMapPolygonExcelDTO;
import com.ruoyi.ibs.custmap.domain.vo.CustMapExcelVO;
import com.ruoyi.ibs.custmap.domain.vo.CustMapVO;
import com.ruoyi.ibs.custmap.service.CustMapExportService;
import com.ruoyi.ibs.custmap.service.CustMapSelectService;
import com.ruoyi.ibs.custmap.service.DeptAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

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

@Api(tags = "地图拓客接口")
@Slf4j
@RestController
@RequestMapping("/cust/map")
public class CustMapController extends BaseController {
    @Resource
    private CustMapSelectService custMapSelectService;

    @Resource
    private CustMapExportService custMapExportService;

    @Resource
    private DeptAddressService deptAddressService;

    @ApiOperation("坐标和半径查询用户列表")
    @Log(title = "地图拓客-坐标和半径查询用户列表")
    @PostMapping("/search/circle")
    public AjaxResult searchCustByCircle(@RequestBody @Valid CustMapCircleDTO custMapCircleDTO) {
        List<CustMapVO> custMapVOS = custMapSelectService.selectCustListByCircle(custMapCircleDTO);
        return success(custMapVOS);
    }

    @ApiOperation("坐标和半径导出用户")
    @Log(title = "地图拓客-坐标和半径导出用户")
    @PostMapping("/export/circle")
    public AjaxResult exportCustByCircle(@RequestBody @Valid CustMapCircleExcelDTO custMapCircleExcelDTO) {
        if (custMapExportService.checkCustMapExportRequest()){
            return AjaxResult.error("请勿重复点击导出");
        }
        custMapCircleExcelDTO.setLatRange(custMapCircleExcelDTO.getDist() / 111.0);
        custMapCircleExcelDTO.setLngRange(custMapCircleExcelDTO.getDist() / (111.0 * Math.cos(Math.toRadians(custMapCircleExcelDTO.getTargetLat()))));
        custMapCircleExcelDTO.setUserName(getUsername());
        custMapCircleExcelDTO.setDept(SecurityUtils.getHeadId());
        custMapExportService.exportCustMapList(custMapCircleExcelDTO);
        return success("开始生成客户导出文件");
    }
    @ApiOperation("坐标和半径搜索用户数量")
    @Log(title ="地图拓客-坐标和半径搜索用户数量")
    @PostMapping("/search/circle/count")
    public AjaxResult countCustMapListByCircle(@RequestBody @Valid CustMapCircleDTO custMapCircleDTO) {
        return success(custMapSelectService.countCustMapListByCircle(custMapCircleDTO));
    }

    @ApiOperation("多边形坐标查询用户列表")
    @Log(title = "地图拓客-多边形坐标查询用户列表")
    @PostMapping("/search/polygon")
    public AjaxResult searchCustByPolygon(@RequestBody @Valid CustMapPolygonDTO custMapPolygonDTO) {
        List<CustMapVO> custMapVOS = custMapSelectService.selectCustListByPolygon(custMapPolygonDTO);
        return success(custMapVOS);
    }

    @ApiOperation("多边形坐标搜索用户数量")
    @Log(title = "地图拓客-多边形坐标搜索用户数量")
    @PostMapping("/search/polygon/count")
    public AjaxResult countCustMapListByPolygon(@RequestBody @Valid CustMapPolygonDTO custMapPolygonDTO) {
        return success(custMapSelectService.countCustMapListByPolygon(custMapPolygonDTO));
    }

    @ApiOperation("多边形坐标导出用户")
    @Log(title = "地图拓客-多边形坐标导出用户")
    @PostMapping("/export/polygon")
    public AjaxResult exportCustMapListByPolygon(@RequestBody @Valid CustMapPolygonExcelDTO custMapPolygonExcelDTO) {
        if (custMapExportService.checkCustMapExportRequest()){
            return AjaxResult.error("请勿重复点击导出");
        }
        custMapPolygonExcelDTO.setUserName(getUsername());
        custMapPolygonExcelDTO.setDept(SecurityUtils.getHeadId());
        custMapExportService.exportCustMapList(custMapPolygonExcelDTO);
        return success("开始生成客户导出文件");
    }

    @ApiOperation("获取用户默认地址")
    @Log(title = "地图拓客-获取用户默认地址")
    @GetMapping("/loc")
    public AjaxResult getDefaultLoc() {
        return success(deptAddressService.selectDeptAddressById());
    }

    @ApiOperation("获取新华社数据")
    @Log(title = "地图拓客-获取新华社数据")
    @GetMapping("/info")
    public AjaxResult getRadarInfo(@RequestParam Integer id) {
        return success(custMapSelectService.selectCustInfoRadarById(id));
    }

    @ApiOperation("选择导出用户")
    @Log(title = "地图拓客-选择导出用户")
    @PostMapping("/export/select")
    public void exportTopGridTemplate(HttpServletResponse response, @RequestBody List<Integer> ids) {
        if(Objects.isNull(ids) || ids.isEmpty()){
            throw new ServiceException("请选择导出客户");
        }
        List<CustMapExcelVO> custMapExcelVOS = custMapExportService.exportCustMapList(ids);
        ExcelUtil<CustMapExcelVO> util = new ExcelUtil<>(CustMapExcelVO.class);
        util.exportExcel(response, custMapExcelVOS, "地图拓客导出");
    }

    @Anonymous
    @ApiOperation("获取用户默认地址")
    @GetMapping("/loc/init")
    public AjaxResult initDefaultLoc() {
        deptAddressService.initDeptAddress();
        return success();
    }

}
