package com.casic.missiles.service.Impl.workbench;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.casic.missiles.dto.ReturnDTO;
import com.casic.missiles.dto.ReturnUtil;
import com.casic.missiles.dto.workbench.RemindMessageRequest;
import com.casic.missiles.dto.workbench.RemindMessageUpdateRequest;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.mapper.workbench.WorkbenchRemindMessageMapper;
import com.casic.missiles.model.workbench.WorkbenchRemindMessage;
import com.casic.missiles.service.workbench.IWorkbenchRemindMessageService;
import com.casic.missiles.utils.DictCodeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 所有业务消息提醒 服务实现类
 * </p>
 *
 * @author wangpeng
 * @since 2023-02-23
 */
@Service
public class WorkbenchRemindMessageServiceImpl extends ServiceImpl<WorkbenchRemindMessageMapper, WorkbenchRemindMessage> implements IWorkbenchRemindMessageService {
    @Autowired
    private WorkbenchRemindMessageMapper remindMessageMapper;

    @Override
    public Page<WorkbenchRemindMessage> listPage(Page<WorkbenchRemindMessage> page, RemindMessageRequest remindMessageRequest) throws Exception {
        Page<WorkbenchRemindMessage> remindMessagePage = remindMessageMapper.selectPage(page, getWrapper(remindMessageRequest));
        List<WorkbenchRemindMessage> records = remindMessagePage.getRecords();
        for (WorkbenchRemindMessage record : records) {
            DictCodeUtils.convertDictCodeToName(record);
        }
        return remindMessagePage;
    }

    @Override
    public ReturnDTO updateStatus(RemindMessageUpdateRequest updateRequest) {
        WorkbenchRemindMessage remindMessage = new WorkbenchRemindMessage();
        remindMessage.setId(updateRequest.getId());
        remindMessage.setStatus(updateRequest.getStatus());
        if(remindMessageMapper.updateById(remindMessage) > 0){
            return ReturnUtil.success();
        }
        throw new BusinessException(BusinessExceptionEnum.HANDLE_FAILED);
    }

    private QueryWrapper<WorkbenchRemindMessage> getWrapper(RemindMessageRequest request) {
        QueryWrapper<WorkbenchRemindMessage> wrapper = new QueryWrapper<>();
        wrapper.eq(!Objects.isNull(request.getRemindId()), "remind_id", request.getRemindId());
        wrapper.eq(!Objects.isNull(request.getRemindDeptId()), "remind_dept_id", request.getRemindDeptId());
        wrapper.eq(StringUtils.isNotBlank(request.getMessageType()), "message_type", request.getMessageType());
        wrapper.eq(StringUtils.isNotBlank(request.getMessageSourceModule()), "message_source_module", request.getMessageSourceModule());
        wrapper.eq(!Objects.isNull(request.getStatus()),"status", request.getStatus());
        wrapper.apply(StringUtils.isNotBlank(request.getStartTime()),
                "{0} <= DATE_FORMAT(create_time,'%Y-%m-%d')", request.getStartTime().split(" ")[0]);
        wrapper.apply(StringUtils.isNotBlank(request.getEndTime()),
                "{0} >= DATE_FORMAT(create_time,'%Y-%m-%d')", request.getEndTime().split(" ")[0]);
        wrapper.orderByDesc("create_time");
        return wrapper;
    }
}
