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

import com.alibaba.fastjson.JSON;
import com.brframework.common.utils.ConvertObjectUtil;
import com.brframework.commondb.core.AbstractEntityService;
import com.brframework.commondb.core.CommonRepository;
import com.brframework.commondb.core.ControllerPageHandle;
import com.brframework.commonmq.annotation.MQConfig;
import com.brframework.commonmq.core.MQClient;
import com.brframework.commonweb.exception.HandleException;
import com.softwarebr.nunu.dao.res.InviteDao;
import com.softwarebr.nunu.domain.SendMessage;
import com.softwarebr.nunu.dto.res.InviteParamDTO;
import com.softwarebr.nunu.dto.res.PropertyParamDTO;
import com.softwarebr.nunu.entity.res.Invite;
import com.softwarebr.nunu.entity.res.Project;
import com.softwarebr.nunu.entity.res.Property;
import com.softwarebr.nunu.entity.res.PropertyApply;
import com.softwarebr.nunu.entity.user.Message;
import com.softwarebr.nunu.globals.MQTopics;
import com.softwarebr.nunu.globals.SystemConst;
import com.softwarebr.nunu.json.api.center.InviteDetailsResult;
import com.softwarebr.nunu.service.res.InviteService;
import com.softwarebr.nunu.service.res.PropertyApplyService;
import com.softwarebr.nunu.service.user.MessageService;
import com.softwarebr.nunu.util.AmountConversionUtil;
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 static com.brframework.commondb.core.ControllerPageHandle.convertTo;

/**
 * 招聘资源
 *
 * @author lilin
 * @date 2020-02-21 11:48:30
 */

@Slf4j
@Service
@MQConfig
public class InviteServiceImpl extends AbstractEntityService<Invite, Long, Object> implements InviteService {

    @Autowired
    private InviteDao inviteDao;

    @Autowired
    private PropertyApplyService propertyApplyService;

    @Autowired
    private MQClient mqClient;

    @Autowired
    private MessageService messageService;

    @Override
    public CommonRepository<Long, Invite> getRepository() {
        return inviteDao;
    }

    @Override
    public InviteDetailsResult getInviteDetails(Long id, Long memberId) {
        Invite invite = accessObject(id);
        InviteDetailsResult detailsResult = ControllerPageHandle.convertTo(invite, InviteDetailsResult.class);
        if (!detailsResult.getPublicity().equals(SystemConst.PUBLICITY_PUBLIC) && !detailsResult.getPublisher().equals(memberId)) {
            PropertyApply propertyApply = propertyApplyService.getPropertyApplyByMember(PropertyApply.RESOURCE_TYPE_INVITE, id, memberId);
            if (null == propertyApply) {
                detailsResult.setContactPhone("");
                detailsResult.setContact("");
                detailsResult.setEmail("");
                detailsResult.setDisplay(false);
            }
        }
        return detailsResult;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void inviteVerify(Long id, Integer verify, String verifyRemark) {
        Invite invite = accessObject(id);
        invite.setVerifyDate(LocalDateTime.now());
        invite.setVerify(verify);
        String description;
        if (verify.equals(SystemConst.VERIFY_REJECT)) {
            invite.setVerifyRemark(verifyRemark);
            description = String.format("您的交易需求 %s 审核不通过，点击查看详情跳转相应审核结果页", invite.getInviteName());
        } else {
            description = String.format("您的交易需求 %s 审核通过，点击查看详情跳转相应审核结果页", invite.getInviteName());
        }
        save(invite);
        if (invite.getPublisher() > 0) {
            mqClient.publish(MQTopics.MEMBER_SEND_MESSAGE, messageService.sendMessageJSON(invite.getPublisher(), Message.MESSAGE_TYPE_VERIFY,
                    "交易需求审核提醒", description, Message.MESSAGE_TARGET_TYPE_ONE_INVITE, invite.getId().toString()));
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Invite saveInvite(Long publisher, InviteParamDTO inviteParamDTO) {
        Invite invite = convertTo(inviteParamDTO, Invite.class);
        invite.setCreateDate(LocalDateTime.now());
        invite.setVerifyDate(LocalDateTime.now());
        if (publisher > 0) {
            invite.setVerify(SystemConst.VERIFY_AWAIT);
        } else {
            invite.setVerify(SystemConst.VERIFY_ADOPT);
        }
        invite.setJob(inviteParamDTO.getJobJson());
        invite.setPublisher(publisher);
        invite.setIndexType(SystemConst.INDEX_TYPE_NO);
        invite.setUpperStatus(SystemConst.STATUS_LOWER_SHELF);
        invite.setMarker(SystemConst.MARKER_STATUS_NO);
        return inviteDao.save(invite);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateInvite(Long inviteId, InviteParamDTO inviteParamDTO) {
        Invite invite = accessObject(inviteId);
        invite = ConvertObjectUtil.convertJavaBean(invite, inviteParamDTO, true);
        invite.setJob(inviteParamDTO.getJobJson());
        inviteDao.save(invite);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeInviteById(Long inviteId) {
        Invite invite = accessObject(inviteId);
        invite.setMarker(SystemConst.MARKER_STATUS_YES);
        inviteDao.save(invite);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void inviteUpperShelf(Long inviteId) {
        Invite invite = accessObject(inviteId);
        if (invite.getUpperStatus().equals(SystemConst.STATUS_UPPER_SHELF)) {
            throw new HandleException("招聘资源已上架，请勿重复操作");
        }
        invite.setUpperStatus(SystemConst.STATUS_UPPER_SHELF);
        inviteDao.save(invite);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void inviteLowerShelf(Long inviteId) {
        Invite invite = accessObject(inviteId);
        if (invite.getUpperStatus().equals(SystemConst.STATUS_LOWER_SHELF)) {
            throw new HandleException("招聘资源已下架，请勿重复操作");
        }
        invite.setUpperStatus(SystemConst.STATUS_LOWER_SHELF);
        inviteDao.save(invite);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void inviteIndexYes(Long inviteId) {
        Invite invite = accessObject(inviteId);
        if (invite.getIndexType().equals(SystemConst.INDEX_TYPE_YES)) {
            throw new HandleException("招聘资源已推荐至首页，请勿重复操作");
        }
        invite.setIndexType(SystemConst.INDEX_TYPE_YES);
        inviteDao.save(invite);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void inviteIndexNo(Long inviteId) {
        Invite invite = accessObject(inviteId);
        if (invite.getIndexType().equals(SystemConst.INDEX_TYPE_NO)) {
            throw new HandleException("招聘资源已取消推荐至首页，请勿重复操作");
        }
        invite.setIndexType(SystemConst.INDEX_TYPE_NO);
        inviteDao.save(invite);
    }
}