package com.fs.crm.service.impl;

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

import com.fs.common.annotation.DataScope;
import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.StringUtils;
import com.fs.company.domain.Company;
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.CrmCustomerLogsMapper;
import com.fs.crm.mapper.CrmCustomerMapper;
import com.fs.crm.mapper.CrmCustomerUserMapper;
import com.fs.crm.vo.CrmCustomerAssignListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.crm.mapper.CrmCustomerAssignMapper;
import com.fs.crm.domain.CrmCustomerAssign;
import com.fs.crm.service.ICrmCustomerAssignService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 客户分配记录Service业务层处理
 *
 * @author fs
 * @date 2023-03-30
 */
@Service
public class CrmCustomerAssignServiceImpl implements ICrmCustomerAssignService
{
    @Autowired
    private CrmCustomerAssignMapper crmCustomerAssignMapper;
    @Autowired
    private CrmCustomerMapper crmCustomerMapper;
    @Autowired
    private CrmCustomerLogsMapper logsMapper;
    @Autowired
    private CrmCustomerUserMapper crmCustomerUserMapper;
    /**
     * 查询客户分配记录
     *
     * @param assignId 客户分配记录ID
     * @return 客户分配记录
     */
    @Override
    public CrmCustomerAssign selectCrmCustomerAssignById(Long assignId)
    {
        return crmCustomerAssignMapper.selectCrmCustomerAssignById(assignId);
    }

    /**
     * 查询客户分配记录列表
     *
     * @param crmCustomerAssign 客户分配记录
     * @return 客户分配记录
     */
    @Override
    public List<CrmCustomerAssign> selectCrmCustomerAssignList(CrmCustomerAssign crmCustomerAssign)
    {
        return crmCustomerAssignMapper.selectCrmCustomerAssignList(crmCustomerAssign);
    }

    /**
     * 新增客户分配记录
     *
     * @param crmCustomerAssign 客户分配记录
     * @return 结果
     */
    @Override
    public int insertCrmCustomerAssign(CrmCustomerAssign crmCustomerAssign)
    {
        crmCustomerAssign.setCreateTime(DateUtils.getNowDate());
        return crmCustomerAssignMapper.insertCrmCustomerAssign(crmCustomerAssign);
    }

    /**
     * 修改客户分配记录
     *
     * @param crmCustomerAssign 客户分配记录
     * @return 结果
     */
    @Override
    public int updateCrmCustomerAssign(CrmCustomerAssign crmCustomerAssign)
    {
        return crmCustomerAssignMapper.updateCrmCustomerAssign(crmCustomerAssign);
    }

    /**
     * 批量删除客户分配记录
     *
     * @param assignIds 需要删除的客户分配记录ID
     * @return 结果
     */
    @Override
    public int deleteCrmCustomerAssignByIds(Long[] assignIds)
    {
        return crmCustomerAssignMapper.deleteCrmCustomerAssignByIds(assignIds);
    }

    /**
     * 删除客户分配记录信息
     *
     * @param assignId 客户分配记录ID
     * @return 结果
     */
    @Override
    public int deleteCrmCustomerAssignById(Long assignId)
    {
        return crmCustomerAssignMapper.deleteCrmCustomerAssignById(assignId);
    }

    @Override
    public List<CrmCustomerAssignListVO> selectCrmCustomerAssignListVO(CrmCustomerAssign crmCustomerAssign) {
        return crmCustomerAssignMapper.selectCrmCustomerAssignListVO(crmCustomerAssign);
    }

    @Override
    @Transactional
    public R cancel(CrmCustomerAssign param,String operUserName) {

        CrmCustomerAssign crmCustomerAssign=crmCustomerAssignMapper.selectCrmCustomerAssignById(param.getAssignId());
        String[] customerIds=crmCustomerAssign.getCustomerIds().split(",");
        for(String id:customerIds){
            CrmCustomer customer=crmCustomerMapper.selectCrmCustomerById(Long.parseLong(id));
            if(crmCustomerAssign.getAssignType().equals(1)){
                customer.setStatus(0);
                customer.setUpdateTime(new Date());
                customer.setCompanyId(0l);
                crmCustomerMapper.updateCrmCustomer(customer);
                //写日志
                CrmCustomerLogs logs=new CrmCustomerLogs();
                logs.setCustomerId(customer.getCustomerId());
                logs.setCreateTime(new Date());
                logs.setLogsType(CustomerLogEnum.CANCEL_ASSIGN.getValue());
                logs.setTitle(CustomerLogEnum.CANCEL_ASSIGN.getDesc());
                logs.setRemark(operUserName+"撤销分配");
                logsMapper.insertCrmCustomerLogs(logs);
            }
            else if(crmCustomerAssign.getAssignType().equals(2)){
                if(customer.getCustomerUserId()!=null){
                    CrmCustomerUser crmCustomerUser=crmCustomerUserMapper.selectCrmCustomerUserById(customer.getCustomerUserId());
                    if(crmCustomerUser!=null){
//                        crmCustomerUser.setIsPool(1);
//                        crmCustomerUser.setPoolTime(new Date());
                        crmCustomerUserMapper.deleteCrmCustomerUserById(crmCustomerUser.getCustomerUserId());

                    }
                }
                customer.setReceiveUserId(0l);
                customer.setIsReceive(0);
                customer.setPoolTime(new Date());
                customer.setCustomerUserId(0l);
                crmCustomerMapper.updateCrmCustomer(customer);
                //写日志
                CrmCustomerLogs logs=new CrmCustomerLogs();
                logs.setCustomerId(customer.getCustomerId());
                logs.setCreateTime(new Date());
                logs.setLogsType(CustomerLogEnum.CANCEL_ASSIGN.getValue());
                logs.setTitle(CustomerLogEnum.CANCEL_ASSIGN.getDesc());
                logs.setRemark(operUserName+"撤销分配");
                logsMapper.insertCrmCustomerLogs(logs);

            }
            else if(crmCustomerAssign.getAssignType().equals(3)){
                if(customer.getCustomerUserId()!=null){
                    CrmCustomerUser crmCustomerUser=crmCustomerUserMapper.selectCrmCustomerUserById(customer.getCustomerUserId());
                    if(crmCustomerUser!=null){
                        crmCustomerUser.setCompanyUserId(crmCustomerAssign.getSourceUserId());
                        crmCustomerUserMapper.updateCrmCustomerUser(crmCustomerUser);
                    }
                    customer.setReceiveUserId(crmCustomerAssign.getSourceUserId());
                    crmCustomerMapper.updateCrmCustomer(customer);
                }
                //写日志
                CrmCustomerLogs logs=new CrmCustomerLogs();
                logs.setCustomerId(customer.getCustomerId());
                logs.setCreateTime(new Date());
                logs.setLogsType(CustomerLogEnum.CANCEL_ASSIGN.getValue());
                logs.setTitle(CustomerLogEnum.CANCEL_ASSIGN.getDesc());
                logs.setRemark(operUserName+"撤销分配");
                logsMapper.insertCrmCustomerLogs(logs);

            }
            else if(crmCustomerAssign.getAssignType().equals(4)){
                if(customer.getCustomerUserId()!=null){
                    CrmCustomerUser crmCustomerUser=crmCustomerUserMapper.selectCrmCustomerUserById(customer.getCustomerUserId());
                    if(crmCustomerUser!=null){
//                        crmCustomerUser.setIsPool(1);
//                        crmCustomerUser.setPoolTime(new Date());
                        crmCustomerUserMapper.deleteCrmCustomerUserById(crmCustomerUser.getCustomerUserId());

                    }
                }
                customer.setIsLine(1);
                customer.setReceiveUserId(0l);
                customer.setIsReceive(0);
                customer.setCustomerUserId(0l);
                crmCustomerMapper.updateCrmCustomer(customer);
                //写日志
                CrmCustomerLogs logs=new CrmCustomerLogs();
                logs.setCustomerId(customer.getCustomerId());
                logs.setCreateTime(new Date());
                logs.setLogsType(CustomerLogEnum.CANCEL_ASSIGN.getValue());
                logs.setTitle(CustomerLogEnum.CANCEL_ASSIGN.getDesc());
                logs.setRemark(operUserName+"撤销分配");
                logsMapper.insertCrmCustomerLogs(logs);

            }
            crmCustomerAssign.setStatus(1);
            crmCustomerAssignMapper.updateCrmCustomerAssign(crmCustomerAssign);
        }
        return R.ok("撤销成功");
    }

    @Override
    public List<CrmCustomerAssignListVO> selectCompanyCrmCustomerAssignList(CrmCustomerAssign crmCustomerAssign) {
        return crmCustomerAssignMapper.selectCompanyCrmCustomerAssignList(crmCustomerAssign);
    }
}
