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.DataDTO;
import ink.wgink.module.dictionary.pojo.vos.DataVO;
import ink.wgink.module.dictionary.service.IDataService;
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: DataController
 * @Description: 字典
 * @Author: WangGeng
 * @Date: 2019/11/18 14:00
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "字典接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/data")
public class DataController extends DefaultBaseController {

    @Autowired
    private IDataService dataService;

    @ApiOperation(value = "字典新增", notes = "字典新增接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("save")
    @CheckRequestBodyAnnotation
    public SuccessResult save(@RequestBody DataVO dataVO) {
        dataService.save(dataVO);
        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) {
        dataService.remove(Arrays.asList(ids.split("\\_")));
        return new SuccessResult();
    }

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

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

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

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

    @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<DataDTO>> 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("dataParentId", parentId);
        page.setParams(params);
        return dataService.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 dataParentId = "0";
        if (!StringUtils.isBlank(params.get(ISystemConstant.PARAMS_ID) == null ? null : params.get(ISystemConstant.PARAMS_ID).toString())) {
            dataParentId = params.get(ISystemConstant.PARAMS_ID).toString();
        }
        params.put("dataParentId", dataParentId);
        return dataService.listZTree(params);
    }

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

}
