package com.vetti.hotake.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.collection.CollectionUtil;
import com.vetti.common.core.domain.entity.SysDictData;
import com.vetti.common.core.service.BaseServiceImpl;
import com.vetti.common.enums.FillTypeEnum;
import com.vetti.common.utils.DateUtils;
import com.vetti.hotake.domain.HotakeSysFile;
import com.vetti.hotake.domain.dto.HotakeSysNoticeDto;
import com.vetti.hotake.domain.dto.HotakeSysNoticeTypeNameDto;
import com.vetti.hotake.domain.dto.HotakeSysNoticeViewDto;
import com.vetti.hotake.service.IHotakeSysFileService;
import com.vetti.system.service.ISysDictDataService;
import io.swagger.annotations.ApiModelProperty;
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 com.vetti.hotake.mapper.HotakeSysNoticeMapper;
import com.vetti.hotake.domain.HotakeSysNotice;
import com.vetti.hotake.service.IHotakeSysNoticeService;
import org.springframework.web.multipart.MultipartFile;

import static com.vetti.common.utils.SecurityUtils.getLoginUser;

/**
 * 通知Service业务层处理
 *
 * @author ID
 * @date 2025-11-01
 */
@SuppressWarnings("all")
@Service
public class HotakeSysNoticeServiceImpl extends BaseServiceImpl implements IHotakeSysNoticeService {
    @Autowired
    private HotakeSysNoticeMapper hotakeSysNoticeMapper;
    @Autowired
    IHotakeSysFileService hotakeSysFileService;
    @Autowired
    ISysDictDataService sysDictDataService;

    private final String DICT_DATA_TYPE_KEY = "hotake_sys_notice_type";

    private final String MINIO_BUCKET_NAME = "noticetype-fs";//数据字典minio_bucket_name 类型下的

    /**
     * 查询通知
     *
     * @param id 通知主键
     * @return 通知
     */
    @Transactional(readOnly = true)
    @Override
    public HotakeSysNotice selectHotakeSysNoticeById(Long id) {
        return hotakeSysNoticeMapper.selectHotakeSysNoticeById(id);
    }

    /**
     * 查询通知列表
     *
     * @param hotakeSysNotice 通知
     * @return 通知
     */
    @Transactional(readOnly = true)
    @Override
    public List<HotakeSysNotice> selectHotakeSysNoticeList(HotakeSysNotice hotakeSysNotice) {
        return hotakeSysNoticeMapper.selectHotakeSysNoticeList(hotakeSysNotice);
    }

    /**
     * 新增通知
     *
     * @param hotakeSysNotice 通知
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertHotakeSysNotice(List<MultipartFile> files, HotakeSysNotice hotakeSysNotice) {
        fill(FillTypeEnum.INSERT.getCode(), hotakeSysNotice);
        if (CollectionUtil.isNotEmpty(files)) {
            List<String> l = new ArrayList();
            for (MultipartFile file : files) {
                HotakeSysFile sf = hotakeSysFileService.uploadFile(file, MINIO_BUCKET_NAME);
                l.add(sf.getId() + "");
            }
            hotakeSysNotice.setImagePath(l);
        }
        return hotakeSysNoticeMapper.insertHotakeSysNotice(hotakeSysNotice);
    }

    /**
     * 修改通知
     *
     * @param hotakeSysNotice 通知
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateHotakeSysNotice(List<MultipartFile> files, HotakeSysNotice hotakeSysNotice) {
        fill(FillTypeEnum.UPDATE.getCode(), hotakeSysNotice);
        if (CollectionUtil.isNotEmpty(files)) {//传附件就更新，不传不更新，传一个更新之前的所有
            List<String> l = new ArrayList();
            for (MultipartFile file : files) {
                HotakeSysFile sf = hotakeSysFileService.uploadFile(file, MINIO_BUCKET_NAME);
                l.add(sf.getId() + "");
            }
            hotakeSysNotice.setImagePath(l);
        }
        return hotakeSysNoticeMapper.updateHotakeSysNotice(hotakeSysNotice);
    }

    /**
     * 批量删除通知
     *
     * @param ids 需要删除的通知主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteHotakeSysNoticeByIds(Long[] ids) {
        return hotakeSysNoticeMapper.deleteHotakeSysNoticeByIds(ids);
    }

    /**
     * 删除通知信息
     *
     * @param id 通知主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteHotakeSysNoticeById(Long id) {
        return hotakeSysNoticeMapper.deleteHotakeSysNoticeById(id);
    }

    /**
     * 批量新增通知
     *
     * @param hotakeSysNoticeList 通知列表
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int batchInsertHotakeSysNotice(List<HotakeSysNotice> hotakeSysNoticeList) {
        return hotakeSysNoticeMapper.batchInsertHotakeSysNotice(hotakeSysNoticeList);
    }

    @Override
    public HotakeSysNoticeViewDto selectHotakeSysNoticeViewList(HotakeSysNotice hotakeSysNotice) {
        HotakeSysNotice query = new HotakeSysNoticeDto();
        query.setIsDel("0");
        query.setReceiveUserId(getLoginUser().getUserId());
        List<HotakeSysNotice> list = hotakeSysNoticeMapper.selectHotakeSysNoticeList(query);
        if (CollectionUtils.isEmpty(list)) {
            return new HotakeSysNoticeViewDto();
        }
        HotakeSysNoticeViewDto data = new HotakeSysNoticeViewDto();
        List<HotakeSysNoticeDto> unreadList = new ArrayList<>();
        List<HotakeSysNoticeDto> archiveList = new ArrayList<>();
        Map<Long, String> typeNameMap = typeName();
        list.forEach(e -> {
            if ("0".equals(e.getIsView())) {
                unreadList.add(HotakeSysNoticeDto.build(e, typeNameMap, url(e)));
            } else if ("1".equals(e.getIsView())) {
                archiveList.add(HotakeSysNoticeDto.build(e, typeNameMap, url(e)));
            }
        });
        data.setUnreadList(HotakeSysNoticeTypeNameDto.build(unreadList));
        data.setArchiveList(HotakeSysNoticeTypeNameDto.build(archiveList));
        data.setUnreadNum(unreadList.size());
        data.setArchiveNum(archiveList.size());
        return data;
    }

    @Override
    public HotakeSysNoticeDto selectHotakeSysNoticeViewById(Long id) {
        HotakeSysNotice data = hotakeSysNoticeMapper.selectHotakeSysNoticeById(id);
        data.setIsView("1");
        fill(FillTypeEnum.UPDATE.getCode(), data);
        hotakeSysNoticeMapper.updateHotakeSysNotice(data);
        return HotakeSysNoticeDto.build(data, typeName(), url(data));
    }

    @Override
    public int allRead() {
        HotakeSysNotice query = new HotakeSysNoticeDto();
        query.setIsDel("0");
        query.setReceiveUserId(getLoginUser().getUserId());
        List<HotakeSysNotice> list = hotakeSysNoticeMapper.selectHotakeSysNoticeList(query);
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(e -> {
                e.setIsView("1");
                fill(FillTypeEnum.UPDATE.getCode(), e);
                hotakeSysNoticeMapper.updateHotakeSysNotice(e);
            });
        }
        return 1;
    }

    private String url(HotakeSysNotice d) {
        if (CollectionUtils.isNotEmpty(d.getImagePath())) {
            return hotakeSysFileService.url(Long.parseLong(d.getImagePath().get(0)));
        }
        return "";
    }


    private Map<Long, String> typeName() {
        SysDictData query = new SysDictData();
        query.setDictType(DICT_DATA_TYPE_KEY);
        List<SysDictData> list = sysDictDataService.selectDictDataList(query);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        Map<Long, String> map = new HashMap<>();
        list.forEach(e -> map.put(e.getDictCode(), e.getDictLabel()));
        return map;
    }

}
