package com.fs.crm.service.impl;

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

import com.alibaba.fastjson.JSONObject;
import com.fs.common.annotation.DataScope;
import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.crm.domain.CrmCustomer;
import com.fs.crm.domain.CrmCustomerLogs;
import com.fs.crm.domain.CrmCustomerUser;
import com.fs.crm.enums.CustomerLogEnum;
import com.fs.crm.mapper.CrmCustomerMapper;
import com.fs.crm.mapper.CrmCustomerUserMapper;
import com.fs.crm.param.CrmCustomerStatisticsParam;
import com.fs.crm.param.CrmCustomerVisitAddParam;
import com.fs.crm.param.CrmCustomerVisitListParam;
import com.fs.crm.param.CrmCustomerVisitListQueryParam;
import com.fs.crm.vo.CrmCustomerVisitExportVO;
import com.fs.crm.vo.CrmCustomerVisitListVO;
import com.fs.crm.vo.CrmCustomerVisitStatisticsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.crm.mapper.CrmCustomerVisitMapper;
import com.fs.crm.domain.CrmCustomerVisit;
import com.fs.crm.service.ICrmCustomerVisitService;

/**
 * 跟进Service业务层处理
 *
 * @author fs
 * @date 2022-12-21
 */
@Service
public class CrmCustomerVisitServiceImpl implements ICrmCustomerVisitService
{
    @Autowired
    private CrmCustomerVisitMapper crmCustomerVisitMapper;
    @Autowired
    private CrmCustomerUserMapper crmCustomerUserMapper;
    @Autowired
    private CrmCustomerMapper crmCustomerMapper;
    /**
     * 查询跟进
     *
     * @param visitId 跟进ID
     * @return 跟进
     */
    @Override
    public CrmCustomerVisit selectCrmCustomerVisitById(Long visitId)
    {
        return crmCustomerVisitMapper.selectCrmCustomerVisitById(visitId);
    }

    /**
     * 查询跟进列表
     *
     * @param crmCustomerVisit 跟进
     * @return 跟进
     */
    @Override
    public List<CrmCustomerVisit> selectCrmCustomerVisitList(CrmCustomerVisit crmCustomerVisit)
    {
        return crmCustomerVisitMapper.selectCrmCustomerVisitList(crmCustomerVisit);
    }

    /**
     * 新增跟进
     *
     * @param crmCustomerVisit 跟进
     * @return 结果
     */
    @Override
    public int insertCrmCustomerVisit(CrmCustomerVisit crmCustomerVisit)
    {
        crmCustomerVisit.setCreateTime(DateUtils.getNowDate());
        return crmCustomerVisitMapper.insertCrmCustomerVisit(crmCustomerVisit);
    }

    /**
     * 修改跟进
     *
     * @param crmCustomerVisit 跟进
     * @return 结果
     */
    @Override
    public int updateCrmCustomerVisit(CrmCustomerVisit crmCustomerVisit)
    {
        return crmCustomerVisitMapper.updateCrmCustomerVisit(crmCustomerVisit);
    }

    /**
     * 批量删除跟进
     *
     * @param visitIds 需要删除的跟进ID
     * @return 结果
     */
    @Override
    public int deleteCrmCustomerVisitByIds(Long[] visitIds)
    {
        return crmCustomerVisitMapper.deleteCrmCustomerVisitByIds(visitIds);
    }

    /**
     * 删除跟进信息
     *
     * @param visitId 跟进ID
     * @return 结果
     */
    @Override
    public int deleteCrmCustomerVisitById(Long visitId)
    {
        return crmCustomerVisitMapper.deleteCrmCustomerVisitById(visitId);
    }

    @Override
    @DataScope(userAlias = "cu",deptAlias = "cu")
    public List<CrmCustomerVisitListVO> selectCrmCustomerVisitListVO(CrmCustomerVisitListParam param) {
        return crmCustomerVisitMapper.selectCrmCustomerVisitListVO(param);
    }

    @Override
    @DataScope(userAlias = "cu",deptAlias = "cu")
    public List<CrmCustomerVisitListVO> selectCrmCustomerVisitListQuery(CrmCustomerVisitListQueryParam param) {
        return crmCustomerVisitMapper.selectCrmCustomerVisitListQuery(param);
    }

    @Override
    public R addVisit(CrmCustomerVisitAddParam param) {
        CrmCustomer customer=crmCustomerMapper.selectCrmCustomerById(param.getCustomerId());
        if(customer.getIsReceive()!=null&&customer.getIsReceive()==1){
            CrmCustomerUser crmCustomerUser=crmCustomerUserMapper.selectCrmCustomerUserById(customer.getCustomerUserId());
            if(crmCustomerUser!=null){
                if(crmCustomerUser.getCompanyUserId()!=null&&!crmCustomerUser.getCompanyUserId().equals(param.getCompanyUserId())){
                    return R.error("请先认领客户");
                }
                CrmCustomerVisit visit=new CrmCustomerVisit();
                BeanUtils.copyProperties(param,visit);
                visit.setCustomerUserStatus(param.getCustomerUserStatus());
                visit.setCompanyId(customer.getCompanyId());
                visit.setCreateTime(new Date());
                if(crmCustomerVisitMapper.insertCrmCustomerVisit(visit)>0){
                    //更新状态
                    crmCustomerUser.setStatus(param.getCustomerUserStatus());
                    crmCustomerUser.setLastTime(new Date());
                    crmCustomerUserMapper.updateCrmCustomerUser(crmCustomerUser);
                    customer.setVisitStatus(param.getCustomerUserStatus());
                    if(param.getCustomerType()!=null&&param.getCustomerType()>0){
                        customer.setCustomerType(param.getCustomerType());
                    }
                    customer.setVisitTime(new Date());
                    crmCustomerMapper.updateCrmCustomer(customer);
                    return R.ok();
                }
                else{
                    return R.error("操作失败");
                }

            }
            else{
                return R.error("未认领");
            }
        }
        else{
            return R.error("请先认领客户");
        }



    }

    @Override
    public List<CrmCustomerVisitStatisticsVO> selectCrmCustomerVisitStatisticsList(CrmCustomerStatisticsParam param) {
        return crmCustomerVisitMapper.selectCrmCustomerVisitStatisticsList(param);
    }

    @Override
    public List<JSONObject> selectCrmCustomerVisitCounts(Map<String, Object> toMap) {
        return crmCustomerVisitMapper.selectCrmCustomerVisitCounts(toMap);
    }

    @Override
    @DataScope(userAlias = "cu",deptAlias = "cu")
    public List<CrmCustomerVisitExportVO> selectCrmCustomerVisitExportListVO(CrmCustomerVisitListParam param) {
        return crmCustomerVisitMapper.selectCrmCustomerVisitExportListVO(param);
    }
}
