package com.bonus.sharedstation.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.sharedstation.api.DictDataApi;
import com.bonus.sharedstation.api.vo.DictDataInfoDO;
import com.bonus.sharedstation.app.domain.TherapyAppointments;
import com.bonus.sharedstation.web.domain.NoticeInfo;
import com.bonus.sharedstation.web.dto.NoticeInfoDTO;
import com.bonus.sharedstation.web.mapper.NoticeInfoMapper;
import com.bonus.sharedstation.web.service.INoticeInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 问卷调查答案 Service实现
 *
 * @author hyq
 * @date 2024-03-15
 */
@Service
@Transactional(readOnly = true)
@Slf4j
public class NoticeInfoServiceImpl extends ServiceImpl<NoticeInfoMapper, NoticeInfo> implements INoticeInfoService {

    @Resource
    private NoticeInfoMapper noticeInfoMapper;

    @Resource
    private DictDataApi dictDataApi;

    @Override
    @Transactional(readOnly = false)
    public AjaxResult insert(NoticeInfo dto) {
        log.info("新增公告入参:{} ", dto);
        //得到登录人
        dto.setCreatedBy(SecurityUtils.getUsername());
        dto.setCreatedTime(new Date());
        boolean b = save(dto);

        if (!b) {
            return AjaxResult.error("新增公告失败");
        }
        return AjaxResult.success("新增公告成功");
    }

    @Override
    @Transactional(readOnly = false)
    public AjaxResult updateInfoById(NoticeInfo firstAidInfoDTO) {
        log.info("修改 入参: {}", firstAidInfoDTO);

        firstAidInfoDTO.setCreatedBy(SecurityUtils.getUsername());
        firstAidInfoDTO.setCreatedTime(new Date());
        boolean b = updateById(firstAidInfoDTO);
        if (!b) {
            return AjaxResult.error("公告修改失败");
        }
        return AjaxResult.success("公告修改成功");
    }


    @Override
    @Transactional(readOnly = false)
    public AjaxResult delete(NoticeInfoDTO firstAidInfoDTO) {
        return AjaxResult.success(noticeInfoMapper.deleteById(firstAidInfoDTO.getId()));
    }

    /**
     * 查询全部
     *
     * @param firstAidInfoDTO
     * @return
     */
    @Override
    public AjaxResult selectAll(NoticeInfoDTO firstAidInfoDTO) {
        LambdaQueryWrapper<NoticeInfo> queryWrapper = new LambdaQueryWrapper<>();
        // 创建分页对象
        queryWrapper
                .like(NoticeInfo::getNoticeName, firstAidInfoDTO.getNoticeName())
                .eq(NoticeInfo::getPublishStatus, firstAidInfoDTO.getPublishStatus())
                .eq(NoticeInfo::getIsDeleted, firstAidInfoDTO.getIsDeleted())
                .orderByDesc(NoticeInfo::getCreatedTime);     // 降序排序

        List<NoticeInfo> firstAidInfos = noticeInfoMapper.selectList(queryWrapper);
        return AjaxResult.success(firstAidInfos);
    }


    /**
     * 详情
     *
     * @param firstAidInfoDTO
     * @return
     */
    @Override
    public AjaxResult selectInfoById(NoticeInfoDTO firstAidInfoDTO) {
        NoticeInfo noticeInfo = noticeInfoMapper.selectById(firstAidInfoDTO.getId());
        if (StringUtils.isNull(noticeInfo)) {
            return AjaxResult.error("公告不存在");
        }
        List<DictDataInfoDO> dictDataInfoDOList = dictDataApi.getDictionaryInfo("notice_type");
        Map<String, String> noticeTypeMap = dictDataInfoDOList.stream()
                .collect(Collectors.toMap(DictDataInfoDO::getDataCode, DictDataInfoDO::getDataValue));
        List<DictDataInfoDO> parkTypeList = dictDataApi.getDictionaryInfo("park_type");
        Map<String, String> parkTypeMap = parkTypeList.stream()
                .collect(Collectors.toMap(DictDataInfoDO::getDataCode, DictDataInfoDO::getDataValue));
        noticeInfo.setNoticeTypeName(noticeTypeMap.get(noticeInfo.getNoticeType()));


        noticeInfo.setParkTypeName(parkTypeMap.get(noticeInfo.getParkType()));


        return AjaxResult.success(noticeInfo);
    }

    @Override
    public AjaxResult selectDirCode(TherapyAppointments dto) {
        return AjaxResult.success(dictDataApi.getDictionaryInfo("park_type"));
    }

}
