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

import com.alibaba.fastjson.JSONObject;
import com.brframework.commonweb.exception.HandleException;
import com.softwarebr.nunu.dto.org.InvestOrgParamDTO;
import com.softwarebr.nunu.entity.hall.InvestOrg;
import com.softwarebr.nunu.dao.hall.InvestOrgDao;
import com.softwarebr.nunu.entity.user.Member;
import com.softwarebr.nunu.globals.SystemConst;
import com.softwarebr.nunu.service.hall.InvestOrgService;
import com.brframework.commondb.core.AbstractEntityService;
import com.brframework.commondb.core.CommonRepository;
import com.softwarebr.nunu.service.user.MemberService;
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-25 15:59:36
 */

@Slf4j
@Service
public class InvestOrgServiceImpl extends AbstractEntityService<InvestOrg, Long, Object> implements InvestOrgService {

    @Autowired
    private InvestOrgDao investOrgDao;

    @Autowired
    private MemberService memberService;

    @Override
    public CommonRepository<Long, InvestOrg> getRepository() {
        return investOrgDao;
    }


    @Override
    public void checkInvestOrgVerify(Long memberId) {
        InvestOrg investOrg = investOrgDao.findTopByMemberIdAndVerifyOrderByCreateDateDesc(memberId, SystemConst.VERIFY_ADOPT);
        if (null == investOrg) {
            throw new HandleException("机构认证成为投资机构即可查看项目详情");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public InvestOrg saveInvestOrg(Long memberId, InvestOrgParamDTO investOrgParamDTO) {
        InvestOrg investOrg = convertTo(investOrgParamDTO, InvestOrg.class);
        if (memberId > 0) {
            Member member = memberService.accessObject(memberId);
            if (!member.getIdentityType().equals(Member.IDENTITY_TYPE_ENTERPRISE)) {
                throw new HandleException("用户非企业身份，认证投资机构失败！");
            }
            investOrg.setVerify(SystemConst.VERIFY_AWAIT);
        } else {
            investOrg.setVerify(SystemConst.VERIFY_ADOPT);
        }
        investOrg.setMemberId(memberId);
        investOrg.setStatus(SystemConst.STATUS_LOWER_SHELF);
        investOrg.setCreateDate(LocalDateTime.now());
        investOrg.setInvestInfos(JSONObject.toJSONString(investOrgParamDTO.getInvestInfo()));
        InvestOrg org = investOrgDao.save(investOrg);
        return org;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateInvestOrg(Long investOrgId, InvestOrgParamDTO investOrgParamDTO) {
        InvestOrg investOrg = accessObject(investOrgId);
        investOrg.setCompanyName(investOrgParamDTO.getCompanyName());
        investOrg.setCompanyType(investOrgParamDTO.getCompanyType());
        investOrg.setCoverUri(investOrgParamDTO.getCoverUri());
        investOrg.setDescription(investOrgParamDTO.getDescription());
        investOrg.setBackGround(investOrgParamDTO.getBackGround());
        investOrg.setInvestInfos(JSONObject.toJSONString(investOrgParamDTO.getInvestInfo()));
        investOrg.setContact(investOrgParamDTO.getContact());
        investOrg.setContactPhone(investOrgParamDTO.getContactPhone());
        investOrgDao.save(investOrg);
    }

    @Override
    public InvestOrg getInvestOrgResult(Long memberId) {
        return investOrgDao.findTopByMemberIdOrderByCreateDateDesc(memberId);
    }

    @Override
    public Long countInvestOrgByVerifyAdopt(LocalDateTime startDate, LocalDateTime endDate) {
        return investOrgDao.countByVerifyAndCreateDateBetween(SystemConst.VERIFY_ADOPT, startDate, endDate);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void investOrgUpperShelf(Long investOrgId) {
        InvestOrg investOrg = accessObject(investOrgId);
        if (investOrg.getStatus().equals(SystemConst.STATUS_UPPER_SHELF)) {
            throw new HandleException("投资机构已上架，请勿重复操作！");
        }
        investOrg.setStatus(SystemConst.STATUS_UPPER_SHELF);
        investOrgDao.save(investOrg);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void investOrgLowerShelf(Long investOrgId) {
        InvestOrg investOrg = accessObject(investOrgId);
        if (investOrg.getStatus().equals(SystemConst.STATUS_LOWER_SHELF)) {
            throw new HandleException("投资机构已下架，请勿重复操作！");
        }
        investOrg.setStatus(SystemConst.STATUS_LOWER_SHELF);
        investOrgDao.save(investOrg);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void investOrgVerify(Long investOrgId, Integer verify, String verifyRemark) {
        InvestOrg investOrg = accessObject(investOrgId);
        if (!investOrg.getVerify().equals(SystemConst.VERIFY_AWAIT)) {
            throw new HandleException("投资机构已审核，请勿重复操作！");
        }
        investOrg.setVerify(verify);
        investOrg.setVerifyRemark(verifyRemark);
        investOrgDao.save(investOrg);
    }

}