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

import com.brframework.common.utils.ConvertObjectUtil;
import com.brframework.commondb.core.AbstractEntityService;
import com.brframework.commondb.core.CommonRepository;
import com.brframework.commonmq.annotation.MQConfig;
import com.brframework.commonweb.exception.HandleException;
import com.softwarebr.nunu.dao.res.TalentDao;
import com.softwarebr.nunu.dto.res.TalentParamDTO;
import com.softwarebr.nunu.entity.res.Talent;
import com.softwarebr.nunu.globals.SystemConst;
import com.softwarebr.nunu.service.res.TalentService;
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 TalentServiceImpl extends AbstractEntityService<Talent, Long, Object> implements TalentService {

    @Autowired
    private TalentDao talentDao;

    @Override
    public CommonRepository<Long, Talent> getRepository() {
        return talentDao;
    }

    /**
     * 保存简历
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Talent saveTalent(Long publisher, TalentParamDTO talentParamDTO) {

        Talent talent = convertTo(talentParamDTO, Talent.class);
        talent.setCreateDate(LocalDateTime.now());
        talent.setIndexType(0);
        talent.setMarker(1);
        talent.setPublisher(publisher);
        talent.setUpperStatus(0);
        if (publisher > 0) {
            talent.setVerify(SystemConst.VERIFY_AWAIT);
        } else {
            talent.setVerifyDate(LocalDateTime.now());
            talent.setVerify(SystemConst.VERIFY_ADOPT);
        }
        talentDao.save(talent);
        return talent;

    }

    /**
     * 更新资源
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Talent updateTalent(Long id, TalentParamDTO talentParamDTO) {
        Talent talent = accessObject(id);
        talent = ConvertObjectUtil.convertJavaBean(talent, talentParamDTO, true);
        return talentDao.save(talent);
    }

    /**
     * 资源上架
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void talentUpperShelf(Long id) {
        Talent talent = accessObject(id);
        if (talent.getUpperStatus().equals(SystemConst.STATUS_UPPER_SHELF)) {
            throw new HandleException("简历已上架，请勿重复操作");
        }
        talent.setUpperStatus(SystemConst.STATUS_UPPER_SHELF);
        talentDao.save(talent);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void talentLowerShelf(Long id) {

        Talent talent = accessObject(id);
        if (talent.getUpperStatus().equals(SystemConst.STATUS_LOWER_SHELF)) {
            throw new HandleException("简历已下架，请勿重复操作");
        }
        talent.setUpperStatus(SystemConst.STATUS_LOWER_SHELF);
        talentDao.save(talent);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void talentIndexYes(Long id) {

        Talent talent = accessObject(id);
        if (talent.getIndexType().equals(SystemConst.INDEX_TYPE_YES)) {
            throw new HandleException("简历已推荐至首页，请勿重复操作");
        }
        talent.setIndexType(SystemConst.INDEX_TYPE_YES);
        talentDao.save(talent);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void talentIndexNo(Long id) {
        Talent talent = accessObject(id);
        if (talent.getIndexType().equals(SystemConst.INDEX_TYPE_NO)) {
            throw new HandleException("简历已取消推荐至首页，请勿重复操作");
        }
        talent.setIndexType(SystemConst.INDEX_TYPE_NO);
        talentDao.save(talent);
    }

    /**
     * 审核
     */
    @Override
    public void talentVerify(Long id, Integer verify, String verifyRemark) {
        Talent talent = accessObject(id);
        talent.setVerifyDate(LocalDateTime.now());
        talent.setVerify(verify);
        if (verify.equals(SystemConst.VERIFY_REJECT)) {
            talent.setVerifyRemark(verifyRemark);
        }
        save(talent);
//        if (talent.getPublisher() > 0) {
//            mqClient.publish(MQTopics.MEMBER_SEND_MESSAGE, messageService.sendMessageJSON(talent.getPublisher(), Message.MESSAGE_TYPE_VERIFY,
//                    "交易需求审核提醒", description, Message.MESSAGE_TARGET_TYPE_ONE_INVITE, talent.getId().toString()));
//        }
    }
}