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

import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.NoticeMsgConstants;
import com.hzs.common.core.enums.*;
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.BdNotice;
import com.hzs.common.domain.system.ext.BdNoticeExt;
import com.hzs.common.security.service.UserTokenService;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.system.base.service.IBdNoticeService;
import com.hzs.system.base.vo.BdNoticeAdminShowVO;
import com.hzs.system.base.vo.BdNoticeVO;
import com.hzs.system.base.vo.NoticePetitionVO;
import com.hzs.system.sys.dto.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


/**
 * Created with IntelliJ IDEA.
 * Author: yuhui
 * Description:
 */
@RestController
@RequestMapping("/manage/notice")
public class BdNoticeController extends BaseController {
    @Autowired
    private IBdNoticeService noticeService;

    @Autowired
    private UserTokenService userTokenService;

    /**
     * 查询前台公告列表
     *
     * @return
     */
    @Log(module = EOperationModule.NOTICE_DETAIL, business = EOperationBusiness.NOTICE_LIST, method = EOperationMethod.SELECT, remark = "前台公告列表")
    @PostMapping("member-list")
    public TableDataInfo memberList(@RequestBody BdNoticeVO noticeVO) {
        noticeVO.setPkCountry(SecurityUtils.getPkCountry());
        noticeVO.setWebsiteType(ENoticeWebsiteType.MEMBER.getValue());
        startPage();
        Object[] objects = noticeService.getMemberNotice(noticeVO);
        TableDataInfo tableDataInfo = getDataTable((List<BdNotice>) objects[0]);
        tableDataInfo.setRows((List<BdNoticeVO>) objects[1]);
        return tableDataInfo;
    }

    /**
     * 添加公告信息
     *
     * @return
     */
    @Log(module = EOperationModule.NOTICE_DETAIL, business = EOperationBusiness.NOTICE_RELEASE, method = EOperationMethod.INSERT, remark = "前台发布公告")
    @PostMapping("save")
    public AjaxResult save(@RequestBody NoticePetitionVO noticePetitionVO) {
        BdNoticeVO noticeVO = noticePetitionVO.getNotice();
        LoginUser loginUser = userTokenService.getLoginUser();
        noticeVO.setPkMember(SecurityUtils.getUserId());
        noticeVO.setPkCountry(SecurityUtils.getPkCountry());
        noticeVO.setLoginUser(loginUser);
        return toAjax(noticeService.saveNotice(noticePetitionVO));
    }


    /**
     * 后台发布公告
     *
     * @return
     */
    @Log(module = EOperationModule.NOTICE_DETAIL, business = EOperationBusiness.NOTICE_RELEASE, method = EOperationMethod.INSERT, remark = "后台发布公告")
    @PostMapping("admin-save")
    public AjaxResult adminSave(@RequestBody NoticePetitionVO noticePetitionVO) {
        BdNoticeVO noticeVO = noticePetitionVO.getNotice();
        noticeVO.setPkMember(SecurityUtils.getUserId());
        noticeVO.setPkCountry(SecurityUtils.getPkCountry());
        noticeVO.setLoginUser(userTokenService.getLoginUser());
        return toAjax(noticeService.adminSaveNotice(noticePetitionVO));
    }


    /**
     * 查询后台公告列表
     *
     * @return
     */
    @Log(module = EOperationModule.NOTICE_DETAIL, method = EOperationMethod.SELECT, remark = "后台公告列表")
    @PostMapping("admin-list")
    public TableDataInfo adminList(@RequestBody BdNoticeVO noticeVO) {
        noticeVO.setPkCountry(SecurityUtils.getPkCountry());
        startPage();
        Object[] objects = noticeService.getAdminNotice(noticeVO);
        TableDataInfo tableDataInfo = getDataTable((List<BdNotice>) objects[0]);
        tableDataInfo.setRows((List<BdNoticeVO>) objects[1]);
        return tableDataInfo;
    }


    /**
     * 查询后台公告首页右侧显示
     *
     * @return
     */
    @Log(module = EOperationModule.NOTICE_DETAIL, method = EOperationMethod.SELECT, remark = "后台公告首页右侧显示")
    @GetMapping("admin-right-list")
    public TableDataInfo adminIndexRightList() {
        BdNoticeVO noticeVO = BdNoticeVO.builder().pkCountry(SecurityUtils.getPkCountry()).build();
        startPage();
        Object[] objects = noticeService.getAdminIndexNotice(noticeVO);
        TableDataInfo tableDataInfo = getDataTable((List<BdNotice>) objects[0]);
        tableDataInfo.setRows((List<BdNoticeVO>) objects[1]);
        return tableDataInfo;
    }


    /**
     * 查看详情
     *
     * @return
     */
    @Log(module = EOperationModule.NOTICE_DETAIL, method = EOperationMethod.SELECT, remark = "查看详情")
    @GetMapping("/show")
    public AjaxResult showNoticeInfo(Long pkId) {
        return AjaxResult.success(noticeService.showNoticeDetail(pkId));
    }


    /**
     * 发起签呈显示
     *
     * @param noticeVO 公告入参
     * @return AjaxResult
     */
    @Log(module = EOperationModule.NOTICE_DETAIL, business = EOperationBusiness.NOTICE_RELEASE, method = EOperationMethod.SELECT, remark = "发起签呈")
    @PostMapping("/show-notice-petition")
    public AjaxResult showNoticePetition(@RequestBody BdNoticeVO noticeVO) {
        Integer websiteType = noticeVO.getWebsiteType();
        if (!websiteType.equals(ENoticeWebsiteType.ADMIN.getValue()) && StringUtils.isEmpty(noticeVO.getGrade()) && StringUtils.isEmpty(noticeVO.getAwards())) {
            return AjaxResult.error(TransactionUtils.getContent(NoticeMsgConstants.LEAST_ONE_LEVEL_AWARD));
        }
        if (!websiteType.equals(ENoticeWebsiteType.ADMIN.getValue()) && StringUtils.isEmpty(noticeVO.getPublishLocation())) {
            return AjaxResult.error(TransactionUtils.getContent(NoticeMsgConstants.PUBLISH_LOCATION_NOT_EMPTY));
        }
        if (websiteType.equals(ENoticeWebsiteType.ADMIN.getValue()) && StringUtils.isEmpty(noticeVO.getRole())) {
            return AjaxResult.error(TransactionUtils.getContent(NoticeMsgConstants.ROLE_NOT_EMPTY));
        }
        if (StringUtils.isEmpty(noticeVO.getTitle())) {
            return AjaxResult.error(TransactionUtils.getContent(NoticeMsgConstants.TITLE_NOT_EMPTY));
        }
        if (null == noticeVO.getIsPopScreen()) {
            return AjaxResult.error(TransactionUtils.getContent(NoticeMsgConstants.POPUP_WINDOW_NOT_EMPTY));
        }
        return AjaxResult.success(noticeService.showNoticePetition(noticeVO));

    }


    /**
     * 查询后台公告配置列表
     *
     * @return
     */
    @Log(module = EOperationModule.NOTICE_DETAIL, business = EOperationBusiness.NOTICE_LIST, method = EOperationMethod.SELECT, remark = "后台公告首页显示")
    @GetMapping("admin-index-list")
    public TableDataInfo adminIndexList() {
        startPage();
        Object[] objects = noticeService.selectAdminNotice(SecurityUtils.getPkCountry());
        TableDataInfo tableDataInfo = getDataTable((List<BdNoticeExt>) objects[0]);
        tableDataInfo.setRows((List<BdNoticeAdminShowVO>) objects[1]);
        return tableDataInfo;
    }


    /**
     * 查询后台公告配置列表
     *
     * @return
     */
    @Log(module = EOperationModule.NOTICE_DETAIL, business = EOperationBusiness.NOTICE_LIST, method = EOperationMethod.SELECT, remark = "后台公告根据角色弹窗")
    @GetMapping("show-admin")
    public AjaxResult adminIndex(String roles) {
        if (StringUtils.isEmpty(roles)) {
            return AjaxResult.error(TransactionUtils.getContent(NoticeMsgConstants.ROLE_NOT_EMPTY));
        }
        List roleList = Arrays.asList(roles.split(","));
        BdNoticeVO noticeVO = BdNoticeVO.builder()
                .pkCountry(SecurityUtils.getPkCountry())
                .websiteType(ENoticeWebsiteType.ADMIN.getValue())
                .functionType(ENoticeFunctionType.NOTICE.getValue())
                .roles(roleList)
                .build();
        return AjaxResult.success(noticeService.selectAdminNoticeByRole(noticeVO));
    }


    /**
     * 更新公告信息
     *
     * @return
     */
    @Log(module = EOperationModule.NOTICE_DETAIL, business = EOperationBusiness.NOTICE_LIST, method = EOperationMethod.UPDATE)
    @GetMapping("/delete")
    public AjaxResult deleteNoticeInfo(Long pkId) {
        BdNoticeVO bdNoticeVO = noticeService.showNoticeDetail(pkId);
        if (null != bdNoticeVO && bdNoticeVO.getApproveState().equals(EApproveRechargeStatus.WAIT.getValue())) {
            return AjaxResult.success(noticeService.deleteNoticeById(pkId, SecurityUtils.getUserId()));
        } else {
            return AjaxResult.error(TransactionUtils.getContent(NoticeMsgConstants.NOT_DELETE));
        }

    }

    /**
     * 上移
     *
     * @return
     */
    @Log(module = EOperationModule.NOTICE_DETAIL, business = EOperationBusiness.NOTICE_LIST, method = EOperationMethod.UP_MOVE)
    @GetMapping("/up-move")
    public AjaxResult upMove(Long pkId, Long sort) {
        return AjaxResult.success(noticeService.upMove(pkId, sort, SecurityUtils.getUserId(), SecurityUtils.getPkCountry()));
    }

    /**
     * 下移
     *
     * @return
     */
    @Log(module = EOperationModule.NOTICE_DETAIL, business = EOperationBusiness.NOTICE_LIST, method = EOperationMethod.DOWN_MOVE)
    @GetMapping("/down-move")
    public AjaxResult downMove(Long pkId, Long sort) {
        return AjaxResult.success(noticeService.downMove(pkId, sort, SecurityUtils.getUserId(), SecurityUtils.getPkCountry()));
    }


    /**
     * 置顶
     *
     * @return
     */
    @Log(module = EOperationModule.NOTICE_DETAIL, business = EOperationBusiness.NOTICE_LIST, method = EOperationMethod.TOP)
    @GetMapping("/top")
    public AjaxResult top(Long pkId, Long sort) {
        return AjaxResult.success(noticeService.top(pkId, sort, SecurityUtils.getUserId()));
    }


    /**
     * 取消置顶
     *
     * @return
     */
    @Log(module = EOperationModule.NOTICE_DETAIL, business = EOperationBusiness.NOTICE_LIST, method = EOperationMethod.CANCEL_TOP)
    @GetMapping("/cancel-top")
    public AjaxResult cancelTop(Long pkId, Long sort) {
        return AjaxResult.success(noticeService.cancelTop(pkId, sort, SecurityUtils.getUserId()));
    }


    /**
     * 删除公告提交签呈
     *
     * @param noticePetitionVO 参数
     * @return AjaxResult
     */
    @Log(module = EOperationModule.NOTICE_DETAIL, business = EOperationBusiness.NOTICE_LIST, method = EOperationMethod.SUBMIT)
    @PostMapping("delete-notice-petition")
    public AjaxResult deleteNoticePetition(@RequestBody NoticePetitionVO noticePetitionVO) {
        LoginUser loginUser = userTokenService.getLoginUser();
        BdNoticeVO notice = noticePetitionVO.getNotice();
        notice.setLoginUser(loginUser);
        notice.setPkMember(SecurityUtils.getUserId());
        notice.setPkCountry(SecurityUtils.getPkCountry());
        return AjaxResult.success(noticeService.deleteNoticePetition(noticePetitionVO));
    }

}
