package com.ruoyi.zhdd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.zhdd.domain.Incident;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhdd.domain.bo.DutyReportBo;
import com.ruoyi.zhdd.domain.vo.DutyReportVo;
import com.ruoyi.zhdd.domain.DutyReport;
import com.ruoyi.zhdd.mapper.DutyReportMapper;
import com.ruoyi.zhdd.service.IDutyReportService;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 值班报告信息Service业务层处理
 *
 * @author xintong
 * @date 2021-10-11
 */
@Service
public class DutyReportServiceImpl extends ServicePlusImpl<DutyReportMapper, DutyReport, DutyReportVo> implements IDutyReportService {

    @Override
    public DutyReportVo queryById(String id) {
        return getVoById(id);
    }

    @Override
    public TableDataInfo<DutyReportVo> queryPageList(DutyReportBo bo) {
        PagePlus<DutyReport, DutyReportVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<DutyReportVo> queryList(DutyReportBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<DutyReport> buildQueryWrapper(DutyReportBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DutyReport> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getReportContent()), DutyReport::getReportContent, bo.getReportContent());
        lqw.eq(bo.getStatus() != null, DutyReport::getStatus, bo.getStatus());
        if (params.get("beginTime") != null && params.get("endTime") != null) {
            lqw.between(DutyReport::getDutyDate, Timestamp.valueOf(Convert.toStr(params.get("beginTime"))+ " 00:00:00"), Timestamp.valueOf(Convert.toStr(params.get("endTime")) + " 23:59:59"));
        }
        lqw.orderByDesc(DutyReport::getUpdateTime);
        return lqw;
    }

    @Override
    public Boolean insertByBo(DutyReportBo bo) {
        DutyReport add = BeanUtil.toBean(bo, DutyReport.class);
        validEntityBeforeSave(add);
        add.setDutyDate(new Date());
        return save(add);
    }

    @Override
    public Boolean updateByBo(DutyReportBo bo) {
        // 如果是已提交则不能修改
        DutyReportVo dutyReportVo = this.queryById(bo.getId());
        if (dutyReportVo.getStatus() == 2) {
            throw new ServiceException("报告已提交不能修改");
        }
        DutyReport update = BeanUtil.toBean(bo, DutyReport.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(DutyReport entity) {
        //TODO 做一些数据校验,如唯一约束
        if (entity.getStatus() != 1 && entity.getStatus() != 2) {
            throw new ServiceException("状态值非法-" + entity.getStatus());
        }
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
