package ink.wgink.module.dictionary.controller.api;

import ink.wgink.annotation.CheckRequestBodyAnnotation;
import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.module.dictionary.pojo.dtos.AreaDTO;
import ink.wgink.module.dictionary.pojo.vos.AreaVO;
import ink.wgink.module.dictionary.service.IAreaService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.ZTreeDTO;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.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.Arrays;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private IAreaService areaService;

    @ApiOperation(value = "地区字典新增", notes = "地区字典新增接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("save")
    @CheckRequestBodyAnnotation
    public SuccessResult save(@RequestBody AreaVO areaVO) {
        areaService.save(areaVO);
        return new SuccessResult();
    }

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

    @ApiOperation(value = "地区字典修改", notes = "地区字典修改接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaId", value = "地区字典ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("update/{areaId}")
    @CheckRequestBodyAnnotation
    public SuccessResult update(@PathVariable("areaId") String areaId, @RequestBody AreaVO dataAreaVO) {
        areaService.update(areaId, dataAreaVO);
        return new SuccessResult();
    }

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

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

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

    @ApiOperation(value = "地区字典分页列表", notes = "地区字典分页列表接口")
    @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("listpage")
    public SuccessResultList<List<AreaDTO>> listPage(ListPage page) {
        Map<String, Object> params = requestParams();
        String parentId = "0";
        if (!StringUtils.isBlank(params.get(ISystemConstant.PARAMS_PARENT_ID) == null ? null : params.get(ISystemConstant.PARAMS_PARENT_ID).toString())) {
            parentId = params.get(ISystemConstant.PARAMS_PARENT_ID).toString();
        }
        params.put("areaParentId", parentId);
        page.setParams(params);
        return areaService.listPage(page);
    }

    @ApiOperation(value = "地区字典zTree列表", notes = "地区字典zTree列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "父ID", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listztree")
    public List<ZTreeDTO> listZTree() {
        Map<String, Object> params = requestParams();
        String parentId = "0";
        if (!StringUtils.isBlank(params.get(ISystemConstant.PARAMS_ID) == null ? null : params.get(ISystemConstant.PARAMS_ID).toString())) {
            parentId = params.get(ISystemConstant.PARAMS_ID).toString();
        }
        params.put("areaParentId", parentId);
        return areaService.listZTree(params);
    }

    @ApiOperation(value = "地区字典列表（通过地区字典编码）", notes = "地区字典列表（通过地区字典编码）接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaCode", value = "地区字典编码", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listbycode/{areaCode}")
    public List<AreaDTO> listByCode(@PathVariable("areaCode") String areaCode) {
        return areaService.listByCode(areaCode);
    }

    @ApiOperation(value = "更新短标题和合并名称", notes = "更新短标题和合并名称接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("update-sort-and-merger-name")
    public SuccessResult updateShortAndMergerName() {
        areaService.updateShortAndMergerName();
        return new SuccessResult();
    }

}
