package com.hzs.system.base.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.enums.EFeedbackStatus;
import com.hzs.common.domain.system.base.BdFeedback;
import com.hzs.common.domain.system.ext.BdFeedbackExt;
import com.hzs.system.base.mapper.BdFeedbackMapper;
import com.hzs.system.base.service.IBdFeedbackReplyService;
import com.hzs.system.base.service.IBdFeedbackService;
import com.hzs.system.base.vo.BdFeedbackReplyVO;
import com.hzs.system.base.vo.FeedbackVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 意见反馈 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2023-02-05
 */
@Service
public class BdFeedbackServiceImpl extends ServiceImpl<BdFeedbackMapper, BdFeedback> implements IBdFeedbackService {

    @Autowired
    IBdFeedbackReplyService bdFeedbackReplyService;

    @Override
    public Integer saveFeedback(FeedbackVO feedbackVO) {
        BdFeedback bdFeedback = BeanUtil.copyProperties(feedbackVO, BdFeedback.class);
        bdFeedback.setPkCreator(feedbackVO.getPkMember());
        return baseMapper.insert(bdFeedback);
    }

    @Override
    public Object[] selectFeedbackList(FeedbackVO feedbackVO) {
        FeedbackVO feedback;
        List<FeedbackVO> feedbackVOList = new ArrayList<>();
        List<BdFeedbackExt> feedbackExtList = baseMapper.selectFeedbackList(feedbackVO);
        if (CollUtil.isNotEmpty(feedbackExtList)) {
            for (BdFeedbackExt feedbackExt : feedbackExtList) {
                List<BdFeedbackReplyVO> bdFeedbackReplyVOList = bdFeedbackReplyService.selectFeedbackReplyById(feedbackExt.getPkId(), feedbackExt.getPkCountry());
                feedback = BeanUtil.copyProperties(feedbackExt, FeedbackVO.class);
                feedback.setFeedbackReplyList(bdFeedbackReplyVOList);
                feedbackVOList.add(feedback);
            }
        }
        Object[] objects = new Object[]{feedbackExtList, feedbackVOList};
        return objects;
    }

    @Override
    public Integer deleteById(FeedbackVO feedbackVO) {
        return baseMapper.deleteById(feedbackVO);

    }

    @Override
    public FeedbackVO showFeedbackVOById(Long pkId) {
        FeedbackVO feedbackVO = BeanUtil.copyProperties(baseMapper.showFeedbackVOById(pkId), FeedbackVO.class);
        List<BdFeedbackReplyVO> bdFeedbackReplyVOList = bdFeedbackReplyService.selectFeedbackReplyById(feedbackVO.getPkId(), feedbackVO.getPkCountry());
        feedbackVO.setFeedbackReplyList(bdFeedbackReplyVOList);
        return feedbackVO;
    }

    @Override
    public Boolean updateFeedbackById(Long pkMember, Long pkId) {
        UpdateWrapper<BdFeedback> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("STATUS", EFeedbackStatus.FEEDBACK_PROVIDED.getValue());
        updateWrapper.set(SystemFieldConstants.MODIFIED_TIME, new Date());
        updateWrapper.set(SystemFieldConstants.PK_MODIFIED, pkMember);
        updateWrapper.eq("PK_ID", pkId);
        return update(updateWrapper);
    }

    @Override
    public Integer selectIndexFeedbackCount(Integer pkCountry,Long pkMember) {
        return baseMapper.selectIndexFeedbackCount(pkCountry,pkMember);
    }


}
