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.StuNeeds;
import com.xunwang.servicebase.utils.JwtUtils;
import com.xunwang.servicebase.utils.R;
import com.xunwang.servicebase.utils.RedisIdWorker;
import com.xunwang.education.entity.MySuggestion;
import com.xunwang.education.service.MySuggestionService;
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 10:17
 */
@RestController
@RequestMapping("/education/my-suggestion")
@ApiSupport(author = "YL")
public class MySuggestionController {

    @Autowired
    private MySuggestionService mySuggestionService;

    @Resource
    private RedisIdWorker redisIdWorker;

    /**
     * headers 需要携带 token
     * Params 需要传递参数（pageNum,pageSize）
     * 查看个人提出的所有建议
     */
    @ApiOperation("查询个人建议列表")
    @GetMapping("/findPersonAdvise")
    public R findPersonAdvise(HttpServletRequest request){
        List<MySuggestion> list = mySuggestionService.findPersonAdvise(request);
        Map<String,Object> map = new HashMap<>();
        map.put("data",list);
        return R.ok().data(map);
    }

    /**
     * headers需要携带 token
     * Params 需要传递参数（pageNum,pageSize）
     * 建议管理数据接口
     * 管理员查看所有的建议
     */
    @ApiOperation("查询所有建议")
    @GetMapping("/findAllAdvise")
    public R findAllAdvise(HttpServletRequest request){

        HashMap<String,Object> map = mySuggestionService.findAllAdvise(request);
        return R.ok().data(map);
    }

    /**
     * Headers 需要携带 token
     * @param mySuggestion 请求体需要传递的数据(adviseTitle,adviseDetail)
     * @return 返回值为R.ok() 或者R.error() 不携带data
     */
    @ApiOperation("写建议")
    @PostMapping("/writeAdvise")
    public R writeAdvise(@RequestBody MySuggestion mySuggestion,HttpServletRequest request){
        String uid = JwtUtils.getUIdByJwtToken(request);
        LocalDateTime localDateTime = LocalDateTime.now();
        localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        mySuggestion.setFistDatetime(String.valueOf(localDateTime));
        mySuggestion.setId(String.valueOf(redisIdWorker.nextId("advise")));
        mySuggestion.setStatus("未处理");
        mySuggestion.setFromUid(uid);
        if (mySuggestionService.save(mySuggestion)){
            return R.ok();
        }
        return R.error().message("建议失败");
    }

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

    /**
     * Headers 需要携带 token
     * 建议处理数据接口
     * @param mySuggestion 请求体需要传入的数据(id,result)
     * @return 返回值为R.ok() 或者R.error() 不携带data
     */
    @ApiOperation("建议处理,处理完状态为已处理")
    @PostMapping("/handleAdvise")
    public R handleAdvise(@RequestBody MySuggestion mySuggestion, HttpServletRequest request){
        if (mySuggestionService.handleAdvise(mySuggestion,request)) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * TODO
     *
     * @Description
     * @Author Paul  Email:8203193@qq.com
     * @Version 1.0
     * @Date 2023/6/18 15:01
     */
    @ApiOperation("查询所有 未处理的建议")
    @GetMapping("/findAllNotStartAdviseList")
    public R findAllNotStartAdviseList(){
        LambdaQueryWrapper<MySuggestion> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MySuggestion::getStatus, "未处理");

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

    /**
     * TODO
     *
     * @Description
     * @Author Paul  Email:8203193@qq.com
     * @Version 1.0
     * @Date 2023/6/18 15:40
     */
    @ApiOperation("更新 未处理 和 处理中 的建议")
    @GetMapping("updateAdviseInfo")
    public R updateAdviseInfo(HttpServletRequest request) {
        String adviseid = request.getParameter("adviseid");
        String status = request.getParameter("status");
        String text = request.getParameter("text");
        String procuid = request.getParameter("procuid");

        LambdaQueryWrapper<MySuggestion> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MySuggestion::getId, adviseid);
        MySuggestion one = mySuggestionService.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 = mySuggestionService.update(one, new QueryWrapper<MySuggestion>().eq("id", adviseid));
        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/18 16:53
     */
    @ApiOperation("查询所有 处理中 的建议")
    @GetMapping("/findAllNotEndAdviseList")
    public R findAllNotEndAdviseList(){
        LambdaQueryWrapper<MySuggestion> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MySuggestion::getStatus, "处理中");

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

}
