package com.ruoyi.project.check.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.base.domain.TBasePoint;
import com.ruoyi.project.base.mapper.TBasePointMapper;
import com.ruoyi.project.check.domain.TCheckRepairpoints;
import com.ruoyi.project.check.mapper.TCheckRepairpointsMapper;
import com.ruoyi.project.task.domain.TTaskInspection;
import com.ruoyi.project.task.mapper.TTaskInspectionMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.check.mapper.TCheckCheckpointsMapper;
import com.ruoyi.project.check.domain.TCheckCheckpoints;
import com.ruoyi.project.check.service.ITCheckCheckpointsService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 检测点Service业务层处理
 *
 * @author ruoyi
 * @date 2022-12-01
 */
@Service
public class TCheckCheckpointsServiceImpl implements ITCheckCheckpointsService
{
    @Resource
    private TCheckCheckpointsMapper tCheckCheckpointsMapper;

    @Resource
    private TCheckRepairpointsMapper tCheckRepairpointsMapper;

    @Resource
    private TBasePointMapper tBasePointMapper;

    @Resource
    private TTaskInspectionMapper tTaskInspectionMapper;

    /**
     * 查询检测点
     *
     * @param checkId 检测点主键
     * @return 检测点
     */
    @Override
    public TCheckCheckpoints selectTCheckCheckpointsByCheckId(Long checkId)
    {
        return tCheckCheckpointsMapper.selectTCheckCheckpointsByCheckId(checkId);
    }

    /**
     * 查询检测点列表
     *
     * @param tCheckCheckpoints 检测点
     * @return 检测点
     */
    @Override
    public List<TCheckCheckpoints> selectTCheckCheckpointsList(TCheckCheckpoints tCheckCheckpoints)
    {
        return tCheckCheckpointsMapper.selectTCheckCheckpointsList(tCheckCheckpoints);
    }

    @Override
    public TCheckCheckpoints selectTCheckCheckpointByEntity(TCheckCheckpoints tCheckCheckpoints)
    {
        return tCheckCheckpointsMapper.selectTCheckCheckpointByEntity(tCheckCheckpoints);
    }

    @Override
    public List<TCheckCheckpoints> selectPointsByIds(Long[] checkIds)
    {
        return tCheckCheckpointsMapper.selectPointsByIds(checkIds);
    }

    /**
     * 新增检测点
     *
     * @param tCheckCheckpoints 检测点
     * @return 结果
     */
    @Override
    public int insertTCheckCheckpoints(TCheckCheckpoints tCheckCheckpoints)
    {
        return tCheckCheckpointsMapper.insertTCheckCheckpoints(tCheckCheckpoints);
    }
    @Override
    public int insertTCheckCheckpointsByList(List<TCheckCheckpoints> tCheckCheckpoints)
    {
        return tCheckCheckpointsMapper.insertTCheckCheckpointsByList(tCheckCheckpoints);
    }

    /**
     * 修改检测点
     *
     * @param tCheckCheckpoints 检测点
     * @return 结果
     */
    @Override
    public int updateTCheckCheckpoints(TCheckCheckpoints tCheckCheckpoints)
    {
        return tCheckCheckpointsMapper.updateTCheckCheckpoints(tCheckCheckpoints);
    }
    @Override
    public int updateTCheckCheckpointsByUpload(TCheckCheckpoints tCheckCheckpoints)
    {
        return tCheckCheckpointsMapper.updateTCheckCheckpointsByUpload(tCheckCheckpoints);
    }

    @Override
    public int updateTCheckCheckpointsByCheckIds(TCheckCheckpoints tCheckCheckpoints)
    {
        return tCheckCheckpointsMapper.updateTCheckCheckpointsByCheckIds(tCheckCheckpoints);
    }

    @Override
    public int updateApproveStatusByIds(TCheckCheckpoints tCheckCheckpoints)
    {
        return tCheckCheckpointsMapper.updateApproveStatusByIds(tCheckCheckpoints);
    }

    @Override
    public int deleteCheckpoints(TCheckCheckpoints tCheckCheckpoints)
    {
        return tCheckCheckpointsMapper.deleteCheckpoints(tCheckCheckpoints);
    }

    /**
     * 批量删除检测点
     *
     * @param checkIds 需要删除的检测点主键
     * @return 结果
     */
    @Override
    public int deleteTCheckCheckpointsByCheckIds(Long[] checkIds)
    {
        return tCheckCheckpointsMapper.deleteTCheckCheckpointsByCheckIds(checkIds);
    }

    /**
     * 删除检测点信息
     *
     * @param checkId 检测点主键
     * @return 结果
     */
    @Override
    public int deleteTCheckCheckpointsByCheckId(Long checkId)
    {
        return tCheckCheckpointsMapper.deleteTCheckCheckpointsByCheckId(checkId);
    }

    @Override
    @Transactional
    public AjaxResult toApprove(TCheckCheckpoints tCheckCheckpoints) {
        int i = tCheckCheckpointsMapper.updateApproveStatusByIds(tCheckCheckpoints);//更新检测点审核状态
        List<TCheckRepairpoints> tCheckRepairpoints = new ArrayList<>();
        if (i > 0) {//更新成功
            if (2 == tCheckCheckpoints.getApproveStatus()) {// 审核通过
//                查询当前任务
                TTaskInspection tTaskInspection = tTaskInspectionMapper.selectTTaskInspectionById(tCheckCheckpoints.getInspectionId());
                //根据检测id查询所有监测点
                List<TCheckCheckpoints> points = tCheckCheckpointsMapper.selectPointsByIds(tCheckCheckpoints.getCheckIds());
                //更新已检测点数和未检测点数
                tTaskInspection.setTaskDoneNum(new BigDecimal(StringUtils.isNotEmpty(tTaskInspection.getTaskDoneNum()) ? tTaskInspection.getTaskDoneNum() : "0").add(new BigDecimal(points.size())).toString());
                tTaskInspection.setTaskUndoneNum(new BigDecimal(tTaskInspection.getTaskNum()).subtract(new BigDecimal(tTaskInspection.getTaskDoneNum())).toString());
                tTaskInspectionMapper.updateTTaskInspection(tTaskInspection);
                for (TCheckCheckpoints point : points) {
                    // 如果当前检测点泄露程度不为未泄露，将该检测点添加到待维修点中
                    if (!point.getLeakageDegree().equals("1")) {
                        TCheckRepairpoints tCheckRepairpoint = new TCheckRepairpoints();
                        tCheckRepairpoint.setPointId(point.getPointId());
                        tCheckRepairpoint.setNetTestValue(point.getNetTestValue());
                        tCheckRepairpoint.setPlantName(point.getPlantName());
                        tCheckRepairpoint.setRegionName(point.getRegionName());
                        tCheckRepairpoint.setLayer(point.getLayer());
                        tCheckRepairpoint.setDevName(point.getDevName());
                        tCheckRepairpoint.setDevCode(point.getDevCode());
                        tCheckRepairpoint.setGroupCode(point.getGroupCode());
                        tCheckRepairpoint.setExtendCode(point.getExtendCode());
                        tCheckRepairpoint.setPointType(point.getPointType());
                        tCheckRepairpoint.setInstrumentCode(point.getInstrumentCode());
                        tCheckRepairpoint.setLeakagePosition(point.getLeakagePosition());
                        tCheckRepairpoint.setLeakageDegree(point.getLeakageDegree());
//                        tCheckRepairpoint.setApproveStatus(0L);
                        tCheckRepairpoint.setCreaterCode(getUserId());
                        tCheckRepairpoint.setCreatedate(new Date());
                        tCheckRepairpoint.setUpdaterCode(getUserId());
                        tCheckRepairpoint.setUpdatedate(new Date());
                        tCheckRepairpoint.setPlantId(point.getPlantId());
                        tCheckRepairpoint.setRegionId(point.getRegionId());
                        tCheckRepairpoint.setDevId(point.getDevId());
                        tCheckRepairpoint.setInstrumentId(point.getInstrumentId());
                        tCheckRepairpoint.setPlantType(point.getPlantType());
                        tCheckRepairpoint.setMediumType(point.getMediumType());
                        tCheckRepairpoints.add(tCheckRepairpoint);
                    }
                    // 更新密封点数据
                    TBasePoint tBasePoint = new TBasePoint();
                    tBasePoint.setPointId(point.getPointId());
                    tBasePoint.setNetTestValue(point.getNetTestValue());
                    tBasePoint.setLeakageDegree(point.getLeakageDegree());
                    tBasePointMapper.updateTBasePoint(tBasePoint);
                }
                if (CollectionUtils.isNotEmpty(tCheckRepairpoints)) {
                    tCheckRepairpointsMapper.insertTCheckRepairpointsByList(tCheckRepairpoints);
                }
            }
        } else
            return AjaxResult.error("");
        return AjaxResult.success();
    }

    @Override
    @Transactional
    public AjaxResult approveAll(TCheckCheckpoints tCheckCheckpoints) {
        long status = tCheckCheckpoints.getApproveStatus();
        tCheckCheckpoints.setApproveStatus(null);
        List<TCheckCheckpoints> list = tCheckCheckpointsMapper.selectTCheckCheckpointsList(tCheckCheckpoints);
        if (list.size() == 0) {
            return AjaxResult.success();
        }
        List<Long> ids = new ArrayList<>();
        for (TCheckCheckpoints t : list
        ) {
            ids.add(t.getCheckId());
        }
        TCheckCheckpoints newCheckpoints = new TCheckCheckpoints();
        newCheckpoints.setApproveStatus(status);
        newCheckpoints.setCheckIds(ids.toArray(new Long[ids.size()]));
        int i = tCheckCheckpointsMapper.updateApproveStatusByIds(newCheckpoints);
        List<TCheckRepairpoints> tCheckRepairpoints = new ArrayList<>();
        if (i > 0) {
            if (2 == status) {
                TTaskInspection tTaskInspection = tTaskInspectionMapper.selectTTaskInspectionById(tCheckCheckpoints.getInspectionId());
                List<TCheckCheckpoints> points = tCheckCheckpointsMapper.selectPointsByIds(newCheckpoints.getCheckIds());
                tTaskInspection.setTaskDoneNum(new BigDecimal(StringUtils.isNotEmpty(tTaskInspection.getTaskDoneNum()) ? tTaskInspection.getTaskDoneNum() : "0").add(new BigDecimal(points.size())).toString());
                tTaskInspection.setTaskUndoneNum(new BigDecimal(tTaskInspection.getTaskNum()).subtract(new BigDecimal(tTaskInspection.getTaskDoneNum())).toString());
                tTaskInspectionMapper.updateTTaskInspection(tTaskInspection);
                for (TCheckCheckpoints point : points) {
                    if (!point.getLeakageDegree().equals("1")) {
                        TCheckRepairpoints tCheckRepairpoint = new TCheckRepairpoints();
                        tCheckRepairpoint.setPointId(point.getPointId());
                        tCheckRepairpoint.setNetTestValue(point.getNetTestValue());
                        tCheckRepairpoint.setPlantName(point.getPlantName());
                        tCheckRepairpoint.setRegionName(point.getRegionName());
                        tCheckRepairpoint.setLayer(point.getLayer());
                        tCheckRepairpoint.setDevName(point.getDevName());
                        tCheckRepairpoint.setDevCode(point.getDevCode());
                        tCheckRepairpoint.setGroupCode(point.getGroupCode());
                        tCheckRepairpoint.setExtendCode(point.getExtendCode());
                        tCheckRepairpoint.setPointType(point.getPointType());
                        tCheckRepairpoint.setInstrumentCode(point.getInstrumentCode());
                        tCheckRepairpoint.setLeakagePosition(point.getLeakagePosition());
                        tCheckRepairpoint.setLeakageDegree(point.getLeakageDegree());
//                        tCheckRepairpoint.setApproveStatus(0L);
                        tCheckRepairpoint.setCreaterCode(getUserId());
                        tCheckRepairpoint.setCreatedate(new Date());
                        tCheckRepairpoint.setUpdaterCode(getUserId());
                        tCheckRepairpoint.setUpdatedate(new Date());
                        tCheckRepairpoint.setPlantId(point.getPlantId());
                        tCheckRepairpoint.setRegionId(point.getRegionId());
                        tCheckRepairpoint.setDevId(point.getDevId());
                        tCheckRepairpoint.setInstrumentId(point.getInstrumentId());
                        tCheckRepairpoint.setPlantType(point.getPlantType());
                        tCheckRepairpoint.setMediumType(point.getMediumType());
                        tCheckRepairpoints.add(tCheckRepairpoint);
                    }
                }
                new Thread(() -> {
                    for (TCheckCheckpoints point : points) {
                        TBasePoint tBasePoint = new TBasePoint();
                        tBasePoint.setPointId(point.getPointId());
                        tBasePoint.setNetTestValue(point.getNetTestValue());
                        tBasePoint.setLeakageDegree(point.getLeakageDegree());
                        tBasePointMapper.updateTBasePoint(tBasePoint);// 更新密封点数据
                    }
                },"更新密封点数据线程").start();
                if (CollectionUtils.isNotEmpty(tCheckRepairpoints)) {
                    tCheckRepairpointsMapper.insertTCheckRepairpointsByList(tCheckRepairpoints);
                }
            }
        } else
            return AjaxResult.error();
        return AjaxResult.success();
    }
}
