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

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.building.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.building.BuildingDO;
import com.jojubanking.boot.module.system.convert.building.BuildingConvert;
import com.jojubanking.boot.module.system.service.building.BuildingService;

@Api(tags = "管理后台 - 楼层导视")
@RestController
@RequestMapping("/system/building")
@Validated
public class BuildingController {

    @Resource
    private BuildingService buildingService;

    @PostMapping("/create")
    @ApiOperation("创建楼层导视")
    @PreAuthorize("@ss.hasPermission('system:building:create')")
    public CommonResult<Long> createBuilding(@Valid @RequestBody BuildingCreateReqVO createReqVO) {
        return success(buildingService.createBuilding(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新楼层导视")
    @PreAuthorize("@ss.hasPermission('system:building:update')")
    public CommonResult<Boolean> updateBuilding(@Valid @RequestBody BuildingUpdateReqVO updateReqVO) {
        buildingService.updateBuilding(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除楼层导视")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:building:delete')")
    public CommonResult<Boolean> deleteBuilding(@RequestParam("id") Long id) {
        buildingService.deleteBuilding(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得楼层导视")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:building:query')")
    public CommonResult<BuildingRespVO> getBuilding(@RequestParam("id") Long id) {
        BuildingDO building = buildingService.getBuilding(id);
        return success(BuildingConvert.INSTANCE.convert(building));
    }

    @GetMapping("/list")
    @ApiOperation("获得楼层导视列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:building:query')")
    public CommonResult<List<BuildingRespVO>> getBuildingList(@RequestParam("ids") Collection<Long> ids) {
        List<BuildingDO> list = buildingService.getBuildingList(ids);
        return success(BuildingConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/list01")
    @ApiOperation("获得楼层导视列表01")
    @PreAuthorize("@ss.hasPermission('system:building:query')")
    public CommonResult<List<BuildingRespVO>> listDepts(BuildingListReqVO reqVO) {
        List<BuildingDO> list = buildingService.getSimpleBuildings(reqVO);
        list.sort(Comparator.comparing(BuildingDO::getFid));
        return success(BuildingConvert.INSTANCE.convertList(list));
    }
    @GetMapping("/list-all-simple")
    @ApiOperation(value = "获取楼层精简信息列表", notes = "只包含被开启的，主要用于前端的下拉选项")
    public CommonResult<List<BuildingSimpleRespVO>> getSimpleBuildings() {
        // 获得部门列表，只要开启状态的
        BuildingListReqVO reqVO = new BuildingListReqVO();
        reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        List<BuildingDO> list = buildingService.getSimpleBuildings(reqVO);
        // 排序后，返回给前端
        list.sort(Comparator.comparing(BuildingDO::getFid));
        return success(BuildingConvert.INSTANCE.convertList03(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得楼层导视分页")
    @PreAuthorize("@ss.hasPermission('system:building:query')")
    public CommonResult<PageResult<BuildingRespVO>> getBuildingPage(@Valid BuildingPageReqVO pageVO) {
        PageResult<BuildingDO> pageResult = buildingService.getBuildingPage(pageVO);
        return success(BuildingConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出楼层导视 Excel")
    @PreAuthorize("@ss.hasPermission('system:building:export')")
    @OperateLog(type = EXPORT)
    public void exportBuildingExcel(@Valid BuildingExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BuildingDO> list = buildingService.getBuildingList(exportReqVO);
        // 导出 Excel
        List<BuildingExcelVO> datas = BuildingConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "楼层导视.xls", "数据", BuildingExcelVO.class, datas);
    }

}
