package cn.pluss.platform.controller.notice;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.dict.DictService;
import cn.pluss.platform.entity.Dict;
import cn.pluss.platform.entity.Notice;
import cn.pluss.platform.entity.NoticeTemplate;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.notice.NoticeService;
import cn.pluss.platform.notice.template.NoticeTemplateService;
import cn.pluss.platform.userApp.UserAppService;
import com.alibaba.fastjson.JSONObject;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping("notice")
public class NoticeController extends BaseModelController<NoticeService, Notice> {

    private Integer PAGE_SIZE = 12;

    @Setter(onMethod_ = {@Autowired})
    private HttpServletRequest request;

    @Setter(onMethod_ = {@Autowired})
    private NoticeService noticeService;

    @Setter(onMethod_ = {@Autowired})
    private UserAppService userAppService;

    @Setter(onMethod_ = {@Autowired})
    private DictService dictService;

    @Setter(onMethod_ = {@Autowired})
    private NoticeTemplateService noticeTemplateService;

    /**
     * 引导页
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        List<Dict> typeFirstList = dictService.getByCode(Collections.singletonList("NOTICE_TYPE_FIRST"));
        List<Dict> typeSecondList0 = dictService.getByCode(Collections.singletonList("NOTICE_TYPE_SECOND_0"));
        List<Dict> typeSecondList1 = dictService.getByCode(Collections.singletonList("NOTICE_TYPE_SECOND_1"));

        request.setAttribute("typeFirstList", typeFirstList);
        request.setAttribute("typeSecondList0", typeSecondList0);
        request.setAttribute("typeSecondList1", typeSecondList1);

        return "notice/noticeList";
    }

    /**
     * 查询详情
     */
    @GetMapping({"/detail/{id}", "/detail"})
    public String queryNoticeDetails(HttpServletRequest request,
                                     @PathVariable(value = "id", required = false) Integer id,
                                     Integer pageIndex,
                                     Long userId) {
        List<Dict> templateTypeList = dictService.getByCode(Arrays.asList("notice_template_type"));
        if (id != null && id != 0) {
            Notice notice = new Notice();
            notice.setId(id);
            notice = noticeService.queryNotice(notice);

            if ("-1".equals(notice.getUserId())) {
                request.setAttribute("parentName", "无");
            } else {
                UserApp userApp = new UserApp();
                userApp.setUserId(Long.parseLong(notice.getUserId()));
                userApp = userAppService.queryUserApp(userApp);
                if (userApp.getParentId() != null) {
                    UserApp parentUserApp = new UserApp().setUserId(userApp.getParentId());
                    parentUserApp = userAppService.queryUserApp(parentUserApp);
                    request.setAttribute("parentName", parentUserApp.getUserName());
                } else {
                    request.setAttribute("parentName", "无");
                }
            }
            request.setAttribute("notice", notice);
            request.setAttribute("pageIndex", pageIndex);
        } else {
            if (userId != null) {
                UserApp userApp = new UserApp();
                userApp.setUserId(userId);
                userApp = userAppService.queryUserApp(userApp);
                final Map<String, Object> userData = userAppService.getUserByUid(userId.toString());
                request.setAttribute("userData", userData);

                Notice notice = new Notice();
                notice.setUserId(userId.toString());
                notice.setUserName(userApp.getUserName());
                request.setAttribute("notice", notice);

                if (userApp.getParentId() != null) {
                    UserApp parentUserApp = new UserApp().setUserId(userApp.getParentId());
                    parentUserApp = userAppService.queryUserApp(parentUserApp);
                    request.setAttribute("parentName", parentUserApp.getUserName());
                } else {
                    request.setAttribute("parentName", "无");
                }
            }
        }
        request.setAttribute("templateTypeList", templateTypeList);
        List<NoticeTemplate> templateList = noticeTemplateService.list();

        String templateStr = JSONObject.toJSONString(templateList);
        request.setAttribute("templateList", templateStr);
        return "notice/noticeDetails";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryNoticeBaseInfo", method = RequestMethod.GET)
    public String queryNoticeBaseInfo(HttpServletRequest request, Integer id, Integer pageIndex) {

        if (id != null && id != 0) {
            Notice notice = new Notice();
            notice.setId(id);
            notice = noticeService.queryNotice(notice);
            request.setAttribute("notice", notice);
            request.setAttribute("pageIndex", pageIndex);
        }
        return "notice/noticeBaseInfo";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryNoticeExtendsInfo", method = RequestMethod.GET)
    public String queryNoticeExtendsInfo() {

        return "notice/noticeExtendsInfo";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryNoticeFunctionsInfo", method = RequestMethod.GET)
    public String queryNoticeFunctionsInfo() {

        return "notice/noticeFunctionsInfo";
    }

    /**
     * 保存对象
     */
    @RequestMapping(value = "/saveNotice", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveNotice(Notice notice) {

        if (notice.getId() != null && notice.getId() != 0) {
            noticeService.updateNotice(notice);
        } else {
            noticeService.saveNotice(notice);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

    /**
     * 修改对象
     */
    @RequestMapping(value = "/updateNotice", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateNotice(Integer id) {

        Notice notice = new Notice();
        notice.setId(id);
        noticeService.updateNotice(notice);
        Map<String, Object> result = new HashMap<>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

    /**
     * 删除对象
     */
    @RequestMapping(value = "/deleteNotice", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteNotice(Integer id) {
        Notice notice = new Notice();
        notice.setId(id);
        noticeService.deleteNotice(notice);
        Map<String, Object> result = new HashMap<>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

    @RequestMapping(value = "/sendTempNotice", method = RequestMethod.POST)
    @ResponseBody
    public Result<?> saveTempNotice(@RequestBody Notice notice) {
        noticeService.sendTempNotice(notice, notice.getScheduleFlag());
        return ResultGenerator.genSuccessResult("操作成功", null);
    }
}


