package org.jeecg.modules.maintenanceWork.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.dynamicStaticAnalysis.entity.AnalysisBatch;
import org.jeecg.modules.maintenanceWork.dto.*;
import org.jeecg.modules.maintenanceWork.entity.WorkBatch;
import org.jeecg.modules.maintenanceWork.entity.WorkBatchDetail;
import org.jeecg.modules.maintenanceWork.service.IWorkBatchService;
import org.jeecg.modules.maintenanceWork.vo.WorkBatchDetailModifyVO;
import org.jeecg.modules.maintenanceWork.vo.WorkBatchVO;
import org.jeecg.modules.maintenanceWork.service.IWorkBatchDetailService;
import org.jeecg.modules.maintenanceWork.service.IWorkBatchDetailModifyService;
import org.jeecg.modules.maintenanceWork.vo.WorkUnitDeviceModifyVO;
import org.springframework.web.bind.annotation.*;

import org.jeecg.common.system.base.controller.JeecgController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 维修作业管理-作业计划批次管理表 前端控制器
 * </p>
 *
 * @author hkl
 * @since 2023-08-09
 */
@RestController
@RequestMapping("/maintenanceWork/workBatch")
@Api(tags = "维修作业管理-作业计划批次管理")
public class WorkBatchController extends JeecgController<WorkBatch, IWorkBatchService> {

    @Resource
    private IWorkBatchDetailService workBatchDetailService;

    @Resource
    private IWorkBatchDetailModifyService workBatchDetailModifyService;

    @AutoLog(value = "维修作业管理-作业计划批次管理-分页列表查询")
    @ApiOperation(value = "维修作业管理-作业计划批次管理-分页列表查询", notes = "维修作业管理-作业计划批次管理-分页列表查询")
    @GetMapping(value = "/listRecord")
    public Result<IPage<WorkBatchVO>> listRecord(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                 WorkBatchDTO dto) {
        Page<WorkBatchVO> pageData = new Page<>(pageNo, pageSize);
        pageData = this.service.listRecord(pageData, dto);
        return Result.OK(pageData);
    }

    @AutoLog(value = "维修作业管理-作业计划批次管理-维修作业管理页面新增")
    @ApiOperation(value = "维修作业管理-作业计划批次管理-维修作业管理页面新增", notes = "维修作业管理-作业计划批次管理-维修作业管理页面新增")
    @PostMapping(value = "/saveRecord")
    public Result<String> saveRecord(@RequestBody WorkBatchSaveOrUpdateDTO dto) {
        // 去重
        WorkBatch exist = this.service.lambdaQuery()
                .eq(WorkBatch::getWorkBatchCode, dto.getWorkBatchCode())
                .ne(ObjectUtil.isNotEmpty(dto.getId()), WorkBatch::getId, dto.getId())
                .last("limit 1")
                .one();
        if (exist != null) {
            throw new JeecgBootException("【" + dto.getWorkBatchCode() + "】作业批次已存在");
        }

        this.service.saveRecord(dto);

        return Result.OK("操作成功");
    }

    @AutoLog(value = "维修作业管理-作业计划批次管理-超限页面新增")
    @ApiOperation(value = "维修作业管理-作业计划批次管理-超限页面新增", notes = "维修作业管理-作业计划批次管理-超限页面新增")
    @PostMapping(value = "/transfiniteSaveRecord")
    public Result<String> transfiniteSaveRecord(@RequestBody WorkBatchTransfiniteSaveDTO dto) {
        WorkBatch exist = this.service.lambdaQuery()
                .eq(WorkBatch::getWorkBatchCode, dto.getWorkBatchCode())
                .last("limit 1")
                .one();
        if (exist != null) {
            throw new JeecgBootException("【" + dto.getWorkBatchCode() + "】作业批次已存在");
        }
        this.service.transfiniteSaveRecord(dto);

        return Result.OK("操作成功");
    }

    @AutoLog(value = "维修作业管理-作业计划批次管理-超限页面新增")
    @ApiOperation(value = "维修作业管理-作业计划批次管理-超限页面新增", notes = "维修作业管理-作业计划批次管理-超限页面新增")
    @PostMapping(value = "/batchSaveRecord")
    public Result<String> batchSaveRecord(@RequestBody AnalysisBatchSaveDTO dto) {
        WorkBatch exist = this.service.lambdaQuery()
                .eq(WorkBatch::getWorkBatchCode, dto.getWorkBatchCode())
                .last("limit 1")
                .one();
        if (exist != null) {
            throw new JeecgBootException("【" + dto.getWorkBatchCode() + "】作业批次已存在");
        }

        this.service.batchSaveRecord(dto);

        return Result.OK("操作成功");
    }


    @AutoLog(value = "维修作业管理-作业计划批次管理-通过id删除")
    @ApiOperation(value = "维修作业管理-作业计划批次管理-通过id删除", notes = "维修作业管理-作业计划批次管理-通过id删除")
    @GetMapping(value = "/deleteRecord")
    public Result<String> deleteRecord(@RequestParam(name = "id", required = true) String id) {


        this.service.deleteRecord(id);


        return Result.OK("删除成功!");
    }

    @AutoLog(value = "维修作业管理-作业计划批次管理-获取里程单元设备")
    @ApiOperation(value = "维修作业管理-作业计划批次管理-获取里程单元设备", notes = "维修作业管理-作业计划批次管理-获取里程单元设备")
    @PostMapping(value = "/getUnitDeviceList")
    public Result<Page<Map<String, Object>>> getUnitDevicePage(@RequestBody UnitDeviceDTO dto) {
        Page<Map<String, Object>> pageData = new Page<>(dto.getPageNo(), dto.getPageSize());
        pageData = this.service.getUnitDevicePage(pageData, dto);
        return Result.OK(pageData);
    }


    @AutoLog(value = "维修作业管理-作业计划批次管理-获取整改")
    @ApiOperation(value = "维修作业管理-作业计划批次管理-获取整改", notes = "维修作业-维修作业批列表-获取设备列表")
    @GetMapping(value = "/getUnitDeviceModify")
    public Result<WorkUnitDeviceModifyVO> getUnitDeviceModify(String workBatchDetailId) {

        WorkBatchDetail workBatchDetail = workBatchDetailService.getById(workBatchDetailId);
        WorkUnitDeviceModifyVO vo = new WorkUnitDeviceModifyVO();
        vo.setWorkBatchDetailId(workBatchDetail.getId());

        if (ObjectUtil.isNotEmpty(workBatchDetail)) {
            List<WorkBatchDetailModifyVO> detailList = workBatchDetailModifyService.getUnitDeviceModify(workBatchDetailId);
            vo.setDetailList(detailList);
        }
        return Result.ok(vo);
    }


}
