package com.foreverwin.mesnac.quality.service.impl;

import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.FrontPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.quality.model.SelfReport;
import com.foreverwin.mesnac.quality.mapper.SelfReportMapper;
import com.foreverwin.mesnac.quality.service.SelfReportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.visiprise.common.exception.BaseException;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
/**
 * <p>
 * 自报代办事项 服务实现类
 * </p>
 *
 * @author Philip
 * @since 2021-06-17
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SelfReportServiceImpl extends ServiceImpl<SelfReportMapper, SelfReport> implements SelfReportService {


    @Autowired
    private SelfReportMapper selfReportMapper;

    static final String STATE_NEW="NEW";

    static final String STATE_CONFIRM="CONF";

    static final String STATE_REJECT="REJ";

    @Override
    public IPage<SelfReport> selectPage(FrontPage<SelfReport> frontPage, SelfReport selfReport) {
        QueryWrapper<SelfReport> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(selfReport);
        return super.page(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<SelfReport> selectList(SelfReport selfReport) {
        QueryWrapper<SelfReport> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(selfReport);
        return super.list(queryWrapper);
    }
    @Override
    public List<SelfReport> selectList(QueryWrapper<SelfReport> queryWrapper, String locale, LocalDate startTime, LocalDate endTime) {
        return selfReportMapper.selectList(queryWrapper,locale,startTime,endTime);
    }

    @Override
    public Object reject(String handle) {
        SelfReport selfReport = getById(handle);
        if (!selfReport.getState().equals(STATE_NEW)){
            throw new BaseException("状态不为新建，请重新检索");
        }
        selfReport.setState(STATE_REJECT);
        selfReport.setModifiedDateTime(LocalDateTime.now());
        selfReport.setModifyUser(CommonMethods.getUser());
        return updateById(selfReport);
    }


}