package com.copy.trade.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.copy.trade.entity.form.TraderDataQueryForm;
import com.copy.trade.entity.po.Customer;
import com.copy.trade.entity.po.TraderData;
import com.copy.trade.entity.vo.TraderDataVo;
import com.copy.trade.constant.RedisKey;
import com.copy.trade.dao.CustomerMapper;
import com.copy.trade.dao.TraderDataMapper;
import com.copy.trade.service.ICustomerService;
import com.copy.trade.service.IStatisticsDataService;
import com.copy.trade.service.ITraderDataService;
import com.copy.trade.util.LocalDateUtils;
import com.copy.trade.util.RedisUtils;
import com.copy.trade.util.WebAssert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

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

/**
 * <p>
 * 交易员统计数据 服务实现类
 * </p>
 *
 * @author wz
 * @since 2022-12-01
 */
@Service
public class TraderDataServiceImpl extends ServiceImpl<TraderDataMapper, TraderData> implements ITraderDataService {

    @Autowired
    private RedisUtils redisUtils;
    
    @Autowired
    private IStatisticsDataService statisticsDataServiceImpl;
	
    @Override
    public TraderData getByTraderId(Long traderId) {
        QueryWrapper<TraderData> traderDataQueryWrapper = new QueryWrapper<>();
        traderDataQueryWrapper.eq("trader_id",traderId);
        return getOne(traderDataQueryWrapper);
    }
    
    @Override
    public Boolean edit(TraderData traderDataForm) {
    	TraderData traderData = getByTraderId(traderDataForm.getTraderId());
        WebAssert.isNotNull(traderData, "chk.trader.info.null");
        
        traderData.setDayYield14(traderDataForm.getDayYield14());
        traderData.setDayYield30(traderDataForm.getDayYield30());
        traderData.setWinRate14(traderDataForm.getWinRate14());
        traderData.setProfitAmount(traderDataForm.getProfitAmount());
        traderData.setFollowCustomer(traderDataForm.getFollowCustomer());
        traderData.setOrderDeal14(traderDataForm.getOrderDeal14());
        //traderData.setRetractionRate14(traderDataForm.getRetractionRate14());
        traderData.setTradingDays(traderDataForm.getTradingDays());
        traderData.setProfitDays(traderDataForm.getProfitDays());
        traderData.setCurrentMonthFollow(traderDataForm.getCurrentMonthFollow());
        traderData.setTeamProfit14(traderDataForm.getTeamProfit14());
        
        updateById(traderData);
        
        statisticsDataServiceImpl.updateMaxDateDate(traderData.getTraderId(),traderData.getDayYield14());
        
        return true;
    }

	@Override
	public Page<TraderDataVo> traderDataList(TraderDataQueryForm traderDataQueryForm) {
		Page page = traderDataQueryForm.newFormPage();
		List<TraderDataVo> list = ((TraderDataMapper)baseMapper).traderDataList(page, traderDataQueryForm);
		page.setRecords(list);
		return page;
	}
	
	
}
