package com.storlead.sales.service.impl.customer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.storlead.framework.common.constant.CommonConstant;
import com.storlead.framework.common.result.Result;
import com.storlead.framework.mybatis.entity.SysBaseField;
import com.storlead.framework.mybatis.service.impl.MyBaseServiceImpl;
import com.storlead.sales.pojo.dto.customer.DynamicLogSearchDTO;
import com.storlead.sales.pojo.entity.customer.DynamicLogEntity;
import com.storlead.sales.pojo.vo.customer.DynamicLogVO;
import com.storlead.sales.mapper.customer.DynamicLogMapper;
import com.storlead.sales.service.customer.DynamicLogService;
import com.storlead.user.pojo.entity.UserEntity;
import com.storlead.user.service.IUserService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 工单动态表 服务实现类
 * </p>
 *
 * @author YPZ
 * @since 2024-04-29
 */
@Service
public class DynamicLogServiceImpl extends MyBaseServiceImpl<DynamicLogMapper, DynamicLogEntity> implements DynamicLogService {

    @Resource
    private IUserService userService;
    @Override
    public Result<IPage<DynamicLogVO>> selectListVO(DynamicLogSearchDTO dynamicLogSearchDTO) {

        IPage<DynamicLogVO> pageVO = new Page<>(dynamicLogSearchDTO.getPageIndex(), dynamicLogSearchDTO.getPageSize());
        IPage<DynamicLogEntity> page = new Page<>(dynamicLogSearchDTO.getPageIndex(), dynamicLogSearchDTO.getPageSize());
        LambdaQueryWrapper<DynamicLogEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DynamicLogEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        wrapper.eq(DynamicLogEntity::getResourceType, dynamicLogSearchDTO.getResourceType());
        wrapper.eq(DynamicLogEntity::getRecordId, dynamicLogSearchDTO.getRecordId());
        page = page(page, wrapper);
        if (CollectionUtils.isEmpty(page.getRecords())){
            return Result.result(pageVO);
        }
//        List<CustomerListVO> customerListVO = new ArrayList<>();
        //复制list给VO类
        pageVO = SysBaseField.page(page, DynamicLogVO.class);
        SysBaseField.dictFileds(pageVO.getRecords());
        SysBaseField.orgFileds(pageVO.getRecords());
        //pageVO中加入用户头像和名字信息
        List<Long> createByList = pageVO.getRecords().stream().map(DynamicLogVO::getCreateBy).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(createByList)){
            Map<Long, UserEntity> createByMap = userService.IdListToUserMap(createByList);

            pageVO.getRecords().forEach(item->{
                item.setAvatar(createByMap.get(item.getCreateBy()).getAvatar());
                item.setUserName(createByMap.get(item.getCreateBy()).getRealName());
            });
        }
        return Result.result(pageVO);
    }
}
