package com.chinaweal.aiccs.aiccs.market.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chinaweal.aiccs.aiccs.abnormal.entity.EBaseinfo;
import com.chinaweal.aiccs.aiccs.abnormal.service.EBaseinfoService;
import com.chinaweal.aiccs.aiccs.market.ai.AreaSliceClaimAI;
import com.chinaweal.aiccs.aiccs.market.ai.util.GeometryUtil;
import com.chinaweal.aiccs.aiccs.market.entity.AreaDsm;
import com.chinaweal.aiccs.aiccs.market.entity.AreaSlice;
import com.chinaweal.aiccs.aiccs.market.entity.AreaSlicePolygon;
import com.chinaweal.aiccs.aiccs.market.service.AreaSlicePolygonService;
import com.chinaweal.aiccs.aiccs.market.service.AreaSliceService;
import com.chinaweal.aiccs.aiccs.market.service.IAreaDsmService;
import com.chinaweal.aiccs.aiccs.market.vo.AreaDsmVo;
import com.chinaweal.aiccs.common.base.controller.BaseController;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.aicorg.model.AICUser;
import com.chinaweal.youfool.framework.springboot.exception.custom.BusinessException;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.vividsolutions.jts.geom.Point;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 控制器
 * </p>
 *
 * @author chinaweal
 * @since 2021-11-26
 */
@Api(tags = "")
@Slf4j
@ApiSort(1)
@RestController
@RequestMapping("/market/areaDsm")
public class AreaDsmController extends BaseController {

    @Resource
    private IAreaDsmService iAreaDsmService;
    @Autowired
    private AreaSliceService areaSliceService;
    @Autowired
    private AreaSliceClaimAI areaSliceClaimAI;
    @Autowired
    private EBaseinfoService eBaseinfoService;
    @Autowired
    private AreaSlicePolygonService areaSlicePolygonService;

    @GetMapping("/getInfoBySparentCode")
    @ApiOperation("根据SparentCode获取下级目录信息")
    public RestResult<?> getInfoBySparentCode(String sparentCode, String sareaLevel) {
        return RestResult.ok(iAreaDsmService.getInfoBySparentCode(sparentCode, sareaLevel));
    }

    /**
     * 通过当前登录用户的组织架构，获取用户可视的片区数据
     *
     * @param request 请求
     * @param isUnit  是否为单位（0否1是），传空查全部
     * @return 查询结果树
     * @author lroyia
     * @since 2021年12月11日 10:39:54
     */
    @GetMapping("getInfoByUserRegion")
    @ApiOperation("通过当前登录用户的组织架构，获取用户可视的片区数据")
    public RestResult<?> getInfoByUserRegion(HttpServletRequest request,
                                             @ApiParam("是否为单位（0否1是），传空查全部") String isUnit) {
        AICUser loginUser = getLoginUser(request);
        String regionID = loginUser.getRegionID();
        for (int i = regionID.length() - 1; i > -1; i--) {
            if (regionID.charAt(i) != '0') {
                regionID = regionID.substring(0, i + 1);
                break;
            }
        }
        // 查询该用户组织架构下可看到的片区数据
        List<AreaDsm> list = iAreaDsmService.list(new LambdaQueryWrapper<AreaDsm>()
                .likeRight(AreaDsm::getOrgNumber, regionID)
                .eq(StringUtils.isNotBlank(isUnit), AreaDsm::getBisUnit, isUnit)
                .orderByAsc(AreaDsm::getSareaCode));
        // 结果树封装
        Map<String, AreaDsm> codeMap = new HashMap<>();
        List<AreaDsm> result = new ArrayList<>();
        for (AreaDsm each : list) {
            codeMap.put(each.getSliceId(), each);
            if (StringUtils.isBlank(each.getSparentCode()) || codeMap.get(each.getSparentCode()) == null) {
                result.add(each);
                continue;
            }
            AreaDsm parent = codeMap.get(each.getSparentCode());
            List<AreaDsm> children = parent.getChildren();
            if (children == null) {
                children = new ArrayList<>();
                parent.setChildren(children);
            }
            children.add(each);
        }
        return RestResult.ok(result);
    }

    @GetMapping("/getInfoBySareaCode")
    @ApiOperation("根据Code获取信息")
    public RestResult getInfoBySareaCode(String sareaCode) {
        return RestResult.ok(iAreaDsmService.getById(sareaCode));
    }

    @GetMapping("/getInfoBySliceId")
    @ApiOperation("根据SliceId获取信息")
    public RestResult getInfoBySliceId(String sliceId) {
        return RestResult.ok(iAreaDsmService.getInfoBySliceId(sliceId));
    }

    @GetMapping("/getAllBySareaCode")
    @ApiOperation("根据SareaCode获取所有信息")
    public RestResult getAllBySareaCode(String sareaCode) {
        return RestResult.ok(iAreaDsmService.getAllBySareaCode(sareaCode));
    }

    @GetMapping("/deleteAreaDsm")
    @ApiOperation("删除片区/地址信息")
    public RestResult deleteAreaDsm(String sareaCode) {
        return iAreaDsmService.deleteAreaDsm(sareaCode);
    }

    @PostMapping("/saveAreaDsm")
    @ApiOperation("保存片区/地址信息")
    public RestResult saveAreaDsm(HttpServletRequest request, @RequestBody AreaDsmVo areaDsmVo) {
        return iAreaDsmService.saveAreaDsm(request, areaDsmVo);
    }

    @GetMapping("/anyscAreaSlice")
    @ApiOperation("把导入数据同步到片区设置")
    public RestResult anyscAreaSlice() {
        return iAreaDsmService.anyscAreaSlice();
    }
}
