package org.jeecg.modules.checkData.equipmentCheckData.controller;


import cn.hutool.core.util.ObjectUtil;
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.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.checkData.equipmentCheckData.dto.RecordsMasterCheckDTO;
import org.jeecg.modules.checkData.equipmentCheckData.entity.RecordsMasterCheck;
import org.jeecg.modules.checkData.equipmentCheckData.entity.TrackBedManagementCheck;
import org.jeecg.modules.checkData.equipmentCheckData.service.IRecordsMasterCheckService;
import org.jeecg.modules.checkData.equipmentCheckData.service.ITrackBedManagementCheckService;
import org.jeecg.modules.checkData.equipmentCheckData.vo.RecordsMasterCheckVO;
import org.jeecg.modules.checkData.equipmentCheckData.vo.TrackBedManagementCheckVO;
import org.jeecg.modules.deviceAsset.entity.TrackBedManagement;
import org.jeecg.modules.deviceAsset.service.ITrackBedManagementService;
import org.jeecg.modules.utils.BeanCopyUtil;
import org.jeecg.modules.utils.ExcelUtils;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 设备检查记录主表 前端控制器
 * </p>
 *
 * @author hkl
 * @since 2023-07-07
 */
@RestController
@RequestMapping("/recordsMaster/check")
@Api(tags = "设备检查记录主表控制器")
public class RecordsMasterCheckController extends JeecgController<RecordsMasterCheck, IRecordsMasterCheckService> {
    @Autowired
    private ITrackBedManagementService trackBedManagementService;
    @Autowired
    private ITrackBedManagementCheckService trackBedManagementCheckService;
    /**
     * 设备检查记录-分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param dto
     * @return
     */
    @AutoLog(value = "设备检查记录-分页列表查询")
    @ApiOperation(value = "设备检查记录-分页列表查询", notes = "设备检查记录-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<RecordsMasterCheckVO>> queryPageList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                             RecordsMasterCheckDTO dto) {
        Page<RecordsMasterCheckVO> pageData = new Page<>(pageNo, pageSize);
        pageData = this.service.queryPageList(pageData, dto);
        return Result.OK(pageData);
    }

    /**
     * 根据区间id和线别id查询道床列表
     * @param sectionId
     * @param lineAliasId
     * @return
     */
    @AutoLog(value = "根据区间id查询道床列表")
    @ApiOperation(value = "根据区间id查询道床列表", notes = "根据区间id和线别id查询道床列表")
    @GetMapping(value = "/getTrackBedBySubwayIdAndLineId")
    public Result<List<TrackBedManagement>> getTrackBedBySubwayIdAndLineId(@ApiParam(name = "区间id") String sectionId,
                                                      @ApiParam(name = "线别id") String lineAliasId) {
        List<TrackBedManagement> listResult = trackBedManagementService.lambdaQuery()
                .eq(ObjectUtil.isNotEmpty(sectionId), TrackBedManagement::getSectionId, sectionId)
                .eq(ObjectUtil.isNotEmpty(lineAliasId), TrackBedManagement::getLineAliasId, lineAliasId)
                .orderByAsc(TrackBedManagement::getTrackBedCode)
                .list();
        return Result.OK(listResult);
    }
    /**
     * 根据道床使用情况记录数据表的道床id查询对应的实用情况
     * @param managementId 道床id
     * @return
     */
    @AutoLog(value = "根据道床使用情况记录数据表的道床id和检查记录主表的id查询对应的实用情况")
    @ApiOperation(value = "根据道床使用情况记录数据表的道床id和检查记录主表的id查询对应的实用情况", notes = "根据道床使用情况记录数据表的道床id和检查记录主表的id查询对应的实用情况")
    @GetMapping(value = "/getTrackBedByManagementIdAndRecordId")
    public Result<List<TrackBedManagementCheckVO>> getTrackBedByManagementId(@ApiParam(name = "道床id") String managementId,@ApiParam(name = "检查记录表主键id") String recordId) {
        List<TrackBedManagementCheck> listResult = trackBedManagementCheckService.lambdaQuery()
                .eq(ObjectUtil.isNotEmpty(managementId), TrackBedManagementCheck::getTrakBedManagementId, managementId)
                .eq(ObjectUtil.isNotEmpty(recordId), TrackBedManagementCheck::getRecordsMasterCheckId, recordId)
                .eq(TrackBedManagementCheck::getDelFlag,"0")
                .orderByAsc(TrackBedManagementCheck::getCreateTime)
                .list();
        if(listResult!=null){
            List<TrackBedManagementCheckVO> resultList = BeanCopyUtil.copyListProperties(listResult, TrackBedManagementCheckVO::new);
            return Result.OK(resultList);
        }
        return Result.OK(new ArrayList<>());
    }

    /**
     * 设备检查记录导出excel
     * @param request
     * @param recordsMasterCheck
     * @return
     */
    @AutoLog(value = "设备检查记录导出excel")
    @ApiOperation(value = "设备检查记录导出excel", notes = "设备检查记录导出excel")
    @GetMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request,RecordsMasterCheck recordsMasterCheck) {
        return super.exportXls(request, recordsMasterCheck, RecordsMasterCheck.class, "道床使用情况检查");
    }

    /**
     * 导出excel
     *
     * @param request
     */
    @AutoLog(value = "设备检查记录-导出")
    @ApiOperation(value = "设备检查记录-导出", notes = "设备检查记录-导出")
    @GetMapping(value = "/exportXl")
    public ModelAndView exportXl(RecordsMasterCheck recordsMasterCheck, HttpServletRequest request) {
        // Step.1 组装查询条件
        //Step.2 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        List<RecordsMasterCheck> resultList = this.service.list(new LambdaQueryWrapper<RecordsMasterCheck>()
                .like(ObjectUtil.isNotEmpty(recordsMasterCheck.getEkCode()), RecordsMasterCheck::getEkCode, recordsMasterCheck.getEkCode())
                .eq(ObjectUtil.isNotEmpty(recordsMasterCheck.getSubwaySectionId()), RecordsMasterCheck::getSubwaySectionId, recordsMasterCheck.getSubwaySectionId())
                .eq(ObjectUtil.isNotEmpty(recordsMasterCheck.getLightRailId()), RecordsMasterCheck::getLightRailId, recordsMasterCheck.getLightRailId())
                .eq(ObjectUtil.isNotEmpty(recordsMasterCheck.getLineAliasId()), RecordsMasterCheck::getLineAliasId, recordsMasterCheck.getLineAliasId())
                .orderByAsc(RecordsMasterCheck::getCreateTime));
        //导出文件名称
        mv.addObject(NormalExcelConstants.FILE_NAME, "道床使用情况检查");
        mv.addObject(NormalExcelConstants.CLASS, RecordsMasterCheck.class);
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("道床使用情况检查列表数据", "导出人:", "导出信息"));
        mv.addObject(NormalExcelConstants.DATA_LIST, resultList);
        return mv;
    }
    /**
     * 设备检查记录-导出
     *
     * @param response
     */
    @AutoLog(value = "设备检查记录-导出")
    @ApiOperation(value = "设备检查记录-导出", notes = "设备检查记录-导出")
    @GetMapping("/exportXls2")
    public void export(RecordsMasterCheck recordsMasterCheck, HttpServletResponse response) {
        List<RecordsMasterCheck> resultList = this.service.list(new LambdaQueryWrapper<RecordsMasterCheck>()
                .like(ObjectUtil.isNotEmpty(recordsMasterCheck.getEkCode()), RecordsMasterCheck::getEkCode, recordsMasterCheck.getEkCode())
                .eq(ObjectUtil.isNotEmpty(recordsMasterCheck.getSubwaySectionId()), RecordsMasterCheck::getSubwaySectionId, recordsMasterCheck.getSubwaySectionId())
                .eq(ObjectUtil.isNotEmpty(recordsMasterCheck.getLightRailId()), RecordsMasterCheck::getLightRailId, recordsMasterCheck.getLightRailId())
                .eq(ObjectUtil.isNotEmpty(recordsMasterCheck.getLineAliasId()), RecordsMasterCheck::getLineAliasId, recordsMasterCheck.getLineAliasId())
                .orderByAsc(RecordsMasterCheck::getCreateTime));
        int num = 0;
        for (RecordsMasterCheck s : resultList) {
            num = num + 1;
            s.setRowNum(num);
        }
        ExcelUtils.export(response, "设备检查记录", resultList, RecordsMasterCheck.class);
    }
}
