package com.zfire.mall.manager.logic.customer;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfire.mall.manager.bean.admin.AdminUserCom;
import com.zfire.mall.manager.bean.manage.customer.CustomerApplyBean;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.plus.entity.CustomerApply;
import com.zfire.mall.manager.plus.entity.CustomerApplyFollow;
import com.zfire.mall.manager.plus.service.CustomerApplyFollowService;
import com.zfire.mall.manager.plus.service.CustomerApplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class CustomerApplyLogic {

    @Autowired
    CustomerApplyService customerApplyService;
    @Autowired
    CustomerApplyFollowService customerApplyFollowService;
    @Autowired
    CommonLogic commonLogic;



    public void add(CustomerApply customerApply){
        String customerName = customerApply.getCustomerName();
        Integer count = customerApplyService.lambdaQuery().eq(CustomerApply::getCustomerName, customerName).count();
        if(count > 0){
            throw new RemoteServiceException(customerName + "已申请入驻，请耐心等待消息");
        }
        customerApply.insert();
    }

    public IPage<CustomerApply> list(String customerName,String linkName,String linkPhone,String email,Integer pageNo,Integer pageSize){
        IPage<CustomerApply> page = customerApplyService.lambdaQuery()
                .like(StringUtils.isNotEmpty(customerName), CustomerApply::getCustomerName, customerName)
                .like(StringUtils.isNotEmpty(linkName), CustomerApply::getLinkName, linkName)
                .like(StringUtils.isNotEmpty(linkPhone), CustomerApply::getLinkPhone, linkPhone)
                .like(StringUtils.isNotEmpty(email), CustomerApply::getEmail, email)
                .orderByDesc(CustomerApply::getCreateTime)
                .page(new Page<>(pageNo, pageSize));
        return page;
    }


    public CustomerApplyBean detail(String id){
        CustomerApply customerApply = customerApplyService.getById(id);
        CustomerApplyBean customerApplyBean = new CustomerApplyBean();
        BeanUtils.copyProperties(customerApply,customerApplyBean);
        //跟进记录
        List<CustomerApplyFollow> list = customerApplyFollowService.lambdaQuery()
                .eq(CustomerApplyFollow::getCustomerApplyId, id)
                .orderByDesc(CustomerApplyFollow::getCreateTime)
                .list();
        customerApplyBean.setCustomerApplyFollows(list);
        return customerApplyBean;
    }

    @Transactional
    public void follow(String id,Integer status,String remark){
        CustomerApply customerApply = customerApplyService.getById(id);
        if(customerApply.getStatus() == 2){
            throw new RemoteServiceException("已关闭的信息不可跟进");
        }
        AdminUserCom adminUser = commonLogic.getAdminUser();
        CustomerApplyFollow bean = new CustomerApplyFollow();
        bean.setCustomerApplyId(id);
        bean.setStatus(status);
        bean.setRemark(remark);
        bean.setFollowName(adminUser.getNickName());
        bean.setFollowTime(new Date());
        bean.insert();

        customerApply.setLastFollowerName(bean.getFollowName());
        customerApply.setLastFollowerTime(bean.getFollowTime());
        customerApply.updateById();
    }


}
