package com.crm.web.controller;

import com.alibaba.fastjson.JSON;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.FeignClientAnnotation;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.system.email.SysEmailSendEntity;
import com.crm.rely.backend.core.entity.web.notice.record.WebNoticeRecordAndEmailMqEntity;
import com.crm.rely.backend.model.dto.web.notice.record.WebNoticeRecordDto;
import com.crm.rely.backend.model.entity.custom.info.CustomInfoEntity;
import com.crm.rely.backend.model.entity.web.notice.record.WebNoticeRecordAddEntity;
import com.crm.rely.backend.model.entity.web.notice.record.WebNoticeRecordReadEntity;
import com.crm.rely.backend.model.entity.web.notice.record.WebNoticeRecordSearchPageEntity;
import com.crm.web.service.WebNoticeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.validation.annotation.Validated;
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.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 群发通知
 *
 * @author houn
 */
@RestController
@RequestMapping("/web/notice")
public class WebNoticeController {

    @Autowired
    private WebNoticeRecordService webNoticeRecordService;

    @PostMapping("/custom/search/list/record")
    @FeignClientAnnotation
    public ResultWithPagerDto<WebNoticeRecordDto> customPageListRecord(@RequestBody @Validated WebNoticeRecordSearchPageEntity entity, CustomInfoEntity infoEntity) throws Exception {
        entity.setCustomId(infoEntity.getId());
        ResultWithPagerDto<WebNoticeRecordDto> result = webNoticeRecordService.searchPageList(entity);

        return result;
    }

    @PostMapping("/custom/search/single")
    @FeignClientAnnotation
    public BaseResultDto<WebNoticeRecordDto> singleRecord(@RequestBody @Validated SingleLongEntity entity, CustomInfoEntity infoEntity) throws Exception {

        WebNoticeRecordReadEntity webNoticeRecordReadEntity = new WebNoticeRecordReadEntity();
        List<Long> list = new ArrayList<>();
        list.add(entity.getId());
        webNoticeRecordReadEntity.setIds(list);
        webNoticeRecordReadEntity.setReadTime(new Date());
        webNoticeRecordReadEntity.setRead(1);
        webNoticeRecordReadEntity.setCustomId(infoEntity.getId());
        webNoticeRecordService.read(webNoticeRecordReadEntity);

        WebNoticeRecordDto webNoticeRecordDto = webNoticeRecordService.searchSingle(entity);

        return BaseResultDto.success(webNoticeRecordDto);
    }

    @PostMapping("/custom/search/read")
    @FeignClientAnnotation
    public BaseResultDto countRead(@RequestBody @Validated WebNoticeRecordSearchPageEntity entity,
                                   CustomInfoEntity infoEntity) throws Exception {
        entity.setCustomId(infoEntity.getId());

        Integer countRead = webNoticeRecordService.countRead(entity);

        return BaseResultDto.success(countRead);
    }

    @PostMapping("/custom/record/read")
    @FeignClientAnnotation
    public BaseResultDto read(@RequestBody WebNoticeRecordReadEntity entity, CustomInfoEntity infoEntity) throws Exception {
        entity.setReadTime(new Date());
        entity.setRead(1);
        entity.setCustomId(infoEntity.getId());
        webNoticeRecordService.read(entity);

        return BaseResultDto.success();
    }

    @JmsListener(destination = Constants.WEB_NOTICE_PUT_RECORD)
    public void webNoticePutRecode(String content) throws Exception {
        WebNoticeRecordAndEmailMqEntity mqEntity = JSON.parseObject(content, WebNoticeRecordAndEmailMqEntity.class);
        if (mqEntity == null) {
            return;
        }
        addRecord(mqEntity.getEmailSendEntity(), mqEntity.getCId(), mqEntity.getCustomId());
    }

    private BaseResultDto addRecord(@RequestBody @Validated SysEmailSendEntity emailSendEntity,
                                    Long cId, Long customId) throws Exception {
        if (cId == null) {
            return BaseResultDto.error(Constants.PARAMETER_ERROR);
        }
        WebNoticeRecordAddEntity entity = setWebNoticeRecordAddEntity(emailSendEntity, cId, customId);

        webNoticeRecordService.add(entity);

        return BaseResultDto.success();
    }

    @SuppressWarnings("Duplicates")
    private WebNoticeRecordAddEntity setWebNoticeRecordAddEntity(SysEmailSendEntity emailSendEntity, Long cId,
                                                                 Long customId) {
        WebNoticeRecordAddEntity entity = new WebNoticeRecordAddEntity();
        entity.setMap(emailSendEntity.getMap());
        entity.setContent(emailSendEntity.getContent());
        entity.setSubject(emailSendEntity.getSubject());
        entity.setTemplateName(emailSendEntity.getTemplateName());
        entity.setCId(cId);
        entity.setCustomId(customId);
        entity.setRead(0);
        return entity;
    }
}
