package com.bs.pm.controller;

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

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bs.common.annotation.Log;
import com.bs.common.core.controller.BaseController;
import com.bs.common.core.domain.AjaxResult;
import com.bs.common.core.page.TableDataInfo;
import com.bs.common.enums.BusinessType;
import com.bs.common.utils.poi.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import com.bs.pm.domain.PmFacilityMaintenaceRecord;
import com.bs.pm.service.IPmFacilityMaintenaceRecordService;
import javax.annotation.Resource;

/**
 * 设施维保记录Controller
 *
 * @author bs
 * @date 2025-10-04
 */
@Api(tags = "设施维保记录")
@RestController
@RequestMapping("/facility/record")
public class PmFacilityMaintenaceRecordController extends BaseController {
    @Resource
    private IPmFacilityMaintenaceRecordService pmFacilityMaintenaceRecordService;

    /**
     * 分页查询设施维保记录列表
     */
    @ApiOperation("分页查询设施维保记录列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(PmFacilityMaintenaceRecord pmFacilityMaintenaceRecord) {
        startPage();
        LambdaQueryWrapper<PmFacilityMaintenaceRecord> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,pmFacilityMaintenaceRecord);
        List<PmFacilityMaintenaceRecord> list = pmFacilityMaintenaceRecordService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询设施维保记录列表
     */
    @ApiOperation("查询设施维保记录列表")
    @GetMapping("/list")
    public AjaxResult list(PmFacilityMaintenaceRecord pmFacilityMaintenaceRecord) {
        LambdaQueryWrapper<PmFacilityMaintenaceRecord> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,pmFacilityMaintenaceRecord);
        List<PmFacilityMaintenaceRecord> list = pmFacilityMaintenaceRecordService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出设施维保记录列表
     */
    @ApiOperation("导出设施维保记录列表")
    @Log(title = "设施维保记录导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PmFacilityMaintenaceRecord pmFacilityMaintenaceRecord) {
        LambdaQueryWrapper<PmFacilityMaintenaceRecord> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,pmFacilityMaintenaceRecord);
        List<PmFacilityMaintenaceRecord> list = pmFacilityMaintenaceRecordService.list(queryWrapper);
        ExcelUtil<PmFacilityMaintenaceRecord> util = new ExcelUtil<PmFacilityMaintenaceRecord>(PmFacilityMaintenaceRecord. class);
        util.exportExcel(response, list, "设施维保记录数据");
    }

    /**
     * 获取设施维保记录详细信息
     */
    @ApiOperation("获取设施维保记录详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(pmFacilityMaintenaceRecordService.getById(id));
    }

    /**
     * 新增设施维保记录
     */
    @ApiOperation("新增设施维保记录")
    @Log(title = "设施维保记录新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PmFacilityMaintenaceRecord pmFacilityMaintenaceRecord) {
        return toAjax(pmFacilityMaintenaceRecordService.save(pmFacilityMaintenaceRecord));
    }

    /**
     * 修改设施维保记录
     */
    @ApiOperation("修改设施维保记录")
    @Log(title = "设施维保记录修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PmFacilityMaintenaceRecord pmFacilityMaintenaceRecord) {
        return toAjax(pmFacilityMaintenaceRecordService.updateById(pmFacilityMaintenaceRecord));
    }

    /**
     * 删除设施维保记录
     */
    @ApiOperation("删除设施维保记录")
    @Log(title = "设施维保记录删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(pmFacilityMaintenaceRecordService.removeBatchByIds(ids));
    }

    /**
      * 条件设置
      */
    private void condition (LambdaQueryWrapper<PmFacilityMaintenaceRecord> queryWrapper,PmFacilityMaintenaceRecord pmFacilityMaintenaceRecord){


        //id
        if(null != (pmFacilityMaintenaceRecord.getId())){
            queryWrapper.eq(PmFacilityMaintenaceRecord::getId,pmFacilityMaintenaceRecord.getId());
        }

        //计划id
        if(null != (pmFacilityMaintenaceRecord.getPlanId())){
            queryWrapper.eq(PmFacilityMaintenaceRecord::getPlanId,pmFacilityMaintenaceRecord.getPlanId());
        }

        //设施id
        if(null != (pmFacilityMaintenaceRecord.getFacilityId())){
            queryWrapper.eq(PmFacilityMaintenaceRecord::getFacilityId,pmFacilityMaintenaceRecord.getFacilityId());
        }

        //楼栋id
        if(null != (pmFacilityMaintenaceRecord.getBuildingId())){
            queryWrapper.eq(PmFacilityMaintenaceRecord::getBuildingId,pmFacilityMaintenaceRecord.getBuildingId());
        }

        //项目id
        if(null != (pmFacilityMaintenaceRecord.getProjectId())){
            queryWrapper.eq(PmFacilityMaintenaceRecord::getProjectId,pmFacilityMaintenaceRecord.getProjectId());
        }

        //设施名称
        if(StringUtils.isNotEmpty(pmFacilityMaintenaceRecord.getFacilityName())){
            queryWrapper.eq(PmFacilityMaintenaceRecord::getFacilityName,pmFacilityMaintenaceRecord.getFacilityName());
        }

        //执行人
        if(null != (pmFacilityMaintenaceRecord.getExecutorId())){
            queryWrapper.eq(PmFacilityMaintenaceRecord::getExecutorId,pmFacilityMaintenaceRecord.getExecutorId());
        }

        //执行部门
        if(null != (pmFacilityMaintenaceRecord.getExecutorDept())){
            queryWrapper.eq(PmFacilityMaintenaceRecord::getExecutorDept,pmFacilityMaintenaceRecord.getExecutorDept());
        }

        //执行时间
        if(null != (pmFacilityMaintenaceRecord.getExecTime())){
            queryWrapper.eq(PmFacilityMaintenaceRecord::getExecTime,pmFacilityMaintenaceRecord.getExecTime());
        }

        //实际维保内容
        if(StringUtils.isNotEmpty(pmFacilityMaintenaceRecord.getContent())){
            queryWrapper.eq(PmFacilityMaintenaceRecord::getContent,pmFacilityMaintenaceRecord.getContent());
        }

        //耗材使用
        if(StringUtils.isNotEmpty(pmFacilityMaintenaceRecord.getConsumables())){
            queryWrapper.eq(PmFacilityMaintenaceRecord::getConsumables,pmFacilityMaintenaceRecord.getConsumables());
        }

        //维保费用
        if(null != (pmFacilityMaintenaceRecord.getCostAmount())){
            queryWrapper.eq(PmFacilityMaintenaceRecord::getCostAmount,pmFacilityMaintenaceRecord.getCostAmount());
        }

        //维保状态
        if(StringUtils.isNotEmpty(pmFacilityMaintenaceRecord.getStatus())){
            queryWrapper.eq(PmFacilityMaintenaceRecord::getStatus,pmFacilityMaintenaceRecord.getStatus());
        }

        //维保结果
        if(StringUtils.isNotEmpty(pmFacilityMaintenaceRecord.getResult())){
            queryWrapper.eq(PmFacilityMaintenaceRecord::getResult,pmFacilityMaintenaceRecord.getResult());
        }

        //文件id
        if(null != (pmFacilityMaintenaceRecord.getFileId())){
            queryWrapper.eq(PmFacilityMaintenaceRecord::getFileId,pmFacilityMaintenaceRecord.getFileId());
        }

        //备注
        if(StringUtils.isNotEmpty(pmFacilityMaintenaceRecord.getRemark())){
            queryWrapper.eq(PmFacilityMaintenaceRecord::getRemark,pmFacilityMaintenaceRecord.getRemark());
        }

        //删除标志（0代表存在 2代表删除）
        if(StringUtils.isNotEmpty(pmFacilityMaintenaceRecord.getDelFlag())){
            queryWrapper.eq(PmFacilityMaintenaceRecord::getDelFlag,pmFacilityMaintenaceRecord.getDelFlag());
        }

        //创建部门
        if(null != (pmFacilityMaintenaceRecord.getCreateDept())){
            queryWrapper.eq(PmFacilityMaintenaceRecord::getCreateDept,pmFacilityMaintenaceRecord.getCreateDept());
        }

        //创建人
        if(null != (pmFacilityMaintenaceRecord.getCreateBy())){
            queryWrapper.eq(PmFacilityMaintenaceRecord::getCreateBy,pmFacilityMaintenaceRecord.getCreateBy());
        }

        //创建时间
        if(null != (pmFacilityMaintenaceRecord.getCreateTime())){
            queryWrapper.eq(PmFacilityMaintenaceRecord::getCreateTime,pmFacilityMaintenaceRecord.getCreateTime());
        }

        //修改人
        if(null != (pmFacilityMaintenaceRecord.getUpdateBy())){
            queryWrapper.eq(PmFacilityMaintenaceRecord::getUpdateBy,pmFacilityMaintenaceRecord.getUpdateBy());
        }

        //修改时间
        if(null != (pmFacilityMaintenaceRecord.getUpdateTime())){
            queryWrapper.eq(PmFacilityMaintenaceRecord::getUpdateTime,pmFacilityMaintenaceRecord.getUpdateTime());
        }

        //租户ID
        if(StringUtils.isNotEmpty(pmFacilityMaintenaceRecord.getTenantId())){
            queryWrapper.eq(PmFacilityMaintenaceRecord::getTenantId,pmFacilityMaintenaceRecord.getTenantId());
        }

        }

}
