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.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.PropertyDao;
import com.softwarebr.nunu.dto.res.PropertyParamDTO;
import com.softwarebr.nunu.entity.res.Invite;
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.PropertyDetailsResult;
import com.softwarebr.nunu.service.res.PropertyApplyService;
import com.softwarebr.nunu.service.res.PropertyService;
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;

/**
 * @date 2020-02-21 11:49:00
 * @author lilin
 */

@Slf4j
@Service
@MQConfig
public class PropertyServiceImpl extends AbstractEntityService<Property, Long, Object> implements PropertyService {

	@Autowired
	private PropertyDao propertyDao;

	@Autowired
	private PropertyApplyService propertyApplyService;

	@Autowired
	private MQClient mqClient;

	@Autowired
	private MessageService messageService;

	@Override
	public CommonRepository<Long, Property> getRepository() {
		return propertyDao;
	}

	@Override
	public PropertyDetailsResult getPropertyDetails(Long id, Long memberId){
		Property property = accessObject(id);
		PropertyDetailsResult detailsResult = ControllerPageHandle.convertTo(property, PropertyDetailsResult.class);
		if(!detailsResult.getPublicity().equals(SystemConst.PUBLICITY_PUBLIC) && !detailsResult.getPublisher().equals(memberId)){
			PropertyApply propertyApply = propertyApplyService.getPropertyApplyByMember(PropertyApply.RESOURCE_TYPE_PROPERTY, id, memberId);
			if(null == propertyApply){
				detailsResult.setContactPhone("");
				detailsResult.setContact("");
				detailsResult.setEmail("");
				detailsResult.setDisplay(false);
			}
		}
		return detailsResult;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateStatus(Long propertyId, Integer status){
		Property property = accessObject(propertyId);
		if(!property.getVerify().equals(SystemConst.VERIFY_ADOPT)){
			throw new HandleException("当前载体资源未审核通过，不支持修改操作");
		}

		propertyDao.updateStatus(propertyId, status);
	}

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

	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Property saveProperty(Long publisher, PropertyParamDTO propertyParamDTO) {
		Property property = convertTo(propertyParamDTO, Property.class);
		property.setPrice(AmountConversionUtil.input(propertyParamDTO.getPrice()));
		property.setCreateDate(LocalDateTime.now());
		property.setVerifyDate(LocalDateTime.now());
		if (publisher > 0) {
			property.setVerify(SystemConst.VERIFY_AWAIT);
		} else {
			property.setVerify(SystemConst.VERIFY_ADOPT);
		}
		property.setPublisher(publisher);
		property.setIndexType(SystemConst.INDEX_TYPE_NO);
		property.setUpperStatus(SystemConst.STATUS_LOWER_SHELF);
		property.setMarker(SystemConst.MARKER_STATUS_NO);
		return propertyDao.save(property);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateProperty(Long propertyId, PropertyParamDTO propertyParamDTO) {
		Property property = accessObject(propertyId);
		property = ConvertObjectUtil.convertJavaBean(property, propertyParamDTO, true);
		property.setPrice(AmountConversionUtil.input(propertyParamDTO.getPrice()));
		propertyDao.save(property);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void removePropertyById(Long propertyId) {
		Property property = accessObject(propertyId);
		property.setMarker(SystemConst.MARKER_STATUS_YES);
		propertyDao.save(property);
	}


	@Override
	@Transactional(rollbackFor = Exception.class)
	public void propertyUpperShelf(Long propertyId) {
		Property property = accessObject(propertyId);
		if (property.getUpperStatus().equals(SystemConst.STATUS_UPPER_SHELF)) {
			throw new HandleException("载体资源已上架，请勿重复操作");
		}
		property.setUpperStatus(SystemConst.STATUS_UPPER_SHELF);
		propertyDao.save(property);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void propertyLowerShelf(Long propertyId) {
		Property property = accessObject(propertyId);
		if (property.getUpperStatus().equals(SystemConst.STATUS_LOWER_SHELF)) {
			throw new HandleException("载体资源已下架，请勿重复操作");
		}
		property.setUpperStatus(SystemConst.STATUS_LOWER_SHELF);
		propertyDao.save(property);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void propertyIndexYes(Long propertyId) {
		Property property = accessObject(propertyId);
		if (property.getIndexType().equals(SystemConst.INDEX_TYPE_YES)) {
			throw new HandleException("载体资源已推荐至首页，请勿重复操作");
		}
		property.setIndexType(SystemConst.INDEX_TYPE_YES);
		propertyDao.save(property);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void propertyIndexNo(Long propertyId) {
		Property property = accessObject(propertyId);
		if (property.getIndexType().equals(SystemConst.INDEX_TYPE_NO)) {
			throw new HandleException("载体资源已取消推荐至首页，请勿重复操作");
		}
		property.setIndexType(SystemConst.INDEX_TYPE_NO);
		propertyDao.save(property);
	}
}