package com.usky.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.fire.domain.PostInspect;
import com.usky.fire.mapper.PostInspectMapper;
import com.usky.fire.service.BaseUserCompanyService;
import com.usky.fire.service.PostInspectService;
import com.usky.fire.service.util.OnlineMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 查岗信息 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-08-24
 */
@Service
public class PostInspectServiceImpl extends AbstractCrudService<PostInspectMapper, PostInspect> implements PostInspectService {

    @Autowired
    private BaseUserCompanyService baseUserCompanyService;

    @Override
    public void postInspectRecept(PostInspect postInspect) {
        this.save(postInspect);
    }

    @Override
    public List<PostInspect> postInspectPlatform() {
        List<PostInspect> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<String> companyIdList = baseUserCompanyService.companyIdList();
        if (CollectionUtils.isNotEmpty(companyIdList)) {
            Date date = new Date();
            String startTime = OnlineMethod.minuteDateTime(new Date(), -10);
            String endTime = sdf.format(date);
            LambdaQueryWrapper<PostInspect> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.in(PostInspect::getCompanyId, companyIdList)
                    .between(PostInspect::getInspectTime, startTime, endTime)
                    .eq(PostInspect::getInspectStatus, 0);
            list = this.list(queryWrapper);
            for (int i = 0; i < list.size(); i++) {
                long data1 = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
                long data2 = list.get(i).getInspectTime().toEpochSecond(ZoneOffset.of("+8"));
                long time = data1 - data2;
                list.get(i).setCountdown(600 - time);
            }
        }
        return list;
    }


    @Override
    public List<PostInspect> postInspectAnswerRecept() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String startTime = OnlineMethod.minuteDateTime(new Date(), -5);
        String endTime = sdf.format(date);
        LambdaQueryWrapper<PostInspect> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.between(PostInspect::getInspectTime, startTime, endTime);
        List<PostInspect> list = this.list(queryWrapper);
        for (int i = 0; i < list.size(); i++) {
            long data1 = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
            long data2 = list.get(i).getInspectTime().toEpochSecond(ZoneOffset.of("+8"));
            long time = data1 - data2;
            if (list.get(i).getInspectStatus() == 0) {
                if (time < 600) {
                    list.get(i).setCountdown(600 - time);
                }
            }
        }
        return list;
    }

    @Override
    public void postInspectAnswerUpdate(PostInspect postInspect) {
        LambdaQueryWrapper<PostInspect> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PostInspect::getId, postInspect.getId());
        List<PostInspect> postInspectMap = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(postInspectMap)) {
            long data1 = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
            long data2 = postInspectMap.get(0).getInspectTime().toEpochSecond(ZoneOffset.of("+8"));
            long time = data1 - data2;
            postInspect.setSpentTime((int) time);
            postInspect.setReplyTime(LocalDateTime.now());
            postInspect.setUpdatePerson(SecurityUtils.getUsername());
            postInspect.setUpdateTime(LocalDateTime.now());
            this.updateById(postInspect);
        }
    }

    @Override
    public Integer postInspectCount(String companyId, Integer inspectStatus) {
        LambdaQueryWrapper<PostInspect> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PostInspect::getCompanyId, companyId)
                .eq(inspectStatus != null, PostInspect::getInspectStatus, inspectStatus);
        int count = this.count(queryWrapper);
        return count;
    }

}
