package com.fdkankan.platform.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.constant.ServerCode;
import com.fdkankan.common.constant.TbStatus;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.common.validation.ValidationUtils;
import com.fdkankan.platform.agent.request.RequestNote;
import com.fdkankan.platform.agent.request.RequestSearch;
import com.fdkankan.platform.user.entity.IntercomMessage;
import com.fdkankan.platform.user.mapper.IIntercomMessageMapper;
import com.fdkankan.platform.user.request.RequestMessage;
import com.fdkankan.platform.user.service.IIntercomMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 咨询留言表 服务实现类
 * </p>
 *
 * @author 
 * @since 2021-12-24
 */
@Service
public class IntercomMessageServiceImpl extends ServiceImpl<IIntercomMessageMapper, IntercomMessage> implements IIntercomMessageService {

    @Override
    public String saveByParam(RequestMessage message) {
        if (StringUtils.isEmpty(message.getContact()) || StringUtils.isEmpty(message.getContent())){
            throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
        }
        IntercomMessage interComMessage = new IntercomMessage();
        interComMessage.setContent(message.getContent());
        interComMessage.setContact(message.getContact());
        interComMessage.setCreateTime(new Date());
        interComMessage.setUpdateTime(new Date());
        interComMessage.setNoteType(1);
        interComMessage.setState(1);
        if(!this.save(interComMessage)){
            throw new BusinessException(ErrorCode.ERROR_MSG);
        }
        return ServerCode.SUCCESS.message();
    }

    @Override
    public List<IntercomMessage> getByEmail(String email) {
        if (StringUtils.isEmpty(email)){
            throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
        }
        LambdaQueryWrapper<IntercomMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IntercomMessage::getTbStatus, TbStatus.VALID.code());
        queryWrapper.eq(IntercomMessage::getEmail,email);
        queryWrapper.orderByDesc(IntercomMessage::getCreateTime);
        return this.list(queryWrapper);
    }

    @Override
    public String updateActive(Long id, String state) {
        IntercomMessage messageEntity = this.getById(id);
        int iState = Integer.parseInt(state);
        if (messageEntity == null || !ValidationUtils.validateState(iState)) {
            throw new BusinessException(ErrorCode.NOT_RECORD);
        }
        messageEntity.setState(iState);
        if(!this.updateById(messageEntity)){
            throw new BusinessException(ErrorCode.ERROR_MSG);
        }
        return ServerCode.SUCCESS.message();
    }

    @Override
    public String updateNote(RequestNote param) {
        IntercomMessage messageEntity = this.getById(param.getId());
        if (messageEntity == null ) {
            throw new BusinessException(ErrorCode.NOT_RECORD);
        }
        if (!ValidationUtils.validateInteger(param.getNoteType())){
            throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
        }
        messageEntity.setNoteType(param.getNoteType());
        messageEntity.setNoteContent(param.getNoteContent());
        if(!this.updateById(messageEntity)){
            throw new BusinessException(ErrorCode.ERROR_MSG);
        }
        return ServerCode.SUCCESS.message();
    }

    @Override
    public Page<IntercomMessage> searchLike(RequestSearch param) {
        Page<IntercomMessage> page = new Page<>(param.getPageNum(), param.getPageSize());
        QueryWrapper<IntercomMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().ge(IntercomMessage::getTbStatus,TbStatus.VALID.code());
        if(StringUtils.isNotBlank(param.getStartDate())){
            queryWrapper.lambda().ge(IntercomMessage::getCreateTime,param.getStartDate());
        }
        if(StringUtils.isNotBlank(param.getEndDate())){
            queryWrapper.lambda().le(IntercomMessage::getCreateTime,param.getStartDate());
        }
        if(StringUtils.isNotBlank(param.getItemName())){
            queryWrapper.lambda().like(IntercomMessage::getContact,param.getItemName())
            .or().like(IntercomMessage::getNoteContent,param.getItemName());
        }
        return this.page(page,queryWrapper);
    }
}
