package cn.com.ty.lift.enterprise.oa.controller;

import cn.com.ty.lift.common.model.CountPage;
import cn.com.ty.lift.common.model.PushMessage;
import cn.com.ty.lift.common.model.PushUserInfo;
import cn.com.ty.lift.common.verify.Val;
import cn.com.ty.lift.common.verify.Validation;
import cn.com.ty.lift.enterprise.oa.dto.AnnouncementRequest;
import cn.com.ty.lift.enterprise.oa.dto.AnnouncementResponse;
import cn.com.ty.lift.enterprise.oa.entity.Announcement;
import cn.com.ty.lift.enterprise.oa.entity.AnnouncementImg;
import cn.com.ty.lift.enterprise.oa.service.AnnouncementImgService;
import cn.com.ty.lift.enterprise.oa.service.AnnouncementService;
import cn.com.ty.lift.enterprise.push.PushUserService;
import cn.com.xwy.boot.web.dto.RestResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jms.core.JmsMessagingTemplate;
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 javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * <p>
 *     公司公告 前端控制器
 * </p>
 *
 * @author wcz
 * @since 2019-12-04
 */

@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/oa/announcement")
public class AnnouncementController {

    private AnnouncementService    announcementService;
    private AnnouncementImgService announcementImgService;
    private JmsMessagingTemplate   jmsMessagingTemplate;
    private PushUserService        pushUserService;

    /**
     * 根据id查找公告
     * @param request AnnouncementRequest
     * @return RestResponse
     */
    @PostMapping("findOne")
    @Validation(fields = {"id"})
    public RestResponse findOne(@Val @RequestBody AnnouncementRequest request) {
        AnnouncementResponse result = announcementService.infoById(request);
        if(Objects.nonNull(result)){
            result.setImgs(announcementImgService.listByAnnouncement(request.getId()));
        }
        return RestResponse.success(result);
    }

    /**
     * 根据条件分页查找公告
     * @param request AnnouncementRequest
     * @return RestResponse
     */
    @PostMapping("list")
    @Validation(fields = "mtCompanyId")
    public RestResponse list(@Val @RequestBody AnnouncementRequest request) {
        //公告管理： 按发布时间由近及远排序
        request.addDesc("post_date");
        IPage<AnnouncementResponse> pages = announcementService.pageByCondition(request);
        List<AnnouncementResponse> records = pages.getRecords();
        long count = announcementService.countByMtCompany(request.getMtCompanyId());
        if(records.size() > 0){
            List<Long> idList = records.stream().map(AnnouncementResponse::getId).collect(Collectors.toList());
            List<AnnouncementImg> imgList = announcementImgService.listByAnnouncementIds(idList);
            Map<Long, List<AnnouncementImg>> listMap = imgList.stream().collect(Collectors.groupingBy(AnnouncementImg::getAnnouncementId));
            for (AnnouncementResponse record : records) {
                record.setImgs(listMap.get(record.getId()));
            }
        }
        return RestResponse.success(CountPage.getCountPage(count, pages));
    }

    /**
     * 添加公告
     * @param entity Announcement
     * @return RestResponse
     */
    @PostMapping("add")
    public RestResponse add(@Valid @RequestBody Announcement entity) {
        entity.setPostDate(LocalDateTime.now());
        RestResponse result = announcementService.add(entity);
        if(Objects.equals("1", result.getStatusCode())){
            long pageNum = 1;
            long pages;
            do {
                IPage<PushUserInfo> iPage = pushUserService.pageByMtCompany(new Page<>(pageNum, 20), entity.getMtCompanyId());
                pages = iPage.getPages();
                List<PushUserInfo> pushUserInfos = iPage.getRecords();
                if(pushUserInfos.size() > 0){
                    PushMessage.announcementNotice(entity.getContent()).sendTokenOnPlatform(jmsMessagingTemplate, pushUserInfos);
                }
                pageNum++;
            } while (pageNum <= pages);
        }
        return result;
    }

    /**
     * 根据id删除公告
     * @param request AnnouncementRequest
     * @return RestResponse
     */
    @PostMapping("delete")
    @Validation(fields = {"id", "mtCompanyId"})
    public RestResponse delete(@Val @RequestBody AnnouncementRequest request) {
        return announcementService.removeByIdAndMtCompany(request.getId(), request.getMtCompanyId());
    }
}
