package com.cm.common.plugin.controller.app.dataarea;

import com.cm.common.annotation.CheckRequestBodyAnnotation;
import com.cm.common.base.AbstractController;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.exception.SearchException;
import com.cm.common.plugin.pojo.dtos.dataarea.DataAreaDTO;
import com.cm.common.plugin.pojo.vos.dataarea.DataAreaVO;
import com.cm.common.plugin.service.dataarea.IDataAreaService;
import com.cm.common.pojo.ListPage;
import com.cm.common.pojo.dtos.ZTreeDTO;
import com.cm.common.result.ErrorResult;
import com.cm.common.result.SuccessResult;
import com.cm.common.result.SuccessResultList;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: AreaAreaAppController
 * @Description: 地区字典
 * @Author: WangGeng
 * @Date: 2019/11/18 14:00
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_APP_PREFIX + "地区字典管理接口")
@RestController
@RequestMapping(ISystemConstant.APP_PREFIX + "/dataarea")
public class DataAreaAppController extends AbstractController {

    @Autowired
    private IDataAreaService dataAreaService;

    @ApiOperation(value = "地区字典新增", notes = "地区字典新增接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("savearea")
    @CheckRequestBodyAnnotation
    public SuccessResult saveArea(@RequestHeader("token") String token, @RequestBody DataAreaVO dataAreaVO) throws Exception {
        return dataAreaService.saveArea(token, dataAreaVO);
    }

    @ApiOperation(value = "地区字典删除", notes = "通过id列表批量删除地区字典接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @ApiImplicitParam(name = "ids", value = "地区字典ID列表，用下划线分隔", paramType = "path", example = "1_2_3")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("removearea/{ids}")
    public SuccessResult removeArea(@RequestHeader("token") String token, @PathVariable("ids") String ids) {
        return dataAreaService.removeArea(token, ids);
    }

    @ApiOperation(value = "地区字典修改", notes = "地区字典修改接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @ApiImplicitParam(name = "areaId", value = "地区字典ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("updatearea/{areaId}")
    @CheckRequestBodyAnnotation
    public SuccessResult updateArea(@RequestHeader("token") String token, @PathVariable("areaId") String areaId, @RequestBody DataAreaVO dataAreaVO) throws Exception {
        return dataAreaService.updateArea(token, areaId, dataAreaVO);
    }

    @ApiOperation(value = "地区字典详情（ID查询）", notes = "地区字典详情（ID查询）接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @ApiImplicitParam(name = "areaId", value = "地区字典ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("getareabyid/{areaId}")
    public DataAreaDTO getAreaById(@RequestHeader("token") String token, @PathVariable("areaId") String areaId) {
        return getAreaByIdRelease(areaId);
    }

    @ApiOperation(value = ISystemConstant.API_TAGS_RELEASE_PREFIX + "地区字典详情（ID查询）", notes = ISystemConstant.API_TAGS_RELEASE_PREFIX + "地区字典详情（ID查询）接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaId", value = "地区字典ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("getareabyid" + ISystemConstant.RELEASE_SUFFIX + "/{areaId}")
    public DataAreaDTO getAreaByIdRelease(@PathVariable("areaId") String areaId) {
        return dataAreaService.getAreaById(areaId);
    }

    @ApiOperation(value = "地区字典列表（上级ID查询）", notes = "地区字典列表（上级ID查询）接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @ApiImplicitParam(name = "areaParentId", value = "地区字典上级ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listareabyparentid/{areaParentId}")
    public List<DataAreaDTO> listAreaByParentId(@RequestHeader("token") String token, @PathVariable("areaParentId") String areaParentId) {
        return listAreaByParentIdRelease(areaParentId);
    }

    @ApiOperation(value = ISystemConstant.API_TAGS_RELEASE_PREFIX + "地区字典列表（上级ID查询）", notes = ISystemConstant.API_TAGS_RELEASE_PREFIX + "地区字典列表（上级ID查询）接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaParentId", value = "地区字典上级ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listareabyparentid" + ISystemConstant.RELEASE_SUFFIX + "/{areaParentId}")
    public List<DataAreaDTO> listAreaByParentIdRelease(@PathVariable("areaParentId") String areaParentId) {
        return dataAreaService.listAreaByParentId(areaParentId);
    }

    @ApiOperation(value = "地区字典全部列表（上级ID查询）", notes = "地区字典全部列表（上级ID查询）接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @ApiImplicitParam(name = "areaParentId", value = "地区字典上级ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listareaallbyparentid/{areaParentId}")
    public List<DataAreaDTO> listArea(@RequestHeader("token") String token, @PathVariable("areaParentId") String areaParentId) throws SearchException {
        return listAreaAllByParentIdRelease(areaParentId);
    }

    @ApiOperation(value = ISystemConstant.API_TAGS_RELEASE_PREFIX + "地区字典全部列表（上级ID查询）", notes = ISystemConstant.API_TAGS_RELEASE_PREFIX + "地区字典全部列表（上级ID查询）接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaParentId", value = "地区字典上级ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listareaallbyparentid" + ISystemConstant.RELEASE_SUFFIX + "/{areaParentId}")
    public List<DataAreaDTO> listAreaAllByParentIdRelease(@PathVariable("areaParentId") String areaParentId) throws SearchException {
        return dataAreaService.listAreaAllByParentId(areaParentId);
    }

    @ApiOperation(value = "分页地区字典列表", notes = "分页地区字典列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @ApiImplicitParam(name = "parentId", value = "上级ID", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "int", defaultValue = "20"),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpagearea")
    public SuccessResultList<List<DataAreaDTO>> listPageArea(@RequestHeader("token") String token, ListPage page) {
        return listPageAreaRelease(page);
    }

    @ApiOperation(value = ISystemConstant.API_TAGS_RELEASE_PREFIX + "分页地区字典列表", notes = ISystemConstant.API_TAGS_RELEASE_PREFIX + "分页地区字典列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "上级ID", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "int", defaultValue = "20"),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpagearea" + ISystemConstant.RELEASE_SUFFIX)
    public SuccessResultList<List<DataAreaDTO>> listPageAreaRelease(ListPage page) {
        Map<String, Object> params = requestParams();
        String areaParentId = "0";
        if (!StringUtils.isBlank(params.get(ISystemConstant.PARAMS_PARENT_ID) == null ? null : params.get(ISystemConstant.PARAMS_PARENT_ID).toString())) {
            areaParentId = params.get(ISystemConstant.PARAMS_PARENT_ID).toString();
        }
        params.put("areaParentId", areaParentId);
        page.setParams(params);
        return dataAreaService.listPageArea(page);
    }

    @ApiOperation(value = "zTree列表", notes = "zTree列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @ApiImplicitParam(name = "id", value = "父ID", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listztreearea")
    public List<ZTreeDTO> listZTreeArea(@RequestHeader("token") String token) throws SearchException {
        return listZTreeAreaRelease();
    }

    @ApiOperation(value = ISystemConstant.API_TAGS_RELEASE_PREFIX + "zTree列表", notes = ISystemConstant.API_TAGS_RELEASE_PREFIX + "zTree列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "父ID", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listztreearea" + ISystemConstant.RELEASE_SUFFIX)
    public List<ZTreeDTO> listZTreeAreaRelease() throws SearchException {
        Map<String, Object> params = requestParams();
        String areaParentId = "0";
        if (!StringUtils.isBlank(params.get(ISystemConstant.PARAMS_ID) == null ? null : params.get(ISystemConstant.PARAMS_ID).toString())) {
            areaParentId = params.get(ISystemConstant.PARAMS_ID).toString();
        }
        params.put("areaParentId", areaParentId);
        return dataAreaService.listZTreeArea(params);
    }

}
