package cn.iocoder.yudao.module.pms.controller.admin.iotoperationplan;

import cn.iocoder.yudao.module.pms.controller.admin.inspect.plan.vo.IotInspectPlanRespVO;
import cn.iocoder.yudao.module.pms.service.iotoperationplandev.IotOperationPlanDevService;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserUpdateStatusReqVO;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import com.xingyuv.captcha.util.StringUtils;
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 java.util.stream.Collectors;

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.*;

import cn.iocoder.yudao.module.pms.controller.admin.iotoperationplan.vo.*;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotoperationplan.IotOperationPlanDO;
import cn.iocoder.yudao.module.pms.service.iotoperationplan.IotOperationPlanService;

@Tag(name = "管理后台 - 运行记录计划")
@RestController
@RequestMapping("/rq/iot-operation-plan")
@Validated
public class IotOperationPlanController {

    @Resource
    private IotOperationPlanService iotOperationPlanService;
    @Resource
    private IotOperationPlanDevService planDevService;
    @Resource
    private DeptService deptService;
    @Resource
    private AdminUserApi adminUserApi;
    @Autowired
    private DeptApi deptApi;

    @PutMapping("/update-status")
    @Operation(summary = "修改巡检计划状态")
    @PreAuthorize("@ss.hasPermission('rq:iot-inspect-plan:update')")
    public CommonResult<Boolean> updateUserStatus(@Valid @RequestBody UserUpdateStatusReqVO reqVO) {
        iotOperationPlanService.updateInspectPlanStatus(reqVO.getId(), reqVO.getStatus());
        return success(true);
    }



    @PostMapping("/create")
    @Operation(summary = "创建运行记录计划")
    @PreAuthorize("@ss.hasPermission('rq:iot-operation-plan:create')")
    public CommonResult<Long> createIotOperationPlan(@Valid @RequestBody IotOperationPlanSaveReqVO createReqVO) {
        return success(iotOperationPlanService.createIotOperationPlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新运行记录计划")
    @PreAuthorize("@ss.hasPermission('rq:iot-operation-plan:update')")
    public CommonResult<Boolean> updateIotOperationPlan(@Valid @RequestBody IotOperationPlanSaveReqVO updateReqVO) {
        iotOperationPlanService.updateIotOperationPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除运行记录计划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-operation-plan:delete')")
    public CommonResult<Boolean> deleteIotOperationPlan(@RequestParam("id") Long id) {
        iotOperationPlanService.deleteIotOperationPlan(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得运行记录计划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-operation-plan:query')")
    public CommonResult<IotOperationPlanRespVO> getIotOperationPlan(@RequestParam("id") Long id) {
        IotOperationPlanDO iotOperationPlan = iotOperationPlanService.getIotOperationPlan(id);
        return success(BeanUtils.toBean(iotOperationPlan, IotOperationPlanRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得运行记录计划分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-operation-plan:query')")
    public CommonResult<PageResult<IotOperationPlanRespVO>> getIotOperationPlanPage(@Valid IotOperationPlanPageReqVO pageReqVO) {
        Set<Long> idList = new HashSet<>();
        if(Objects.nonNull(pageReqVO.getDeptId())){
            idList =  deptService.getChildDeptIdListFromCache(pageReqVO.getDeptId());
            idList.add(pageReqVO.getDeptId());
        }
        PageResult<IotOperationPlanDO> pageResult = iotOperationPlanService.getIotOperationPlanPage(pageReqVO,idList);

        List<IotOperationPlanRespVO> collect = pageResult.getList().stream().map(e -> {
            IotOperationPlanRespVO planRespVO = new IotOperationPlanRespVO();
            BeanUtils.copyProperties(e, planRespVO);
            if (StringUtils.isNotBlank(e.getCreator())) {
                AdminUserRespDTO user = adminUserApi.getUser(Long.valueOf(e.getCreator()));
                if (Objects.nonNull(user)) {
                    planRespVO.setCreateName(user.getNickname());
                    if (Objects.nonNull(e.getDeptId())) {
                        DeptRespDTO dept = deptApi.getDept(e.getDeptId());
                        planRespVO.setDeptName(Objects.nonNull(dept)?dept.getName():"");
                    }
                }
            }
            return planRespVO;
        }).collect(Collectors.toList());


        return success(new PageResult<>(collect, pageResult.getTotal()));
    }

    /*@GetMapping("/export-excel")
    @Operation(summary = "导出运行记录计划 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-operation-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotOperationPlanExcel(@Valid IotOperationPlanPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotOperationPlanDO> list = iotOperationPlanService.getIotOperationPlanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "运行记录计划.xls", "数据", IotOperationPlanRespVO.class,
                        BeanUtils.toBean(list, IotOperationPlanRespVO.class));
    }*/

}