package cn.iocoder.yudao.module.inspection.controller;

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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.inspection.entity.InspectionInfo;
import cn.iocoder.yudao.module.inspection.entity.InspectionWorkNode;
import cn.iocoder.yudao.module.inspection.service.IInspectionInfoService;
import cn.iocoder.yudao.module.inspection.service.IInspectionWorkNodeService;
import cn.iocoder.yudao.util.ExcelUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.iocoder.yudao.module.core.controller.BaseController;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2023-08-13
 */
@RestController
@RequestMapping("/admin-api/system/info")
public class InspectionInfoController extends BaseController {
    @Autowired
    private IInspectionInfoService inspectionInfoService;

    @Autowired
    private IInspectionWorkNodeService inspectionWorkNodeService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/list")
    public CommonResult list(InspectionInfo inspectionInfo,
                             @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        Page<InspectionInfo> page = new Page<>(pageNum, pageSize);
        IPage<InspectionInfo> list = inspectionInfoService.selectInspectionInfoList(page, inspectionInfo);
        return success(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, InspectionInfo inspectionInfo,
                       @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                       @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        Page<InspectionInfo> page = new Page<>(pageNum, pageSize);
        IPage<InspectionInfo> list = inspectionInfoService.selectInspectionInfoList(page, inspectionInfo);
        ExcelUtil<InspectionInfo> util = new ExcelUtil<InspectionInfo>(InspectionInfo.class);
        util.exportExcel(response, list.getRecords(), "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping(value = "/{id}")
    public CommonResult getInfo(@PathVariable("id") Long id) {
        return success(inspectionInfoService.selectInspectionInfoById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PostMapping("/add")
    public CommonResult add(@RequestBody InspectionInfo inspectionInfo) throws Exception {

        return toAjax(inspectionInfoService.insertInspectionInfo(inspectionInfo));
    }

    /**
     * 清账
     */
    @PostMapping("/settleAccounts")
    public CommonResult settleAccounts(Long InspectionId) {
        inspectionInfoService.settleAccounts(InspectionId);
        return success();
    }


    @PostMapping("/update")
    public CommonResult edit(@RequestBody InspectionInfo inspectionInfo) throws Exception {
        return toAjax(inspectionInfoService.updateInspectionInfo(inspectionInfo));
    }


    @PostMapping("/del")
    public CommonResult remove(@RequestParam("id") Long id) {
        return toAjax(inspectionInfoService.deleteInspectionInfoByIds(id));
    }

    /**
     * 根据当前登陆人获取可以选择的工单
     *
     * @return
     */
    @GetMapping("geStelectInspection")
    public CommonResult geStelectInspection(InspectionInfo inspectionInfo,
                                            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        Page<InspectionInfo> page = new Page<>(pageNum, pageSize);
        return success(inspectionInfoService.geStelectInspection(page,inspectionInfo));
    }

    /**
     * 获取工单详情
     *
     * @param inspectionId 工单id
     * @param workNodeId   节点id （geStelectInspection这个方法中返回了）
     * @return
     */
    @GetMapping("getInspectionInfo")
    public CommonResult getInspectionInfo(Integer inspectionId, String workNodeId) {
        return success(inspectionInfoService.getWorkNode(inspectionId, workNodeId));
    }

    /**
     * 员工接单
     *
     * @param inspectionId 工单id
     * @param workNodeId   节点id （geStelectInspection这个方法中返回了）
     * @return
     */
    @PostMapping("orderTaking")
    public CommonResult orderTaking(Integer inspectionId,String workNodeId) {
        inspectionWorkNodeService.updateInspectionWorkNode(inspectionId, workNodeId);
        return success("接单成功");
    }

    /**
     * 项目操作（退办理、重审、项目完成）
     * @param inspectionWorkNode
     */
    @PostMapping("controls")
    public void controls(@RequestBody InspectionWorkNode inspectionWorkNode){
        inspectionWorkNodeService.updateImageAndStep(inspectionWorkNode);
    }

    /**
     * 通过工单id获取工单流程
     * @param inspectionId
     * @return
     */
    @GetMapping("getWeorkNodesById")
    public CommonResult getWeorkNodesById(Integer inspectionId) {
        return success(inspectionInfoService.getWeorkNodesById(inspectionId));
     }
}
