package com.yida.data.school.letter.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vdurmont.emoji.EmojiManager;
import com.vdurmont.emoji.EmojiParser;
import com.yida.data.common.core.annotation.ControllerLog;
import com.yida.data.common.core.common.ModuleName;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.letter.EduLetterBox;
import com.yida.data.common.core.entity.letter.EduLetterBoxConfig;
import com.yida.data.common.core.entity.letter.EduLetterBoxReply;
import com.yida.data.common.core.entity.system.enums.OperationLogTypeEnum;
import com.yida.data.common.core.utils.Asserts;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.log.annotation.OperationLog;
import com.yida.data.school.letter.service.EduLetterBoxReplyService;
import com.yida.data.school.letter.service.EduLetterBoxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;


@Api(tags = "校长信箱-前台")
@RestController
@RequestMapping(value = "/letter/H5/letter")
public class H5EduLetterBoxController {

    @Autowired
    private EduLetterBoxService eduLetterBoxService;

    @Autowired
    private EduLetterBoxReplyService eduLetterBoxReplyService;

    /**
     * @api {GET} /a/H5/letter/addLetter 新增信箱提问
     * @apiGroup order
     * @apiVersion 0.0.1
     * @apiDescription 点单签到
     * @apiParam {String} appId 手机用户ID:必传,作为判断签到状态
     * @apiParam {Integer} coreOrderId 参加的点单id:必传,作为判断
     * @apiParam {String} header 用户头像
     * @apiParam {String} nikeName 用户昵称
     * @apiParamExample {json} 请求样例： { coreOrderId : 1 appId : demoData nikeName : demoData header : demoData }
     * @apiSuccess (200) {String} msg 信息
     * @apiSuccess (200) {int} code 200 代表无错误  其他 代表错误
     * @apiSuccess (200) {int} count 总条数
     * @apiSuccess (200) {object} data 数据详情
     * @apiSuccessExample {json} 返回样例: { "code": 200, "msg": "操作成功", "count": 0, "data": null }
     */
    @ApiOperation("新增信箱提问")
    @PostMapping("/addLetter")
    @OperationLog(module = ModuleName.LETTER_BOX, methods = "新增信箱提问", type = OperationLogTypeEnum.INSERT)
    public ResultBean addLetter(@RequestBody EduLetterBox eduLetterBox) {
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        if (ObjectUtil.isEmpty(currentUser)) {
            return ResultBean.buildError("用户未登录");
        }
        if (ObjectUtil.isEmpty(eduLetterBox.getAppPhone())) {
            eduLetterBox.setAppPhone(currentUser.getMobile());
        }
        if (ObjectUtil.isEmpty(eduLetterBox.getSchoolId())) {
            eduLetterBox.setSchoolId(currentUser.getDeptId());
        }

        if (EmojiManager.containsEmoji(eduLetterBox.getTitle())) {
            return ResultBean.buildError("标题不能含有表情符号");
        }
        eduLetterBoxService.addLetter(eduLetterBox, currentUser);
        return ResultBean.buildSuccess();
    }


    /***
     * @Author wjm
     * @Description 分页查询信息列表
     * @Date 2020/6/7 18:24
     */
    @ApiOperation("分页信箱列表")
    @GetMapping("/findLetterPageList")
    public ResultBean<IPage<EduLetterBox>> findLetterPageList(@RequestParam(defaultValue = "1") Integer pageNo,
        @RequestParam(defaultValue = "10") Integer pageSize,
        @ApiParam("学校id") @RequestParam Long schoolId,
        @ApiParam("标题") @RequestParam(required = false) String title,
        @ApiParam("浏览次数传0按照创建时间排序，传1按照浏览数量排序") @RequestParam(required = false) Integer viewsNum,
        @ApiParam("identityId") @RequestParam(required = false) Long identityId) {
        Asserts.isNotNull(schoolId, "学校id为空");
        IPage<EduLetterBox> list = eduLetterBoxService.findViewList(new Page(pageNo, pageSize), title, schoolId,
            identityId, viewsNum);
        return ResultBean.buildSuccess(list);
    }


    /***
     * @Author wjm
     * @Description 当前提问详情
     * @Date 2020/6/7 18:24
     */
    @ApiOperation("查询信箱详情")
    @GetMapping("/getLetter")
    public ResultBean getLetter(@ApiParam("信箱表id") @RequestParam Integer id) {

        if (ObjectUtil.isEmpty(id)) {
            return ResultBean.buildError("id为空");
        }

        return ResultBean.buildSuccess(eduLetterBoxService.getLetter(id, FebsUtil.getCurrentUser().getUserId()));
    }


    /***
     * @Author wjm
     * @Description 回复提问
     * @Date 2020/6/7 18:24
     * 需传入id(是追问问题传入追问问题id，不是则传入主id),appId,replyContent(回复的内容),questionMark(是否为追问问题：0否，1是)
     */
    @ApiOperation("回复信箱")
    @PostMapping("/replyLetter")
    @OperationLog(module = ModuleName.LETTER_BOX, methods = "回复信箱", type = OperationLogTypeEnum.INSERT)
    public ResultBean replyLetter(@RequestBody EduLetterBox eduLetterBox) {
        if (ObjectUtil.isEmpty(eduLetterBox.getIdentityId()) ||
            ObjectUtil.isEmpty(eduLetterBox.getReplyContent()) ||
            ObjectUtil.isEmpty(eduLetterBox.getId())) {
            return ResultBean.buildError("需传入id,appId,replyContent");
        }
        // 需要处理是处理初次提问的问题还是追问的问题
        if (StringUtils.equals("1", eduLetterBox.getQuestionMark())) {
            EduLetterBoxReply eduLetterBoxReply = new EduLetterBoxReply();
            eduLetterBoxReply.setId(eduLetterBox.getId());
            eduLetterBoxReply.setContent(eduLetterBox.getReplyContent());
            eduLetterBoxReply.setReply("1");
            eduLetterBoxReply.setUpdateId(eduLetterBox.getIdentityId().toString());
            eduLetterBoxReplyService.updateById(eduLetterBoxReply);
        } else {
            eduLetterBox.setReply(1);
            eduLetterBox.setUpdateId(eduLetterBox.getIdentityId());
            eduLetterBoxService.updateById(eduLetterBox);
        }
        return ResultBean.buildSuccess();
    }

    @ApiOperation("信箱点赞")
    @GetMapping("/upOrDownLetter")
    @OperationLog(module = ModuleName.LETTER_BOX, methods = "信箱点赞", type = OperationLogTypeEnum.INSERT)
    public ResultBean upOrDownLetter(@ApiParam("信箱表id") @RequestParam Integer letterId,
        @ApiParam("0为有用，1为没用") @RequestParam Integer status,
        @ApiParam("0为点赞，1为取消") @RequestParam Integer type) {
        eduLetterBoxService.upOrDownLetter(letterId, status, FebsUtil.getCurrentUser().getUserId(), type);
        return ResultBean.buildSuccess();
    }

}

