package com.mortals.xhx.busiz.web;


import cn.hutool.core.date.DateUtil;
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.PageInfo;
import com.mortals.framework.model.Result;
import com.mortals.framework.web.BaseJsonBodyController;
import com.mortals.xhx.busiz.req.AppealReq;
import com.mortals.xhx.busiz.req.FeedbackReq;
import com.mortals.xhx.busiz.req.PerformReq;
import com.mortals.xhx.busiz.rsp.AppealStatInfo;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.module.check.service.*;
import com.mortals.xhx.module.feedback.model.FeedbackEntity;
import com.mortals.xhx.module.feedback.model.FeedbackStaffEntity;
import com.mortals.xhx.module.feedback.service.FeedbackService;
import com.mortals.xhx.module.perform.model.PerformAttendAppealEntity;
import com.mortals.xhx.module.perform.model.PerformAttendAppealQuery;
import lombok.extern.slf4j.Slf4j;
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.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

/**
 * h5 申诉
 *
 * @author: zxfei
 * @date: 2023/7/7 15:08
 */
@RestController
@Slf4j
@RequestMapping("/api/v1/feedback")
public class FeedbackApiController extends AbstractBaseController<FeedbackReq> {

    @Autowired
    private FeedbackService feedbackService;

    /**
     * 反馈列表
     */
    @PostMapping(value = "list")
    public Rest<Object> feedbackList(@RequestBody FeedbackReq feedbackReq) {
        String busiDesc = "个人反馈列表";
        log.info("【{}】【请求体】--> {}", busiDesc, JSONObject.toJSONString(feedbackReq));
        Rest<Object> rest = Rest.ok();
        Map<String, Object> model = new HashMap<>();
        Context context = this.getContext();

        if (ObjectUtils.isEmpty(context) || ObjectUtils.isEmpty(context.getUser())) {
            throw new AppException(ERROR_TOKEN_EXPIRED, ERROR_TOKEN_EXPIRED_CONTENT);
        }
        try {
            if (ObjectUtils.isEmpty(feedbackReq.getFeedBackStartDate())) {
                //未设置时间的情况，默认为当月
                feedbackReq.setFeedBackStartDate(DateUtil.beginOfMonth(new Date()).toDateStr());
                feedbackReq.setFeedBackEndDate(DateUtil.today());
            }
            PageInfo pageInfo = buildPageInfo(feedbackReq);
            //todo

            if (YesNoEnum.YES.getValue() == feedbackReq.getFeedbackStatus()) {
                //已反馈
            } else {
                //未反馈
            }


//            model.put(KEY_RESULT_DATA, result.getList());
//            model.put(PAGEINFO_KEY, result.getPageInfo());
//            parsePageInfo(model, result.getPageInfo());

            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            log.error(busiDesc, e);
            rest = Rest.fail(super.convertException(e));
        }
        return rest;
    }

    /**
     * 反馈问卷新增
     */
    @PostMapping(value = "save")
    public Rest<FeedbackEntity> feedbackSave(@RequestBody FeedbackEntity feedbackEntity) {
        Context context = this.getContext();
        String busiDesc = "反馈回答";
        Rest<FeedbackEntity> rest = Rest.ok(busiDesc + " 【成功】");

        if (ObjectUtils.isEmpty(context) || ObjectUtils.isEmpty(context.getUser())) {
            throw new AppException(ERROR_TOKEN_EXPIRED, ERROR_TOKEN_EXPIRED_CONTENT);
        }
        try {
            //todo

            FeedbackEntity saveEntity = feedbackService.save(feedbackEntity, context);
            rest.setData(saveEntity);
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            log.error(busiDesc, e);
            rest = Rest.fail(super.convertException(e));
        }
        return rest;
    }


    protected PageInfo buildPageInfo(FeedbackReq query) {
        PageInfo pageInfo = new PageInfo();
        if (!ObjectUtils.isEmpty(query) && !ObjectUtils.isEmpty(query.getPage())) {
            pageInfo.setCurrPage(query.getPage());
        }

        if (!ObjectUtils.isEmpty(query) && !ObjectUtils.isEmpty(query.getSize())) {
            pageInfo.setPrePageResult(query.getSize());
        }

        return pageInfo;
    }


    protected void parsePageInfo(Map<String, Object> model, PageInfo pageInfo) {
        model.put(TOTAL, pageInfo.getTotalResult());
        model.put(PER_PAGE, pageInfo.getPrePageResult());
        model.put(CURRENT_PAGE, pageInfo.getCurrPage());
        model.put(LAST_PAGE, pageInfo.getTotalPage());
        model.put(PAGEINFO_KEY, pageInfo);
    }


    public static void main(String[] args) {

    }

}
