package com.mortals.xhx.busiz.applets.web;


import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.OrderCol;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.model.Result;
import com.mortals.xhx.busiz.applets.req.FeedbackReq;
import com.mortals.xhx.busiz.applets.req.NewsReq;
import com.mortals.xhx.busiz.applets.req.StaffReq;
import com.mortals.xhx.busiz.applets.rsp.NewsListInfo;
import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.module.feedback.model.FeedbackEntity;
import com.mortals.xhx.module.feedback.model.FeedbackQuery;
import com.mortals.xhx.module.feedback.service.FeedbackService;
import com.mortals.xhx.module.news.model.NewsEntity;
import com.mortals.xhx.module.news.model.NewsQuery;
import com.mortals.xhx.module.staff.model.StaffEntity;
import com.mortals.xhx.module.staff.model.StaffRecordEntity;
import com.mortals.xhx.module.staff.service.StaffRecordService;
import com.mortals.xhx.module.staff.service.StaffService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.mortals.framework.ap.SysConstains.PAGEINFO_KEY;
import static com.mortals.xhx.common.key.ErrorCode.ERROR_TOKEN_EXPIRED;
import static com.mortals.xhx.common.key.ErrorCode.ERROR_TOKEN_EXPIRED_CONTENT;


/**
 * @author ZYW
 * @date 2023-10-09 9:39
 */
@RestController
@Slf4j
@RequestMapping("/api/v1/feedback")
public class FeedbackApiController extends AbstractBaseController<FeedbackReq>{
    @Autowired
    private FeedbackService feedbackService;


    /**
     * 新闻列表
     */
    @PostMapping(value = "feedbackList")
    public Rest<Object> feedbackList(FeedbackReq feedbackReq) {
        String busiDesc = "反馈列表";
        Rest<Object> rest = Rest.ok();
        Map<String, Object> model = new HashMap<>();
        Context context = this.getContext();
        if (ObjectUtils.isEmpty(context) || ObjectUtils.isEmpty(context.getUser())) {
            return Rest.fail(ERROR_TOKEN_EXPIRED, ERROR_TOKEN_EXPIRED_CONTENT);
        }
        try {
            PageInfo pageInfo = buildPageInfo(feedbackReq);
            FeedbackQuery query = new FeedbackQuery();

            if(!ObjectUtils.isEmpty(feedbackReq.getId())){
                query.setId(feedbackReq.getId());
            }

            query.setOrderColList(Arrays.asList(new OrderCol("createTime", OrderCol.DESCENDING)));
            Result<FeedbackEntity> result = feedbackService.find(query, pageInfo, context);
            List<FeedbackEntity> collect = result.getList().stream().map(item -> {
                FeedbackEntity feedbackEntity = new FeedbackEntity();
                BeanUtils.copyProperties(item, feedbackEntity, BeanUtil.getNullPropertyNames(item));
                return feedbackEntity;
            }).collect(Collectors.toList());
            model.put(KEY_RESULT_DATA, collect);
            model.put(PAGEINFO_KEY, result.getPageInfo());
            super.parsePageInfo(model, result.getPageInfo());
            rest.setData(model);
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            log.error(busiDesc, e);
            rest = Rest.fail(super.convertException(e));
        }
        return rest;
    }
}
