package cn.iocoder.yudao.module.project.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.project.entity.RepairProject;
import cn.iocoder.yudao.module.project.service.RepairProjectService;
import cn.iocoder.yudao.module.project.vo.RepairProjectPageReqVO;
import cn.iocoder.yudao.module.project.vo.RepairProjectRespVO;
import cn.iocoder.yudao.module.project.vo.RepairProjectSaveReqVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.Comparator;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 维修项目")
@RestController
@RequestMapping("/repair/project")
@Validated
public class RepairProjectController {

    @Resource
    private RepairProjectService repairProjectService;

    /**
     * 创建维修项目
     *
     * @param createReqVO RepairProjectSaveReqVO实体
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<java.lang.Boolean>
     * @author PQZ
     * @date 14:56 2024/9/12
     **/
    @PostMapping("/create")
    @Operation(summary = "创建维修项目")
    public CommonResult<RepairProject> createRepairProject(@Valid @RequestBody RepairProjectSaveReqVO createReqVO) {
        RepairProject repairProject = repairProjectService.saveRepairProject(createReqVO);
        return success(repairProject);
    }

    /**
     * 更新维修项目
     *
     * @param updateReqVO RepairProjectSaveReqVO实体
     * @return cn.iocoder.yudao.framewor.k.common.pojo.CommonResult<java.lang.Boolean>
     * @author PQZ
     * @date 14:56 2024/9/12
     **/
    @PutMapping("/update")
    @Operation(summary = "更新维修项目")
    public CommonResult<Boolean> updateRepairProject(@Valid @RequestBody RepairProjectSaveReqVO updateReqVO) {
        repairProjectService.saveRepairProject(updateReqVO);
        return success(true);
    }

    /**
     * 删除维修项目
     *
     * @param id 维修项目id
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<java.lang.Boolean>
     * @author PQZ
     * @date 14:59 2024/9/12
     **/
    @DeleteMapping("/delete")
    @Operation(summary = "删除维修项目")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteRepairProject(@RequestParam("id") String id) {
        repairProjectService.deleteRepairProject(id);
        return success(true);
    }

    /**
     * 根据维修项目id获取维修项目
     *
     * @param id 维修项目id
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<cn.iocoder.yudao.module.project.vo.RepairProjectRespVO>
     * @author PQZ
     * @date 14:59 2024/9/12
     **/
    @GetMapping("/get")
    @Operation(summary = "获得维修项目")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<RepairProjectRespVO> getRepairProject(@RequestParam("id") String id) {
        RepairProject repairProject = repairProjectService.getRepairProject(id);
        return success(BeanUtils.toBean(repairProject, RepairProjectRespVO.class));
    }


    /**
     * 分页获取维修项目分页
     *
     * @param pageReqVO RepairProjectPageReqVO
     * @param pageNo    分页参数
     * @param pageSize  分页参数
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<cn.iocoder.yudao.framework.common.pojo.PageResult < cn.iocoder.yudao.module.project.vo.RepairProjectRespVO>>
     * @author PQZ
     * @date 15:04 2024/9/12
     **/
    @GetMapping("/page")
    @Operation(summary = "获得维修项目分页")
    public CommonResult<IPage<?>> getRepairProjectPage(RepairProjectPageReqVO pageReqVO,
                                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<RepairProjectRespVO> page = new Page<>(pageNo, pageSize);
        return success(repairProjectService.queryListPage(pageReqVO,page));
    }

    /**
     * 根据名称获取最新的数据
     *
     * @author 小李
     * @date 18:01 2024/9/25
    **/
    @GetMapping("/getByName")
    @Operation(summary = "根据名称获取最新的数据")
    public CommonResult<?> getRepairProjectByName(@RequestParam("name") String name) {
        List<RepairProject> list = repairProjectService.list(new LambdaQueryWrapper<RepairProject>().eq(RepairProject::getName, name));
        return success(list.stream().max(Comparator.comparing(RepairProject::getCreateTime)).orElse(null));
    }

    /**
     * 获取维修项目和分类
     * @return
     */
    @GetMapping("/getRepairProjectAndCateGory")
    public CommonResult getRepairProjectAndCateGory(){
        return success(repairProjectService.getRepairProjectAndCateGory());
    }
}