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.PmMaintenanceOrder;
import com.bs.pm.service.IPmMaintenanceOrderService;
import javax.annotation.Resource;

/**
 * 报修单Controller
 *
 * @author bs
 * @date 2025-10-05
 */
@Api(tags = "报修单")
@RestController
@RequestMapping("/maintenance/order")
public class PmMaintenanceOrderController extends BaseController {
    @Resource
    private IPmMaintenanceOrderService pmMaintenanceOrderService;

    /**
     * 分页查询报修单列表
     */
    @ApiOperation("分页查询报修单列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(PmMaintenanceOrder pmMaintenanceOrder) {
        startPage();
        LambdaQueryWrapper<PmMaintenanceOrder> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,pmMaintenanceOrder);
        List<PmMaintenanceOrder> list = pmMaintenanceOrderService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询报修单列表
     */
    @ApiOperation("查询报修单列表")
    @GetMapping("/list")
    public AjaxResult list(PmMaintenanceOrder pmMaintenanceOrder) {
        LambdaQueryWrapper<PmMaintenanceOrder> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,pmMaintenanceOrder);
        List<PmMaintenanceOrder> list = pmMaintenanceOrderService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出报修单列表
     */
    @ApiOperation("导出报修单列表")
    @Log(title = "报修单导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PmMaintenanceOrder pmMaintenanceOrder) {
        LambdaQueryWrapper<PmMaintenanceOrder> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,pmMaintenanceOrder);
        List<PmMaintenanceOrder> list = pmMaintenanceOrderService.list(queryWrapper);
        ExcelUtil<PmMaintenanceOrder> util = new ExcelUtil<PmMaintenanceOrder>(PmMaintenanceOrder. class);
        util.exportExcel(response, list, "报修单数据");
    }

    /**
     * 获取报修单详细信息
     */
    @ApiOperation("获取报修单详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(pmMaintenanceOrderService.getById(id));
    }

    /**
     * 新增报修单
     */
    @ApiOperation("新增报修单")
    @Log(title = "报修单新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PmMaintenanceOrder pmMaintenanceOrder) {
        return toAjax(pmMaintenanceOrderService.save(pmMaintenanceOrder));
    }

    /**
     * 修改报修单
     */
    @ApiOperation("修改报修单")
    @Log(title = "报修单修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PmMaintenanceOrder pmMaintenanceOrder) {
        return toAjax(pmMaintenanceOrderService.updateById(pmMaintenanceOrder));
    }

    /**
     * 删除报修单
     */
    @ApiOperation("删除报修单")
    @Log(title = "报修单删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(pmMaintenanceOrderService.removeBatchByIds(ids));
    }

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


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

        //业主类型
        if(null != (pmMaintenanceOrder.getOwnerType())){
            queryWrapper.eq(PmMaintenanceOrder::getOwnerType,pmMaintenanceOrder.getOwnerType());
        }

        //业主id
        if(null != (pmMaintenanceOrder.getOwnerId())){
            queryWrapper.eq(PmMaintenanceOrder::getOwnerId,pmMaintenanceOrder.getOwnerId());
        }

        //房屋id
        if(null != (pmMaintenanceOrder.getHouseId())){
            queryWrapper.eq(PmMaintenanceOrder::getHouseId,pmMaintenanceOrder.getHouseId());
        }

        //房屋业主id
        if(null != (pmMaintenanceOrder.getHouseOwnerId())){
            queryWrapper.eq(PmMaintenanceOrder::getHouseOwnerId,pmMaintenanceOrder.getHouseOwnerId());
        }

        //报修单号
        if(StringUtils.isNotEmpty(pmMaintenanceOrder.getOrderNo())){
            queryWrapper.eq(PmMaintenanceOrder::getOrderNo,pmMaintenanceOrder.getOrderNo());
        }

        //故障类型
        if(StringUtils.isNotEmpty(pmMaintenanceOrder.getFaultType())){
            queryWrapper.eq(PmMaintenanceOrder::getFaultType,pmMaintenanceOrder.getFaultType());
        }

        //故障描述
        if(StringUtils.isNotEmpty(pmMaintenanceOrder.getFaultDesc())){
            queryWrapper.eq(PmMaintenanceOrder::getFaultDesc,pmMaintenanceOrder.getFaultDesc());
        }

        //故障照片
        if(null != (pmMaintenanceOrder.getFaultPhoto())){
            queryWrapper.eq(PmMaintenanceOrder::getFaultPhoto,pmMaintenanceOrder.getFaultPhoto());
        }

        //报修渠道
        if(StringUtils.isNotEmpty(pmMaintenanceOrder.getReportChannel())){
            queryWrapper.eq(PmMaintenanceOrder::getReportChannel,pmMaintenanceOrder.getReportChannel());
        }

        //报修时间
        if(null != (pmMaintenanceOrder.getReportTime())){
            queryWrapper.eq(PmMaintenanceOrder::getReportTime,pmMaintenanceOrder.getReportTime());
        }

        //紧急程度
        if(StringUtils.isNotEmpty(pmMaintenanceOrder.getUrgencyLevel())){
            queryWrapper.eq(PmMaintenanceOrder::getUrgencyLevel,pmMaintenanceOrder.getUrgencyLevel());
        }

        //状态
        if(StringUtils.isNotEmpty(pmMaintenanceOrder.getStatus())){
            queryWrapper.eq(PmMaintenanceOrder::getStatus,pmMaintenanceOrder.getStatus());
        }

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

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

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

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

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

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

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

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

        }

}
