package com.xunwang.education.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.xunwang.education.entity.MySuggestion;
import com.xunwang.servicebase.utils.JwtUtils;
import com.xunwang.servicebase.utils.R;
import com.xunwang.servicebase.utils.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();
    }

    /**
     * TODO
     *
     * @Description
     * @Author Paul  Email:8203193@qq.com
     * @Version 1.0
     * @Date 2023/6/19 10:00
     */
    @ApiOperation("查询所有 未处理的 投诉")
    @GetMapping("/findAllNotStartComplaintList")
    public R findAllNotStartComplaintList(){
        LambdaQueryWrapper<Complaint> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Complaint::getStatus, "未处理");

        List<Complaint> list = complaintService.list(wrapper);
        return R.ok().data("list", list);
    }

    /**
     * TODO
     *
     * @Description  updateComplaint
     * @Author Paul  Email:8203193@qq.com
     * @Version 1.0
     * @Date 2023/6/19 10:05
     */
    @ApiOperation("更新 未处理 和 处理中 的投诉")
    @GetMapping("updateComplaintInfo")
    public R updateComplaintInfo(HttpServletRequest request) {
        String complaintid = request.getParameter("complaintid");
        String status = request.getParameter("status");
        String text = request.getParameter("text");
        String procuid = request.getParameter("procuid");

        LambdaQueryWrapper<Complaint> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Complaint::getId, complaintid);
        Complaint one = complaintService.getOne(wrapper);

        one.setStatus(status);
        // one.setResult(text);
        one.setProcessorUid(procuid);
        if(status.equals("已处理")){
            LocalDateTime localDateTime = LocalDateTime.now();
            localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            one.setClosetime(String.valueOf(localDateTime));
            one.setResult(text);
        }else{
            LocalDateTime localDateTime = LocalDateTime.now();
            localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            one.setResult( localDateTime + text);
        }

        boolean uid1 = complaintService.update(one, new QueryWrapper<Complaint>().eq("id", complaintid));
        if (uid1) {
            return R.ok().message("状态设为：【" + status + "】,成功");
        } else {
            return R.ok().message("状态设为：【" + status + "】,失败");
        }
    }


    /**
     * TODO
     *
     * @Description
     * @Author Paul  Email:8203193@qq.com
     * @Version 1.0
     * @Date 2023/6/19 10:09
     */
    @ApiOperation("查询所有 处理中 的投诉")
    @GetMapping("/findAllNotEndComplaintList")
    public R findAllNotEndComplaintList(){
        LambdaQueryWrapper<Complaint> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Complaint::getStatus, "处理中");

        List<Complaint> list = complaintService.list(wrapper);
        return R.ok().data("list", list);
    }

}
