package com.mortals.xhx.module.notice.web;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.OrderCol;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.code.NoticeStatusEnum;
import com.mortals.xhx.module.notice.model.NoticeEntity;
import com.mortals.xhx.module.notice.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
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.notice.model.NoticeStudyEntity;
import com.mortals.xhx.module.notice.service.NoticeStudyService;
import org.apache.commons.lang3.ArrayUtils;
import com.mortals.framework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import static com.mortals.framework.ap.SysConstains.*;
/**
*
* 公告学习人员
*
* @author zxfei
* @date 2023-02-23
*/
@RestController
@RequestMapping("notice/study")
public class NoticeStudyController extends BaseCRUDJsonBodyMappingController<NoticeStudyService,NoticeStudyEntity,Long> {

    @Autowired
    private ParamService paramService;
    @Autowired
    private NoticeService noticeService;


    public NoticeStudyController(){
        super.setModuleDesc( "公告学习人员");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "noticetType", paramService.getParamBySecondOrganize("NoticeStudy","noticetType"));
        this.addDict(model, "noticeStatus", paramService.getParamBySecondOrganize("NoticeStudy","noticeStatus"));
        this.addDict(model, "top", paramService.getParamBySecondOrganize("NoticeStudy","top"));
        super.init(model, context);
    }


    @Override
    protected void doListBefore(NoticeStudyEntity query, Map<String, Object> model, Context context) throws AppException {
        super.doListBefore(query, model, context);
        query.setUserId(this.service.getContextUserId(context));
        List<OrderCol> orderColList = new ArrayList<>();
        orderColList.add(new OrderCol("top", OrderCol.DESCENDING));
        orderColList.add(new OrderCol("noticeStatus", OrderCol.ASCENDING));
        orderColList.add(new OrderCol("createTime", OrderCol.DESCENDING));
        query.setOrderColList(orderColList);
    }

    @Override
    protected int viewAfter(Long id, Map<String, Object> model, NoticeStudyEntity entity, Context context) throws AppException {
        NoticeEntity noticeEntity = noticeService.get(entity.getNoticeId());
        entity.setNoticeContent(noticeEntity.getNoticeContent());
        entity.setFileName(noticeEntity.getFileName());
        entity.setFilePath(noticeEntity.getFilePath());
        if(ObjectUtils.isEmpty(entity.getViewTime())&& NoticeStatusEnum.未读.getValue()==entity.getNoticeStatus()){
            entity.setViewTime(new Date());
            entity.setNoticeStatus(NoticeStatusEnum.已读.getValue());
            entity.setUpdateUserId(this.getContextUserId(context));
            entity.setUpdateTime(new Date());
            this.service.update(entity,context);
        }
        return super.viewAfter(id, model, entity, context);
    }
}