package ink.wgink.service.department.controller.resources;

import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.exceptions.ParamsException;
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.pos.DepartmentPO;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResultData;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.pojo.vos.IdsVO;
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.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_RESOURCE_PREFIX + "组织部门")
@RestController
@RequestMapping(ISystemConstant.RESOURCE_PREFIX + "/department")
public class DepartmentResourceController extends DefaultBaseController {

    @Autowired
    private IDepartmentService departmentService;

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

    @ApiOperation(value = "组织部门列表", notes = "组织部门列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentParentId", value = "组织部门上级ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("list/ids")
    public List<DepartmentDTO> listByIds(@RequestBody IdsVO idsVO) {
        if (idsVO.getIds().isEmpty()) {
            throw new ParamsException("id列表不能为空");
        }
        return departmentService.listByIds(idsVO.getIds());
    }

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

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

    @ApiOperation(value = "组织部门zTree列表", notes = "组织部门zTree列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "父ID", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-ztree")
    public List<ZTreeDTO> listZTree() {
        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 = "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(@PathVariable("departmentAreaCode") String departmentAreaCode,
                                                           @PathVariable("departmentParentId") String departmentParentId,
                                                           @PathVariable("departmentType") Integer departmentType) {
        return departmentService.listZTreeByAreaCodeAndParentIdAndType(departmentAreaCode, departmentParentId, departmentType);
    }

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

    @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<DepartmentDTO>> listPage(ListPage page) {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return departmentService.listPage(page);
    }

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

    @ApiOperation(value = "组织部门详情", notes = "组织部门详情接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-po")
    public List<DepartmentPO> listPO() {
        Map<String, Object> params = requestParams();
        return departmentService.listPO(params);
    }

    @ApiOperation(value = "组织部门ID列表", notes = "组织部门ID列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-id")
    public List<String> listId() {
        Map<String, Object> params = requestParams();
        return departmentService.listId(params);
    }

    @ApiOperation(value = "组织部门列表", notes = "组织部门列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentParentId", value = "上级部门ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-po/parentid/{departmentParentId}")
    public List<DepartmentPO> listPOByParentId(@PathVariable("departmentParentId") String departmentParentId) {
        return departmentService.listPOByParentId(departmentParentId);
    }

    @ApiOperation(value = "组织部门列表", notes = "组织部门列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "部门ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("list-po/ids")
    public List<DepartmentPO> listPO(@RequestBody IdsVO idsVO) {
        return departmentService.listPO(idsVO.getIds());
    }

    @ApiOperation(value = "组织部门列表", notes = "组织部门列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentParentId", value = "上级部门ID", paramType = "path"),
            @ApiImplicitParam(name = "addDate", value = "添加时间，yyyy-MM-dd", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-po/parentid/{departmentParentId}/add-date/{addDate}")
    public List<DepartmentPO> listPOByParentIdAndAddDate(@PathVariable("departmentParentId") String departmentParentId, @PathVariable("addDate") String addDate) {
        return departmentService.listPOByParentIdAndAddDate(departmentParentId, addDate);
    }

    @ApiOperation(value = "组织部门列表", notes = "组织部门列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentParentId", value = "上级部门ID", paramType = "path"),
            @ApiImplicitParam(name = "updateDate", value = "修改时间，yyyy-MM-dd", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-po/parentid/{departmentParentId}/update-date/{updateDate}")
    public List<DepartmentPO> listPOByParentIdAndUpdateDate(@PathVariable("departmentParentId") String departmentParentId, @PathVariable("updateDate") String updateDate) {
        return departmentService.listPOByParentIdAndUpdateDate(departmentParentId, updateDate);
    }

    @ApiOperation(value = "组织部门列表", notes = "组织部门列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaCode", value = "地区编码", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-po/area-code/{areaCode}")
    public List<DepartmentPO> listPOByAreaCode(@PathVariable("areaCode") String areaCode) {
        return departmentService.listPOByAreaCode(areaCode);
    }

    @ApiOperation(value = "组织部门列表", notes = "组织部门列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaCode", value = "地区编码", paramType = "path"),
            @ApiImplicitParam(name = "departmentType", value = "组织部门类型", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-po/area-code/{areaCode}/type/{departmentType}")
    public List<DepartmentPO> listPOByAreaCode(@PathVariable("areaCode") String areaCode, @PathVariable("departmentType") Integer departmentType) {
        return departmentService.listPOByAreaCodeAndType(areaCode, departmentType);
    }

    @ApiOperation(value = "统计", notes = "统计接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("count")
    public SuccessResultData<Integer> count() {
        Map<String, Object> params = requestParams();
        Integer count = departmentService.count(params);
        return new SuccessResultData<>(count);
    }
}
