package com.njcn.product.system.dept.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.common.utils.LogUtil;

import com.njcn.product.system.dept.pojo.dto.AreaTreeDTO;
import com.njcn.product.system.dept.pojo.param.AreaParam;
import com.njcn.product.system.dept.pojo.po.Area;
import com.njcn.product.system.dept.pojo.vo.AreaTreeVO;
import com.njcn.product.system.dept.service.IAreaService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器（行政区域）
 * </p>
 *
 * @author hongawen
 * @since 2021-12-13
 */
@Slf4j
@RestController
@RequestMapping("/area")
@Api(tags = "行政区域管理")
@AllArgsConstructor
public class AreaController extends BaseController {

    private final IAreaService areaService;


    /**
     * 根据行政区域id详情
     *
     * @param id 行政区域id
     * @return 行政区域详情
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/selectIdArea/{id}")
    @ApiOperation("根据行政区域id查询详情")
    @ApiImplicitParam(name = "id", value = "查询参数", required = true)
    public HttpResult<Area> selectIdArea(@PathVariable("id") String id) {
        String methodDescribe = getMethodDescribe("selectIdArea");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, id);
        Area result = areaService.selectIdArea(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    /**
     * 根据行政区域id详情
     *
     * @param list 行政区域id集合
     * @return 行政区域详情
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/areaNameByList")
    @ApiOperation("根据行政区域id集合查询名称")
    @ApiImplicitParam(name = "list", value = "查询参数", required = true)
    public HttpResult<List<Area>> areaNameByList(@RequestBody List<String> list) {
        String methodDescribe = getMethodDescribe("areaNameByList");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, list);
        List<Area> result = areaService.selectAreaByList(list);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }




    /**
     * 新增企业区域
     *
     * @param areaParam 企业区域
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增企业区域")
    @ApiImplicitParam(name = "areaParam", value = "企业区域数据", required = true)
    public HttpResult<Object> add(@RequestBody @Validated AreaParam areaParam) {
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，字典类型数据为：{}", methodDescribe, areaParam);
        boolean result = areaService.addAreaParam(areaParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    /**
     * 修改企业区域
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改企业区域")
    @ApiImplicitParam(name = "updateParam", value = "企业区域", required = true)
    public HttpResult<Object> update(@RequestBody @Validated AreaParam.AreaUpdateParam updateParam) {
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log, "{}，字典数据为：{}", methodDescribe, updateParam);
        boolean result = areaService.updateArea(updateParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    /**
     * 根据选中的行政区域id查询是否含有子节点
     *
     * @param ids 行政区域ids
     * @return 行政区域查看所有子节点
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/selectPid")
    @ApiOperation("根据行政区域id查询")
    @ApiImplicitParam(name = "ids", value = "查询参数", required = true)
    public HttpResult<Object> selectPid(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("selectPid");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, ids);
        List<Area> result = areaService.selectPid(ids);
        if (!result.isEmpty()) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.DELETE_PID_EXIST, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.DELETE_PID_UNEXIST, null, methodDescribe);
        }

    }

    /**
     * 批量删除企业区域
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("删除企业区域")
    @ApiImplicitParam(name = "ids", value = "企业区域索引", required = true)
    public HttpResult<Object> delete(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("delete");
        LogUtil.njcnDebug(log, "{}，企业区域数据为：{}", methodDescribe, ids);
        boolean result = areaService.deleteArea(ids);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }



    /**
     * 根据区域id获取省份
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/areaPro")
    @ApiOperation("根据区域id获取省份")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "区域id"),
            @ApiImplicitParam(name = "type", value = "区域类型", required = true)
    })
    public HttpResult<Area> areaPro(@RequestParam(required = false) @ApiParam("id") String id, @RequestParam("type") Integer type) {
        String methodDescribe = getMethodDescribe("areaDeptTree");
        Area result = areaService.areaPro(id, type);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    /**
     * 根据行政区域名称查询详细
     *
     * @param name 行政区域名称
     * @return 行政区域详情
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/selectAreaByName/{name}")
    @ApiOperation("根据行政区域名称查询详细")
    @ApiImplicitParam(name = "name", value = "查询参数", required = true)
    public HttpResult<Area> selectAreaByName(@PathVariable("name") String name) {
        String methodDescribe = getMethodDescribe("selectAreaByName");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, name);
        Area result = areaService.selectAreaByName(name);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * 根据部门id获取省份
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/areaDeptPro")
    @ApiOperation("根据区域id获取省份")
    @ApiImplicitParam(name = "id", value = "部门id")
    public HttpResult<Area> areaDeptPro(@RequestParam(required = false) @ApiParam("id") String id) {
        String methodDescribe = getMethodDescribe("areaDeptTree");
        Area result = areaService.areaDeptPro(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * @description 获取省市区下拉框
     * @author clam
     * @date 2023/4/11
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/areaSelect")
    @ApiOperation("获取省市区下拉框")
    public HttpResult<List<AreaTreeVO>> areaSelect() {
        String methodDescribe = getMethodDescribe("areaSelect");
        List<AreaTreeVO> result = areaService.areaSelect();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * 获取指定区域父级的子级区域集合
     * @author cdf
     * @date 2023/10/25
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/getPidAreaList")
    @ApiOperation("获取指定区域父级的子级区域集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaId",value = "区域id"),
            @ApiImplicitParam(name = "type", value = "区域类型", required = true)
    })
    public HttpResult<List<Area>> getPidAreaList(@RequestParam("areaId")String areaId , @RequestParam("type") Integer type) {
        String methodDescribe = getMethodDescribe("getPidAreaList");
        List<Area> result = areaService.getPidAreaList(areaId,type);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


}

