package com.jojubanking.boot.module.system.controller.admin.deptxq;

import com.jojubanking.boot.framework.common.enums.CommonStatusEnum;
import com.jojubanking.boot.module.system.controller.admin.dept.vo.dept.DeptListReqVO;
import com.jojubanking.boot.module.system.controller.admin.dept.vo.dept.DeptRespVO;
import com.jojubanking.boot.module.system.controller.admin.dept.vo.dept.DeptSimpleRespVO;
import com.jojubanking.boot.module.system.convert.dept.DeptConvert;
import com.jojubanking.boot.module.system.dal.dataobject.dept.DeptDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.jojubanking.boot.framework.common.pojo.PageResult;
import com.jojubanking.boot.framework.common.pojo.CommonResult;
import static com.jojubanking.boot.framework.common.pojo.CommonResult.success;

import com.jojubanking.boot.framework.excel.core.util.ExcelUtils;

import com.jojubanking.boot.framework.operatelog.core.annotations.OperateLog;
import static com.jojubanking.boot.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.jojubanking.boot.module.system.controller.admin.deptxq.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.deptxq.DeptXqDO;
import com.jojubanking.boot.module.system.convert.deptxq.DeptXqConvert;
import com.jojubanking.boot.module.system.service.deptxq.DeptXqService;

@Api(tags = "管理后台 - 部门")
@RestController
@RequestMapping("/system/dept-xq")
@Validated
public class DeptXqController {

    @Resource
    private DeptXqService deptXqService;

    @PostMapping("/create")
    @ApiOperation("创建部门")
    @PreAuthorize("@ss.hasPermission('system:dept-xq:create')")
    public CommonResult<Long> createDeptXq(@Valid @RequestBody DeptXqCreateReqVO createReqVO) {
        return success(deptXqService.createDeptXq(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新部门")
    @PreAuthorize("@ss.hasPermission('system:dept-xq:update')")
    public CommonResult<Boolean> updateDeptXq(@Valid @RequestBody DeptXqUpdateReqVO updateReqVO) {
        deptXqService.updateDeptXq(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除部门")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:dept-xq:delete')")
    public CommonResult<Boolean> deleteDeptXq(@RequestParam("id") Long id) {
        deptXqService.deleteDeptXq(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得部门")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:dept-xq:query')")
    public CommonResult<DeptXqRespVO> getDeptXq(@RequestParam("id") Long id) {
        DeptXqDO deptXq = deptXqService.getDeptXq(id);
        return success(DeptXqConvert.INSTANCE.convert(deptXq));
    }

//    @GetMapping("/list")
//    @ApiOperation("获得部门列表")
//    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
//    @PreAuthorize("@ss.hasPermission('system:dept-xq:query')")
//    public CommonResult<List<DeptXqRespVO>> getDeptXqList(@RequestParam("ids") Collection<Long> ids) {
//        List<DeptXqDO> list = deptXqService.getDeptXqList(ids);
//        return success(DeptXqConvert.INSTANCE.convertList(list));
//    }

    @GetMapping("/page")
    @ApiOperation("获得部门分页")
    @PreAuthorize("@ss.hasPermission('system:dept-xq:query')")
    public CommonResult<PageResult<DeptXqRespVO>> getDeptXqPage(@Valid DeptXqPageReqVO pageVO) {
        PageResult<DeptXqDO> pageResult = deptXqService.getDeptXqPage(pageVO);
        return success(DeptXqConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出部门 Excel")
    @PreAuthorize("@ss.hasPermission('system:dept-xq:export')")
    @OperateLog(type = EXPORT)
    public void exportDeptXqExcel(@Valid DeptXqExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<DeptXqDO> list = deptXqService.getDeptXqList(exportReqVO);
        // 导出 Excel
        List<DeptXqExcelVO> datas = DeptXqConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "部门.xls", "数据", DeptXqExcelVO.class, datas);
    }

    @GetMapping("/list")
    @ApiOperation("获取部门列表")
    @PreAuthorize("@ss.hasPermission('system:dept-xq:query')")
    public CommonResult<List<DeptXqRespVO>> listDepts(DeptXqListReqVO reqVO) {
        List<DeptXqDO> list = deptXqService.getSimpleXqDepts(reqVO);
        list.sort(Comparator.comparing(DeptXqDO::getSort));
        return success(DeptXqConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/list-all-simple")
    @ApiOperation(value = "获取部门精简信息列表", notes = "只包含被开启的部门，主要用于前端的下拉选项")
    public CommonResult<List<DeptXqSimpleRespVO>> getSimpleDepts() {
        // 获得部门列表，只要开启状态的
        DeptXqListReqVO reqVO = new DeptXqListReqVO();
        reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        List<DeptXqDO> list = deptXqService.getSimpleXqDepts(reqVO);
        // 排序后，返回给前端
        list.sort(Comparator.comparing(DeptXqDO::getSort));
        return success(DeptXqConvert.INSTANCE.convertList03(list));
    }
}
