package com.m2pool.pool.controller;

import com.github.pagehelper.PageHelper;
import com.m2pool.common.core.text.Convert;
import com.m2pool.common.core.utils.StringUtils;
import com.m2pool.common.core.web.Result.AjaxResult;
import com.m2pool.common.core.web.controller.BaseController;
import com.m2pool.common.core.web.page.TableDataInfo;
import com.m2pool.common.security.annotation.Logical;
import com.m2pool.common.security.annotation.RequiresLogin;
import com.m2pool.common.security.annotation.RequiresRoles;
import com.m2pool.pool.dto.IdsDto;
import com.m2pool.pool.dto.TicketListDto;
import com.m2pool.pool.service.TicketService;
import com.m2pool.pool.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;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

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

    @Autowired
    private TicketService ticketService;

    @PostMapping("/uploadFile")
    @ApiOperation(value = "文件上传")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile[] file){
        try {
            return  ticketService.uploadFiles(file);
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.error("服务器异常，请联系管理员");
        }

    }

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

        ticketService.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  ticketService.submitTicket(vo);

    }

    @PostMapping("/resubmitTicket")
    @ApiOperation(value = "工单内容补充工单")
    @RequiresLogin
    @RequiresRoles(value = {"admin", "registered"}, logical = Logical.OR)
    public AjaxResult resubmitTicket(@RequestBody ResubmitTicketVo vo){
        return  ticketService.resubmitTicket(vo);
    }

    @PostMapping({"/getPrivateTicket","privateConsume"})
    @ApiOperation(value = "个人工单列表")
    @RequiresLogin
    @RequiresRoles(value = {"admin", "registered"}, logical = Logical.OR)
    public AjaxResult getPrivateTicket(@RequestBody(required = false) PrivateTicketListVo vo){
        if(StringUtils.isNull(vo)){
            vo = new PrivateTicketListVo();
            vo.setStatus(0);
        }
        return  ticketService.getPrivateTicketByStatus(vo);
    }

    @PostMapping({"/privateTicketDetails","getTicketDetails"})
    @ApiOperation(value = "个人工单详情")
    @RequiresLogin
    public AjaxResult getPrivateTicketDetails(@RequestBody() IdVo vo){
        return  ticketService.getPrivateTicketDetails(vo.getId());
    }

    @PostMapping("/endTicket")
    @ApiOperation(value = "用户关闭工单")
    @RequiresLogin
    @RequiresRoles(value = {"admin", "registered"}, logical = Logical.OR)
    public AjaxResult endTicket(@RequestBody(required = false) EndTicketVo vo){

        return  ticketService.endTicket(vo);
    }


/*********************               admin相关接口                      *************************/
    //@PostMapping("/getTicketList")
    //@ApiOperation(value = "按状态搜索后台工单列表")
    //@RequiresLogin
    //@RequiresRoles(value = {"admin", "accounting","support","maintainer"}, logical = Logical.OR)
    //public TableDataInfo getTicketList(@RequestBody StatusVo com.m2pool.chat.vo){
    //    PageHelper.startPage(com.m2pool.chat.vo.getPage(),com.m2pool.chat.vo.getLimit());
    //    List<TicketListDto> list = ticketService.getTicketList(com.m2pool.chat.vo.getStatus());
    //    return  getDataTable(list);
    //}

    @PostMapping("/bk/getAllTicket")
    @ApiOperation(value = "条件搜索后台所有工单列表")
    @RequiresLogin
    @RequiresRoles("back_admin")
    public TableDataInfo getAllTicket(@RequestBody AllTicketListVo vo){
        System.out.println("com.m2pool.chat.vo"+vo);
        if(StringUtils.isNotBlank(vo.getCond())){
            System.out.println("不为空");
            Pattern pattern = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
            if(pattern.matcher(vo.getCond()).matches()){
                //验证是否是邮箱
                System.out.println("邮箱"+vo.getCond());
                vo.setEmail(vo.getCond());
            }else {
                //判断是否是id
                Long id = Convert.toLong(vo.getCond(), 0L);
                System.out.println(id);
                if(id > 0){
                    System.out.println("工单id："+vo.getCond());
                    vo.setId(id);
                }else {
                    return getDataTable(new ArrayList<>());
                }
            }
        }

        PageHelper.startPage(vo.getPage(),vo.getLimit());
        return getDataTable(ticketService.getAllTicket(vo)) ;
    }

    @PostMapping("/bk/endTicket")
    @ApiOperation(value = "关闭用户工单")
    @RequiresLogin
    @RequiresRoles(value = {"back_admin"}, logical = Logical.OR)
    public AjaxResult endUserTicket(@RequestBody(required = false) IdVo vo){

        return  ticketService.closeTicket(vo);
    }


    @PostMapping("/bk/list")
    @ApiOperation(value = "根据状态获取type为线上(type=0)的工单")
    @RequiresLogin
    @RequiresRoles(value = {"back_admin"}, logical = Logical.OR)
    public TableDataInfo onlineTicketList(@RequestBody StatusVo vo){

        PageHelper.startPage(vo.getPage(),vo.getLimit());
        List<TicketListDto> list = ticketService.getSupportTicketList(vo);
        return  getDataTable(list);
    }

    @PostMapping("/bk/details")
    @ApiOperation(value = "工单详情")
    @RequiresLogin
    @RequiresRoles(value = {"back_admin"}, logical = Logical.OR)
    public AjaxResult onlineTicketDetails(@RequestBody IdVo vo){
        if(StringUtils.isNull(vo)){
            return AjaxResult.error("工单id缺失");
        }
        return  ticketService.getTicketDetails(vo.getId());
    }

    @PostMapping("/bk/respon")
    @ApiOperation(value = "回复工单")
    @RequiresLogin
    @RequiresRoles(value = {"back_admin"}, logical = Logical.OR)
    public AjaxResult onlineResponTicket(@RequestBody ResponTicketVo vo){
        return  ticketService.onlineResponTicket(vo);
    }






}
