package com.willalp.web.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.willalp.clockingin.domain.BizLeave;
import com.willalp.clockingin.service.IBizLeaveService;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.core.page.TableDataInfo;
import com.willalp.common.utils.StringUtils;
import com.willalp.flow.service.IHsFlowTaskService;
import com.willalp.msg.domain.HsAppUserMsg;
import com.willalp.msg.service.IHsAppUserMsgService;
import com.willalp.system.service.IHsPostConfigService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api("app接口")
@RestController
@RequestMapping("/app")
public class AppTwoController extends BaseController {

    @Resource
    private IHsPostConfigService hsPostConfigService;

    @Resource
    private IHsAppUserMsgService hsAppUserMsgService;

    @Resource
    private IHsFlowTaskService taskService;

    @Resource
    private IBizLeaveService bizLeaveService;

    /**
     * 查询岗位配置列表
     */
    @GetMapping("/pL")
    public AjaxResult list() {
        return AjaxResult.success(hsPostConfigService.getTreePost());
    }

    /**
     * 获取消息列表
     *
     * @param request
     * @return
     */
    @RequestMapping("/gum")
    public TableDataInfo getUserMsg(HttpServletRequest request) {
        startPage();
        return getDataTable(
                hsAppUserMsgService.getBaseMapper().selectList(new QueryWrapper<HsAppUserMsg>()
                        .eq("user_code", request.getHeader("UserName"))
                        .last("order by create_time desc")));
    }

    /**
     * 更新消息已读
     *
     * @param uc
     * @param request
     * @return
     */
    @RequestMapping("/rm")
    public AjaxResult readMsg(String id, String uc, HttpServletRequest request) {
        hsAppUserMsgService.readMsgById(id);
        hsAppUserMsgService.allReadMsgByUserCode(uc);
        return AjaxResult.success();
    }

    /**
     * 更新审批任务已读
     *
     * @param taskId
     * @return
     */
    @RequestMapping("/rt")
    public AjaxResult readTask(String taskId, HttpServletRequest request) {
        taskService.readTask(taskId);
        hsAppUserMsgService.readMsgByTaskId(taskId, request.getHeader("UserName"));
        return AjaxResult.success();
    }

    /**
     * 更新我的发起已读
     *
     * @param id
     * @return
     */
    @RequestMapping("/rl")
    public AjaxResult readTask(String id) {
        if (StringUtils.isNotBlank(id)) {
            BizLeave byId = bizLeaveService.getById(id);
            byId.setRemark("");
            bizLeaveService.updateById(byId);
        }
        return AjaxResult.success();
    }

    /**
     * 查询消息总信息
     *
     * @param request
     * @return
     */
    @RequestMapping("/msgs")
    public AjaxResult msgSum(HttpServletRequest request) {
        return AjaxResult.success(hsAppUserMsgService.msgSum(request.getHeader("UserName")));
    }

}
