package com.bonus.material.fieldMaintenance.controller;

import cn.hutool.core.convert.Convert;
import com.bonus.common.biz.config.ListPagingUtil;
import com.bonus.common.core.utils.ServletUtils;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import com.bonus.material.common.annotation.PreventRepeatSubmit;
import com.bonus.material.fieldMaintenance.domain.FieldApplyDetails;
import com.bonus.material.fieldMaintenance.domain.FieldApplyInfo;
import com.bonus.material.fieldMaintenance.service.FieldMaintenanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

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

@Api(tags = "现场维修接口")
@RestController
@RequestMapping("/field_apply_info")
public class FieldMaintenanceController extends BaseController {
    @Resource
    private FieldMaintenanceService fieldMaintenanceService;

    /**
     * 查询现场维修列表
     */
    @ApiOperation(value = "查询现场维修列表")
    @GetMapping("/list")
    public AjaxResult list(FieldApplyInfo fieldApplyInfo) {
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<FieldApplyInfo> list = fieldMaintenanceService.selectFieldApplyInfoList(fieldApplyInfo);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }


    /**
     * 查询现场维修列表
     */
    @ApiOperation(value = "查询现场维修列表")
    @GetMapping("/listDetails")
    public AjaxResult listDetails(FieldApplyDetails fieldApplyDetails) {
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<FieldApplyDetails> list = fieldMaintenanceService.selectFieldApplyDetailsList(fieldApplyDetails);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }


    /**
     * 新增维修任务app
     *
     * @param fieldApplyInfo
     * @return
     */
    @ApiOperation(value = "新增任务app")
    @PreventRepeatSubmit
    @SysLog(title = "现场维修任务", businessType = OperaType.INSERT, logType = 1, module = "仓储管理->新增现场维修任务app")
    @PostMapping("/insert")
    public AjaxResult insertApp(@RequestBody FieldApplyInfo fieldApplyInfo) {
        try {
            return fieldMaintenanceService.insert(fieldApplyInfo);
        } catch (Exception e) {
            return error();
        }
    }


    @ApiOperation(value = "获取任务详情")
    @PreventRepeatSubmit
    @SysLog(title = "现场维修任务", businessType = OperaType.INSERT, logType = 1, module = "仓储管理->获取任务详情")
    @PostMapping("/selectFieldApplyInfoById")
    public AjaxResult selectFieldApplyInfoById(@RequestBody FieldApplyInfo fieldApplyInfo) {
        try {
            return fieldMaintenanceService.selectFieldApplyInfoById(fieldApplyInfo.getId());
        } catch (Exception e) {
            return error();
        }
    }


    @ApiOperation(value = "删除任务")
    @PreventRepeatSubmit
    @SysLog(title = "现场维修任务", businessType = OperaType.INSERT, logType = 1, module = "仓储管理->删除任务")
    @PostMapping("/deleteFieldApplyInfoById")
    public AjaxResult deleteFieldApplyInfoById(@RequestBody FieldApplyInfo fieldApplyInfo) {
        try {
            return fieldMaintenanceService.deleteFieldApplyInfoById(fieldApplyInfo.getId(), fieldApplyInfo.getTaskId());
        } catch (Exception e) {
            return error();
        }
    }

    @ApiOperation(value = "提交任务")
    @PreventRepeatSubmit
    @SysLog(title = "现场维修任务", businessType = OperaType.INSERT, logType = 1, module = "仓储管理->删除任务")
    @PostMapping("/submitTask")
    public AjaxResult submitTask(@RequestBody FieldApplyInfo fieldApplyInfo) {
        try {
            return fieldMaintenanceService.submitTask(fieldApplyInfo.getTaskId());
        } catch (Exception e) {
            return error();
        }
    }

    @ApiOperation(value = "提交任务")
    @PreventRepeatSubmit
    @SysLog(title = "现场维修任务", businessType = OperaType.INSERT, logType = 1, module = "仓储管理->删除任务")
    @PostMapping("/deleteFieldApplyDetailsById")
    public AjaxResult deleteFieldApplyDetailsById(@RequestBody FieldApplyDetails fieldApplyDetails) {
        try {
            return fieldMaintenanceService.deleteFieldApplyDetailsById(fieldApplyDetails.getParentId(), fieldApplyDetails.getTypeId());
        } catch (Exception e) {
            return error();
        }
    }

    @ApiOperation(value = "提交任务")
    @PreventRepeatSubmit
    @SysLog(title = "现场维修任务", businessType = OperaType.INSERT, logType = 1, module = "仓储管理->删除任务")
    @PostMapping("/insertDetails")
    public AjaxResult insertDetails(@RequestBody List<FieldApplyDetails> fieldApplyDetailsList) {
        // 示例逻辑：遍历每条记录
        for (FieldApplyDetails detail : fieldApplyDetailsList) {
            // TODO: 插入数据库，例如调用 service.insert(detail)
            System.out.println("处理 detail: " + detail);
            fieldMaintenanceService.insertDetails(detail);
        }

        return AjaxResult.success("插入成功，共 " + fieldApplyDetailsList.size() + " 条");
    }


}
