/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.dispatch.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
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.tofly.common.core.entity.ResultRespone;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.dispatch.dto.DispatchLogDto;
import com.tofly.dispatch.dto.DispatchLogQuery;
import com.tofly.dispatch.entity.DispatchLog;
import com.tofly.dispatch.service.DispatchLogService;
import com.tofly.dispatch.service.DispatchService;
import com.tofly.dispatch.util.DateTimeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tofly.dispatch.mapper.DispatchLogMapper;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

/**
 * 调度日志记录
 *
 * @author admin
 * @date Thu Nov 10 00:00:00 CST 2022
 */
@Service("dispatchLogService")
public class DispatchLogServiceImpl extends ServiceImpl<DispatchLogMapper, DispatchLog> implements DispatchLogService {
@Autowired
private DispatchService dispatchService;

    @Override
    public boolean saveDispatchLog(DispatchLogDto dispatchLogDto, boolean isAdd) {
        DispatchLog dispatchLog = new DispatchLog();
        BeanUtils.copyProperties(dispatchLogDto, dispatchLog); //类转换
        if (dispatchLog.getDispatchId() != null && dispatchLog.getDispatchId().equals(0)) {
            dispatchLog.setDispatchId(null);
        }
        if (dispatchLog.getDutyPerson() != null && dispatchLog.getDutyPerson().equals(0)) {
            throw new RuntimeException("值班人员参数错误");
        }
        if (isAdd) {
            return super.save(dispatchLog);
        } else {

            DispatchLog dispatchLog1 = super.getById(dispatchLog.getId());
            if (dispatchLog1 == null) {
                throw new RuntimeException("参数id值错误");
            }
            //修改权限控制

            if (!DateTimeUtil.getCurrentDate(DateTimeUtil.DAY_FORMAT).equals(
                    DateTimeUtil.getDateStr(dispatchLog1.getCreateTime(), DateTimeUtil.DAY_FORMAT)
            ) || !dispatchLog1.getCreateUser().equals(SecurityUtils.getUserId())) {
                throw new RuntimeException("只能修改当天的自己新建的调度日志数据");
            }


            return super.updateById(dispatchLog);
        }
    }

    @Override
    public boolean deleteByIds(String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        QueryWrapper<DispatchLog> querySWrapper = new QueryWrapper<>();
        querySWrapper.in("ID", list);
        querySWrapper.ne("CREATE_USER", SecurityUtils.getUserId());
        Integer count_check = this.baseMapper.selectCount(querySWrapper);
        if (count_check > 0) {
            throw new RuntimeException("只能删除自己创建的数据");
        }
        return this.removeByIds(list);
    }

    @Override
    public Page<DispatchLog> getPageData(Page page, DispatchLogQuery query) {

        if (query.getDispatchStartTime() != null && query.getDispatchStartTime() != null) {
            if (query.getDispatchEndTime().getTime() > query.getDispatchEndTime().getTime()) {
                throw new RuntimeException("调度开始时间不能大于结束时间");
            }
        }
        if (query.getStartTime() != null && query.getStartTime() != null) {
            if (query.getStartTime().getTime() > query.getEndTime().getTime()) {
                throw new RuntimeException("开始时间不能大于结束时间");
            }
        }

        return this.baseMapper.getPageData(page, query);
    }

    @Override
    public DispatchLog getById(Long id){
        DispatchLog dispatchLog=this.baseMapper.selectById(id);
        if(dispatchLog.getDispatchId()!=null && dispatchLog.getDispatchId()>0){
            dispatchLog.setDispatchInfo(dispatchService.getById(dispatchLog.getDispatchId()));

        }
        return dispatchLog;
    }
}
