package com.xunwang.education.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.xunwang.commonutils.JwtUtils;
import com.xunwang.commonutils.R;
import com.xunwang.commonutils.RedisIdWorker;
import com.xunwang.education.entity.Complaint;
import com.xunwang.education.service.ComplaintService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Yl
 * @Date: 2023/1/3 15:37
 */
@RestController
@RequestMapping("/education/my-complaint")
@ApiSupport(author = "YL")
public class ComplaintController {

    @Autowired
    private ComplaintService complaintService;

    @Resource
    private RedisIdWorker redisIdWorker;

    /**
     * 查看个人的所有投诉
     * @param request 获取投诉者的uid
     */
    @ApiOperation("查询个人投诉列表")
    @GetMapping("/findPersonComplaint")
    public R findPersonComplaint(HttpServletRequest request){

        List<Complaint> list = complaintService.findPersonComplaint(request);
        Map<String,Object> map = new HashMap<>();
        map.put("data",list);
        return R.ok().data(map);
    }

    /**
     * 投诉管理数据接口
     * 管理员查看所有的投诉
     */
    @ApiOperation("查询所有投诉")
    @GetMapping("/findAllComplaint")
    public R findAllComplaint(HttpServletRequest request){

       HashMap<String,Object> map  = complaintService.findAllComplaint(request);

       return R.ok().data(map);

    }

    /**
     *
     * @param complaint 请求体需要传递的数据(complaintTitle,complaintDetail)
     * @param request 用于获取投诉者的uid
     * @return 返回值为R.ok() 或者R.error() 不携带data
     */
    @ApiOperation("写投诉")
    @PostMapping("/writeComplaint")
    public R writeComplaint(@RequestBody Complaint complaint,HttpServletRequest request){
        String uid = JwtUtils.getUIdByJwtToken(request);
        LocalDateTime localDateTime = LocalDateTime.now();
        localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        complaint.setFistDatetime(String.valueOf(localDateTime));
        complaint.setStatus("未处理");
        complaint.setFromUid(uid);
        complaint.setId(String.valueOf(redisIdWorker.nextId("complaint")));
        if (complaintService.save(complaint)){
            return R.ok();
        }
        return R.error().message("投诉失败");
    }

    /**
     * Headers 需要携带 token
     * @param  complaint 请求体需要传入的数据(id)
     * @return  返回值为R.ok() 或者R.error() 不携带data
     */
    @ApiOperation("处理中状态，点击处理按钮异步执行接口")
    @PostMapping("/handlingComplaint")
    public R handlingComplaint(@RequestBody Complaint complaint, HttpServletRequest request){
        if (complaintService.handlingComplaint(complaint,request)){
            return R.ok();
        }
        else return R.error();
    }

    /**
     * 投诉处理数据接口
     * @param complaint 请求体需要传入的数据(id,result)
     * @param request 用于获取处理人的uid
     * @return 返回值为R.ok() 或者R.error() 不携带data
     */
    @ApiOperation("投诉处理")
    @PostMapping("/handleComplaint")
    public R handleComplaint(@RequestBody Complaint complaint, HttpServletRequest request){
        if (complaintService.handleComplaint(complaint,request)) {
            return R.ok();
        }
        return R.error();
    }
}
