package com.ruoyi.ibs.draw.controller;

import com.github.pagehelper.Page;
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.core.domain.R;
import com.ruoyi.common.core.page.TableDataPageInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ibs.draw.domain.dto.draw.ShapeAddDTO;
import com.ruoyi.ibs.draw.domain.dto.draw.ShapeInfoEditDTO;
import com.ruoyi.ibs.draw.domain.dto.draw.ShapeOverlapDTO;
import com.ruoyi.ibs.draw.domain.dto.draw.ShapeWktEditDTO;
import com.ruoyi.ibs.draw.domain.entity.DrawShape;
import com.ruoyi.ibs.draw.domain.entity.DrawShapeCust;
import com.ruoyi.ibs.draw.domain.vo.DrawShapeVO;
import com.ruoyi.ibs.draw.service.DrawGridCustService;
import com.ruoyi.ibs.draw.service.DrawShapeService;
import com.ruoyi.ibs.grid.domain.dto.DeleteDTO;
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.validation.Valid;
import java.util.List;

/**
 * @Author 吴凯程
 * @Date 2024/12/24
 **/
@Api(tags = "绘制图形接口")
@RestController
@RequestMapping("/draw/shape")
public class DrawShapeController extends BaseController {

    @Resource
    private DrawShapeService drawShapeService;

    @Resource
    private DrawGridCustService drawGridCustService;

    @ApiOperation("创建图形")
    @Log(title = "区域绘制-创建图形", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<String> addShapeToLayer(@RequestBody ShapeAddDTO shapeAddDTO) {
        return R.ok(drawShapeService.addShapeToLayer(shapeAddDTO));
    }

    @ApiOperation("编辑图形信息")
    @Log(title = "区域绘制-编辑图形信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit/info")
    public R<String> editShapeInfo(@RequestBody ShapeInfoEditDTO shapeInfoEditDTO) {
        return R.ok(drawShapeService.editShapeInfo(shapeInfoEditDTO));
    }

    @Log(title = "区域绘制-编辑图形")
    @ApiOperation("编辑图形")
    @PostMapping("/edit/wkt")
    public R<String> editShapeWkt(@RequestBody ShapeWktEditDTO shapeWktEditDTO) {
        return R.ok(drawShapeService.editShapeWkt(shapeWktEditDTO));
    }

    @ApiOperation("批量删除图形")
    @Log(title = "区域绘制-批量删除图形", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    public R<String> deleteShape(@RequestBody @Valid DeleteDTO deleteDTO) {
        return R.ok(drawShapeService.deleteShape(deleteDTO.getIdList()));
    }

    @Log(title = "区域绘制-校验图形wkt重叠")
    @ApiOperation("校验图形wkt重叠")
    @PostMapping("/check/wkt")
    public R<Boolean> checkShapeOverlap(@RequestBody ShapeOverlapDTO shapeOverlapDTO) {
        return R.ok(drawShapeService.checkShapeOverlap(shapeOverlapDTO));
    }
    @Log(title = "区域绘制-获取图层内图形列表")
    @ApiOperation("获取图层内图形列表")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "layerId", value = "图层id", dataType = "Long", dataTypeClass = Long.class, required = true),
            @ApiImplicitParam(name = "shapeName", value = "区域名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "updateBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "opsDept", value = "业务部门归属", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "auditStatus", value = "审批状态", dataType = "String", dataTypeClass = String.class)
    })
    public TableDataPageInfo<DrawShapeVO> getShapeList(@RequestParam Long layerId,Long shapeId,String shapeName,String createBy,String opsDept,String auditStatus) {
        Page<Object> page = startPage();
        List<DrawShapeVO> shapeList = drawShapeService.getShapeList(layerId, shapeId,shapeName, createBy, opsDept, null,auditStatus);
        return getDataTable(shapeList, page);
    }
    @Log(title = "区域绘制-获取图层内可选图形列表")
    @ApiOperation("获取图层内可选图形列表")
    @GetMapping("/list/available")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "layerId", value = "图层id", dataType = "Long", dataTypeClass = Long.class, required = true),
            @ApiImplicitParam(name = "opsDept", value = "业务部门归属", dataType = "String", dataTypeClass = String.class)
    })
    public TableDataPageInfo<DrawShapeVO> getShapeAvailableList(@RequestParam Long layerId,@RequestParam String opsDept) {
        Page<Object> page = startPage();
        //只选取审核通过的图形数据
        List<DrawShapeVO> shapeList = drawShapeService.getShapeAvailableList(layerId, opsDept);
        return getDataTable(shapeList, page);
    }

    @Log(title = "区域绘制-获取图形内客户列表")
    @ApiOperation("获取图形内客户列表")
    @GetMapping("/cust/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shapeId", value = "图形id", dataType = "Long", dataTypeClass = Long.class, required = true),
            @ApiImplicitParam(name = "custType", value = "客户类型", dataType = "String", dataTypeClass = String.class, required = true)
    })
    public TableDataPageInfo<DrawShapeCust> drawGridCustService(@RequestParam Long shapeId,@RequestParam String custType) {
        Page<Object> page = startPage();
        List<DrawShapeCust> custListByShape = drawGridCustService.getCustListByShape(shapeId,custType);
        for (DrawShapeCust drawShapeCust : custListByShape) {
            if (drawShapeCust.getCustType().equals("0")) {
                drawShapeCust.setCustType("个人");
            } else if (drawShapeCust.getCustType().equals("1")) {
                drawShapeCust.setCustType("商户");
            } else if (drawShapeCust.getCustType().equals("2")) {
                drawShapeCust.setCustType("企业");
            }
        }
        return getDataTable(custListByShape, page);
    }

    @Log(title = "区域绘制-判断是否有编辑权限")
    @ApiOperation("判断是否有编辑权限")
    @GetMapping("/edit/check")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shapeId", value = "图形id", dataType = "Long", dataTypeClass = Long.class, required = true)
    })
    public R<String> isEdit(@RequestParam Long shapeId){
        return R.ok(drawShapeService.isEdit(shapeId));
    }


    @Log(title = "区域绘制-重新提交审核")
    @ApiOperation("重新提交审核")
    @PostMapping("/reAudit")
    public AjaxResult drawShapeReAudit(@RequestBody DrawShape drawShape) {
        return success(drawShapeService.drawShapeReAudit(drawShape));
    }

    @Anonymous
    @ApiOperation("全量更新图形列表")
    @PostMapping("/update/all")
    public void updateShapeList() {
        drawGridCustService.updateShapeList();
    }

    @Anonymous
    @ApiOperation("更新和行社图形列表")
    @PostMapping("/update/dept")
    public void updateShapeListByDept(String deptCode){
        drawGridCustService.updateShapeListByDept(deptCode);
    }

}
