package com.copy.trade.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.copy.trade.dao.MessageConfigMapper;
import com.copy.trade.entity.form.MessageConfigForm;
import com.copy.trade.entity.po.Customer;
import com.copy.trade.entity.po.MessageConfig;
import com.copy.trade.service.ICustomerService;
import com.copy.trade.service.IMessageConfigService;

@Service
public class MessageConfigServiceImpl extends ServiceImpl<MessageConfigMapper, MessageConfig> implements IMessageConfigService {

	@Autowired
	private ICustomerService customerServiceImpl;
	
	public MessageConfig get(Long memberId) {
		Customer customer = customerServiceImpl.getByMemberId(memberId);
		if(customer == null)
			return null;
		MessageConfig messageConfig = getById(customer.getId());
		if(messageConfig == null) {
			messageConfig = new MessageConfig();
			messageConfig.setId(customer.getId());
			messageConfig.setNoDisturb(true);
			messageConfig.setOpenSuccess(true);
			messageConfig.setOpenFailure(true);
			messageConfig.setCloseSuccess(true);
			messageConfig.setForceClose(true);
			messageConfig.setPositionLoss(true);
			messageConfig.setPositionProfit(true);
			messageConfig.setAccountProfit(true);
			messageConfig.setAccountLoss(true);
			messageConfig.setEmptyRemind(true);
			messageConfig.setUnfollowRemind(true);
			this.save(messageConfig);
		}
		return messageConfig;
	}
	public Boolean update(MessageConfigForm form, Long memberId) {
		Customer customer = customerServiceImpl.getByMemberId(memberId);
		if(customer == null)
			return false;
		MessageConfig messageConfig = getById(customer.getId());
		if(messageConfig == null)
			return false;
		if(form.getNoDisturb()!=null) {
			messageConfig.setNoDisturb(form.getNoDisturb());
		}
		if(form.getOpenSuccess()!=null) {
			messageConfig.setOpenSuccess(form.getOpenSuccess());
		}
		if(form.getOpenFailure()!=null) {
			messageConfig.setOpenFailure(form.getOpenFailure());
		}
		if(form.getCloseSuccess()!=null) {
			messageConfig.setCloseSuccess(form.getCloseSuccess());
		}
		if(form.getForceClose()!=null) {
			messageConfig.setForceClose(form.getForceClose());
		}
		if(form.getPositionLoss()!=null) {
			messageConfig.setPositionLoss(form.getPositionLoss());
		}
		if(form.getPositionProfit()!=null) {
			messageConfig.setPositionProfit(form.getPositionProfit());
		}
		if(form.getAccountProfit()!=null) {
			messageConfig.setAccountProfit(form.getAccountProfit());
		}
		if(form.getAccountLoss()!=null) {
			messageConfig.setAccountLoss(form.getAccountLoss());
		}
		if(form.getEmptyRemind()!=null) {
			messageConfig.setEmptyRemind(form.getEmptyRemind());
		}
		if(form.getUnfollowRemind()!=null) {
			messageConfig.setUnfollowRemind(form.getUnfollowRemind());
		}
		this.saveOrUpdate(messageConfig);
		return true;
	}
}
