package com.unis.module.system.controller.admin.arealevel;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.unis.module.system.dal.dataobject.user.AdminUserDO;
import com.unis.module.system.service.user.AdminUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.unis.framework.common.pojo.PageParam;
import com.unis.framework.common.pojo.PageResult;
import com.unis.framework.common.pojo.CommonResult;
import com.unis.framework.common.util.object.BeanUtils;

import static com.unis.framework.common.pojo.CommonResult.error;
import static com.unis.framework.common.pojo.CommonResult.success;

import com.unis.framework.excel.core.util.ExcelUtils;

import com.unis.framework.apilog.core.annotation.ApiAccessLog;

import static com.unis.framework.apilog.core.enums.OperateTypeEnum.*;
import static com.unis.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import com.unis.module.system.controller.admin.arealevel.vo.*;
import com.unis.module.system.dal.dataobject.arealevel.AreaLevelDO;
import com.unis.module.system.service.arealevel.AreaLevelService;

@Tag(name = "管理后台 - 行政区划")
@RestController
@RequestMapping("/system/area-level")
@Validated
public class AreaLevelController {

    @Resource
    private AreaLevelService areaLevelService;
    @Resource
    private AdminUserService userService;

    @PostMapping("/create")
    @Operation(summary = "创建行政区划")
    @PreAuthorize("@ss.hasPermission('system:area-level:create')")
    public CommonResult<Long> createAreaLevel(@Valid @RequestBody AreaLevelSaveReqVO createReqVO) {
        return success(areaLevelService.createAreaLevel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新行政区划")
    @PreAuthorize("@ss.hasPermission('system:area-level:update')")
    public CommonResult<Boolean> updateAreaLevel(@Valid @RequestBody AreaLevelSaveReqVO updateReqVO) {
        areaLevelService.updateAreaLevel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除行政区划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:area-level:delete')")
    public CommonResult<Boolean> deleteAreaLevel(@RequestParam("id") Long id) {
        areaLevelService.deleteAreaLevel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得行政区划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:area-level:query')")
    public CommonResult<AreaLevelRespVO> getAreaLevel(@RequestParam("id") Long id) {
        AreaLevelDO areaLevel = areaLevelService.getAreaLevel(id);
        return success(BeanUtils.toBean(areaLevel, AreaLevelRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得行政区划下拉")
    @Parameter(name = "id", description = "编号", required = false, example = "1024")
    public CommonResult<List<AreaLevelDO>> getAreaLevelList(String areaCode) {
        if (ObjectUtils.isNotEmpty(areaCode)) {
            try {
//                AreaLevelDO areaLevel = areaLevelService.getAreaLevel(uCode);
//                if (ObjectUtils.isEmpty(areaLevel)) {
//                    return error(400, "无此id对应行政区划");
//                }
                List<AreaLevelDO> areaLevelList = areaLevelService.getAreaListByParentCode(areaCode);
                return success(areaLevelList);
            } catch (Exception e) {
                return error(400, "获取行政下拉错误");
            }
        } else {
            List<AreaLevelDO> areaLevelDOList = areaLevelService.getAreaShengLevel();
            return success(areaLevelDOList);
        }
    }

    @GetMapping("/listAll")
    @Operation(summary = "获得行政区划所有")
    @Parameter(name = "id", description = "编号")
    public CommonResult<List<AreaLevelDO>> getAreaLevelListAll() {
        AdminUserDO user = userService.getUser(getLoginUserId());
        if (ObjectUtils.isEmpty(user)) {
            return error(400, "无此用户");
        }
        List<AreaLevelDO> areaLevelAll = areaLevelService.getAreaLevelListAll(user);
        return success(areaLevelAll);
    }


    @GetMapping("/page")
    @Operation(summary = "获得行政区划分页")
    @PreAuthorize("@ss.hasPermission('system:area-level:query')")
    public CommonResult<PageResult<AreaLevelRespVO>> getAreaLevelPage(@Valid AreaLevelPageReqVO pageReqVO) {
        PageResult<AreaLevelDO> pageResult = areaLevelService.getAreaLevelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AreaLevelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出行政区划 Excel")
    @PreAuthorize("@ss.hasPermission('system:area-level:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAreaLevelExcel(@Valid AreaLevelPageReqVO pageReqVO,
                                     HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AreaLevelDO> list = areaLevelService.getAreaLevelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "行政区划.xls", "数据", AreaLevelRespVO.class,
                BeanUtils.toBean(list, AreaLevelRespVO.class));
    }

    @GetMapping("/getLoginUserXzqhConcise")
    @Operation(summary = "获得当前登录人行政区划简洁版")
    @Parameter(name = "id", description = "编号")
    public CommonResult<List<AreaLevelDO>> getLoginUserXzqhConcise() {
        AdminUserDO user = userService.getUser(getLoginUserId());
        if (ObjectUtils.isEmpty(user)) {
            return error(400, "无此用户");
        }

        return success(areaLevelService.getLoginUserXzqhConcise(user.getAreaCode()));

    }

}