package com.ygtx.gxt.controller;

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

import com.ygtx.common.utils.StringUtils;
import com.ygtx.gxt.domain.*;
import com.ygtx.gxt.service.IGxtOrderScoreService;
import com.ygtx.gxt.service.IGxtRepairOrderService;
import com.ygtx.gxt.service.IGxtWorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
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.ygtx.common.annotation.Log;
import com.ygtx.common.core.controller.BaseController;
import com.ygtx.common.core.domain.AjaxResult;
import com.ygtx.common.enums.BusinessType;
import com.ygtx.common.utils.poi.ExcelUtil;
import com.ygtx.common.core.page.TableDataInfo;
import com.ygtx.common.core.page.PageDomain;
import com.ygtx.common.core.page.TableSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

/**
 * 工单评分Controller
 * 
 * @author lingming
 * @date 2025-11-12
 */
@RestController
@RequestMapping("/gxt/orderScore")
@Api(tags = "工单评分管理")
public class GxtOrderScoreController extends BaseController
{
    @Autowired
    private IGxtRepairOrderService gxtRepairOrderService;

    @Autowired
    private IGxtWorkOrderService gxtWorkOrderService;
    
    @Autowired
    private IGxtOrderScoreService gxtOrderScoreService;

    /**
     * 查询工单评分列表（合并维修工单和维保工单）
     */
    @PreAuthorize("@ss.hasPermi('gxt:orderScore:list')")
    @ApiOperation("查询工单评分列表（合并维修工单和维保工单）")
    @GetMapping("/list")
    public TableDataInfo list(GxtRepairOrder repairOrder, GxtWorkOrder workOrder)
    {
        // 开启分页插件自动分页
        startPage();
        // 查询合并的工单列表
        List<OrderScoreInfo> list = gxtOrderScoreService.selectUnionOrderList(repairOrder, workOrder, null);
        return getDataTable(list);
    }
    
    /**
     * 查询移动端工单评分列表（合并维修工单和维保工单）
     */
    @ApiOperation("查询移动端工单评分列表（合并维修工单和维保工单）")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "keyword", value = "关键词", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class),
        @ApiImplicitParam(name = "scoringStatus", value = "评分状态", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class)
    })
    @GetMapping("/mobile/list")
    public TableDataInfo mobileList(GxtRepairOrder repairOrder, GxtWorkOrder workOrder, String keyword , String scoringStatus)
    {
        // 开启分页插件自动分页
        startPage();
        // 查询合并的工单列表
        /*if(StringUtils.isNotEmpty(scoringStatus)){
            repairOrder.setScoringStatus(scoringStatus);
            workOrder.setScoringStatus(scoringStatus);
        }*/
        List<OrderScoreInfo> list = gxtOrderScoreService.selectUnionOrderList(repairOrder, workOrder, keyword);
        return getDataTable(list);
    }

    /**
     * 获取工单评分统计信息
     */
    @ApiOperation("获取工单评分统计信息")
    @ApiImplicitParam(name = "month", value = "月份", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class)
    @GetMapping("/statistics")
    public AjaxResult getStatistics(GxtRepairOrder repairOrder, GxtWorkOrder workOrder, String month)
    {
        Map<String, Object> statistics = gxtOrderScoreService.getOrderScoreStatistics(repairOrder, workOrder, month);
        return AjaxResult.success(statistics);
    }

    /**
     * 获取工单评分详细信息
     */
    @ApiOperation("获取工单评分详细信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "orderType", value = "工单类型", required = true, dataType = "Integer", paramType = "path", dataTypeClass = Integer.class),
        @ApiImplicitParam(name = "orderId", value = "工单ID", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    })
    @GetMapping(value = "/{orderType}/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderType") Integer orderType, @PathVariable("orderId") Long orderId)
    {
        if (orderType == 1) {
            // 维修工单
            GxtRepairOrder order = gxtRepairOrderService.selectGxtRepairOrderById(orderId);
            return success(order);
        } else if (orderType == 2) {
            // 维保工单
            GxtWorkOrder order = gxtWorkOrderService.selectGxtWorkOrderById(orderId);
            return success(order);
        }
        return AjaxResult.error("无效的工单类型");
    }

    /**
     * 保存自评信息
     */
    @PreAuthorize("@ss.hasPermi('gxt:orderScore:selfEvaluation')")
    @Log(title = "工单评分", businessType = BusinessType.UPDATE)
    @ApiOperation("保存自评信息")
    @PutMapping("/selfEvaluation")
    public AjaxResult selfEvaluation(@RequestBody OrderScoreInfo orderScoreInfo)
    {
        try {
            int result = gxtOrderScoreService.selfEvaluation(orderScoreInfo);
            if (result > 0) {
                return toAjax(result);
            }
            return AjaxResult.error("无效的工单类型");
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 保存复评信息
     */
    @PreAuthorize("@ss.hasPermi('gxt:orderScore:review')")
    @Log(title = "工单评分", businessType = BusinessType.UPDATE)
    @ApiOperation("保存复评信息")
    @PutMapping("/review")
    public AjaxResult review(@RequestBody OrderScoreInfo orderScoreInfo)
    {
        try {
            int result = gxtOrderScoreService.review(orderScoreInfo);
            if (result > 0) {
                return toAjax(result);
            }
            return AjaxResult.error("无效的工单类型");
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 保存终评信息
     */
    @PreAuthorize("@ss.hasPermi('gxt:orderScore:finalEvaluation')")
    @Log(title = "工单评分", businessType = BusinessType.UPDATE)
    @ApiOperation("保存终评信息")
    @PutMapping("/finalEvaluation")
    public AjaxResult finalEvaluation(@RequestBody OrderScoreInfo orderScoreInfo)
    {
        try {
            int result = gxtOrderScoreService.finalEvaluation(orderScoreInfo);
            if (result > 0) {
                return toAjax(result);
            }
            return AjaxResult.error("无效的工单类型");
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 保存确认信息
     */
    @PreAuthorize("@ss.hasPermi('gxt:orderScore:confirm')")
    @Log(title = "工单评分", businessType = BusinessType.UPDATE)
    @ApiOperation("保存确认信息")
    @PutMapping("/confirm")
    public AjaxResult confirm(@RequestBody OrderScoreInfo orderScoreInfo)
    {
        try {
            int result = gxtOrderScoreService.confirm(orderScoreInfo);
            if (result > 0) {
                return toAjax(result);
            }
            return AjaxResult.error("无效的工单类型");
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
    
    /**
     * 发起申诉
     */
    @PreAuthorize("@ss.hasPermi('gxt:orderScore:appeal')")
    @Log(title = "工单评分", businessType = BusinessType.UPDATE)
    @ApiOperation("发起申诉")
    @PutMapping("/appeal")
    public AjaxResult appeal(@RequestBody OrderScoreInfo orderScoreInfo)
    {
        try {
            int result = gxtOrderScoreService.appeal(orderScoreInfo);
            if (result > 0) {
                return toAjax(result);
            }
            return AjaxResult.error("无效的工单类型");
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}