package com.ruoyi.xq.service.impl;

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.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.xq.domain.Dynamic;
import com.ruoyi.xq.domain.DynamicImage;
import com.ruoyi.xq.domain.User;
import com.ruoyi.xq.domain.UserInfoAudit;
import com.ruoyi.xq.dto.admin.dynamic.DynamicAdminVo;
import com.ruoyi.xq.dto.app.dynamic.AddDynamicReq;
import com.ruoyi.xq.dto.app.dynamic.DynamicListVo;
import com.ruoyi.xq.dto.app.dynamic.DynamicQuery;
import com.ruoyi.xq.enums.common.AuditEnum;
import com.ruoyi.xq.enums.user.UserInfoAuditTypeEnum;
import com.ruoyi.xq.mapper.DynamicMapper;
import com.ruoyi.xq.service.DynamicImageService;
import com.ruoyi.xq.service.DynamicService;
import com.ruoyi.xq.service.UserService;
import com.ruoyi.xq.util.AgeUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 动态审核Service业务层处理
 *
 * @author 77
 * @date 2024-03-04
 */
@RequiredArgsConstructor
@Service
public class DynamicServiceImpl extends ServiceImpl<DynamicMapper,Dynamic> implements DynamicService {

    @Autowired
    private UserService userService;
    @Autowired
    private DynamicImageService dynamicImageService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void push(AddDynamicReq req) {
        User user = userService.getById(req.getUserId());
        Dynamic dynamic = new Dynamic();
        dynamic.setUserId(user.getId());
        dynamic.setUsercode(user.getUsercode());
        dynamic.setContent(req.getContent());
        dynamic.setAuditStatus(AuditEnum.AUDITING.getCode());
        this.save(dynamic);
        List<String> imagesList = req.getImageList();
        for (String image : imagesList) {
            DynamicImage dynamicImage = new DynamicImage();
            dynamicImage.setUserId(user.getId());
            dynamicImage.setUsercode(user.getUsercode());
            dynamicImage.setDynamicId(dynamic.getId());
            dynamicImage.setUrl(image);
            dynamicImageService.save(dynamicImage);
        }
    }

    @Override
    public Page<DynamicListVo> pageApp(PageQuery pageQuery, DynamicQuery dynamicQuery) {
        Page<DynamicListVo> page = baseMapper.pageApp(pageQuery.build(), dynamicQuery);
        List<DynamicListVo> records = page.getRecords();
        if(CollectionUtils.isEmpty(records)){
            return page;
        }
        List<Long> dynamicIds = records.stream().map(DynamicListVo::getId).collect(Collectors.toList());
        List<DynamicImage> list = dynamicImageService.list(Wrappers.lambdaQuery(DynamicImage.class).in(DynamicImage::getDynamicId, dynamicIds));
        Map<Long, List<DynamicImage>> map = list.stream().collect(Collectors.groupingBy(DynamicImage::getDynamicId));
        for (DynamicListVo record : records) {
            record.setAge(AgeUtil.getAge(record.getBirthday()));
            List<DynamicImage> images = map.get(record.getId());
            if(images == null){
                images = new ArrayList<>();
            }
            record.setImageList(images.stream().map(DynamicImage::getUrl).collect(Collectors.toList()));
        }
        return page;
    }

    @Override
    public Page<DynamicAdminVo> pageAdmin(PageQuery pageQuery, DynamicAdminVo bo) {
        Page<DynamicAdminVo> page = baseMapper.pageAdmin(pageQuery.build(), bo);
        List<DynamicAdminVo> records = page.getRecords();
        if(CollectionUtils.isEmpty(records)){
            return page;
        }
        List<Long> dynamicIds = records.stream().map(DynamicAdminVo::getId).collect(Collectors.toList());
        List<DynamicImage> list = dynamicImageService.list(Wrappers.lambdaQuery(DynamicImage.class).in(DynamicImage::getDynamicId, dynamicIds));
        Map<Long, List<DynamicImage>> map = list.stream().collect(Collectors.groupingBy(DynamicImage::getDynamicId));
        for (DynamicAdminVo record : records) {
            List<DynamicImage> images = map.get(record.getId());
            if(images == null){
                images = new ArrayList<>();
            }
            record.setImageList(images.stream().map(DynamicImage::getUrl).collect(Collectors.toList()));
        }
        return page;
    }

    @Override
    public void auditSuccess(String ids) {
        String[] idArray = ids.split(",");
        for (String id : idArray) {
            this.update(Wrappers.lambdaUpdate(Dynamic.class)
                    .eq(Dynamic::getId, id)
                    .eq(Dynamic::getAuditStatus, AuditEnum.AUDITING.getCode())
                    .set(Dynamic::getAuditStatus, AuditEnum.SUCCESS.getCode())
                    .set(Dynamic::getAuditIp, ServletUtils.getClientIP())
                    .set(Dynamic::getAuditOpId, LoginHelper.getUserId())
                    .set(Dynamic::getAuditOpName, LoginHelper.getUsername())
                    .set(Dynamic::getAuditTime, LocalDateTime.now())
                    .set(Dynamic::getAuditRemark, "审核成功"));
        }
    }

    @Override
    public void auditFail(Long id, String auditRemark) {
        this.update(Wrappers.lambdaUpdate(Dynamic.class)
                .eq(Dynamic::getId, id)
                .eq(Dynamic::getAuditStatus, AuditEnum.AUDITING.getCode())
                .set(Dynamic::getAuditStatus, AuditEnum.FAIL.getCode())
                .set(Dynamic::getAuditIp, ServletUtils.getClientIP())
                .set(Dynamic::getAuditOpId, LoginHelper.getUserId())
                .set(Dynamic::getAuditOpName, LoginHelper.getUsername())
                .set(Dynamic::getAuditTime, LocalDateTime.now())
                .set(Dynamic::getAuditRemark, auditRemark));
    }
}
