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


import cn.hutool.core.collection.CollUtil;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.EnumsPrefixConstants;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.poi.ExcelUtil;
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.ext.BdFeedbackExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.system.base.service.IBdFeedbackReplyService;
import com.hzs.system.base.service.IBdFeedbackService;
import com.hzs.system.base.vo.BdFeedbackReplyVO;
import com.hzs.system.base.vo.FeedbackVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 意见反馈 前端控制器
 * </p>
 *
 * @author hzs
 * @since 2023-02-05
 */
@RestController
@RequestMapping("/manage/feedback")
public class BdFeedbackController extends BaseController {

    @Autowired
    IBdFeedbackService bdFeedbackService;

    @Autowired
    IBdFeedbackReplyService bdFeedbackReplyService;


    @Autowired
    ITransactionCommonService iTransactionCommonService;

    /**
     * 查询列表
     *
     * @return
     */
    @Log(module = EOperationModule.MEMBER_FEEDBACK, business = EOperationBusiness.MEMBER_FEEDBACK, method = EOperationMethod.SELECT)
    @PostMapping("list")
    public TableDataInfo getFeedbackList(@RequestBody FeedbackVO feedbackVO) {
        feedbackVO.setPkCountry(SecurityUtils.getPkCountry());
        startPage();
        Object[] objects = bdFeedbackService.selectFeedbackList(feedbackVO);

        TableDataInfo tableDataInfo = getDataTable((List<BdFeedbackExt>) objects[0]);
        tableDataInfo.setRows((List<FeedbackVO>) objects[1]);
        return tableDataInfo;

    }


    /**
     * 意见反馈列表导出
     *
     * @param response   响应
     * @param feedbackVO 查询入参
     */
    @Log(module = EOperationModule.MEMBER_FEEDBACK, business = EOperationBusiness.MEMBER_FEEDBACK, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FeedbackVO feedbackVO) {
        feedbackVO.setPkCountry(SecurityUtils.getPkCountry());
        List<FeedbackVO> feedbackVOList = (List<FeedbackVO>) bdFeedbackService.selectFeedbackList(feedbackVO)[1];
        if (CollUtil.isNotEmpty(feedbackVOList)) {
            //枚举翻译
            Map<String, String> transactionMap = iTransactionCommonService.exportEnumTransaction(EFeedbackType.values(), EFeedbackStatus.values());
            for (FeedbackVO feedback : feedbackVOList) {
                feedback.setTypeVal(transactionMap.get(EnumsPrefixConstants.FEEDBACK_TYPE + feedback.getType()));
                feedback.setStatusVal(transactionMap.get(EnumsPrefixConstants.FEEDBACK_STATUS + feedback.getStatus()));
            }
        }
        ExcelUtil<FeedbackVO> util = new ExcelUtil<>(FeedbackVO.class);
        util.exportExcel(response, feedbackVOList, "意见反馈");
    }

    /**
     * 删除公告信息
     *
     * @param pkId
     * @return
     */
    @Log(module = EOperationModule.MEMBER_FEEDBACK, business = EOperationBusiness.MEMBER_FEEDBACK, method = EOperationMethod.DELETE)
    @GetMapping("/delete")
    public AjaxResult delete(Long pkId) {
        FeedbackVO feedbackVO = new FeedbackVO();
        feedbackVO.setPkId(pkId);
        feedbackVO.setPkModified(SecurityUtils.getUserId());
        return toAjax(bdFeedbackService.deleteById(feedbackVO));
    }

    /**
     * 查询修改状态
     *
     * @return
     */
    @Log(module = EOperationModule.MEMBER_FEEDBACK, business = EOperationBusiness.MEMBER_FEEDBACK, method = EOperationMethod.UPDATE)
    @GetMapping("/show")
    public AjaxResult showFeedbackVOById(Long pkId) {
        return AjaxResult.success(bdFeedbackService.showFeedbackVOById(pkId));
    }


    /**
     * 添加意见反馈
     *
     * @return
     */
    @Log(module = EOperationModule.MEMBER_FEEDBACK, business = EOperationBusiness.MEMBER_FEEDBACK, method = EOperationMethod.INSERT, remark = "意见反馈")
    @PostMapping("save")
    public AjaxResult save(@RequestBody BdFeedbackReplyVO feedbackReplyVO) {

        if (null == feedbackReplyVO.getReplyContent()) {
            return AjaxResult.error("回复内容不能为空！");
        }
        feedbackReplyVO.setPkCreator(SecurityUtils.getUserId());
        feedbackReplyVO.setPkCountry(SecurityUtils.getPkCountry());
        return toAjax(bdFeedbackReplyService.saveFeedbackReplyInfo(feedbackReplyVO));
    }

}
