package com.jxy.marketall.controller;

import com.jxy.common.core.utils.StringUtils;
import com.jxy.common.core.web.Result.AjaxResult;
import com.jxy.common.security.annotation.RequiresLogin;
import com.jxy.marketall.dto.IdsDto;
import com.jxy.marketall.service.UserHelpService;
import com.jxy.marketall.vo.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;

/**
 * @Description 统一请求接口
 * @Date 2022/5/27 9:54
 * @Author 杜懿
 */
@RestController
@RequestMapping("/ticket")
public class UserHelpController {

    @Autowired
    private UserHelpService userHelpService;

    @PostMapping("/getQuestionType")
    @ApiOperation(value = "获取问题分类")
    @RequiresLogin
    public AjaxResult getQuestionType(){

        return  userHelpService.getTypeList();

    }

    @PostMapping("/uploadFile")
    @ApiOperation(value = "文件上传")
    @RequiresLogin
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile[] file){

        try {
            return  userHelpService.uploadFiles(file);
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.error("服务器异常，请联系管理员");
        }

    }

    @GetMapping("/downloadFile")
    @ApiOperation(value = "文件批量下载")
    //@RequiresLogin
    public void  download(IdsDto dto, HttpServletRequest request, HttpServletResponse response){

            userHelpService.downloadByIds(dto.getIds(),request,response);

    }



    @PostMapping("/submitTicket")
    @ApiOperation(value = "提交工单")
    @RequiresLogin
    public AjaxResult submitTicket(@RequestBody(required = false) TicketVo vo){
        if(StringUtils.isNull(vo)){vo = new TicketVo();}
        return  userHelpService.submitTicket(vo);

    }

    @PostMapping("/resubmitTicket")
    @ApiOperation(value = "工单继续提交 已提交内容补充")
    @RequiresLogin
    public AjaxResult resubmitTicket(@RequestBody ResubmitTicketVo vo){
        return  userHelpService.resubmitTicket(vo);
    }

    @PostMapping("/retractTicket")
    @ApiOperation(value = "已提交工单撤回")
    @RequiresLogin
    public AjaxResult retractTicket(@RequestBody IdVo vo){
        return  userHelpService.retractTicket(vo.getId());
    }

    @PostMapping("/readTicket")
    @ApiOperation(value = "工单状态设为已读")
    @RequiresLogin
    public AjaxResult readTicket(@RequestBody IdVo vo){
        return  userHelpService.readTicket(vo.getId());
    }

    @PostMapping("/endTicket")
    @ApiOperation(value = "工单状态设为一解决")
    @RequiresLogin
    public AjaxResult endTicket(@RequestBody IdVo vo){
        return  userHelpService.endTicket(vo.getId());
    }

    @PostMapping("/submitAuditTicket")
    @ApiOperation(value = "提交工单审核")
    @RequiresLogin
    public AjaxResult submitAuditTicket(@RequestBody  IdVo vo){
        return  userHelpService.submitAuditTicket(vo.getId());
    }

    @PostMapping("/getPrivateTicket")
    @ApiOperation(value = "个人工单列表")
    @RequiresLogin
    public AjaxResult getPrivateTicket(@RequestBody(required = false) StatusVo vo){
        if(StringUtils.isNull(vo)){
            vo = new StatusVo();
            vo.setStatus(0);
        }
        return  userHelpService.getPrivateTicketByStatus(vo.getStatus());
    }

    @PostMapping("/getTicketDetails")
    @ApiOperation(value = "工单详情")
    @RequiresLogin
    public AjaxResult getTicketDetails(@RequestBody IdVo vo){
        return  userHelpService.getTicketDetails(vo.getId());
    }

    @PostMapping("/getUnreadCount")
    @ApiOperation(value = "个人未读工单数")
    @RequiresLogin
    public AjaxResult getUnreadCount(){
        return  userHelpService.getUnreadCount();
    }

    @PostMapping("/getTicketList")
    @ApiOperation(value = "后台工单列表")
    @RequiresLogin
    public AjaxResult getTicket(@RequestBody StatusVo vo){
        return  userHelpService.getTicketList(vo.getStatus());
    }

    @PostMapping("/getDailyCount")
    @ApiOperation(value = "每日工单数")
    @RequiresLogin
    public AjaxResult getDailyCount(@RequestBody(required = false) QueryVo vo){
        if(StringUtils.isNull(vo)){vo = new QueryVo();}
        return  userHelpService.getDailyCount(vo);
    }

    @PostMapping("/responTicket")
    @ApiOperation(value = "回复工单")
    @RequiresLogin
    public AjaxResult responTicket(@RequestBody ResponTicketVo vo){
        return  userHelpService.responTicket(vo);
    }

    @PostMapping("/auditTicket")
    @ApiOperation(value = "审核工单")
    @RequiresLogin
    public AjaxResult auditTicket(@RequestBody(required = false) AuditTicketVo vo){
        if(StringUtils.isNull(vo)){vo = new AuditTicketVo();}
        return  userHelpService.auditTicket(vo);
    }

}
