package com.fjhx.common.service.employee.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.common.entity.employee.dto.EmployeeHandoverDto;
import com.fjhx.common.entity.employee.dto.EmployeeHandoverSelectDto;
import com.fjhx.common.entity.employee.po.EmployeeHandover;
import com.fjhx.common.entity.employee.vo.EmployeeHandoverVo;
import com.fjhx.common.mapper.employee.EmployeeHandoverMapper;
import com.fjhx.common.service.CommService;
import com.fjhx.common.service.employee.EmployeeHandoverService;
import com.fjhx.common.utils.Assert;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 员工交接 服务实现类
 * </p>
 *
 * @author
 * @since 2023-07-17
 */
@Service
public class EmployeeHandoverServiceImpl extends ServiceImpl<EmployeeHandoverMapper, EmployeeHandover> implements EmployeeHandoverService {

    @Autowired
    private CommService commService;
    @Autowired
    private ISysUserService userService;

    @Override
    public Page<EmployeeHandoverVo> getPage(EmployeeHandoverSelectDto dto) {
        IWrapper<EmployeeHandover> wrapper = getWrapper();
        //关键字搜索
        if (ObjectUtil.isNotEmpty(dto.getKeyword())) {
            DynamicDataSourceContextHolder.push(SourceConstant.BASE);
            List<SysUser> userList = userService.list(Wrappers.<SysUser>lambdaQuery().like(SysUser::getNickName, dto.getKeyword()));
            List<Long> userIds = userList.stream().map(SysUser::getUserId).collect(Collectors.toList());
            DynamicDataSourceContextHolder.poll();
            wrapper.and(q -> q
                    .like(EmployeeHandoverVo::getReason, dto.getKeyword())
                    .or().like(EmployeeHandoverVo::getRemark, dto.getKeyword())
                    .or().in(EmployeeHandoverVo::getHandoverPersonId, userIds)
                    .or().in(EmployeeHandoverVo::getRecipientId, userIds)
            );
        }
        wrapper.orderByDesc("eh", EmployeeHandover::getId);
        Page<EmployeeHandoverVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<EmployeeHandoverVo> records = page.getRecords();
        //赋值交接人
        UserUtil.assignmentNickName(records, EmployeeHandoverVo::getHandoverPersonId, EmployeeHandoverVo::setHandoverPersonName);
        //赋值接收人
        UserUtil.assignmentNickName(records, EmployeeHandoverVo::getRecipientId, EmployeeHandoverVo::setRecipientName);
        return page;
    }

    @Override
    @DSTransactional
    public void add(EmployeeHandoverDto employeeHandoverDto) {
        String content = employeeHandoverDto.getContent();
        Assert.notEmpty(content, "交接内容不能为空");
        String[] split = content.split(",");
        for (String s : split) {
            //1客户信息
            if ("1".equals(s)) {
                //把交接人的客户信息全部修改至接收人
                commService.updateCustomerSalesperson(employeeHandoverDto.getHandoverPersonId(), employeeHandoverDto.getRecipientId());
            } else {
                throw new ServiceException("未知交接内容：" + s);
            }
        }
        this.save(employeeHandoverDto);
    }

}
