package cn.iocoder.yudao.module.pms.controller.admin.inspect.route;

import cn.iocoder.yudao.module.pms.controller.admin.inspect.item.vo.IotInspectItemPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.inspect.item.vo.IotInspectItemRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.inspect.route.vo.IotInspectRoutePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.inspect.route.vo.IotInspectRouteRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.inspect.route.vo.IotInspectRouteSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.inspect.IotInspectItemDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.inspect.IotInspectRouteDO;
import cn.iocoder.yudao.module.pms.service.inspect.IotInspectItemService;
import cn.iocoder.yudao.module.pms.service.inspect.IotInspectRouteService;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import org.springframework.beans.factory.annotation.Autowired;
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.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

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

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 巡检路线")
@RestController
@RequestMapping("/rq/iot-inspect-route")
@Validated
public class IotInspectRouteController {

    @Resource
    private IotInspectRouteService iotInspectRouteService;
    @Resource
    private IotInspectItemService iotInspectItemService;
    @Autowired
    private DeptApi deptApi;

    @GetMapping("/items")
    @Operation(summary = "获得巡检项分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-inspect-item:query')")
    public CommonResult<PageResult<IotInspectItemRespVO>> getIotInspectItemPage(@Valid IotInspectItemPageReqVO pageReqVO) {
        PageResult<IotInspectItemDO> pageResult = iotInspectItemService.getRouteItem(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotInspectItemRespVO.class));
    }
    @PostMapping("/create")
    @Operation(summary = "创建巡检路线")
    @PreAuthorize("@ss.hasPermission('rq:iot-inspect-route:create')")
    public CommonResult<Long> createIotInspectRoute(@Valid @RequestBody IotInspectRouteSaveReqVO createReqVO) {
        return success(iotInspectRouteService.createIotInspectRoute(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新巡检路线")
    @PreAuthorize("@ss.hasPermission('rq:iot-inspect-route:update')")
    public CommonResult<Boolean> updateIotInspectRoute(@Valid @RequestBody IotInspectRouteSaveReqVO updateReqVO) {
        iotInspectRouteService.updateIotInspectRoute(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除巡检路线")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-inspect-route:delete')")
    public CommonResult<Boolean> deleteIotInspectRoute(@RequestParam("id") Long id) {
        iotInspectRouteService.deleteIotInspectRoute(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得巡检路线")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-inspect-route:query')")
    public CommonResult<IotInspectRouteRespVO> getIotInspectRoute(@RequestParam("id") Long id) {
        IotInspectRouteDO iotInspectRoute = iotInspectRouteService.getIotInspectRoute(id);
        return success(BeanUtils.toBean(iotInspectRoute, IotInspectRouteRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得巡检路线分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-inspect-route:query')")
    public CommonResult<PageResult<IotInspectRouteRespVO>> getIotInspectRoutePage(@Valid IotInspectRoutePageReqVO pageReqVO) {
        PageResult<IotInspectRouteDO> pageResult = iotInspectRouteService.getIotInspectRoutePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotInspectRouteRespVO.class));
    }

    @GetMapping("/plan/page")
    @Operation(summary = "获得设备巡检路线分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-inspect-route:query')")
    public CommonResult<PageResult<IotInspectRouteRespVO>> getDeviceIotInspectRoutePage(@Valid IotInspectRoutePageReqVO pageReqVO) {
        PageResult<IotInspectRouteDO> pageResult = iotInspectRouteService.getRoute(pageReqVO);
        PageResult<IotInspectRouteRespVO> bean = BeanUtils.toBean(pageResult, IotInspectRouteRespVO.class);
        for (IotInspectRouteRespVO iotInspectRouteRespVO : bean.getList()) {
            DeptRespDTO dept = deptApi.getDept(iotInspectRouteRespVO.getDeptId());
            if (Objects.nonNull(dept)) {
                iotInspectRouteRespVO.setDeptName(dept.getName());
            }
        }
        return success(bean);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出巡检路线 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-inspect-route:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotInspectRouteExcel(@Valid IotInspectRoutePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotInspectRouteDO> list = iotInspectRouteService.getIotInspectRoutePage(pageReqVO).getList();
        List<IotInspectRouteRespVO> bean = BeanUtils.toBean(list, IotInspectRouteRespVO.class);
        List<DeptRespDTO> deptList = deptApi.getDeptList();
        bean.forEach(e -> {
            deptList.stream().filter(f -> f.getId().equals(e.getDeptId())).findFirst().ifPresent(f -> {
                e.setDeptName(f.getName());
            });
        });
        // 导出 Excel
        ExcelUtils.write(response, "巡检路线.xls", "数据", IotInspectRouteRespVO.class,
                        BeanUtils.toBean(list, IotInspectRouteRespVO.class));
    }

}
