package com.yunzhi.marketing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.dto.StatisticsDTO;
import com.yunzhi.marketing.entity.TaChannelPerson;
import com.yunzhi.marketing.mapper.TaChannelPersonMapper;
import com.yunzhi.marketing.po.CustomerRankListVO;
import com.yunzhi.marketing.service.ChannelCustomerStatisticsService;
import com.yunzhi.marketing.xlk.entity.BuildingChannel;
import com.yunzhi.marketing.xlk.mapper.BuildingChannelMapper;
import com.yunzhi.marketing.xlk.mapper.ChannelCustomerMapper;
import com.yunzhi.marketing.xlk.mapper.CustomerVisitMapper;
import com.yunzhi.marketing.xlk.vo.CustomerDetailVO;
import org.ietf.jgss.ChannelBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ChannelCustomerStatisticsServiceImpl implements ChannelCustomerStatisticsService {

    @Autowired
    private BuildingChannelMapper buildingChannelMapper;

    @Autowired
    private ChannelCustomerMapper channelCustomerMapper;

    @Autowired
    private TaChannelPersonMapper channelPersonMapper;

    @Autowired
    private CustomerVisitMapper customerVisitMapper;

    @Override
    public ResponseBean getRanklist(StatisticsDTO statisticsDTo) {
        LambdaQueryWrapper<TaChannelPerson> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TaChannelPerson::getPersonId,statisticsDTo.getPersonId());
        TaChannelPerson taChannelPerson = channelPersonMapper.selectOne(lambdaQueryWrapper);

        if (null == taChannelPerson) {
            return ResponseBean.error("此用户未绑定渠道商", ResponseBean.ERROR_UNAVAILABLE);
        }
        statisticsDTo.setChannelId(taChannelPerson.getChannelId());
        List<CustomerRankListVO> list = channelCustomerMapper.getRanklist(statisticsDTo);
        return ResponseBean.success(list);
    }

    /**
     * 获取销售简报
     *
     * @param statisticsDTo
     * @return
     */
    @Override
    public ResponseBean getBriefing(StatisticsDTO statisticsDTo) {
        LambdaQueryWrapper<TaChannelPerson> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TaChannelPerson::getPersonId,statisticsDTo.getPersonId());
        TaChannelPerson taChannelPerson = channelPersonMapper.selectOne(lambdaQueryWrapper);

        if (null == taChannelPerson) {
            return ResponseBean.error("此用户未绑定渠道商", ResponseBean.ERROR_UNAVAILABLE);
        }
        statisticsDTo.setChannelId(taChannelPerson.getChannelId());

        // 获取新增客户
        int newCustomerNum = channelCustomerMapper.getNewCustomer(statisticsDTo);
        // 获取跟进客户
        int followCustomerNum =  customerVisitMapper.getFollowCustomer(statisticsDTo);
        // 获取成交客户
        int successCustomerNum = channelCustomerMapper.getSuccessCustomer(statisticsDTo);
        Map<String,Object> map = new HashMap<>();
        map.put("newCustomerNum",newCustomerNum);
        map.put("followCustomerNum",followCustomerNum);
        map.put("successCustomerNum",successCustomerNum);
        return ResponseBean.success(map);
    }

    /**
     * 获取折线图数据
     *
     * @param statisticsDTo
     * @return
     */
    @Override
    public ResponseBean getBriefingDetail(StatisticsDTO statisticsDTo) {
        LambdaQueryWrapper<TaChannelPerson> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TaChannelPerson::getPersonId,statisticsDTo.getPersonId());
        TaChannelPerson taChannelPerson = channelPersonMapper.selectOne(lambdaQueryWrapper);

        if (null == taChannelPerson) {
            return ResponseBean.error("此用户未绑定渠道商", ResponseBean.ERROR_UNAVAILABLE);
        }
        statisticsDTo.setChannelId(taChannelPerson.getChannelId());

        List<CustomerDetailVO> customerDetail = new ArrayList<>();
        if ("new".equals(statisticsDTo.getCustomerType())){
            // 获取新增客户
            customerDetail = channelCustomerMapper.getNewCustomerDetail(statisticsDTo);
        }else if ("follow".equals(statisticsDTo.getCustomerType())){
            // 获取跟进客户
            customerDetail = customerVisitMapper.getFollowCustomerDetail(statisticsDTo);
        }else if ("success".equals(statisticsDTo.getCustomerType())) {
            // 获取成交客户
            customerDetail = channelCustomerMapper.getSuccessCustomerDetail(statisticsDTo);
        }
        return ResponseBean.success(customerDetail);
    }
}
