package com.mortals.xhx.module.news.web;

import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.base.system.user.model.UserQuery;
import com.mortals.xhx.base.system.user.service.UserService;
import com.mortals.xhx.busiz.applets.req.NewsReq;
import com.mortals.xhx.module.dept.model.DeptEntity;
import com.mortals.xhx.module.dept.model.DeptQuery;
import com.mortals.xhx.module.dept.service.DeptService;
import com.mortals.xhx.module.news.model.NewsCategoryEntity;
import com.mortals.xhx.module.news.model.NewsCategoryQuery;
import com.mortals.xhx.module.news.service.NewsCategoryService;
import org.apache.commons.lang3.ObjectUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.mortals.framework.model.Context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.module.news.model.NewsEntity;
import com.mortals.xhx.module.news.service.NewsService;
import org.apache.commons.lang3.ArrayUtils;
import com.mortals.framework.util.StringUtils;

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

import com.alibaba.fastjson.JSONObject;

import java.util.Arrays;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import static com.mortals.framework.ap.SysConstains.*;

/**
 * 新闻
 *
 * @author zxfei
 * @date 2023-09-18
 */
@RestController
@RequestMapping("news")
public class NewsController extends BaseCRUDJsonBodyMappingController<NewsService, NewsEntity, Long> {

    @Autowired
    private ParamService paramService;
    @Autowired
    private NewsCategoryService newsCategoryService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private UserService userService;


    public NewsController() {
        super.setModuleDesc("新闻");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "top", paramService.getParamBySecondOrganize("News", "top"));
        this.addDict(model, "viewNums", paramService.getParamBySecondOrganize("News", "viewNums"));
        this.addDict(model, "categoryId", newsCategoryService.find(new NewsCategoryQuery()).stream().collect(Collectors.toMap(x -> x.getId().toString(), y -> y.getName(), (o, n) -> n)));
        this.addDict(model, "deptId", deptService.find(new DeptQuery()).stream().collect(Collectors.toMap(x -> x.getId().toString(), y -> y.getDeptName(), (o, n) -> n)));

        this.addDict(model, "createUserId", userService.find(new UserQuery()).stream().collect(Collectors.toMap(x -> x.getId().toString(), y -> y.getRealName(), (o, n) -> n)));

        super.init(model, context);
    }


    @Override
    protected void saveBefore(NewsEntity entity, Map<String, Object> model, Context context) throws AppException {

        if (!ObjectUtils.isEmpty(entity.getCategoryId())) {
            NewsCategoryEntity newsCategoryEntity = newsCategoryService.get(entity.getCategoryId(), context);
            entity.setCategoryName(newsCategoryEntity.getName());
        }

        if (!ObjectUtils.isEmpty(entity.getDeptId())) {
            DeptEntity deptEntity = deptService.get(entity.getDeptId(), context);
            entity.setDeptName(deptEntity.getDeptName());
        }

    }

    /**
     * 新闻浏览
     */
    @PostMapping(value = "view")
    public Rest<NewsEntity> viewNews(@RequestBody NewsReq newsReq) {
        String busiDesc = "新闻浏览";
        Rest<NewsEntity> rest = Rest.ok(busiDesc + " 【成功】");
        Context context = this.getContext();
        Map<String, Object> model = new HashMap<>();
        try {
            if (org.springframework.util.ObjectUtils.isEmpty(newsReq.getId())) {
                throw new AppException("新闻id不能为空!");
            }
            NewsEntity newsEntity =service.viewNews(newsReq.getId(),context);
            rest.setData(newsEntity);
            this.init(model, context);
            rest.setDict(model.get("dict"));
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            log.error(busiDesc, e);
            rest = Rest.fail(super.convertException(e));
        }
        return rest;
    }

    /**
     * 新闻分享
     */
    @PostMapping(value = "share")
    public Rest<NewsEntity> shareNews(@RequestBody NewsReq newsReq) {
        String busiDesc = "新闻分享";
        Rest<NewsEntity> rest = Rest.ok(busiDesc + " 【成功】");
        Context context = this.getContext();
        Map<String, Object> model = new HashMap<>();
        try {
            if (org.springframework.util.ObjectUtils.isEmpty(newsReq.getId())) {
                throw new AppException("新闻id不能为空!");
            }
            NewsEntity newsEntity = service.shareNews(newsReq.getId(),context);
            rest.setData(newsEntity);
            this.init(model, context);
            rest.setDict(model.get("dict"));
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            log.error(busiDesc, e);
            rest = Rest.fail(super.convertException(e));
        }
        return rest;
    }
}