package com.dayou.service.impl;

import com.dayou.common.BaseEntity;
import com.dayou.entity.BusinessReply;
import com.dayou.mapper.BusinessReplyMapper;
import com.dayou.message.bo.BusinessMessageBO;
import com.dayou.service.IBusinessReplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayou.service.IMessageService;
import com.dayou.utils.LoginContext;
import com.dayou.vo.BusinessReplyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dayou.utils.ExcelUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.ArrayList;
import org.springframework.transaction.annotation.Transactional;
import com.dayou.enums.BatchTaskTypeEnum;

import static com.dayou.message.MessageTitle.BUSINESS_MESSAGE_TITLE;

/**
 * <p>
 * 业务回复表 服务实现类
 * </p>
 *
 * @author wucl
 * @since 2023-03-23
 */
@Service
public class BusinessReplyServiceImpl extends ServiceImpl<BusinessReplyMapper, BusinessReply> implements IBusinessReplyService {

    @Autowired
    private BusinessReplyMapper businessReplyMapper;

    @Autowired
    private IMessageService messageService;

    @Override
    @SuppressWarnings("unchecked")
    public Page<BusinessReplyVO> selectPage(Page page, BusinessReply businessReply){
        Long userId = LoginContext.getUserId();
        businessReplyMapper.updateState(userId,businessReply.getBizTableId(),businessReply.getBizType());
        return businessReplyMapper.getPage(page,businessReply);
    }


    @Override
    public BusinessReply detail(Long id){
        this.update(new LambdaUpdateWrapper<BusinessReply>().set(BusinessReply::getState,1).eq(BaseEntity::getId,id));
        return this.getById(id);
    }

    @Transactional
    @Override
    public Boolean reply(BusinessReply businessReply){
        this.save(businessReply);
        messageService.packagingBusinessMessage(businessReply);
        return  Boolean.TRUE;
    }

    @Override
    public Boolean update(BusinessReply businessReply){
        return  this.updateById(businessReply);
    }

    @Override
    public Boolean delete(Long id){
        //逻辑删除
        return this.removeById(id);
    }

    @Override
    public Integer getNotReadCount(BusinessReply businessReply) {
        Integer count = this.count(new LambdaQueryWrapper<BusinessReply>().eq(BusinessReply::getBizType, businessReply.getBizType())
                .eq(BusinessReply::getBizTableId, businessReply.getBizTableId()).eq(BusinessReply::getReceiverId, businessReply.getReceiverId())
                        .eq(BusinessReply::getState, Boolean.FALSE).eq(BusinessReply::getDeleted, Boolean.FALSE));
        return count;
    }
}
