package com.yunzhi.marketing.xlk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.common.CommConstant;
import com.yunzhi.marketing.common.StringUtils;
import com.yunzhi.marketing.entity.TaRecommendCustomer;
import com.yunzhi.marketing.mapper.TaRecommendCustomerMapper;
import com.yunzhi.marketing.xlk.dto.ChannelCustomerDTO;
import com.yunzhi.marketing.xlk.dto.MarkingChannelCustomerDTO;
import com.yunzhi.marketing.xlk.entity.ChannelCustomer;
import com.yunzhi.marketing.xlk.mapper.ChannelCustomerMapper;
import com.yunzhi.marketing.xlk.service.IChannelCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 渠道报备客户表  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-07-19
 */
@Service
public class ChannelCustomerServiceImpl extends ServiceImpl<ChannelCustomerMapper, ChannelCustomer> implements IChannelCustomerService {

    @Autowired
    private ChannelCustomerMapper channelCustomerMapper;

    @Autowired
    private TaRecommendCustomerMapper taRecommendCustomerMapper;

    /**
     * 审核
     *
     * @param id
     * @param channelCustomerDTO
     * @return
     */
    @Override
    public ResponseBean auditCustomer(Integer id, ChannelCustomerDTO channelCustomerDTO) {
        ChannelCustomer customer = channelCustomerMapper.selectById(id);
        if ("fail".equals(channelCustomerDTO.getAuditResult())){
            // 审核不通过
            customer.setStatus("3");
            channelCustomerMapper.updateById(customer);
            return ResponseBean.success("审核成功");
        }

        // 审核通过 先查看在ta_recommend_customer 手机号是否存在
        LambdaQueryWrapper<TaRecommendCustomer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TaRecommendCustomer::getPhone,customer.getPhone());
        queryWrapper.eq(TaRecommendCustomer::getBuildingId,customer.getBuildingId());
        TaRecommendCustomer recommendCustomer = taRecommendCustomerMapper.selectOne(queryWrapper);
        if (null == recommendCustomer) {
            // 审核通过
            customer.setStatus("2");
            channelCustomerMapper.updateById(customer);
            // 进业务客户表
            TaRecommendCustomer taRecommendCustomer = new TaRecommendCustomer();
            taRecommendCustomer.setName(customer.getName());
            taRecommendCustomer.setPicture(customer.getPicture());
            taRecommendCustomer.setPhone(customer.getPhone());
            taRecommendCustomer.setSex(customer.getSex());
            taRecommendCustomer.setRecommendPerson(customer.getRecommendPerson());
            taRecommendCustomer.setBuildingId(customer.getBuildingId());
            taRecommendCustomer.setReportRecommendStatus(CommConstant.RECOMMENDED);
            taRecommendCustomer.setVerifyStatus(CommConstant.VERIFY_AGREE);
            taRecommendCustomer.setCreateDate(LocalDateTime.now());
            taRecommendCustomer.setReportDate(LocalDateTime.now());
            taRecommendCustomer.setOrgId(customer.getOrgId());
            taRecommendCustomer.setPersonId(customer.getPersonId());
            taRecommendCustomer.setEntryType(CommConstant.ENTRY_VERIFY);
            taRecommendCustomer.setStatus(CommConstant.CUSTOMER_REPORT);
            taRecommendCustomerMapper.insert(taRecommendCustomer);
            return ResponseBean.success("审核成功");
        }

        if (StringUtils.isEmpty(recommendCustomer.getRealtyConsultant()) &&
                (StringUtils.isEmpty(recommendCustomer.getRecommendPerson()) || recommendCustomer.getRecommendPerson().equals(customer.getRecommendPerson()))){
            // 审核通过
            customer.setStatus("2");
            channelCustomerMapper.updateById(customer);
            return ResponseBean.success("审核成功");
        }

        return ResponseBean.error("此客户是私客或已被报备", ResponseBean.ERROR_UNAVAILABLE);
    }

    /**
     * 驻场确认到访
     *
     * @param id
     * @param params
     * @return
     */
    @Override
    public ResponseBean markingChannelCustomer(MarkingChannelCustomerDTO params) {
        // 渠道推荐的客户
        if ("report".equals(params.getType())) {
            ChannelCustomer customer = channelCustomerMapper.selectById(params.getId());
            // 审核通过 先查看在ta_recommend_customer 手机号是否存在
            LambdaQueryWrapper<TaRecommendCustomer> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(TaRecommendCustomer::getPhone,customer.getPhone());
            queryWrapper.eq(TaRecommendCustomer::getBuildingId,customer.getBuildingId());
            TaRecommendCustomer recommendCustomer = taRecommendCustomerMapper.selectOne(queryWrapper);
            if (null == recommendCustomer) {
                // 审核通过
                customer.setStatus("2");
                channelCustomerMapper.updateById(customer);
                // 进业务客户表
                TaRecommendCustomer taRecommendCustomer = new TaRecommendCustomer();
                taRecommendCustomer.setName(customer.getName());
                taRecommendCustomer.setPicture(customer.getPicture());
                taRecommendCustomer.setPhone(customer.getPhone());
                taRecommendCustomer.setSex(customer.getSex());
                taRecommendCustomer.setRecommendPerson(customer.getRecommendPerson());
                taRecommendCustomer.setBuildingId(customer.getBuildingId());
                taRecommendCustomer.setReportRecommendStatus(CommConstant.RECOMMENDED);
                taRecommendCustomer.setVerifyStatus(CommConstant.VERIFY_AGREE);
                taRecommendCustomer.setCreateDate(LocalDateTime.now());
                taRecommendCustomer.setReportDate(LocalDateTime.now());
                taRecommendCustomer.setOrgId(customer.getOrgId());
                taRecommendCustomer.setPersonId(customer.getPersonId());
                taRecommendCustomer.setEntryType(CommConstant.ENTRY_VERIFY);
                taRecommendCustomer.setStatus(CommConstant.CUSTOMER_VISITE);
                taRecommendCustomerMapper.insert(taRecommendCustomer);
                return ResponseBean.success("审核成功");
            }

            if (StringUtils.isEmpty(recommendCustomer.getRealtyConsultant()) &&
                    (StringUtils.isEmpty(recommendCustomer.getRecommendPerson()) || recommendCustomer.getRecommendPerson().equals(customer.getRecommendPerson()))){
                // 审核通过
                customer.setStatus("2");
                channelCustomerMapper.updateById(customer);

                // 修改状态为确认到访状态
                recommendCustomer.setRecommendPerson(customer.getRecommendPerson());
                recommendCustomer.setVerifyStatus(CommConstant.VERIFY_AGREE);
                recommendCustomer.setStatus(CommConstant.CUSTOMER_VISITE);
                taRecommendCustomerMapper.updateById(recommendCustomer);
                return ResponseBean.success("审核成功");
            }
        }else if ("customer".equals(params.getType())) {
            // 置业顾问 经纪人推荐的客户
            TaRecommendCustomer taRecommendCustomer = taRecommendCustomerMapper.selectById(params.getId());
            taRecommendCustomer.setVerifyStatus(CommConstant.VERIFY_AGREE);
            taRecommendCustomer.setStatus(CommConstant.CUSTOMER_VISITE);
            taRecommendCustomerMapper.updateById(taRecommendCustomer);
            return ResponseBean.success("审核成功");
        }

        return ResponseBean.error("此客户是私客或已被报备", ResponseBean.ERROR_UNAVAILABLE);
    }

    /**
     * 驻场 无效报备
     *
     * @param params
     * @return
     */
    @Override
    public ResponseBean invalidCustomerUpdate(MarkingChannelCustomerDTO params) {// 渠道推荐的客户
        if ("channel".equals(params.getType())) {
            ChannelCustomer customer = channelCustomerMapper.selectById(params.getId());
            // 审核不通过 先查看在ta_recommend_customer 手机号是否存在
            LambdaQueryWrapper<TaRecommendCustomer> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(TaRecommendCustomer::getPhone,customer.getPhone());
            queryWrapper.eq(TaRecommendCustomer::getBuildingId,customer.getBuildingId());
            TaRecommendCustomer recommendCustomer = taRecommendCustomerMapper.selectOne(queryWrapper);
            if (null != recommendCustomer) {
                // 审核不通过
                recommendCustomer.setRecommendPerson(customer.getRecommendPerson());
                recommendCustomer.setVerifyStatus(CommConstant.VERIFY_AGREE);
                recommendCustomer.setStatus(CommConstant.CUSTOMER_VISITE);
                taRecommendCustomerMapper.updateById(recommendCustomer);

            }
            customer.setStatus("2");
            channelCustomerMapper.updateById(customer);
            return ResponseBean.success("审核成功");
        }else if ("customer".equals(params.getType())) {
            // 置业顾问 经纪人推荐的客户
            TaRecommendCustomer taRecommendCustomer = taRecommendCustomerMapper.selectById(params.getId());
            taRecommendCustomer.setVerifyStatus(CommConstant.VERIFY_DISAGREE);
            taRecommendCustomerMapper.updateById(taRecommendCustomer);
            return ResponseBean.success("审核成功");
        }
        return ResponseBean.error("无效操作", ResponseBean.ERROR_UNAVAILABLE);
    }
}
