package com.yiqi.system.service.impl;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiqi.common.core.domain.entity.SysUser;
import com.yiqi.common.enums.SourceType;
import com.yiqi.common.enums.UserStatus;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.system.domain.SysNoticeOrgRelation;
import com.yiqi.system.domain.dto.SysNoticeDTO;
import com.yiqi.system.domain.vo.SysNoticeWorkbenchVO;
import com.yiqi.system.service.ISysNoticeOrgRelationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiqi.system.domain.SysNotice;
import com.yiqi.system.mapper.SysNoticeMapper;
import com.yiqi.system.service.ISysNoticeService;

/**
 * 公告 服务层实现
 *
 * @author ruoyi
 */
@Service
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNotice>  implements ISysNoticeService {

    @Autowired
    private ISysNoticeOrgRelationService sysNoticeOrgRelationService;



    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNoticeDTO selectNoticeById(Long noticeId) {
        SysNoticeDTO sysNoticeDTO = new SysNoticeDTO();
        SysNotice sysNotice = baseMapper.selectNoticeById(noticeId);
        BeanUtils.copyProperties(sysNotice, sysNoticeDTO);

        List<SysNoticeOrgRelation> list = sysNoticeOrgRelationService.list(new QueryWrapper<SysNoticeOrgRelation>().lambda()
                                                                .eq(SysNoticeOrgRelation::getNoticeId, noticeId));
        if (CollUtil.isNotEmpty(list)){
            sysNoticeDTO.setRelationList(list);
        }
        return sysNoticeDTO;
    }

    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice) {
        return baseMapper.selectNoticeList(notice);
    }

    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeListByWorkbench(SysNoticeWorkbenchVO notice) {
        notice.setSourceType(SecurityUtils.getLoginUser().getUser().getUserType());
        if (SourceType.STORE.getCode().equals(notice.getSourceType())){
            notice.setOrgId(SecurityUtils.getLoginUser().getUser().getStoreId());
        }
        if (SourceType.FACTORY.getCode().equals(notice.getSourceType())){
            notice.setOrgId(SecurityUtils.getLoginUser().getUser().getFactoryId());
        }
        return baseMapper.selectNoticeListByWorkbench(notice);
    }

    /**
     * 新增公告
     *
     * @param noticeDTO 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(SysNoticeDTO noticeDTO) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        SysNotice sysNotice = new SysNotice();
        sysNotice.setNoticeContent(noticeDTO.getNoticeContent());
        sysNotice.setNoticeTitle(noticeDTO.getNoticeTitle());
        sysNotice.setNoticeType(noticeDTO.getNoticeType());
        sysNotice.setStatus(UserStatus.OK.getCode());
        sysNotice.setCreateBy(user.getUserName());
        sysNotice.setCreateTime(DateUtils.getNowDate());
        this.save(sysNotice);

        if (CollUtil.isNotEmpty(noticeDTO.getRelationList())){
            Long noticeId = sysNotice.getNoticeId();
            noticeDTO.getRelationList().forEach(relation -> {
                relation.setNoticeId(noticeId);
                relation.setCreateBy(user.getUserName());
                relation.setCreateById(user.getUserId());
                relation.setCreateTime(DateUtils.getNowDate());
            });
            sysNoticeOrgRelationService.saveBatch(noticeDTO.getRelationList());
        }
        return 1;
    }

    /**
     * 修改公告
     *
     * @param noticeDTO 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNoticeDTO noticeDTO) {
        SysNotice sysNotice = new SysNotice();
        BeanUtils.copyProperties(noticeDTO, sysNotice);
        baseMapper.updateNotice(sysNotice);

        sysNoticeOrgRelationService.remove(new QueryWrapper<SysNoticeOrgRelation>().lambda()
                .eq(SysNoticeOrgRelation::getNoticeId, sysNotice.getNoticeId()));

        if (CollUtil.isNotEmpty(noticeDTO.getRelationList())){
            noticeDTO.getRelationList().forEach(relation -> {
                relation.setNoticeId(noticeDTO.getNoticeId());
                relation.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserName());
                relation.setCreateById(SecurityUtils.getLoginUser().getUser().getUserId());
                relation.setCreateTime(DateUtils.getNowDate());
            });
            sysNoticeOrgRelationService.saveBatch(noticeDTO.getRelationList());
        }
        return 1;
    }

    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long noticeId) {

        sysNoticeOrgRelationService.remove(new QueryWrapper<SysNoticeOrgRelation>().lambda()
                .eq(SysNoticeOrgRelation::getNoticeId, noticeId));

        return baseMapper.deleteNoticeById(noticeId);
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Long[] noticeIds) {
        sysNoticeOrgRelationService.remove(new QueryWrapper<SysNoticeOrgRelation>().lambda()
                .in(SysNoticeOrgRelation::getNoticeId, Arrays.asList(noticeIds)));
        return baseMapper.deleteNoticeByIds(noticeIds);
    }
}
