package ink.wgink.service.department.controller.app.api;

import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.ZTreeDTO;
import ink.wgink.pojo.dtos.department.DepartmentDTO;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResultData;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.service.department.service.IDepartmentService;
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.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: DepartmentController
 * @Description: 组织部门
 * @Author: wenc
 * @Date: 2019/1/8 7:23 PM
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_APP_PREFIX + "组织部门")
@RestController
@RequestMapping(ISystemConstant.APP_PREFIX + "/department")
public class DepartmentAppController extends DefaultBaseController {

    @Autowired
    private IDepartmentService departmentService;

    @ApiOperation(value = "组织部门列表", notes = "组织部门列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @ApiImplicitParam(name = "departmentParentId", value = "组织部门上级ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list")
    public List<DepartmentDTO> list(@RequestHeader("token") String token) {
        Map<String, Object> params = requestParams();
        return departmentService.list(params);
    }

    @ApiOperation(value = "组织部门列表", notes = "组织部门列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @ApiImplicitParam(name = "departmentParentId", value = "组织部门上级ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-all/{departmentParentId}")
    public List<DepartmentDTO> listAll(@RequestHeader("token") String token,
                                       @PathVariable("departmentParentId") String departmentParentId) {
        Map<String, Object> params = new HashMap<>(0);
        params.put("departmentParentId", departmentParentId);
        return departmentService.listAll(params);
    }

    @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("listztree")
    public List<ZTreeDTO> listZTree(@RequestHeader("token") String token) {
        Map<String, Object> params = requestParams();
        String departmentParentId = "0";
        if (!StringUtils.isBlank(params.get("id") == null ? null : params.get("id").toString())) {
            departmentParentId = params.get("id").toString();
        }
        return departmentService.listZTree(departmentParentId, params);
    }

    @ApiOperation(value = "组织部门zTree列表", notes = "通过地区编码、部门上级ID和部门类型组织部门zTree列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @ApiImplicitParam(name = "departmentAreaCode", value = "地区编码", paramType = "path"),
            @ApiImplicitParam(name = "departmentParentId", value = "组织部门上级ID", paramType = "path"),
            @ApiImplicitParam(name = "departmentType", value = "部门类型，1：机构，2：部门", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-ztree/area-code/{departmentAreaCode}/parent-id/{departmentParentId}/type/{departmentType}")
    public List<ZTreeDTO> listByAreaCodeAndParentIdAndType(@RequestHeader("token") String token,
                                                           @PathVariable("departmentAreaCode") String departmentAreaCode,
                                                           @PathVariable("departmentParentId") String departmentParentId,
                                                           @PathVariable("departmentType") Integer departmentType) {
        return departmentService.listZTreeByAreaCodeAndParentIdAndType(departmentAreaCode, departmentParentId, departmentType);
    }

    @ApiOperation(value = "组织部门详情", notes = "组织部门详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @ApiImplicitParam(name = "departmentId", value = "部门ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get/{departmentId}")
    public DepartmentDTO get(@RequestHeader("token") String token,
                             @PathVariable("departmentId") String departmentId) {
        return departmentService.get(departmentId);
    }

    @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("listpage")
    public SuccessResultList<List<DepartmentDTO>> listPage(@RequestHeader("token") String token,
                                                           ListPage page) {
        Map<String, Object> params = requestParams();
        String departmentParentId = "0";
        if (!StringUtils.isBlank(params.get("parentId") == null ? null : params.get("parentId").toString())) {
            departmentParentId = params.get("parentId").toString();
        }
        params.put("departmentParentId", departmentParentId);
        page.setParams(params);
        return departmentService.listPage(page);
    }

    @ApiOperation(value = "组织部门获得同一级部门数量（按部门名称）", notes = "组织部门获得同一级部门数量（按部门名称）接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @ApiImplicitParam(name = "departmentParentId", value = "上级部门ID", paramType = "path"),
            @ApiImplicitParam(name = "departmentName", value = "上级部门ID", paramType = "path"),
            @ApiImplicitParam(name = "noDepartmentId", value = "不统计的部门ID", paramType = "query"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("countbyparentidandname/{departmentParentId}/{departmentName}")
    public SuccessResultData<Integer> countByParentIdAndName(@RequestHeader("token") String token,
                                                             @PathVariable("departmentParentId") String departmentParentId,
                                                             @PathVariable("departmentName") String departmentName,
                                                             @RequestParam(name = "noDepartmentId", required = false) String noDepartmentId) throws UnsupportedEncodingException {
        Map<String, Object> params = getParams();
        params.put("departmentParentId", departmentParentId);
        params.put("departmentName", URLDecoder.decode(departmentName, "UTF-8"));
        params.put("noDepartmentId", noDepartmentId);
        return new SuccessResultData<>(departmentService.count(params));
    }

}
