package com.fjhx.victoriatourist.service.abnormal.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
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.victoriatourist.entity.abnormal.dto.AbnormalInfoSelectDto;
import com.fjhx.victoriatourist.entity.abnormal.po.AbnormalInfo;
import com.fjhx.victoriatourist.entity.abnormal.vo.AbnormalInfoVo;
import com.fjhx.victoriatourist.mapper.abnormal.AbnormalInfoMapper;
import com.fjhx.victoriatourist.service.abnormal.AbnormalInfoService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.common.utils.wrapper.SqlField;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;


/**
 * <p>
 * 异常记录 服务实现类
 * </p>
 *
 * @author
 * @since 2023-04-11
 */
@DS(SourceConstant.VICTORIATOURIST)
@Service
public class AbnormalInfoServiceImpl extends ServiceImpl<AbnormalInfoMapper, AbnormalInfo> implements AbnormalInfoService {
    @Autowired
    private ISysRoleService sysRoleService;

    @Override
    public Page<AbnormalInfoVo> getPage(AbnormalInfoSelectDto dto) {
        IWrapper<AbnormalInfo> wrapper = getWrapper();
        wrapper.eq("ai",AbnormalInfo::getType, dto.getType())
                .eq("ai",AbnormalInfo::getStatus, dto.getStatus());

        //采购人id过滤
        wrapper.eq("p.create_user", dto.getPurchaseUserId());
        wrapper.keyword(dto.getKeyword(),
                new SqlField("ai",AbnormalInfo::getTitle),
                new SqlField("ai",AbnormalInfo::getLinkCode)
        );

        //添加权限过滤
        DynamicDataSourceContextHolder.push(SourceConstant.BASE);
        Set<String> permissionList = sysRoleService.selectRolePermissionByUserId(SecurityUtils.getUserId());
        DynamicDataSourceContextHolder.poll();
        if (ObjectUtil.isNotEmpty(permissionList)) {
            //if当前用户的角色 in [运营专员, 运营助理] 页面仅展示产品.管理部门 == 当前用户所在部门或下级部门的相关数据
            if (permissionList.contains("E-commerce operation") || permissionList.contains("Operation assistant")) {
                wrapper.and(q -> q.eq("de.dept_id", SecurityUtils.getDeptId())
                        .or()
                        .like("de.ancestors", SecurityUtils.getDeptId()));
            }
        }
        wrapper.groupBy("ai.id");

        wrapper.orderByDesc("ai", AbnormalInfo::getId);
        Page<AbnormalInfoVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        //赋值创建人名称
        UserUtil.assignmentNickName(page.getRecords(), AbnormalInfo::getCreateUser, AbnormalInfoVo::setCreateUserName);
        //赋值跟进人名称
        UserUtil.assignmentNickName(page.getRecords(), AbnormalInfo::getHandleUser, AbnormalInfoVo::setHandleUserName);
        //赋值采购人名称
        UserUtil.assignmentNickName(page.getRecords(), AbnormalInfoVo::getPurchaseUserId, AbnormalInfoVo::setPurchaseUserName);
        return page;
    }

}
