package com.repair.controller;


import com.repair.api.RepairSystemMessagesControllerAPI;
import com.repair.common.utils.AesUtils;
import com.repair.common.utils.paramUtils;
import com.repair.model.enumModel.eIsSuper;
import com.repair.model.pojo.RepairAdmin;
import com.repair.model.pojo.RepairSystemMessages;
import com.repair.model.pojo.RepairUser;
import com.repair.model.result.CommonResult;
import com.repair.model.result.PageUtils;
import com.repair.model.vo.SystemMessagePageVo;
import com.repair.model.vo.UnreadMessageCountVo;
import com.repair.services.RepairAdminService;
import com.repair.services.RepairSystemMessagesService;
import com.repair.services.RepairUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2023-07-20
 */
@RestController
public class RepairSystemMessagesController implements RepairSystemMessagesControllerAPI {

    @Autowired
    private RepairUserService repairUserService;
    @Autowired
    private RepairAdminService repairAdminService;
    @Autowired
    private RepairSystemMessagesService repairSystemMessagesService;

    /**
     * 新增消息中心数据
     *
     * @param rc            消息中心数据
     * @param bindingResult 是否为空判断
     * @return
     */
    @Override
    public CommonResult insertRepairSystemMessages(RepairSystemMessages rc, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        int result = repairSystemMessagesService.insertRepairSystemMessages(rc);

        return result > 0 ? CommonResult.ok("添加成功") : CommonResult.fail("添加失败");
    }

    /**
     * 更新消息中心数据
     *
     * @param rc            消息中心数据
     * @param bindingResult 是否为空判断
     * @return
     */
    @Override
    public CommonResult updateRepairSystemMessagesById(RepairSystemMessages rc, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        int result = repairSystemMessagesService.updateRepairSystemMessages(rc);

        return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
    }

    /**
     * 消息中心分页数据查询
     *
     * @param currentPage 当前页数
     * @param pageCount   一页数据条数
     * @param name        查询名称
     * @return
     */
    @Override
    public CommonResult queryPageRepairSystemMessagess(int currentPage, int pageCount, String name) {
        PageUtils<RepairSystemMessages> result = repairSystemMessagesService.queryPageRepairSystemMessagess(currentPage, pageCount, name);

        return CommonResult.ok(result);
    }

    /**
     * 根据数据ID删除消息中心数据
     *
     * @param id 数据ID
     * @return
     */
    @Override
    public CommonResult deleteRepairSystemMessagesById(int id) {
        RepairSystemMessages data = repairSystemMessagesService.getRepairById(id);
        if (data == null) {
            return CommonResult.fail("当前数据不存在，删除失败！");
        }

        int result = repairSystemMessagesService.deleteRepairSystemMessagesById(id);

        return result > 0 ? CommonResult.ok("删除成功") : CommonResult.fail("删除失败");
    }

    /**
     * 消息中心分页数据查询
     *
     * @param currentPage 当前页数
     * @param pageCount   一页数据条数
     * @param userId      用户ID
     * @return
     */
    @Override
    public CommonResult querySystemMessagePage(int currentPage, int pageCount, int userId) {
        PageUtils<SystemMessagePageVo> result = repairSystemMessagesService.querySystemMessagePage(currentPage, pageCount, userId);
        return CommonResult.ok(result);
    }

    /**
     * 未读数据查询
     *
     * @param userId 用户ID
     * @return
     */
    @Override
    public CommonResult queryMessageUnreadCount(int userId) {
        int result = repairSystemMessagesService.queryMessageUnreadCount(userId);
        UnreadMessageCountVo data = new UnreadMessageCountVo();
        data.setCount(result);
        return CommonResult.ok(data);
    }

    /**
     * 已读操作
     *
     * @param id 数据ID
     * @return
     */
    @Override
    public CommonResult readSystemMessage(int id) {
        RepairSystemMessages rsm = new RepairSystemMessages();
        rsm.setId(id);
        rsm.setIsRead(1);
        int uRsm = repairSystemMessagesService.updateRepairSystemMessages(rsm);
        return CommonResult.ok();
    }

    /**
     * 管理端消息中心分页数据查询
     *
     * @param currentPage 当前页数
     * @param pageCount   一页数据条数
     * @param createTime  创建时间
     * @param statu       状态
     *                    未读：0
     *                    已读：1
     *                    全部：2
     * @param userhead    请求头
     * @return
     */
    @Override
    public CommonResult queryManageMessagePage(int currentPage, int pageCount, String createTime, int statu, String userhead) {
        String userID = AesUtils.decrypt(userhead);
        RepairAdmin operateData = repairAdminService.getRepairById(userID);
        if (operateData == null) {
            return CommonResult.fail("操作人信息不合法，无法获取消息数据！");
        }

        //发送人
        Integer userId = 0;//超级管理员用ID为0
        if (!operateData.getIsSuper().equals(eIsSuper.Super.getValue())) {
            RepairUser user = repairUserService.getRepairByCardNumber(operateData.getCardNumber());
            if (user == null) {
                return CommonResult.fail("管理端账号未与移动端账号绑定，无法获取消息数据！");
            }
            userId = user.getId();
        }

        PageUtils<SystemMessagePageVo> result = repairSystemMessagesService.queryPCSystemMessagePage(currentPage, pageCount, userId, createTime, statu);
        return CommonResult.ok(result);
    }

    /**
     * 一键已读操作
     *
     * @param userhead 请求头
     * @return
     */
    @Override
    public CommonResult readAllManageMessage(String userhead) {
        String userID = AesUtils.decrypt(userhead);
        RepairAdmin operateData = repairAdminService.getRepairById(userID);
        if (operateData == null) {
            return CommonResult.fail("操作人信息不合法，无法进行已读操作！");
        }

        //发送人
        Integer userId = 0;//超级管理员用ID为0
        if (!operateData.getIsSuper().equals(eIsSuper.Super.getValue())) {
            RepairUser user = repairUserService.getRepairByCardNumber(operateData.getCardNumber());
            if (user == null) {
                return CommonResult.fail("管理端账号未与移动端账号绑定，无法进行已读操作！");
            }
            userId = user.getId();
        }

        repairSystemMessagesService.readAllManageMessage(userId);

        return CommonResult.ok();
    }

    /**
     * 管理端未读数据查询
     *
     * @param userhead 请求头
     * @return
     */
    @Override
    public CommonResult queryPCMessageUnreadCount(String userhead) {
        String userID = AesUtils.decrypt(userhead);
        RepairAdmin operateData = repairAdminService.getRepairById(userID);
        if (operateData == null) {
            return CommonResult.fail("操作人信息不合法，无法进行已读操作！");
        }

        //发送人
        Integer userId = 0;//超级管理员用ID为0
        if (!operateData.getIsSuper().equals(eIsSuper.Super.getValue())) {
            RepairUser user = repairUserService.getRepairByCardNumber(operateData.getCardNumber());
            if (user == null) {
                return CommonResult.fail("管理端账号未与移动端账号绑定，无法进行已读操作！");
            }
            userId = user.getId();
        }
        int result = repairSystemMessagesService.queryMessageUnreadCount(userId);
        UnreadMessageCountVo data = new UnreadMessageCountVo();
        data.setCount(result);
        return CommonResult.ok(data);
    }
}

