package com.softwarebr.nunu.service.ser.impl;

import com.brframework.commondb.core.AbstractEntityService;
import com.brframework.commondb.core.CommonRepository;
import com.brframework.commonmq.annotation.MQConfig;
import com.brframework.commonmq.core.MQClient;
import com.brframework.commonweb.exception.HandleException;
import com.softwarebr.nunu.dao.ser.PlatformDao;
import com.softwarebr.nunu.dto.ser.PlatformParamDTO;
import com.softwarebr.nunu.entity.ser.Platform;
import com.softwarebr.nunu.entity.user.Member;
import com.softwarebr.nunu.entity.user.Message;
import com.softwarebr.nunu.globals.MQTopics;
import com.softwarebr.nunu.globals.SystemConst;
import com.softwarebr.nunu.service.ser.PlatformService;
import com.softwarebr.nunu.service.user.MemberService;
import com.softwarebr.nunu.service.user.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

import static com.brframework.commondb.core.ControllerPageHandle.convertTo;

/**
 * @author lilin
 * @date 2020-02-24 13:22:47
 */

@Slf4j
@Service
@MQConfig
public class PlatformServiceImpl extends AbstractEntityService<Platform, Long, Object> implements PlatformService {

    @Autowired
    private PlatformDao platformDao;

    @Autowired
    private MemberService memberService;

    @Autowired
    private MQClient mqClient;

    @Autowired
    private MessageService messageService;


    @Override
    public CommonRepository<Long, Platform> getRepository() {
        return platformDao;
    }

    @Override
    public Platform getPlatformResult(Long memberId) {
        Platform platform = platformDao.findTopByPublisherOrderByCreateDateDesc(memberId);
        return platform;
    }

    @Override
    public List<Platform> getPlatformUpper() {
        List<Platform> platforms = platformDao.findByStatus(SystemConst.STATUS_UPPER_SHELF);
        return platforms;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void platformVerify(Long id, Integer verify, String verifyRemark) {
        Platform platform = accessObject(id);
        platform.setVerify(verify);
        String description;
        if (verify.equals(SystemConst.VERIFY_REJECT)) {
            platform.setVerifyRemark(verifyRemark);
            description = String.format("您的平台入驻 %s 审核不通过，点击查看详情跳转相应审核结果页", platform.getName());
        } else {
            description = String.format("您的平台入驻 %s 审核通过，点击查看详情跳转相应审核结果页", platform.getName());
        }
        save(platform);
        if (platform.getPublisher() > 0) {
            mqClient.publish(MQTopics.MEMBER_SEND_MESSAGE, messageService.sendMessageJSON(platform.getPublisher(), Message.MESSAGE_TYPE_VERIFY,
                    "平台审核入驻审核提醒", description, Message.MESSAGE_TARGET_TYPE_ONE_PLATFORM, platform.getId().toString()));
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Platform savePlatform(Long publisher, PlatformParamDTO platformParamDTO) {
        Platform platform = convertTo(platformParamDTO, Platform.class);
        platform.setCreateDate(LocalDateTime.now());
        if (publisher > 0) {
            Member member = memberService.accessObject(platform.getPublisher());
            if (!member.getIdentityType().equals(Member.IDENTITY_TYPE_ENTERPRISE)) {
                throw new HandleException("用户身份非企业，平台入驻操作失败");
            }
            platform.setVerify(SystemConst.VERIFY_AWAIT);
        } else {
            platform.setVerify(SystemConst.VERIFY_ADOPT);
        }
        platform.setModular(platformParamDTO.getModularJson());
        platform.setPublisher(publisher);
        platform.setIndexType(SystemConst.INDEX_TYPE_NO);
        platform.setStatus(SystemConst.STATUS_LOWER_SHELF);
        return platformDao.save(platform);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePlatform(Long platformId, PlatformParamDTO platformParamDTO) {
        Platform platform = accessObject(platformId);
        platform.setCoverUri(platformParamDTO.getCoverUri());
        platform.setLogoUri(platformParamDTO.getLogoUri());
        platform.setName(platformParamDTO.getName());
        platform.setTemplate(platformParamDTO.getTemplate());
        platform.setModular(platformParamDTO.getModularJson());
        platform.setDescription(platformParamDTO.getDescription());
        platformDao.save(platform);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void platformUpperShelf(Long platformId) {
        Platform platform = accessObject(platformId);
        if (platform.getStatus().equals(SystemConst.STATUS_UPPER_SHELF)) {
            throw new HandleException("服务平台已上架，请勿重复操作");
        }
        platform.setStatus(SystemConst.STATUS_UPPER_SHELF);
        platformDao.save(platform);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void platformLowerShelf(Long platformId) {
        Platform platform = accessObject(platformId);
        if (platform.getStatus().equals(SystemConst.STATUS_LOWER_SHELF)) {
            throw new HandleException("服务平台已下架，请勿重复操作");
        }
        platform.setStatus(SystemConst.STATUS_LOWER_SHELF);
        platformDao.save(platform);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void platformIndexYes(Long platformId) {
        Platform platform = accessObject(platformId);
        if (platform.getIndexType().equals(SystemConst.INDEX_TYPE_YES)) {
            throw new HandleException("服务平台已推荐至首页，请勿重复操作");
        }
        platform.setIndexType(SystemConst.INDEX_TYPE_YES);
        platformDao.save(platform);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void platformIndexNo(Long platformId) {
        Platform platform = accessObject(platformId);
        if (platform.getIndexType().equals(SystemConst.INDEX_TYPE_NO)) {
            throw new HandleException("服务平台已取消推荐至首页，请勿重复操作");
        }
        platform.setIndexType(SystemConst.INDEX_TYPE_NO);
        platformDao.save(platform);
    }

}