package com.hzs.system.base.controller.manage;


import cn.hutool.core.bean.BeanUtil;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.system.base.BdPlatformMessage;
import com.hzs.common.domain.system.base.BdPlatformMessageDetail;
import com.hzs.common.domain.system.ext.BdPlatformMessageDetailExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.base.service.IBdPlatformMessageDetailService;
import com.hzs.system.base.service.IBdPlatformMessageService;
import com.hzs.system.base.vo.BdPlatformMessageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 后台站内信 前端控制器
 * </p>
 *
 * @author hzs
 * @since 2023-04-04
 */
@RestController
@RequestMapping("/manage/platform/message")
public class BdPlatformMessageController extends BaseController {

    @Autowired
    private IBdPlatformMessageService bdPlatformMessageService;

    @Autowired
    private IBdPlatformMessageDetailService bdPlatformMessageDetailService;

    /**
     * 添加后站内信信息
     *
     * @return AjaxResult
     */
    @Log(module = EOperationModule.MESSAGE_CONFIG, business = EOperationBusiness.MESSAGE_CONFIG, method = EOperationMethod.INSERT)
    @PostMapping("save")
    public AjaxResult save(@RequestBody BdPlatformMessageVO messageVO) {
        if (!verificationParam(messageVO).equals(AjaxResult.success())) {
            return verificationParam(messageVO);
        }
        messageVO.setPkMember(SecurityUtils.getUserId());
        messageVO.setPkCountry(SecurityUtils.getPkCountry());
        return toAjax(bdPlatformMessageService.savePlateFormMessage(messageVO));
    }

    /**
     * 更新后站内信信息
     *
     * @return AjaxResult
     */
    @Log(module = EOperationModule.MESSAGE_CONFIG, business = EOperationBusiness.MESSAGE_CONFIG, method = EOperationMethod.UPDATE)
    @PostMapping("update")
    public AjaxResult updatePlatformMessage(@RequestBody BdPlatformMessageVO messageVO) {
        if (!verificationParam(messageVO).equals(AjaxResult.success())) {
            return verificationParam(messageVO);
        }
        BdPlatformMessage platformMessage = BeanUtil.copyProperties(messageVO, BdPlatformMessage.class);
        platformMessage.setPkModified(SecurityUtils.getUserId());
        platformMessage.setModifiedTime(new Date());
        return toAjax(bdPlatformMessageService.updateById(platformMessage));
    }

    /**
     * 删除后站内信信息
     *
     * @return AjaxResult
     */
    @Log(module = EOperationModule.MESSAGE_CONFIG, business = EOperationBusiness.MESSAGE_CONFIG, method = EOperationMethod.DELETE)
    @GetMapping("delete")
    public AjaxResult updatePlatformMessage(Long pkId) {
        if (null == pkId) {
            return AjaxResult.error("请选择要删除的记录！");
        }
        Integer count = bdPlatformMessageDetailService.selectBdPlatformMessageDetailByMessageId(pkId);
        if (count > 0) {
            return AjaxResult.error("该条记录已被绑定，不允许删除！");
        }
        return toAjax(bdPlatformMessageService.deletePlateFormMessageById(pkId));
    }


    /**
     * 删除后站内信信息
     *
     * @return AjaxResult
     */
    @Log(module = EOperationModule.MESSAGE_CONFIG, business = EOperationBusiness.MESSAGE_CONFIG, method = EOperationMethod.SELECT, remark = "查看详情")
    @GetMapping("detail")
    public AjaxResult selectDetailById(Long pkId) {
        if (null == pkId) {
            return AjaxResult.error("请选择要查看的记录！");
        }
        BdPlatformMessage BdPlatformMessage = bdPlatformMessageService.getById(pkId);
        return AjaxResult.success(BeanUtil.copyProperties(BdPlatformMessage, BdPlatformMessageVO.class));
    }


    /**
     * 后台站内信列表
     *
     * @return AjaxResult
     */
    @Log(module = EOperationModule.MESSAGE_CONFIG, business = EOperationBusiness.MESSAGE_CONFIG, method = EOperationMethod.SELECT)
    @GetMapping("show-list")
    public TableDataInfo showPlatformMessage() {
        BdPlatformMessageVO bdPlatformMessageVO = new BdPlatformMessageVO();
        bdPlatformMessageVO.setPkCountry(SecurityUtils.getPkCountry());
        return getDataTable(bdPlatformMessageService.showPlatformMessage(bdPlatformMessageVO));
    }

    /**
     * 显示后台的推送信息
     *
     * @param userId 登录用户
     * @return AjaxResult
     */
    @Log(module = EOperationModule.MESSAGE_CONFIG, business = EOperationBusiness.MESSAGE_CONFIG, method = EOperationMethod.SELECT, remark = "推送信息")
    @GetMapping("show-push-info")
    public TableDataInfo selectAdminPushInfo(Long userId) {
        BdPlatformMessageVO messageVO = BdPlatformMessageVO.builder().pkCountry(SecurityUtils.getPkCountry())
                .pkMember(userId).build();
        startPage();
        Object[] objects = bdPlatformMessageService.selectAdminPushInfo(messageVO);
        TableDataInfo tableDataInfo = getDataTable((List<BdPlatformMessageDetailExt>) objects[0]);
        tableDataInfo.setRows((List<BdPlatformMessageVO>) objects[1]);
        return tableDataInfo;
    }


    /**
     * 更新读取状态
     *
     * @return AjaxResult
     */
    @Log(module = EOperationModule.MESSAGE_CONFIG, business = EOperationBusiness.MESSAGE_CONFIG, method = EOperationMethod.UPDATE, remark = "更新读取状态")
    @GetMapping("update-read-state")
    public AjaxResult updateReadState(Long pkId, Integer readState) {
        BdPlatformMessageDetail messageDetail = new BdPlatformMessageDetail();
        messageDetail.setPkId(pkId);
        messageDetail.setReadState(readState);
        messageDetail.setPkModified(SecurityUtils.getUserId());
        messageDetail.setModifiedTime(new Date());
        return toAjax(bdPlatformMessageDetailService.updateById(messageDetail));
    }

    /**
     * 参数校验
     *
     * @param messageVO 站内信信息
     * @return AjaxResult
     */
    private AjaxResult verificationParam(BdPlatformMessageVO messageVO) {
        if (StringUtils.isEmpty(messageVO.getTitle())) {
            return AjaxResult.error("标题不能为空！");
        }
        if (StringUtils.isEmpty(messageVO.getContent())) {
            return AjaxResult.error("提示语不能为空！");
        }
        if (null == messageVO.getBusinessType()) {
            return AjaxResult.error("业务类型不能为空！");
        }
        if (null == messageVO.getApprovalType()) {
            return AjaxResult.error("审批类型不能为空！");
        }
        if (StringUtils.isEmpty(messageVO.getUrl())) {
            return AjaxResult.error("URL不能为空！");
        }
        return AjaxResult.success();
    }


}
