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

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.meter.TrainLogAddRequest;
import com.casic.missiles.dto.meter.TrainLogDetailResponse;
import com.casic.missiles.dto.meter.TrainLogListRequest;
import com.casic.missiles.dto.meter.TrainLogUpdateRequest;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.enums.PrefixCodeEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.mapper.meter.MeterTrainLogMapper;
import com.casic.missiles.mapper.meter.MeterTrainStaffMapper;
import com.casic.missiles.model.meter.MeterTrainLog;
import com.casic.missiles.service.meter.IMeterTrainLogService;
import com.casic.missiles.utils.ConvertUtils;
import com.casic.missiles.utils.NumberGeneratorUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 计量管理-培训管理-培训计录表 服务实现类
 * </p>
 *
 * @author wangpeng
 * @since 2022-11-26
 */
@Service
public class MeterTrainLogServiceImpl extends ServiceImpl<MeterTrainLogMapper, MeterTrainLog> implements IMeterTrainLogService {
    @Autowired
    private MeterTrainLogMapper trainLogMapper;
    @Autowired
    private MeterTrainStaffMapper trainStaffMapper;

    @Override
    public Page<MeterTrainLog> listPage(Page<MeterTrainLog> page, TrainLogListRequest request) {
        QueryWrapper<MeterTrainLog> wrapper = getWrapper(request);
        Page<MeterTrainLog> trainLogPage = trainLogMapper.selectPage(page, wrapper);
        return trainLogPage;
    }

    @Override
    public List<MeterTrainLog> logList(TrainLogListRequest request) {
        QueryWrapper<MeterTrainLog> wrapper = getWrapper(request);
        List<MeterTrainLog> trainLogs = trainLogMapper.selectList(wrapper);
        return trainLogs;
    }

    @Override
    @Transactional
    public ReturnDTO addTrainLog(TrainLogAddRequest request) {
        //生成编号
        Long maxNo = trainLogMapper.selectMaxTrainLogNo();
        String logNo = NumberGeneratorUtil.getContactNo(PrefixCodeEnum.TRAIN_LOG_PREFIX, maxNo);

        MeterTrainLog meterTrainLog = ConvertUtils.sourceToTarget(request, MeterTrainLog.class);
        meterTrainLog.setLogNo(logNo);
        int insertFlag = trainLogMapper.insert(meterTrainLog);
        //更新培训计划员工考核结果
        int updateFlag = trainStaffMapper.updateForStaff(request.getTrainStaffList());
        if(insertFlag > 0 && updateFlag > 0){
            return ReturnUtil.success();
        }
        return ReturnUtil.failed("新建失败");
    }

    @Override
    @Transactional
    public ReturnDTO updateTrainLog(TrainLogUpdateRequest request) {
        MeterTrainLog meterTrainLog = ConvertUtils.sourceToTarget(request, MeterTrainLog.class);
        int insertFlag = trainLogMapper.updateById(meterTrainLog);
        //更新培训计划员工考核结果
        int updateFlag = trainStaffMapper.updateForStaff(request.getTrainStaffList());
        if(insertFlag > 0 && updateFlag > 0){
            return ReturnUtil.success();
        }
        throw new BusinessException(BusinessExceptionEnum.HANDLE_FAILED);
    }

    @Override
    public ReturnDTO<TrainLogDetailResponse> trainLogDetail(Long id) {
        TrainLogDetailResponse logResponse = trainLogMapper.selectTrainLogById(id);
        return ReturnUtil.success(logResponse);
    }

    private QueryWrapper<MeterTrainLog> getWrapper(TrainLogListRequest request) {
        QueryWrapper<MeterTrainLog> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(request.getLogNo()), "log_no", request.getLogNo());
        wrapper.like(StringUtils.isNotBlank(request.getTrainName()), "train_name", request.getTrainName());
        wrapper.apply(StringUtils.isNotBlank(request.getTrainStartTime()),//apply方法传入参数名不能为空
                "{0} <= DATE_FORMAT(train_time,'%Y-%m-%d')", request.getTrainStartTime().split(" ")[0]);
        wrapper.apply(StringUtils.isNotBlank(request.getTrainEndTime()),
                "{0} >= DATE_FORMAT(train_time,'%Y-%m-%d')", request.getTrainEndTime().split(" ")[0]);
        wrapper.apply(StringUtils.isNotBlank(request.getCreateStartTime()),
                "{0} <= DATE_FORMAT(create_time,'%Y-%m-%d')", request.getCreateStartTime().split(" ")[0]);
        wrapper.apply(StringUtils.isNotBlank(request.getCreateEndTime()),
                "{0} >= DATE_FORMAT(create_time,'%Y-%m-%d')", request.getCreateEndTime().split(" ")[0]);
        wrapper.eq("is_del", 0);
        return wrapper;
    }
}
